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