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

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

Adding 'remapnn' Function to remap to the nearest neightbor a variable using projection from another file

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