Mercurial > repos > ecology > xarray_coords_info
comparison xarray_netcdf2netcdf.py @ 0:fea8a53f8099 draft
"planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 57b6d23e3734d883e71081c78e77964d61be82ba"
author | ecology |
---|---|
date | Sun, 06 Jun 2021 08:50:43 +0000 |
parents | |
children | 3e73f657a998 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fea8a53f8099 |
---|---|
1 #!/usr/bin/env python3 | |
2 # | |
3 # Apply operations on selected variables | |
4 # - scale | |
5 # one can also select the range of time (for timeseries) | |
6 # to apply these operations over the range only | |
7 # when a range of time is selected and when scaling, one | |
8 # can choose to save the entire timeseries or | |
9 # the selected range only. | |
10 # when scaling, one can add additional filters on dimensions | |
11 # (typically used to filter over latitudes and longitudes) | |
12 | |
13 | |
14 import argparse | |
15 import warnings | |
16 | |
17 import xarray as xr # noqa: E402 | |
18 | |
19 | |
20 class netCDF2netCDF (): | |
21 def __init__(self, infile, varname, scale="", | |
22 output="output.netcdf", | |
23 write_all=False, | |
24 filter_list="", | |
25 verbose=False): | |
26 self.infile = infile | |
27 self.verbose = verbose | |
28 self.varname = varname | |
29 self.write_all = write_all | |
30 self.filter = filter_list | |
31 self.selection = {} | |
32 if scale == "" or scale is None: | |
33 self.scale = 1 | |
34 else: | |
35 self.scale = float(scale) | |
36 if output is None: | |
37 self.output = "output.netcdf" | |
38 else: | |
39 self.output = output | |
40 # initialization | |
41 self.dset = None | |
42 self.subset = None | |
43 if self.verbose: | |
44 print("infile: ", self.infile) | |
45 print("varname: ", self.varname) | |
46 print("filter_list: ", self.filter) | |
47 print("scale: ", self.scale) | |
48 print("write_all: ", self.write_all) | |
49 print("output: ", self.output) | |
50 | |
51 def dimension_selection(self, single_filter): | |
52 split_filter = single_filter.split('#') | |
53 dimension_varname = split_filter[0] | |
54 op = split_filter[1] | |
55 ll = int(split_filter[2]) | |
56 if (op == 'sl'): | |
57 rl = int(split_filter[3]) | |
58 self.selection[dimension_varname] = slice(ll, rl) | |
59 elif (op == 'to'): | |
60 self.selection[dimension_varname] = slice(None, ll) | |
61 elif (op == 'from'): | |
62 self.selection[dimension_varname] = slice(ll, None) | |
63 elif (op == 'is'): | |
64 self.selection[dimension_varname] = ll | |
65 | |
66 def filter_selection(self): | |
67 for single_filter in self.filter: | |
68 self.dimension_selection(single_filter) | |
69 if self.write_all: | |
70 self.ds[self.varname] = \ | |
71 self.ds[self.varname].isel(self.selection)*self.scale | |
72 else: | |
73 self.dset = \ | |
74 self.ds[self.varname].isel(self.selection)*self.scale | |
75 | |
76 def compute(self): | |
77 if self.dset is None: | |
78 self.ds = xr.open_dataset(self.infile) | |
79 if self.filter: | |
80 self.filter_selection() | |
81 if self.verbose: | |
82 print(self.selection) | |
83 elif self.write_all is not None: | |
84 self.dset = self.ds[self.varname] | |
85 | |
86 def save(self): | |
87 if self.write_all: | |
88 self.ds.to_netcdf(self.output) | |
89 else: | |
90 self.dset.to_netcdf(self.output) | |
91 | |
92 | |
93 if __name__ == '__main__': | |
94 warnings.filterwarnings("ignore") | |
95 parser = argparse.ArgumentParser() | |
96 parser.add_argument( | |
97 'input', | |
98 help='input filename in netCDF format' | |
99 ) | |
100 parser.add_argument( | |
101 'varname', | |
102 help='Specify which variable to plot (case sensitive)' | |
103 ) | |
104 parser.add_argument( | |
105 '--filter', | |
106 nargs="*", | |
107 help='Filter list variable#operator#value_s#value_e' | |
108 ) | |
109 parser.add_argument( | |
110 '--output', | |
111 help='Output filename to store the resulting netCDF file' | |
112 ) | |
113 parser.add_argument( | |
114 '--scale', | |
115 help='scale factor to apply to selection (float)' | |
116 ) | |
117 parser.add_argument( | |
118 "--write_all", | |
119 help="write all data to netCDF", | |
120 action="store_true") | |
121 parser.add_argument( | |
122 "-v", "--verbose", | |
123 help="switch on verbose mode", | |
124 action="store_true") | |
125 args = parser.parse_args() | |
126 | |
127 dset = netCDF2netCDF(infile=args.input, varname=args.varname, | |
128 scale=args.scale, output=args.output, | |
129 filter_list=args.filter, | |
130 write_all=args.write_all, | |
131 verbose=args.verbose) | |
132 dset.compute() | |
133 dset.save() |