source: lmdz_wrf/trunk/tools/nc_var.py @ 1906

Last change on this file since 1906 was 1889, checked in by lfita, 7 years ago

Adding:

  • `itime': Function to provide information of the time from a netCDF file
  • Property svn:executable set to *
File size: 32.2 KB
Line 
1#!/usr/bin/python
2# Python to manage netCDF files.
3# From L. Fita work in different places: CCRC (Australia), LMD (France)
4# More information at: http://www.xn--llusfb-5va.cat/python/PyNCplot
5#
6# pyNCplot and its component nc_var.py comes with ABSOLUTELY NO WARRANTY.
7# This work is licendes under a Creative Commons
8#   Attribution-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-sa/4.0)
9#
10
11## e.g. # nc_var.py -o lonlat_polygon -f wrfout_d01_1995-01-01_00:00:00 -S star.dat -v XLONG,XLAT
12## e.g. # nc_var.py -o maskvar -f SouthAm_Climzones_masked.nc -S 'geo_em.d01.nc:LANDMASK:Time|0:0' -v region:lon,lat
13## e.g. # nc_var.py -o addDim -S 128 -f SouthAm_Climzones.nc -v Lstring
14## e.g. # nc_var.py -o addVar -S 'reg,Lstring|region_name@name!of!the!regions@-|c' -f SouthAm_Climzones.nc -v regionn
15## e.g. # nc_var.py -o varaddattr -f SouthAm_Climzones.nc -S 'grid_mapping_name|Mercator' -v Mercator
16## e.g. # nc_var.py -o varaddattrk -f SouthAm_Climzones.nc -S 'longitude_of_central_meridian|-60.0|R' -v Mercator
17## e.g. # nc_var.py -o setvar_asciivalues -S 'nombres_zonas.dat' -f SouthAm_Climzones.nc -v regionn
18## e.g. # nc_var.py -o WRF_to_newCF -f wrfout_d01_1995-01-01_00\:00\:00 -S 'XLONG:XLAT:Times:19491201000000:minutes' -v QVAPOR,T2,Q2
19## e.g. # nc_var.py -o reconstruct_matrix_from_vector -f cruncep_halfdeg_1958.nc -S 'nav_lon:nav_lat:-90.:-7.:-67.:15.:latlon:0.5:0.5:0.05' -v all
20## e.g. # nc_var.py -o nc_var.py -o CFmorzization -S 'X|west_east|XLONG,Y|south_north|XLAT,T|Time|WRFtime,Z|bottom_top|ZNU:GlobalAttr1995.inf:proj1995.inf' -f ~/PY/wrfout_d01_1995-01-01_00\:00\:00 -v QFX@instantaneous@None
21## e.g.# nc_var.py -o CFmorzization -S 'X|west_east|XLONG,Y|south_north|XLAT,T|Time|WRFtime,Z|bottom_top|ZNU:GlobalAttr1995.inf:proj1995.inf' -f ~/PY/wrfout_d01_1995-01-01_00\:00\:00 -v 'RAINC@time:!accumulation@WRFtime_bnds|h|3'
22## e.g. # nc_var.py -o dimrm -S removevar -f wrfout_d01_1995-01-01_00:00:00 -v west_east
23## e.g. # nc_var.py -o ifile -f ../PY/wrfout_d01_1995-01-01_00:00:00
24## e.g. # nc_var.py -o splitfile_dim -S bottom_top:ZNU:QVAPOR_ZNU:.4f -v QVAPOR,XLONG,XLAT,Times -f ~/PY/wrfout_d01_1995-01-01_00:00:00
25## e.g. # nc_var.py -o itime -S WRFtime -f ../PY/wrfout_d01_1995-01-01_00:00:00 -v Times
26
27## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1
28## e.g. ccrc468-17 # ./nc_var.py -v prac -f xyz/CCRC_NARCliM_Sydney_DAM_1990-1999_prac.nc -o mname -S pluja
29## e.g. ccrc468-17 # ./nc_var.py -v lluis -f CCRC_NARCliM_Sydney_MOM_1990-1999_prac.nc -o addref -S 'prac:standard_name@lluis_variable:long_name@test variable lluis:units@m s-1:0.'
30## e.g. ccrc468-17 # ./nc_var.py -v lluis66 -f ~/UNSW-CCRC-WRF/tools/python/CCRC_NARCliM_Sydney_MOM_1990-1999_prac.nc -o addattr -S 'comment|Lluis Fita-123456'
31## e.g. ccrc468-17 # ./nc_var.py -v lluis66 -f ~/UNSW-CCRC-WRF/tools/python/CCRC_NARCliM_Sydney_MOM_1990-1999_prac.nc -o rmvattr -S 'comment'
32## e.g. acuna # ./nc_var.py -f /d4/lflmd/etudes/WRF_LMDZ/WaquaL/WRF/control/wrfout/wrfout_d01_1979-12-01_00:00:00 -o checkallvars -S 'DateStrLen,Time,soil_layers_stag,bottom_top_stag,bottom_top,west_east_stag,west_east,south_north,south_north_stag:-3,1,2,2,2,-2,-2,-2,-2'
33## e.g. foudre # nc_var.py -f ~/etudes/WRF_LMDZ/tests/wrf_input/AR40.0/wrfout_d01_1979-01-01_00\:00\:00 -o checkallvars -S 'bottom_top_plus1,num_orchidee_soil_levels,lmdz_ksoil_types,DIM0009,DateStrLen,Time,soil_layers_stag,bottom_top_stag,bottom_top,west_east_stag,west_east,south_north,south_north_stag:2,0,0,1,-3,1,2,2,2,-2,-2,-2,-2'
34## e.g. # nc_var.py -o field_stats -f ~/etudes/domains/Polynesie/geo_em.d03.nc -S full -v HGT_M
35## e.g. # nc_var.py -o filter_2dim -S '80,y,x,lon,lat' -f 'tahiti_5m_ll.grd' -v 'z'
36## e.g. # nc_var.py -o selvar -f /home/lluis/PY/met_em.d01.1979-01-01_00:00:00.nc -S 'west_east@XLONG_M,south_north@XLAT_M,num_metgrid_levels@int,Time@Times' -v TT,UU,VV,SKINTEMP
37## e.g. # nc_var.py -o 'Partialmap_Entiremap' -f carteveg5km.nc -S 'longitude,latitude,std,5000.,Goode,Goode_5km.nc' -v vegetation_map
38## e.g. # nc_var.py -o subbasin -f routing.nc -S Caceres,-57.75,-16.25,left
39## e.g. # nc_var.py -o computevar_model -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S hurs
40## e.g. # nc_var.py -o model_characteristics -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S WRF
41## e.g. # nc_var.py -o WRF_toCF -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S XLONG:XLAT:19491201000000:minutes
42## e.g. # nc_var.py -o cleaning_varsfile -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S T2,XLONG,XLAT,Times
43## e.g. # nc_var.py -o LMDZ_toCF -f LMDZ/AR40/vas_histins_1-1.nc -S 'minutes!since!1949-12-01!00:00:00'
44## e.g. # nc_var.py -o CDO_toCF -f LMDZ/AR40/hfss_reproj-histins.nc
45## e.g. # nc_var.py -o pinterp -f wrfout_d01_2001-11-11_00\:00\:00 -S 100000.:97500.:95000.:92500.:90000.:85000.:80000.:75000.:70000.:65000.:60000.:55000.:50000.:45000.:40000.:35000.:30000.:25000.:20000.:15000.:10000.:5000.:2500.:1000.:500.:250.,1,0 -v WRFt,WRFrh
46## e.g. # nc_var.py -o pinterp -f /media/ExtDiskC_ext4/DATA/etudes/WRF_LMDZ/WaquaL_highres/short_copies/LMDZ/AR40/histins_19790101000000-19790304000000_short.nc -S 100000.:97500.:95000.:92500.:90000.:85000.:80000.:75000.:70000.:65000.:60000.:55000.:50000.:45000.:40000.:35000.:30000.:25000.:20000.:15000.:10000.:5000.:2500.:1000.:500.:250.,1,0 -v temp,ovap
47## e.g. # nc_var.py -o reproject -f analysis/LMDZ/AR40/hurs_histins.nc -S 'lon,lat,analysis/WRF/current/hurs_wrfout.nc,lon,lat,npp,time@time' -v hurs
48## e.g. # nc_var.py -o field_stats_dim -f /home/lluis/PY/wrfout_d01_2001-11-11_00:00:00 -S 'full,None,None,west_east,XLONG,False' -v 'T2'
49## e.g. # nc_var.py -o compute_opersvarsfiles -S 'west_east|XLONG|-1;south_north|XLAT|-1;Time|Times|3@add|wrfout_d01_2001-11-11_00:00:00|T2%west_east|XLONG|-1;south_north|XLAT|-1;Time|Times|3@subc,273.15|wrfout_d01_2001-11-11_00:00:00|None' -v 'tempC,air!temperature,C'
50## e.g. # nc_var.py -o compute_opersvarsfiles -S 'lon|lon|-1;lat|lat|-1;time_counter|time_counter|-1@forwrdderiv,1,1,2|/ccc/store/cont003/gen7593/fitaborl/etudes/DynamicoESM/aquaplanet/AR40/19800101000000-19810101000000/histday.nc|t2m' -v 'tasderiv,x-derivative|of|air|temperature,K
51## e.g. # nc_var.py -o getvars_tofile -S soils_param_Zobler_textXIOS.nc -f soils_param_colorXIOS.nc -v 'soiltext,soiltexttypes'
52## e.g. # nc_var.py -o addVar -S 'lon,lat|mask@mask@1|f' -f new_ORCHIDEE_13PFTmap_2010_cmpi6_LUH2v2h.nc -v mask
53## e.g. # nc_var.py -o setvar_nc -S 'new_ORCHIDEE_13PFTmap_2010_cmpi6_LUH2v2h.nc,maxvegetfrac,veget;0' -f new_ORCHIDEE_13PFTmap_2010_cmpi6_LUH2v2h.nc -v mask
54## e.g. $ nc_var.py -o VarVal_FillValue -S '0.,eq,std' -f new_lai2D.nc -v LAI
55## e.g. # nc_var.py -o rm_FillValue -f new_ORCHIDEE_13PFTmap_2010_cmpi6_LUH2v2h.nc -S 0 -v mask
56## e.g. # nc_var.py -o curve_section -f /home/lluis/PY/test.nc -S 'gridline,x,y,8.,8.,16.,16.,32' -v all
57## e.g. # nc_var.py -o merge_files -S 'plev|plev,time|time:merged.nc' -f 'ncobs/AliceSprings/snd_94326_198201-198201.nc,ncobs/AliceSprings/snd_94326_198202-198202.nc' -v 'plev,time'
58
59from optparse import OptionParser
60import numpy as np
61from netCDF4 import Dataset as NetCDFFile
62import os
63import re
64import nc_var_tools as ncvar
65# Using 'generic_tools.py'
66import generic_tools as gen
67
68# addDim: Operation to add a new dimension in a file
69# addVar: Operation to add a new variable in a file using exisintg dimensions
70# addvals: Function to add values to a given variable at a given dimension
71# CDO_toCF: Function to pass a CDO output file to CF-conventions
72# CFmorzization: Function to provide a CF-compilation version of a variable within a file
73# chdimname: Changing the name of the dimension
74# changevartype: Function to change the type of a variable (when possible)
75# checkallvars: Function to check all variables of a file
76# checkAllValues: Function to check for variables with along all their dimensions with the same value in a file
77# checkNaNs: Function to check for NaN values over all variables in a file
78# chgtimestep: Function to change the values of a given time-step of a variable inside a netCDF for values from a nother file
79# chvarname: Changing the name of the variable
80# cleaning_varsfile: Function to keep a list of varibales from a file
81# compute_deaccum: Function to compute deaccum: deaccumulation of a variable (VAR[t+1]-VAR[t])
82# compute_opersvarsfiles: Function to compute opersvarfiles: operation of variables from different files
83# compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps
84# compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps
85# compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory
86# computevar_model: Function to provide the way to compute a CF-variable providing its name
87# curve_section: Function to provide a section of a file following a given curve
88# DatesFiles: Function to find different time values on a series of WRF files in a folder
89# DataSetSection: Function to get a section (values along a dimension) of a given data-set
90# DataSetSection_multidims: Function to get a section (values along multiple dimensions) of a given data-set
91# DataSetSection_multivars: Function to get a section (values along multiple variables) of a given data-set
92# DYNAMICO_toCF: Function to pass a DYNAMICO original file to CF-conventions
93# dimrm: Removing a dimension from a file
94# dimToUnlimited: Operation to create an unlimited dimension from an existing one
95# dimVar_creation: Function to add a 1D variable with the size of a given dimension in a file
96# fattradd: Adding attributes from a reference file
97# fdimadd: Adding dimension from another reference file
98# fgaddattr: Adding global attributes from a reference file
99# field_stats: Function to retrieve statistics from a field
100# field_stats_dim: Function to retrieve statistics from a field along dimensions
101# file_creation: Operation to create a file with one variable with a given set of dimensions
102# file_oper_alongdims: Function to operate a file along different dimensions of a variable
103# filter_2dim: Function to filter along 2 dimensions (moving grid-box means of a given size) values of the netCDF file
104# flipdim: flips the value following one dimension
105# fvaradd: Adding variable (and all its attributes and dimensions) from a reference file to a file
106# gaddattrk: Add a global attribute to a netCDF caring about the type. Removes previous attribute if it exist
107# gaddattr: Add a global attribute to a netCDF. Removes previous attribute if it exist
108# get_attribute: Function to get an attribute from a netCDF file
109# get_namelist_vars: Function to get namelist-like  values ([varname] = [value])
110# get_Variables: Function to get a list of variables from an existing file
111# getvalues_lonlat: Function to retrieve the values from the closest grid point to a set of longitude, latitude values
112# getvars_tofile: Function to get variables from a file and pass them to another one
113# grattr: Function to read a global atribute
114# grmattr: Removing a global attribute
115# idims: Give all the dimensions names of a file
116# ifile: Function to provide some information from a given file
117# igattrs: Give all the global attributes of a file
118# increaseDimvar: Function to increase with 1 dimension an existing variable within a netcdf file. Values of
119#   the variable will be repeated along the new dimension
120# isgattrs: Give a single global attribute of a file and its type
121# isvattrs: Give a single attribute of a variable
122# itime: Function to provide information of the time from a netCDF file
123# ivars: Give all the variable names of a file
124# ivattrs: Give all the attributes of a variable and its type
125# LMDZ_toCF: Function to pass a LMDZ original file to CF-conventions
126# lonlat_polygon: Function to define a lon/lat region giving the coordinates of the vertexs of a given polygon
127# maskvar: Function to mask a variable using another variable to mask it. Only need to share at least 1 dimension with the same size (no need same name)
128# merge_files: Function to merge variables from two files
129# model_characteristics: Function to provide major characterisitcs of a given model output
130# mthDYNAMICO_toCF: Function to pass a mthDYNAMICO original file to CF-conventions
131# ncreplace: Function to replace something from a netCDF file
132# ncstepdiff: Function to compute differencies between time-steps (deacumulate) a netCDF file
133# netcdf_concatenation: Function to concatenate netCDF files for a given set of variables
134# netcdf_fold_concatenation: Function to concatenate netCDF files in a given folder for a given set of variables
135# netcdf_fold_concatenation_HMT: Function to concatenate netCDF files in a given folder for a given set of variables
136#   giving Header, Middle, Tail for the name files
137# reconstruct_matrix_from_vector: Function to reconstruct a 2D matrix from a pair of syncronized vectors with the positions on x and y coordinates
138# reproject: Function to reproject values to another one
139# rm_FillValue: Operation to remove the _FillValue from a variable inside a netCDF file
140# Partialmap_Entiremap: Function to transform from a partial global map (e.g.: only land points) to an entire one
141#   Coincidence of points is done throughout a first guess from fractions of the total domain of search
142# Partialmap_EntiremapFor: Function to transform from a partial global map (e.g.: only land points) to an entire one
143#   using Fortran code Coincidence of points is done throughout a first guess from fractions of the total domain of search
144# Partialmap_EntiremapForExact: Function to transform from a partial global map (e.g.: only land points) to an entire
145#   one using Fortran code with exact location Coincidence of points is done throughout a first guess from fractions of
146#   the total domain of search
147# pinterp: Function to vertically interpolate using subroutines from the p_interp.F90 NCAR program
148# remapnn: Function to remap to the nearest neightbor a variable using projection from another file
149# seasmean: Function to compute the seasonal mean of a variable
150# sellonlatbox: Function to select a lotlan box from a data-set
151# sellonlatlevbox: Function to select a lotlan box and a given level from a data-set
152# selvar: Function to select a series of variables from a netcdf file. Variables with
153# setvar_asciivalues: Function to set de values of a variable with an ASCII file (common Fortran-like format)
154# setvar_nc: Operation to set values of a variable from values of an other one
155# sorttimesmat: Function to sort the time values of a given file
156# spacemean: Function to retrieve a space mean series from a multidimensional variable of a file
157# SpatialWeightedMean: Function to compute the spatial mean using weights from a netCDF file
158# splitfile_dim: Function to split a file along a given dimension with a new file for each different value along the dimension (assuming resultant vardim of rank-1)
159# statcompare_files: Python script to statistically compare two different files
160# subbasin: Function to retrieve the subbasin (all the sub-flows untila a given lon,lat)
161# submns: Function to retrieve a series of months from a file
162# subyrs: Function to retrieve a series of years from a file
163# TimeInf: Function to print all the information from the variable time
164# time_reset: Function to re-set the time axis of a file
165# TimeSplitmat: Function to transform a file with CFtimes to a matrix [Nyear,Nmonth,Nday,Nhour,Nminute,Nsecond]
166# timemean: Function to retrieve a time mean series from a multidimensional variable of a file
167# valmod: Function to modify the value of a variable
168# valmod_dim: Function to modify the value of a variable at a given dimension and value
169# varaddattrk: Add an attribute to a variable caring about the type
170# varaddattr: Add an attribute to a variable. Removes previous attribute if it exists
171# varaddref: Function to add a variable in an existing file copying characteristics from an existing one
172# var_creation: Operation to create a new variable in a file with a given set of dimensions
173# varout: Function when we want to output variable values
174# varrmattr: Removing an attribute from a variable
175# varrm: Removing a variable from a file
176# VarVal_FillValue: Function to transform a given value from a given variable to _FillValue in a netCDF file
177# vrattr: Function to remove an atribute from a variable
178# WRF_d0Nref: Function for the generation of an extra WRF domain from a given one
179# WRF_CFlonlat_creation: Function to add a CF-convention longitude/latitude variables in a WRF file
180# WRF_CFtime_creation: Function to add a CF-convention time unit in a WRF file
181# WRF_CFxtime_creation: Function to add a CF-convention time unit in a WRF file using variable 'XTIME'
182# WRF_toCF: Function to pass a WRF original file to CF-conventions
183# WRF_to_newCF: Function to pass a WRF original file to new CF-conventions file
184
185operations=['addDim', 'addVar', 'addvals', 'CDO_toCF', 'CFmorzization',              \
186  'chdimname', 'changevartype',                                                      \
187  'checkallvars', 'checkAllValues', 'checkNaNs',                                     \
188  'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compute_deaccum',                \
189  'compute_opersvarsfiles',                                                          \
190  'compute_opervaralltime', 'compute_opervartimes', 'compute_tevolboxtraj',          \
191  'computevar_model', 'curve_section', 'DatesFiles',                                 \
192  'DataSetSection', 'DataSetSection_multidims', 'DataSetSection_multivars',          \
193  'DYNAMICO_toCF', 'dimrm', 'dimToUnlimited', 'dimVar_creation',                     \
194  'fattradd',                                                                        \
195  'fdimadd', 'fgaddattr', 'field_stats', 'field_stats_dim', 'file_creation',         \
196  'file_oper_alongdims', 'filter_2dim',                                              \
197  'flipdim', 'fvaradd', 'gaddattrk', 'gaddattr', 'get_attribute',                    \
198  'get_namelist_vars', 'get_Variables',                                              \
199  'getvalues_lonlat', 'getvars_tofile', 'grattr',                                    \
200  'grmattr', 'idims', 'ifile', 'igattrs', 'increaseDimvar', 'isgattrs',              \
201  'isvattrs', 'itime', 'ivars', 'ivattrs', 'LMDZ_toCF', 'lonlat_polygon', 'maskvar', \
202  'merge_files', 'model_characteristics',                                            \
203  'mthDYNAMICO_toCF', 'ncreplace', 'ncstepdiff', 'netcdf_concatenation',             \
204  'netcdf_fold_concatenation',                                                       \
205  'netcdf_fold_concatenation_HMT', 'reproject', 'Partialmap_Entiremap',              \
206  'Partialmap_EntiremapFor', 'Partialmap_EntiremapForExact',                         \
207  'pinterp', 'reconstruct_matrix_from_vector', 'remapnn', 'rm_FillValue',            \
208  'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues',     \
209  'setvar_nc', 'sorttimesmat', 'spacemean', 'SpatialWeightedMean',                   \
210  'splitfile_dim', 'statcompare_files',                                              \
211  'subbasin', 'submns', 'subyrs', 'TimeInf', 'time_reset',                           \
212  'TimeSplitmat', 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr',    \
213  'varaddref',                                                                       \
214  'var_creation', 'varout', 'varoutold', 'varrmattr', 'varrm', 'VarVal_FillValue',   \
215  'vrattr', 'WRF_d0Nref',                                                            \
216  'WRF_CFlonlat_creation', 'WRF_CFtime_creation', 'WRF_CFxtime_creation',            \
217  'list_operations', 'WRF_toCF', 'WRF_to_newCF']
218
219### Options
220##string_operation="operation to make: " + '\n' + " out, output values -S inidim1,[inidim2,...]:enddim1,[enddim2,...]"
221string_operation="""operation to make:
222  addgattr, add a new global attribute: addatr -S [attrname]|[attrvalue]
223  addvattr, add a new attribute to any given variable: addatr -S [attrname]|[attrvalue]
224  addref, add a new variable with dimension and attributes from an already existing 'variable ref' in the file and -S [variable ref]:[attr name]@[value]:[[attr2]@[value2], ...]:[value/file with values]  mname, modify name -S newname
225  checkallvalrs: Function to check all variables of a file: -S [dimn1],[[dimn2],...,[dimnN]]:[dim1],[[dim2],...,[dimN]]
226  mname, modify name -S newname
227  out, output values -S inidim1,[inidim2,...]:enddim1,[enddim2,...]
228  valmod, modifiy values of variable -S [modification]:
229     sumc,[constant]: add [constant] to variables values
230     subc,[constant]: substract [constant] to variables values
231     mulc,[constant]: multipy by [constant] to variables values
232     divc,[constant]: divide by [constant] to variables values
233  rmgattr, remove a global attribute: rmgattr -S [attrname]
234  rmvattr, remove an attribute to any given variable: rmvattr -S [attrname]
235"""
236
237#print string_operation
238
239operationnames = "'" + gen.numVector_String(operations, "', '") + "'"
240
241parser = OptionParser()
242parser.add_option("-f", "--netCDF_file", dest="ncfile", help="file to use", 
243  metavar="FILE")
244parser.add_option("-o", "--operation", type='choice', dest="operation", 
245  choices=operations, help="operation to make: " + operationnames, metavar="OPER")
246parser.add_option("-S", "--valueS", dest="values", 
247  help="values to use according to the operation (when applicable); '-h' specific help of the operation", metavar="VALUES")
248parser.add_option("-v", "--variable", dest="varname",
249  help="variable to use (when applicable)", metavar="VAR")
250
251(opts, args) = parser.parse_args()
252
253#if opts.help:
254#  parser.print_help()
255#  print string_operation
256#  sys.exit()
257
258#######    #######
259## MAIN
260    #######
261
262# Operations which file name is not a real file
263NotCheckingFile = ['DatesFiles', 'compute_opersvarsfiles', 'file_creation',          \
264  'list_operations', 'merge_files',                                                  \
265  'model_characteristics', 'netcdf_concatenation', 'netcdf_fold_concatenation',      \
266  'netcdf_fold_concatenation_HMT']
267
268####### ###### ##### #### ### ## #
269errormsg='ERROR -- error -- ERROR -- error'
270
271varn=opts.varname
272oper=opts.operation
273
274if opts.operation is None:
275    print errormsg
276    print '  No operation provided !!'
277    print "  an operation must be provided as '-o [operationname]' "
278    quit(-1)
279
280if opts.ncfile is not None and not os.path.isfile(opts.ncfile) and                   \
281  not gen.searchInlist(NotCheckingFile,oper):
282    print errormsg
283    print '  File ' + opts.ncfile + ' does not exist !!'
284    quit(-1)
285elif opts.ncfile is None and not gen.searchInlist(NotCheckingFile,oper) and          \
286  opts.values != 'h':
287    print errormsg
288    print '  No file provided !!'
289    print "  a file must be provided as '-f [filename]' "
290    quit(-1)
291
292if oper == 'addDim':
293    ncvar.addDim(opts.values, opts.ncfile, opts.varname)
294elif oper == 'addVar':
295    ncvar.addVar(opts.values, opts.ncfile, opts.varname)
296elif oper == 'addvals':
297    ncvar.addvals(opts.values, opts.ncfile, opts.varname)
298elif oper == 'CDO_toCF':
299    ncvar.CDO_toCF(opts.ncfile)
300elif oper == 'CFmorzization':
301    ncvar.CFmorzization(opts.values, opts.ncfile, opts.varname)
302elif oper == 'chdimname':
303    ncvar.chdimname(opts.values, opts.ncfile)
304elif oper == 'changevartype':
305    ncvar.changevartype(opts.values, opts.ncfile, opts.varname)
306elif oper == 'checkallvars':
307    ncvar.checkallvars(opts.values, opts.ncfile)
308elif oper == 'checkAllValues':
309    ncvar.checkAllValues(opts.values, opts.ncfile)
310elif oper == 'checkNaNs':
311    ncvar.checkNaNs(opts.values, opts.ncfile)
312elif oper == 'chgtimestep':
313    ncvar.chgtimestep(opts.values, opts.ncfile, opts.varname)
314elif oper == 'chvarname':
315    ncvar.chvarname(opts.values, opts.ncfile, opts.varname)
316elif oper == 'cleaning_varsfile':
317    ncvar.cleaning_varsfile(opts.values, opts.ncfile)
318elif oper == 'compute_deaccum':
319    ncvar.compute_deaccum(opts.values, opts.ncfile, opts.varname)
320elif oper == 'compute_opersvarsfiles':
321    ncvar.compute_opersvarsfiles(opts.values, opts.varname)
322elif oper == 'compute_opervaralltime':
323    ncvar.compute_opervaralltime(opts.values, opts.ncfile, opts.varname)
324elif oper == 'compute_opervartimes':
325    ncvar.compute_opervartimes(opts.values, opts.ncfile, opts.varname)
326elif oper == 'compute_tevolboxtraj':
327    ncvar.compute_tevolboxtraj(opts.values, opts.ncfile, opts.varname)
328elif oper == 'computevar_model':
329    ncvar.computevar_model(opts.values, opts.ncfile)
330elif oper == 'curve_section':
331    ncvar.curve_section(opts.values, opts.ncfile, opts.varname)
332elif oper == 'DataSetSection':
333    ncvar.DataSetSection(opts.values, opts.ncfile)
334elif oper == 'DataSetSection_multidims':
335    ncvar.DataSetSection_multidims(opts.values, opts.ncfile, opts.varname)
336elif oper == 'DataSetSection_multivars':
337    ncvar.DataSetSection_multivars(opts.values, opts.ncfile, opts.varname)
338elif oper == 'DatesFiles':
339    ncvar.DatesFiles(opts.values, opts.ncfile, opts.varname)
340elif oper == 'DYNAMICO_toCF':
341    ncvar.DYNAMICO_toCF(opts.values, opts.ncfile)
342elif oper == 'dimrm':
343    ncvar.dimrm(opts.ncfile, opts.values, opts.varname)
344elif oper == 'dimToUnlimited':
345    ncvar.dimToUnlimited(opts.values, opts.ncfile)
346elif oper == 'dimVar_creation':
347    ncvar.dimVar_creation(opts.values, opts.ncfile)
348elif oper == 'fattradd':
349    ncvar.fattradd(var, opts.values, opts.ncfile)
350elif oper == 'fdimadd':
351    ncvar.fdimadd(opts.values, opts.ncfile)
352elif oper == 'fgaddattr':
353    ncvar.fgaddattr(opts.values, opts.ncfile)
354elif oper == 'file_creation':
355    ncvar.file_creation(opts.values, opts.ncfile, opts.varname)
356elif oper == 'file_oper_alongdims':
357    ncvar.file_oper_alongdims(opts.values, opts.ncfile, opts.varname)
358elif oper == 'field_stats':
359    ncvar.field_stats(opts.values, opts.ncfile, opts.varname)
360elif oper == 'field_stats_dim':
361    ncvar.field_stats_dim(opts.values, opts.ncfile, opts.varname)
362elif oper == 'filter_2dim':
363    ncvar.filter_2dim(opts.values, opts.ncfile, opts.varname)
364elif oper == 'flipdim':
365    ncvar.flipdim(opts.values, opts.ncfile, opts.varname)
366elif oper == 'fvaradd':
367    ncvar.fvaradd(opts.values, opts.ncfile)
368elif oper == 'gaddattrk':
369    ncvar.gaddattrk(opts.values, opts.ncfile)
370elif oper == 'gaddattr':
371    ncvar.gaddattr(opts.values, opts.ncfile)
372elif oper == 'get_attribute':
373    ncvar.get_attribute(opts.values, opts.ncfile, opts.varname)
374elif oper == 'get_namelist_vars':
375    ncvar.get_namelist_vars(opts.values, opts.ncfile)
376elif oper == 'get_Variables':
377    ncvar.get_Variables(opts.values, opts.ncfile, opts.varname)
378elif oper == 'getvalues_lonlat':
379    ncvar.getvalues_lonlat(opts.values, opts.ncfile)
380elif oper == 'getvars_tofile':
381    ncvar.getvars_tofile(opts.values, opts.ncfile, opts.varname)
382elif oper == 'grattr':
383    ncvar.grattr(opts.values, opts.ncfile)
384elif oper == 'grmattr':
385    ncvar.grmattr(opts.values, opts.ncfile)
386elif oper == 'idims':
387    ncvar.idims(opts.ncfile)
388elif oper == 'ifile':
389    ncvar.ifile(opts.ncfile)
390elif oper == 'igattrs':
391    ncvar.igattrs(opts.ncfile)
392elif oper == 'increaseDimvar':
393    ncvar.increaseDimvar(opts.values, opts.ncfile, opts.varname)
394elif oper == 'isgattrs':
395    ncvar.isgattrs(opts.values, opts.ncfile)
396elif oper == 'isvattrs':
397    ncvar.isvattrs(opts.values, opts.ncfile, opts.varname)
398elif oper == 'itime':
399    ncvar.itime(opts.values, opts.ncfile, opts.varname)
400elif oper == 'ivars':
401    ncvar.ivars(opts.ncfile)
402elif oper == 'ivattrs':
403    ncvar.ivattrs(opts.ncfile, opts.varname)
404elif oper == 'list_operations':
405# From: http://www.diveintopython.net/power_of_introspection/all_together.html
406    object = ncvar
407    for opern in operations:
408        if  opern != 'list_operations': 
409            print opern + '_______ ______ _____ ____ ___ __ _'
410            print getattr(object, opern).__doc__
411elif oper == 'LMDZ_toCF':
412    ncvar.LMDZ_toCF(opts.values, opts.ncfile)
413elif oper == 'lonlat_polygon':
414    ncvar.lonlat_polygon(opts.values, opts.ncfile, opts.varname)
415elif oper == 'maskvar':
416    ncvar.maskvar(opts.values, opts.ncfile, opts.varname)
417elif oper == 'merge_files':
418    ncvar.merge_files(opts.values, opts.ncfile, opts.varname)
419elif oper == 'model_characteristics':
420    ncvar.model_characteristics(opts.values, opts.ncfile)
421elif oper == 'mthDYNAMICO_toCF':
422    ncvar.mthDYNAMICO_toCF(opts.values, opts.ncfile)
423elif oper == 'ncreplace':
424    ncvar.ncreplace(opts.values, opts.ncfile, opts.varname)
425elif oper == 'ncstepdiff':
426    ncvar.ncstepdiff(opts.values, opts.ncfile, opts.varname)
427elif oper == 'netcdf_concatenation':
428    ncvar.netcdf_concatenation(opts.ncfile)
429elif oper == 'netcdf_fold_concatenation':
430    ncvar.netcdf_fold_concatenation(opts.values, opts.ncfile, opts.varname)
431elif oper == 'netcdf_fold_concatenation_HMT':
432    ncvar.netcdf_fold_concatenation_HMT(opts.values, opts.ncfile, opts.varname)
433elif oper == 'opersvarsfiles':
434    ncvar.compute_opersvarsfiles(opts.values, opts.varname)
435elif oper == 'pinterp':
436    ncvar.pinterp(opts.values, opts.ncfile, opts.varname)
437elif oper == 'remapnn':
438    ncvar.remapnn(opts.values, opts.ncfile, opts.varname)
439elif oper == 'Partialmap_Entiremap':
440    ncvar.Partialmap_Entiremap(opts.values, opts.ncfile, opts.varname)
441elif oper == 'Partialmap_EntiremapFor':
442    ncvar.Partialmap_EntiremapFor(opts.values, opts.ncfile, opts.varname)
443elif oper == 'Partialmap_EntiremapForExact':
444    ncvar.Partialmap_EntiremapForExact(opts.values, opts.ncfile, opts.varname)
445elif oper == 'reconstruct_matrix_from_vector':
446    ncvar.reconstruct_matrix_from_vector(opts.values, opts.ncfile, opts.varname)
447elif oper == 'reproject':
448    ncvar.reproject(opts.values, opts.ncfile, opts.varname)
449elif oper == 'rm_FillValue':
450    ncvar.rm_FillValue(opts.values, opts.ncfile, opts.varname)
451elif oper == 'seasmean':
452    ncvar.seasmean(timename, opts.ncfile, opts.varname)
453elif oper == 'sellonlatbox':
454    ncvar.sellonlatbox(opts.values, opts.ncfile, opts.varname)
455elif oper == 'sellonlatlevbox':
456    ncvar.sellonlatlevbox(opts.values, opts.ncfile, opts.varname)
457elif oper == 'selvar':
458    ncvar.selvar(opts.values, opts.ncfile, opts.varname)
459elif oper == 'setvar_asciivalues':
460    ncvar.setvar_asciivalues(opts.values, opts.ncfile, opts.varname)
461elif oper == 'setvar_nc':
462    ncvar.setvar_nc(opts.values, opts.ncfile, opts.varname)
463elif oper == 'sorttimesmat':
464    ncvar.sorttimesmat(opts.ncfile, opts.varname)
465elif oper == 'spacemean':
466    ncvar.spacemean(opts.ncfile, opts.varname)
467elif oper == 'SpatialWeightedMean':
468    ncvar.SpatialWeightedMean(opts.values, opts.ncfile, opts.varname)
469elif oper == 'splitfile_dim':
470    ncvar.splitfile_dim(opts.values, opts.ncfile, opts.varname)
471elif oper == 'statcompare_files':
472    ncvar.statcompare_files(opts.values)
473elif oper == 'subbasin':
474    ncvar.subbasin(opts.values, opts.ncfile)
475elif oper == 'submns':
476    ncvar.submns(opts.values, opts.ncfile, opts.varname)
477elif oper == 'subyrs':
478    ncvar.subyrs(opts.values, opts.ncfile, opts.varname)
479elif oper == 'TimeInf':
480    ncvar.TimeInf(opts.ncfile, opts.varname)
481elif oper == 'time_reset':
482    ncvar.time_reset(opts.values, opts.ncfile, opts.varname)
483elif oper == 'TimeSplitmat':
484    ncvar.TimeSplitmat(opts.values, opts.ncfile, opts.varname)
485elif oper == 'timemean':
486    ncvar.timemean(opts.values, opts.ncfile, opts.varname)
487elif oper == 'valmod':
488    ncvar.valmod(opts.values, opts.ncfile, opts.varname)
489elif oper == 'valmod_dim':
490    ncvar.valmod_dim(opts.values, opts.ncfile, opts.varname)
491elif oper == 'varaddattrk':
492    ncvar.varaddattrk(opts.values, opts.ncfile, opts.varname)
493elif oper == 'varaddattr':
494    ncvar.varaddattr(opts.values, opts.ncfile, opts.varname)
495elif oper == 'varaddref':
496    ncvar.varaddref(opts.values, opts.ncfile, opts.varname)
497elif oper == 'var_creation':
498    ncvar.var_creation(opts.values, opts.ncfile, opts.varname)
499elif oper == 'varout':
500    ncvar.varout(opts.values, opts.ncfile, opts.varname)
501elif oper == 'varoutold':
502    ncvar.varoutold(opts.values, opts.ncfile, opts.varname)
503elif oper == 'varrmattr':
504    ncvar.varrmattr(opts.values, opts.ncfile, opts.varname)
505elif oper == 'varrm':
506    ncvar.varrm(opts.ncfile, opts.varname)
507elif oper == 'VarVal_FillValue':
508    ncvar.VarVal_FillValue(opts.values, opts.ncfile, opts.varname)
509elif oper == 'vrattr':
510    ncvar.vrattr(opts.values, opts.ncfile, opts.varname)
511elif oper == 'WRF_d0Nref':
512    ncvar.WRF_d0Nref(opts.values, opts.ncfile)
513elif oper == 'WRF_CFlonlat_creation':
514    ncvar.WRF_CFlonlat_creation(opts.values, opts.ncfile, opts.varname)
515elif oper == 'WRF_CFtime_creation':
516    ncvar.WRF_CFtime_creation(opts.values, opts.ncfile, opts.varname)
517elif oper == 'WRF_CFxtime_creation':
518    ncvar.WRF_CFxtime_creation(opts.values, opts.ncfile, opts.varname)
519elif oper == 'WRF_toCF':
520    ncvar.WRF_toCF(opts.values, opts.ncfile)
521elif oper == 'WRF_to_newCF':
522    ncvar.WRF_to_newCF(opts.values, opts.ncfile, opts.varname)
523else:
524    print errormsg
525    print '   The operation ' + oper + ' is not ready !!'
526    print errormsg
527    quit()
Note: See TracBrowser for help on using the repository browser.