annotate xarray_netcdf2netcdf.py @ 5:00de53d18b99 draft default tip

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