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

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

Adding `DataSetSection_multivars'

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