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

Last change on this file since 948 was 905, checked in by lfita, 9 years ago

Adding `netcdf_fold_concatenation_HMT': Function to concatenate netCDF files in a given folder for a given set of variables giving Header, Middle, Tail for the name files

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