-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.py
More file actions
93 lines (76 loc) · 2.68 KB
/
Copy pathmisc.py
File metadata and controls
93 lines (76 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import numpy as np
from glob import glob
def list_of_files(filenames):
"""
Returns a list of files matching the input
Example usage:
>>> list_of_files('path/to/files/*.csv')
['path/to/files/file1.csv', 'path/to/files/file2.csv']
>>> list_of_files(['path/to/files/*.csv', 'otherfile.txt'])
['path/to/files/file1.csv', 'path/to/files/file2.csv', 'otherfile.txt']
"""
if type(filenames) in (list,tuple):
file_list = []
for filename in filenames:
for f in glob(filename):
file_list.append(f)
else:
file_list = glob(filenames)
return file_list
def get_edges(centers):
centers = np.array(centers)
mid = centers[:-1] + (centers[1:]-centers[:-1])/2
first = centers[0] - (centers[1]-centers[0])/2
last = centers[-1] + (centers[-1]-centers[-2])/2
return np.concatenate([[first], mid, [last]])
def get_centers(edges):
edges = np.array(edges)
centers = (edges[:-1]+edges[1:])/2
return centers
def all_columns_equal(a):
"""
Check if all columns in a are equal
Parameters:
a: (n,m) numpy.ndarray
Returns:
True/False
"""
m = a.shape[1]
return ~(a - np.tile(a[:,0], (m,1)).T).any()
def dataset_metadata2latex_table(ds, type='variables'):
"""
type: 'variables'/'coords'/'both'
Outputs some of the metadata of an xarray.Dataset to latex tabular format.
Note that the latex package makecell is required if description contains
line breaks. (Add \\usepackage{makecell} to the tex-document)
"""
if type == 'variables':
keys = list(ds.data_vars)
elif type == 'coords':
keys = list(ds.coords)
elif type == 'both':
keys = list(ds.coords) + list(ds.data_vars)
print('\\begin{tabular}{llll}')
print('\\textbf{Variable} & \\textbf{Description} & \\textbf{Unit} & \\textbf{Dimension} \\\\')
print('\\hline')
for var in keys:
if 'unit' in ds[var].attrs.keys():
unit = ds[var].attrs['unit']
else:
unit = '-'
# fix line breaks in description
description = ds[var].attrs["description"]
if '\n' in description:
description = '\\makecell[l]{' + description.replace('\n', '\\\\') + '}'
# Remove '' from dimension
dims = str(ds[var].dims).replace("'","")
# Remove () from dimension
dims = dims[1:-1]
# Remove traling , from dimension
if dims[-1] == ',':
dims = dims[:-1]
# Replace _ with \_
row = (f'{var} & {description} & {unit} & {dims}\\\\').replace('_', '\\_')
print(row)
print('\\end{tabular}')
return