Mercurial > repos > climate > shift_longitudes
changeset 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 | |
files | README.md shift-longitudes.xml shift_lon.py test-data/TS.f2000.T31T31.control.cam.h0.0014-12.180.nc test-data/TS.f2000.T31T31.control.cam.h0.0014-12.nc |
diffstat | 5 files changed, 123 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Thu Apr 25 18:12:13 2019 -0400 @@ -0,0 +1,7 @@ + +# shift longitudes from netCDF file + +The wrapper aims at providing a simple utility to shift longitudes ranging from +0. and 360 degrees to -180. and 180. degrees. +The input file must be in netCDF format with geographical coordinates +(latitudes, longitudes) given in degrees.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/shift-longitudes.xml Thu Apr 25 18:12:13 2019 -0400 @@ -0,0 +1,78 @@ +<tool id="shyft_longitudes" name="shift longitudes" version="0.1.0"> + <description>from netCDF data</description> + <requirements> + <requirement type="package" version="3.6.7">python</requirement> + <requirement type="package" version="1.4.1">netcdf4</requirement> + <requirement type="package" version="0.10.9">xarray</requirement> + </requirements> + <command detect_errors="exit_code"><![CDATA[ + python3 '$__tool_directory__/shift_lon.py' + '$ifilename' + '$longitude' + '$ofilename' + ]]></command> + <inputs> + <param name="ifilename" type="data" format="netcdf" label="input with geographical coordinates (netCDF format)"></param> + <param name="longitude" type="text" value="lon" label="variable name for longitude as given in the netCDF file" /> + </inputs> + <outputs> + <data name="ofilename" format="netcdf"></data> + </outputs> + <tests> + <test> + <param name="ifilename" value="TS.f2000.T31T31.control.cam.h0.0014-12.nc" /> + <output name="ofilename" ftype="netcdf" file="TS.f2000.T31T31.control.cam.h0.0014-12.180.nc" compare="sim_size" delta="500"/> + </test> + <test> + <param name="ifilename" value="TS.f2000.T31T31.control.cam.h0.0014-12.180.nc" /> + <output name="ofilename" ftype="netcdf" file="TS.f2000.T31T31.control.cam.h0.0014-12.180.nc" compare="sim_size" delta="500"/> + </test> + </tests> + <help><![CDATA[ + +**Shift longitudes** +================================================ + +This tool wraps the functionality of ``shift-lon.py``. + + +.. class:: infomark + + The wrapper aims at providing a simple utility to shift longitudes ranging from + 0. and 360 degrees to -180. and 180. degrees. + The input file must be in netCDF format with geographical coordinates + (latitudes, longitudes) given in degrees. + +**What it does** +---------------- + +This tools creates a netCDF file with the same variables as the original file but +where longitudes range from -180. to 180. + +**Usage** + +:: + + usage: shift-lon.py [-h] [-v] input output + + +Positional arguments: +~~~~~~~~~~~~~~~~~~~~~ + +- **input**: input filename with geographical coordinates (netCDF format) +- **longitude**: variable name for longitudes as stored in netCDF file +- **output**: output filename for data with shifted longitudes + +Optional arguments: +~~~~~~~~~~~~~~~~~~~~~ + + -h, --help show this help message and exit + -v, --verbose switch on verbose mode + +It uses ``xarray`` python package to generate plots. More information about +``xarray`` can be found at http://xarray.pydata.org/en/stable/ + + ]]></help> + <citations> + </citations> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/shift_lon.py Thu Apr 25 18:12:13 2019 -0400 @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# +import argparse +import warnings + +import xarray as xr + +if __name__ == '__main__': + warnings.filterwarnings("ignore") + parser = argparse.ArgumentParser() + parser.add_argument( + 'input', + help='input filename with geographical coordinates (netCDF format)' + ) + + parser.add_argument( + 'lon', + help='name of the variable for longitudes' + ) + + parser.add_argument( + 'output', + help='output filename to store resulting image (png format)' + ) + parser.add_argument("-v", "--verbose", help="switch on verbose mode", + action="store_true") + args = parser.parse_args() + + dset = xr.open_dataset(args.input, decode_cf=False) + + if dset[args.lon].max() > 180.: + for i in range(dset[args.lon].size): + if dset[args.lon].values[i] > 180.: + dset[args.lon].values[i] = dset[args.lon].values[i] - 360. + + dset.sortby(args.lon).to_netcdf(args.output) + if args.verbose: + print("Longitudes shifted to -180. and 180.")