Mercurial > repos > ecology > timeseries_extraction
comparison timeseries.py @ 0:dd19259f7da5 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:18:44 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:dd19259f7da5 |
|---|---|
| 1 #!/usr/bin/env python3 | |
| 2 # | |
| 3 # | |
| 4 # usage: netCDF_timeseries.py [-h] [--output output.png] | |
| 5 # [--save timeseries.tabular] | |
| 6 # [--config config-file] | |
| 7 # [-v] | |
| 8 # input varname | |
| 9 # positional arguments: | |
| 10 # input input filename with geographical coordinates (netCDF | |
| 11 # format) | |
| 12 # varname Specify which variable to extract (case sensitive) | |
| 13 # | |
| 14 # optional arguments: | |
| 15 # -h, --help show this help message and exit | |
| 16 # --output output.png filename to store image (png format) | |
| 17 # --save timeseries.tabular filename to store timeseries (tabular format) | |
| 18 # --config config file extract parameters | |
| 19 # -v, --verbose switch on verbose mode | |
| 20 # | |
| 21 import argparse | |
| 22 import ast | |
| 23 import warnings | |
| 24 | |
| 25 import cftime # noqa: F401 | |
| 26 | |
| 27 import matplotlib as mpl | |
| 28 mpl.use('Agg') | |
| 29 | |
| 30 import matplotlib.pyplot as plt # noqa: I202,E402 | |
| 31 from matplotlib.dates import DateFormatter # noqa: I202,E402 | |
| 32 | |
| 33 import xarray as xr # noqa: I202,E402 | |
| 34 | |
| 35 | |
| 36 class TimeSeries (): | |
| 37 def __init__(self, input, varname, output, save, verbose=False, | |
| 38 config_file=""): | |
| 39 | |
| 40 li = list(input.split(",")) | |
| 41 if len(li) > 1: | |
| 42 self.input = li | |
| 43 else: | |
| 44 self.input = input | |
| 45 | |
| 46 self.varname = varname | |
| 47 self.xylim_supported = True | |
| 48 if output == "" or output is None: | |
| 49 self.output = "Timeseries.png" | |
| 50 else: | |
| 51 self.output = output | |
| 52 if save == "" or save is None: | |
| 53 self.save = "Timeseries.tabular" | |
| 54 else: | |
| 55 self.save = save | |
| 56 self.verbose = verbose | |
| 57 self.time_start_value = "" | |
| 58 self.time_end_value = "" | |
| 59 self.lon_value = "" | |
| 60 self.lat_value = "" | |
| 61 self.lat_name = 'lat' | |
| 62 self.lon_name = 'lon' | |
| 63 self.time_name = 'time' | |
| 64 self.title = '' | |
| 65 self.xlabel = '' | |
| 66 self.ylabel = '' | |
| 67 self.format_date = '' | |
| 68 if config_file != "" and config_file is not None: | |
| 69 with open(config_file) as f: | |
| 70 sdict = ''.join( | |
| 71 f.read().replace("\n", "").split('{')[1].split('}')[0] | |
| 72 ) | |
| 73 tmp = ast.literal_eval('{' + sdict.strip() + '}') | |
| 74 for key in tmp: | |
| 75 if key == 'time_start_value': | |
| 76 self.time_start_value = tmp[key] | |
| 77 if key == 'time_end_value': | |
| 78 self.time_end_value = tmp[key] | |
| 79 if key == 'lon_value': | |
| 80 self.lon_value = tmp[key] | |
| 81 if key == 'lat_value': | |
| 82 self.lat_value = tmp[key] | |
| 83 if key == 'lon_name': | |
| 84 self.lon_name = tmp[key] | |
| 85 if key == 'lat_name': | |
| 86 self.lat_name = tmp[key] | |
| 87 if key == 'time_name': | |
| 88 self.time_name = tmp[key] | |
| 89 if key == 'title': | |
| 90 self.title = tmp[key] | |
| 91 if key == 'xlabel': | |
| 92 self.xlabel = tmp[key] | |
| 93 if key == 'ylabel': | |
| 94 self.ylabel = tmp[key] | |
| 95 if key == 'format_date': | |
| 96 self.format_date = tmp[key] | |
| 97 self.format_date = self.format_date.replace('X', '%') | |
| 98 | |
| 99 if type(self.input) is list: | |
| 100 self.dset = xr.open_mfdataset(self.input, use_cftime=True) | |
| 101 else: | |
| 102 self.dset = xr.open_dataset(self.input, use_cftime=True) | |
| 103 | |
| 104 if verbose: | |
| 105 print("input: ", self.input) | |
| 106 print("varname: ", self.varname) | |
| 107 if self.time_start_value: | |
| 108 print("time_start_value: ", self.time_start_value) | |
| 109 if self.time_end_value: | |
| 110 print("time_end_value: ", self.time_end_value) | |
| 111 print("output: ", self.output) | |
| 112 if self.lon_value: | |
| 113 print(self.lon_name, self.lon_value) | |
| 114 if self.lat_value: | |
| 115 print(self.lat_name, self.lat_value) | |
| 116 | |
| 117 def plot(self): | |
| 118 if self.lon_value: | |
| 119 lon_c = float(self.lon_value) | |
| 120 if self.lat_value: | |
| 121 lat_c = float(self.lat_value) | |
| 122 if self.lat_value and self.lon_value: | |
| 123 self.df = self.dset.sel({self.lat_name: lat_c, | |
| 124 self.lon_name: lon_c}, | |
| 125 method='nearest') | |
| 126 else: | |
| 127 self.df = self.dset | |
| 128 if self.time_start_value or self.time_end_value: | |
| 129 self.df = self.df.sel({self.time_name: slice(self.time_start_value, | |
| 130 self.time_end_value)}) | |
| 131 # Saving the time series into a tabular | |
| 132 self.df = self.df[self.varname].squeeze().to_dataframe() | |
| 133 self.df.dropna().to_csv(self.save, sep='\t') | |
| 134 # Plot the time series into png image | |
| 135 fig = plt.figure(figsize=(15, 5)) | |
| 136 ax = plt.subplot(111) | |
| 137 self.df[self.varname].plot(ax=ax) | |
| 138 if self.title: | |
| 139 plt.title(self.title) | |
| 140 if self.xlabel: | |
| 141 plt.xlabel(self.xlabel) | |
| 142 if self.ylabel: | |
| 143 plt.ylabel(self.ylabel) | |
| 144 if self.format_date: | |
| 145 ax.xaxis.set_major_formatter(DateFormatter(self.format_date)) | |
| 146 fig.tight_layout() | |
| 147 fig.savefig(self.output) | |
| 148 | |
| 149 | |
| 150 if __name__ == '__main__': | |
| 151 warnings.filterwarnings("ignore") | |
| 152 parser = argparse.ArgumentParser() | |
| 153 parser.add_argument( | |
| 154 'input', | |
| 155 help='input filename with geographical coordinates (netCDF format)' | |
| 156 ) | |
| 157 parser.add_argument( | |
| 158 'varname', | |
| 159 help='Specify which variable to plot (case sensitive)' | |
| 160 ) | |
| 161 parser.add_argument( | |
| 162 '--output', | |
| 163 help='output filename to store resulting image (png format)' | |
| 164 ) | |
| 165 parser.add_argument( | |
| 166 '--save', | |
| 167 help='save resulting tabular file (tabular format) into filename' | |
| 168 ) | |
| 169 parser.add_argument( | |
| 170 '--config', | |
| 171 help='pass timeseries parameters via a config file' | |
| 172 ) | |
| 173 parser.add_argument( | |
| 174 "-v", "--verbose", | |
| 175 help="switch on verbose mode", | |
| 176 action="store_true") | |
| 177 args = parser.parse_args() | |
| 178 | |
| 179 dset = TimeSeries(input=args.input, varname=args.varname, | |
| 180 output=args.output, save=args.save, verbose=args.verbose, | |
| 181 config_file=args.config) | |
| 182 dset.plot() |
