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

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

Adding 'selvar' to select a list of variables from an existing file.
New functions:

'add_dims': add dimensions from a given netCDF object to another one
'add_vars': add variables from a given netCDF object to another one
'add_globattrs': add global attributes from a given netCDF object to another one

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