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

Last change on this file since 740 was 735, checked in by lfita, 9 years ago

Seems to working version (from scratch) as `Partialmap_EntiremapForExact' directly without coarse matrices

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