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

Last change on this file since 1212 was 1181, checked in by lfita, 9 years ago

Adding `field_stats_dim': Function to retrieve statistics from a field along dimensions

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