annotate shift_lon.py @ 0:5708a3a46f2e draft default tip

planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
author climate
date Thu, 25 Apr 2019 18:12:13 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
1 #!/usr/bin/env python3
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
2 #
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
3 import argparse
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
4 import warnings
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
5
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
6 import xarray as xr
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
7
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
8 if __name__ == '__main__':
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
9 warnings.filterwarnings("ignore")
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
10 parser = argparse.ArgumentParser()
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
11 parser.add_argument(
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
12 'input',
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
13 help='input filename with geographical coordinates (netCDF format)'
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
14 )
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
15
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
16 parser.add_argument(
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
17 'lon',
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
18 help='name of the variable for longitudes'
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
19 )
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
20
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
21 parser.add_argument(
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
22 'output',
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
23 help='output filename to store resulting image (png format)'
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
24 )
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
25 parser.add_argument("-v", "--verbose", help="switch on verbose mode",
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
26 action="store_true")
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
27 args = parser.parse_args()
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
28
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
29 dset = xr.open_dataset(args.input, decode_cf=False)
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
30
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
31 if dset[args.lon].max() > 180.:
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
32 for i in range(dset[args.lon].size):
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
33 if dset[args.lon].values[i] > 180.:
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
34 dset[args.lon].values[i] = dset[args.lon].values[i] - 360.
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
35
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
36 dset.sortby(args.lon).to_netcdf(args.output)
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
37 if args.verbose:
5708a3a46f2e planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/shift-longitudes commit c1362af034361b6fb869411f1ea928388f230d72
climate
parents:
diff changeset
38 print("Longitudes shifted to -180. and 180.")