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

Last change on this file since 795 was 778, checked in by lfita, 9 years ago

Fixing som `gen' calls

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