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