source: lmdz_wrf/branches/LMDZ_WRFmeas/tools/nc_var.py

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

Removing WRFV3 folder and keeping only the essential

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