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

Last change on this file since 403 was 397, checked in by lfita, 10 years ago

Adding 'dimToUnlimited' to transform a dimension to unlimited
Adding FillValue? on the variables for 'file_creation' and 'var_creation'

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