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

Last change on this file since 1165 was 1092, checked in by lfita, 9 years ago

Addin `time_reset': Function to re-set the time axis of a file

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