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