annotate xarray_netcdf2netcdf.py @ 3:bf595d613af4 draft

"planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
author ecology
date Thu, 20 Jan 2022 17:07:19 +0000
parents 123a9a629bef
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
1 #!/usr/bin/env python3
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
2 #
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
3 # Apply operations on selected variables
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
4 # - scale
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
5 # one can also select the range of time (for timeseries)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
6 # to apply these operations over the range only
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
7 # when a range of time is selected and when scaling, one
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
8 # can choose to save the entire timeseries or
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
9 # the selected range only.
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
10 # when scaling, one can add additional filters on dimensions
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
11 # (typically used to filter over latitudes and longitudes)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
12
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
13
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
14 import argparse
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
15 import re
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
16 import warnings
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
17 from pathlib import Path
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
18
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
19 import xarray as xr # noqa: E402
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
20
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
21
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
22 class netCDF2netCDF ():
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
23 def __init__(self, infile, varname, scale="",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
24 output="output.netcdf",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
25 write_all=False,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
26 keep_attributes=True,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
27 filter_list="",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
28 where_config="",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
29 other="",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
30 sel=False,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
31 drop=False,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
32 verbose=False):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
33 self.drop = drop
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
34 if Path(where_config).exists():
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
35 f = open(where_config)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
36 self.where = f.read().replace("\n", "")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
37 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
38 self.where = ""
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
39 self.other = other
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
40 self.sel = sel
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
41 li = list(infile.split(","))
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
42 if len(li) > 1:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
43 self.infile = li
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
44 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
45 self.infile = infile
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
46 self.verbose = verbose
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
47 if varname == 'None' or varname is None:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
48 self.varname = varname
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
49 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
50 li = list(varname.split(","))
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
51 self.varname = li
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
52 self.write_all = write_all
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
53 self.keep_attributes = keep_attributes
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
54 if self.keep_attributes:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
55 xr.set_options(keep_attrs=True)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
56 self.filter = filter_list
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
57 self.selection = {}
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
58 self.method = {}
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
59 if scale == "" or scale is None:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
60 self.scale = 1
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
61 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
62 self.scale = float(scale)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
63 if output is None:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
64 self.output = "output.netcdf"
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
65 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
66 self.output = output
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
67 # initialization
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
68 self.dset = None
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
69 self.subset = None
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
70 if self.verbose:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
71 print("infile: ", self.infile)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
72 print("varname: ", self.varname)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
73 print("filter_list: ", self.filter)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
74 print("scale: ", self.scale)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
75 print("write_all: ", self.write_all)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
76 print("keep_attributes: ", self.keep_attributes)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
77 print("sel: ", self.sel)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
78 print("output: ", self.output)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
79
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
80 def apply_selection(self):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
81 self.dset = self.ds
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
82 for key in self.selection:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
83 if 'slice' in str(self.selection[key]):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
84 self.dset = self.dset.sel(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
85 {key: self.selection[key]}
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
86 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
87 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
88 self.dset = self.dset.sel(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
89 {key: self.selection[key]},
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
90 method=self.method[key]
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
91 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
92
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
93 def dimension_selection(self, single_filter):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
94 split_filter = single_filter.split('#')
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
95 dimension_varname = split_filter[0]
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
96 op = split_filter[1]
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
97 if self.sel:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
98 ll = float(split_filter[2])
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
99 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
100 ll = int(split_filter[2])
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
101 if (op == 'sl'):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
102 if self.sel:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
103 rl = float(split_filter[3])
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
104 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
105 rl = int(split_filter[3])
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
106 self.selection[dimension_varname] = slice(ll, rl)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
107 elif (op == 'to'):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
108 self.selection[dimension_varname] = slice(None, ll)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
109 elif (op == 'from'):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
110 self.selection[dimension_varname] = slice(ll, None)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
111 elif (op == 'is'):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
112 self.selection[dimension_varname] = ll
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
113 if self.sel:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
114 rl = split_filter[3]
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
115 if 'None' in rl:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
116 self.method[dimension_varname] = None
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
117 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
118 self.method[dimension_varname] = rl
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
119
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
120 def filter_selection(self):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
121 for single_filter in self.filter:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
122 self.dimension_selection(single_filter)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
123
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
124 if self.sel:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
125 self.apply_selection()
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
126 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
127 self.dset = \
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
128 self.ds.isel(self.selection)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
129
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
130 if self.varname != 'None' and self.varname is not None:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
131 for var in self.varname:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
132 self.dset[var] = \
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
133 self.dset[var]*self.scale
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
134
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
135 def compute(self):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
136 if self.dset is None:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
137 if type(self.infile) is list:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
138 self.ds = xr.open_mfdataset(self.infile)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
139 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
140 self.ds = xr.open_dataset(self.infile)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
141 if self.where != "":
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
142 if self.drop:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
143 if self.verbose:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
144 print("Where with drop=True")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
145 self.ds = self.ds.where(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
146 self.eval_where(self.where),
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
147 drop=True
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
148 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
149 elif self.other is not None and self.other != "":
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
150 if self.verbose:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
151 print("Where with other=", float(self.other))
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
152 self.ds = self.ds.where(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
153 self.eval_where(self.where),
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
154 other=float(self.other)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
155 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
156 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
157 self.ds = self.ds.where(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
158 self.eval_where(self.where)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
159 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
160 self.filter_selection()
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
161 if self.verbose:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
162 print(self.selection)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
163
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
164 def save(self):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
165 if self.varname != 'None' and \
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
166 self.varname is not None and \
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
167 not self.write_all:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
168 self.dset[self.varname].to_netcdf(self.output)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
169 else:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
170 self.dset.to_netcdf(self.output)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
171
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
172 def is_float(self, element) -> bool:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
173 try:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
174 float(element)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
175 return True
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
176 except ValueError:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
177 return False
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
178
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
179 def eval_where(self, where_condition):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
180 eval_cond = None
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
181 list_names = list(set(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
182 list(self.ds.keys()) +
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
183 list(self.ds.coords.keys()))
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
184 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
185 wcond = where_condition
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
186 check_cond = where_condition
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
187 for var in list_names:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
188 wcond = wcond.replace(var, ' self.ds.' + var + ' ')
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
189 check_cond = check_cond.replace(var, '')
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
190 to_remove = "[><=&|()]"
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
191 check_cond = re.sub(to_remove, "", check_cond).replace("!", "")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
192 check_cond = re.sub(' +', ' ', check_cond).strip()
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
193 list_flt = check_cond.split(" ")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
194 no_convert = False
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
195 for num in list_flt:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
196 if not self.is_float(num):
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
197 no_convert = True
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
198 if not no_convert:
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
199 eval_cond = eval(wcond)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
200 return eval_cond
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
201
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
202
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
203 if __name__ == '__main__':
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
204 warnings.filterwarnings("ignore")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
205 parser = argparse.ArgumentParser()
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
206 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
207 'input',
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
208 help='input filename in netCDF format'
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
209 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
210 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
211 'varname',
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
212 help='Specify which variable to plot (case sensitive)'
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
213 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
214 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
215 '--filter',
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
216 nargs="*",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
217 help='Filter list variable#operator#value_s#value_e'
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
218 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
219 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
220 '--where',
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
221 help='filename with where condition to be evaluated'
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
222 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
223 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
224 '--output',
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
225 help='Output filename to store the resulting netCDF file'
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
226 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
227 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
228 '--scale',
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
229 help='scale factor to apply to selection (float)'
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
230 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
231 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
232 '--other',
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
233 help='Value to use for locations where condition is False (float)'
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
234 )
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
235 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
236 "--write_all",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
237 help="write all data to netCDF",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
238 action="store_true")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
239 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
240 "--keep_attributes",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
241 help="Keep all attributes",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
242 action="store_true")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
243 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
244 "-v", "--verbose",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
245 help="switch on verbose mode",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
246 action="store_true")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
247 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
248 "--selection",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
249 help="select by values",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
250 action="store_true")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
251 parser.add_argument(
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
252 "--drop",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
253 help="drop values where condition is not met",
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
254 action="store_true")
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
255 args = parser.parse_args()
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
256
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
257 print("args.selection", args.selection)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
258 dset = netCDF2netCDF(infile=args.input, varname=args.varname,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
259 scale=args.scale, output=args.output,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
260 write_all=args.write_all,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
261 sel=args.selection,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
262 keep_attributes=args.keep_attributes,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
263 filter_list=args.filter,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
264 where_config=args.where,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
265 drop=args.drop, other=args.other,
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
266 verbose=args.verbose)
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
267 dset.compute()
bf595d613af4 "planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/data_manipulation/xarray/ commit 2166974df82f97557b082a9e55135098e61640c4"
ecology
parents: 2
diff changeset
268 dset.save()