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

Last change on this file since 843 was 820, checked in by lfita, 9 years ago

Improving help messages

  • Property svn:executable set to *
File size: 14.2 KB
Line 
1#!/usr/bin/python
2## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1
3## e.g. ccrc468-17 # ./nc_var.py -v prac -f xyz/CCRC_NARCliM_Sydney_DAM_1990-1999_prac.nc -o mname -S pluja
4## 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.'
5## 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'
6## 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'
7## 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'
8## 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'
9## e.g. # nc_var.py -o field_stats -f ~/etudes/domains/Polynesie/geo_em.d03.nc -S full -v HGT_M
10## e.g. # nc_var.py -o filter_2dim -S '80,y,x,lon,lat' -f 'tahiti_5m_ll.grd' -v 'z'
11## 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
12## e.g. # nc_var.py -o 'Partialmap_Entiremap' -f carteveg5km.nc -S 'longitude,latitude,std,5000.,Goode,Goode_5km.nc' -v vegetation_map
13## e.g. # nc_var.py -o subbasin -f routing.nc -S Caceres,-57.75,-16.25
14
15from optparse import OptionParser
16import numpy as np
17from netCDF4 import Dataset as NetCDFFile
18import os
19import re
20import nc_var_tools as ncvar
21# Using 'generic_tools.py'
22import generic_tools as gen
23
24operations=['addvals', 'chdimname', 'changevartype', 'checkallvars',                 \
25  'checkAllValues', 'checkNaNs',                                                     \
26  'chgtimestep', 'chvarname', 'compute_deaccum', 'compute_opersvarsfiles',           \
27  'compute_opervaralltime', 'compute_opervartimes', 'compute_tevolboxtraj',          \
28  'DatesFiles',                                                                      \
29  'DataSetSection', 'DataSetSection_multidims', 'DataSetSection_multivars',          \
30  'dimToUnlimited', 'dimVar_creation',                                               \
31  'fattradd',                                                                        \
32  'fdimadd', 'fgaddattr', 'field_stats', 'file_creation', 'file_oper_alongdims',     \
33  'filter_2dim',                                                                     \
34  'flipdim', 'fvaradd', 'gaddattrk', 'gaddattr', 'get_attribute',                    \
35  'get_namelist_vars',                                                               \
36  'getvalues_lonlat', 'grattr',                                                      \
37  'grmattr', 'igattrs', 'increaseDimvar', 'isgattrs', 'isvattrs', 'ivars', 'ivattrs',\
38  'maskvar',                                                                         \
39  'ncreplace', 'ncstepdiff', 'netcdf_concatenation', 'netcdf_fold_concatenation',    \
40  'Partialmap_Entiremap', 'Partialmap_EntiremapFor', 'Partialmap_EntiremapForExact', \
41  'remapnn',                                                                         \
42  'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues',     \
43  'sorttimesmat', 'spacemean', 'SpatialWeightedMean', 'statcompare_files',           \
44  'subbasin', 'submns', 'subyrs', 'TimeInf',                                         \
45  'TimeSplitmat', 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr',    \
46  'varaddref',                                                                       \
47  'var_creation', 'varout', 'varoutold', 'varrmattr', 'varrm', 'VarVal_FillValue',   \
48  'vrattr', 'WRF_d0Nref',                                                            \
49  'WRF_CFlonlat_creation', 'WRF_CFtime_creation', 'WRF_CFxtime_creation',            \
50  'list_operations']
51
52### Options
53##string_operation="operation to make: " + '\n' + " out, output values -S inidim1,[inidim2,...]:enddim1,[enddim2,...]"
54string_operation="""operation to make:
55  addgattr, add a new global attribute: addatr -S [attrname]|[attrvalue]
56  addvattr, add a new attribute to any given variable: addatr -S [attrname]|[attrvalue]
57  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
58  checkallvalrs: Function to check all variables of a file: -S [dimn1],[[dimn2],...,[dimnN]]:[dim1],[[dim2],...,[dimN]]
59  mname, modify name -S newname
60  out, output values -S inidim1,[inidim2,...]:enddim1,[enddim2,...]
61  valmod, modifiy values of variable -S [modification]:
62     sumc,[constant]: add [constant] to variables values
63     subc,[constant]: substract [constant] to variables values
64     mulc,[constant]: multipy by [constant] to variables values
65     divc,[constant]: divide by [constant] to variables values
66  rmgattr, remove a global attribute: rmgattr -S [attrname]
67  rmvattr, remove an attribute to any given variable: rmvattr -S [attrname]
68"""
69
70#print string_operation
71
72operationnames = "'" + gen.numVector_String(operations, "', '") + "'"
73
74parser = OptionParser()
75parser.add_option("-f", "--netCDF_file", dest="ncfile", help="file to use", 
76  metavar="FILE")
77parser.add_option("-o", "--operation", type='choice', dest="operation", 
78  choices=operations, help="operation to make: " + operationnames, metavar="OPER")
79parser.add_option("-S", "--valueS", dest="values", 
80  help="values to use according to the operation (when applicable); '-h' specific help of the operation", metavar="VALUES")
81parser.add_option("-v", "--variable", dest="varname",
82  help="variable to use (when applicable)", metavar="VAR")
83
84(opts, args) = parser.parse_args()
85
86#if opts.help:
87#  parser.print_help()
88#  print string_operation
89#  sys.exit()
90
91#######    #######
92## MAIN
93    #######
94
95# Operations which file name is not a real file
96NotCheckingFile = ['DatesFiles', 'file_creation', 'list_operations',                 \
97  'netcdf_concatenation', 'netcdf_fold_concatenation']
98
99####### ###### ##### #### ### ## #
100errormsg='ERROR -- error -- ERROR -- error'
101
102varn=opts.varname
103oper=opts.operation
104
105if opts.ncfile is not None and not os.path.isfile(opts.ncfile) and                   \
106  not gen.searchInlist(NotCheckingFile,oper):
107    print errormsg
108    print '  File ' + opts.ncfile + ' does not exist !!'
109    quit(-1)
110
111if oper == 'addvals':
112    ncvar.addvals(opts.values, opts.ncfile, opts.varname)
113elif oper == 'chdimname':
114    ncvar.chdimname(opts.values, opts.ncfile, opts.varname)
115elif oper == 'changevartype':
116    ncvar.changevartype(opts.values, opts.ncfile, opts.varname)
117elif oper == 'checkallvars':
118    ncvar.checkallvars(opts.values, opts.ncfile)
119elif oper == 'checkAllValues':
120    ncvar.checkAllValues(opts.values, opts.ncfile)
121elif oper == 'checkNaNs':
122    ncvar.checkNaNs(opts.values, opts.ncfile)
123elif oper == 'chgtimestep':
124    ncvar.chgtimestep(opts.values, opts.ncfile, opts.varname)
125elif oper == 'chvarname':
126    ncvar.chvarname(opts.values, opts.ncfile, opts.varname)
127elif oper == 'compute_deaccum':
128    ncvar.compute_deaccum(opts.values, opts.ncfile, opts.varname)
129elif oper == 'compute_opersvarsfiles':
130    ncvar.compute_opersvarsfiles(opts.values, opts.varname)
131elif oper == 'compute_opervaralltime':
132    ncvar.compute_opervaralltime(opts.values, opts.ncfile, opts.varname)
133elif oper == 'compute_opervartimes':
134    ncvar.compute_opervartimes(opts.values, opts.ncfile, opts.varname)
135elif oper == 'compute_tevolboxtraj':
136    ncvar.compute_tevolboxtraj(opts.values, opts.ncfile, opts.varname)
137elif oper == 'DataSetSection':
138    ncvar.DataSetSection(opts.values, opts.ncfile)
139elif oper == 'DataSetSection_multidims':
140    ncvar.DataSetSection_multidims(opts.values, opts.ncfile, opts.varname)
141elif oper == 'DataSetSection_multivars':
142    ncvar.DataSetSection_multivars(opts.values, opts.ncfile, opts.varname)
143elif oper == 'DatesFiles':
144    ncvar.DatesFiles(opts.values, opts.ncfile, opts.varname)
145elif oper == 'dimToUnlimited':
146    ncvar.dimToUnlimited(opts.values, opts.ncfile)
147elif oper == 'dimVar_creation':
148    ncvar.dimVar_creation(opts.values, opts.ncfile)
149elif oper == 'fattradd':
150    ncvar.fattradd(var, opts.values, opts.ncfile)
151elif oper == 'fdimadd':
152    ncvar.fdimadd(opts.values, opts.ncfile)
153elif oper == 'fgaddattr':
154    ncvar.fgaddattr(opts.values, opts.ncfile)
155elif oper == 'file_creation':
156    ncvar.file_creation(opts.values, opts.ncfile, opts.varname)
157elif oper == 'file_oper_alongdims':
158    ncvar.file_oper_alongdims(opts.values, opts.ncfile, opts.varname)
159elif oper == 'field_stats':
160    ncvar.field_stats(opts.values, opts.ncfile, opts.varname)
161elif oper == 'filter_2dim':
162    ncvar.filter_2dim(opts.values, opts.ncfile, opts.varname)
163elif oper == 'flipdim':
164    ncvar.flipdim(opts.values, opts.ncfile, opts.varname)
165elif oper == 'fvaradd':
166    ncvar.fvaradd(opts.values, opts.ncfile)
167elif oper == 'gaddattrk':
168    ncvar.gaddattrk(opts.values, opts.ncfile)
169elif oper == 'gaddattr':
170    ncvar.gaddattr(opts.values, opts.ncfile)
171elif oper == 'get_attribute':
172    ncvar.get_attribute(opts.values, opts.ncfile, opts.varname)
173elif oper == 'get_namelist_vars':
174    ncvar.get_namelist_vars(opts.values, opts.ncfile)
175elif oper == 'getvalues_lonlat':
176    ncvar.getvalues_lonlat(opts.values, opts.ncfile)
177elif oper == 'grattr':
178    ncvar.grattr(opts.values, opts.ncfile)
179elif oper == 'grmattr':
180    ncvar.grmattr(opts.values, opts.ncfile)
181elif oper == 'igattrs':
182    ncvar.igattrs(opts.ncfile)
183elif oper == 'increaseDimvar':
184    ncvar.increaseDimvar(opts.values, opts.ncfile, opts.varname)
185elif oper == 'isgattrs':
186    ncvar.isgattrs(opts.values, opts.ncfile)
187elif oper == 'isvattrs':
188    ncvar.isvattrs(opts.values, opts.ncfile, opts.varname)
189elif oper == 'ivars':
190    ncvar.ivars(opts.ncfile)
191elif oper == 'ivattrs':
192    ncvar.ivattrs(opts.ncfile, opts.varname)
193elif oper == 'list_operations':
194# From: http://www.diveintopython.net/power_of_introspection/all_together.html
195    object = ncvar
196    for opern in operations:
197        if  opern != 'list_operations': 
198            print opern + '_______ ______ _____ ____ ___ __ _'
199            print getattr(object, opern).__doc__
200elif oper == 'maskvar':
201    ncvar.maskvar(opts.values, opts.ncfile, opts.varname)
202elif oper == 'ncreplace':
203    ncvar.ncreplace(opts.values, opts.ncfile, opts.varname)
204elif oper == 'ncstepdiff':
205    ncvar.ncstepdiff(opts.values, opts.ncfile, opts.varname)
206elif oper == 'netcdf_concatenation':
207    ncvar.netcdf_concatenation(opts.ncfile)
208elif oper == 'netcdf_fold_concatenation':
209    ncvar.netcdf_fold_concatenation(opts.values, opts.ncfile, opts.varname)
210elif oper == 'opersvarsfiles':
211    ncvar.compute_opersvarsfiles(opts.values, opts.varname)
212elif oper == 'remapnn':
213    ncvar.remapnn(opts.values, opts.ncfile, opts.varname)
214elif oper == 'Partialmap_Entiremap':
215    ncvar.Partialmap_Entiremap(opts.values, opts.ncfile, opts.varname)
216elif oper == 'Partialmap_EntiremapFor':
217    ncvar.Partialmap_EntiremapFor(opts.values, opts.ncfile, opts.varname)
218elif oper == 'Partialmap_EntiremapForExact':
219    ncvar.Partialmap_EntiremapForExact(opts.values, opts.ncfile, opts.varname)
220elif oper == 'seasmean':
221    ncvar.seasmean(timename, opts.ncfile, opts.varname)
222elif oper == 'sellonlatbox':
223    ncvar.sellonlatbox(opts.values, opts.ncfile, opts.varname)
224elif oper == 'sellonlatlevbox':
225    ncvar.sellonlatlevbox(opts.values, opts.ncfile, opts.varname)
226elif oper == 'selvar':
227    ncvar.selvar(opts.values, opts.ncfile, opts.varname)
228elif oper == 'setvar_asciivalues':
229    ncvar.setvar_asciivalues(opts.values, opts.ncfile, opts.varname)
230elif oper == 'sorttimesmat':
231    ncvar.sorttimesmat(opts.ncfile, opts.varname)
232elif oper == 'spacemean':
233    ncvar.spacemean(opts.ncfile, opts.varname)
234elif oper == 'SpatialWeightedMean':
235    ncvar.SpatialWeightedMean(opts.values, opts.ncfile, opts.varname)
236elif oper == 'statcompare_files':
237    ncvar.statcompare_files(opts.values)
238elif oper == 'subbasin':
239    ncvar.subbasin(opts.values, opts.ncfile)
240elif oper == 'submns':
241    ncvar.submns(opts.values, opts.ncfile, opts.varname)
242elif oper == 'subyrs':
243    ncvar.subyrs(opts.values, opts.ncfile, opts.varname)
244elif oper == 'TimeInf':
245    ncvar.TimeInf(opts.ncfile, opts.varname)
246elif oper == 'TimeSplitmat':
247    ncvar.TimeSplitmat(opts.values, opts.ncfile, opts.varname)
248elif oper == 'timemean':
249    ncvar.timemean(opts.values, opts.ncfile, opts.varname)
250elif oper == 'valmod':
251    ncvar.valmod(opts.values, opts.ncfile, opts.varname)
252elif oper == 'valmod_dim':
253    ncvar.valmod_dim(opts.values, opts.ncfile, opts.varname)
254elif oper == 'varaddattrk':
255    ncvar.varaddattrk(opts.values, opts.ncfile, opts.varname)
256elif oper == 'varaddattr':
257    ncvar.varaddattr(opts.values, opts.ncfile, opts.varname)
258elif oper == 'varaddref':
259    ncvar.varaddref(opts.values, opts.ncfile, opts.varname)
260elif oper == 'var_creation':
261    ncvar.var_creation(opts.values, opts.ncfile, opts.varname)
262elif oper == 'varout':
263    ncvar.varout(opts.values, opts.ncfile, opts.varname)
264elif oper == 'varoutold':
265    ncvar.varoutold(opts.values, opts.ncfile, opts.varname)
266elif oper == 'varrmattr':
267    ncvar.varrmattr(opts.values, opts.ncfile, opts.varname)
268elif oper == 'varrm':
269    ncvar.varrm(opts.ncfile, opts.varname)
270elif oper == 'VarVal_FillValue':
271    ncvar.VarVal_FillValue(opts.values, opts.ncfile, opts.varname)
272elif oper == 'vrattr':
273    ncvar.vrattr(opts.values, opts.ncfile, opts.varname)
274elif oper == 'WRF_d0Nref':
275    ncvar.WRF_d0Nref(opts.values, opts.ncfile)
276elif oper == 'WRF_CFlonlat_creation':
277    ncvar.WRF_CFlonlat_creation(opts.values, opts.ncfile, opts.varname)
278elif oper == 'WRF_CFtime_creation':
279    ncvar.WRF_CFtime_creation(opts.values, opts.ncfile, opts.varname)
280elif oper == 'WRF_CFxtime_creation':
281    ncvar.WRF_CFxtime_creation(opts.values, opts.ncfile, opts.varname)
282else:
283    print errormsg
284    print '   The operation ' + oper + ' is not ready !!'
285    print errormsg
286    quit()
Note: See TracBrowser for help on using the repository browser.