[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 | |
---|
| 13 | from optparse import OptionParser |
---|
| 14 | import numpy as np |
---|
| 15 | from netCDF4 import Dataset as NetCDFFile |
---|
| 16 | import os |
---|
| 17 | import re |
---|
| 18 | import nc_var_tools as ncvar |
---|
| 19 | |
---|
[408] | 20 | operations=['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,...]" |
---|
| 46 | string_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 | |
---|
| 64 | operationnames = "'" + ncvar.numVector_String(operations, "', '") + "'" |
---|
| 65 | |
---|
| 66 | parser = OptionParser() |
---|
| 67 | parser.add_option("-f", "--netCDF_file", dest="ncfile", |
---|
| 68 | help="file to use", metavar="FILE") |
---|
| 69 | parser.add_option("-o", "--operation", type='choice', dest="operation", |
---|
| 70 | choices=operations, help="operation to make: " + operationnames, metavar="OPER") |
---|
| 71 | parser.add_option("-S", "--valueS (when applicable)", dest="values", |
---|
| 72 | help="values to use according to the operation", metavar="VALUES") |
---|
| 73 | parser.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] | 88 | NotCheckingFile = ['DatesFiles', 'file_creation', 'list_operations', \ |
---|
| 89 | 'netcdf_concatenation', 'netcdf_fold_concatenation'] |
---|
[192] | 90 | |
---|
| 91 | ####### ###### ##### #### ### ## # |
---|
| 92 | errormsg='ERROR -- error -- ERROR -- error' |
---|
| 93 | |
---|
| 94 | varn=opts.varname |
---|
| 95 | oper=opts.operation |
---|
| 96 | |
---|
| 97 | if 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 | |
---|
| 103 | if oper == 'addvals': |
---|
| 104 | ncvar.addvals(opts.values, opts.ncfile, opts.varname) |
---|
| 105 | elif oper == 'chdimname': |
---|
| 106 | ncvar.chdimname(opts.values, opts.ncfile, opts.varname) |
---|
[408] | 107 | elif oper == 'changevartype': |
---|
| 108 | ncvar.changevartype(opts.values, opts.ncfile, opts.varname) |
---|
[192] | 109 | elif oper == 'checkallvars': |
---|
| 110 | ncvar.checkallvars(opts.values, opts.ncfile) |
---|
| 111 | elif oper == 'checkAllValues': |
---|
| 112 | ncvar.checkAllValues(opts.values, opts.ncfile) |
---|
| 113 | elif oper == 'checkNaNs': |
---|
| 114 | ncvar.checkNaNs(opts.values, opts.ncfile) |
---|
| 115 | elif oper == 'chgtimestep': |
---|
| 116 | ncvar.chgtimestep(opts.values, opts.ncfile, opts.varname) |
---|
| 117 | elif oper == 'chvarname': |
---|
| 118 | ncvar.chvarname(opts.values, opts.ncfile, opts.varname) |
---|
| 119 | elif oper == 'compute_deaccum': |
---|
| 120 | ncvar.compute_deaccum(opts.values, opts.ncfile, opts.varname) |
---|
| 121 | elif oper == 'compute_opersvarsfiles': |
---|
| 122 | ncvar.compute_opersvarsfiles(opts.values, opts.varname) |
---|
| 123 | elif oper == 'compute_opervaralltime': |
---|
| 124 | ncvar.compute_opervaralltime(opts.values, opts.ncfile, opts.varname) |
---|
| 125 | elif oper == 'compute_opervartimes': |
---|
| 126 | ncvar.compute_opervartimes(opts.values, opts.ncfile, opts.varname) |
---|
| 127 | elif oper == 'compute_tevolboxtraj': |
---|
| 128 | ncvar.compute_tevolboxtraj(opts.values, opts.ncfile, opts.varname) |
---|
| 129 | elif oper == 'DataSetSection': |
---|
| 130 | ncvar.DataSetSection(opts.values, opts.ncfile) |
---|
| 131 | elif oper == 'DataSetSection_multidims': |
---|
| 132 | ncvar.DataSetSection_multidims(opts.values, opts.ncfile) |
---|
[576] | 133 | elif oper == 'DataSetSection_multivars': |
---|
| 134 | ncvar.DataSetSection_multivars(opts.values, opts.ncfile, opts.varname) |
---|
[507] | 135 | elif oper == 'DatesFiles': |
---|
| 136 | ncvar.DatesFiles(opts.values, opts.ncfile, opts.varname) |
---|
[397] | 137 | elif oper == 'dimToUnlimited': |
---|
| 138 | ncvar.dimToUnlimited(opts.values, opts.ncfile) |
---|
[192] | 139 | elif oper == 'dimVar_creation': |
---|
| 140 | ncvar.dimVar_creation(opts.values, opts.ncfile) |
---|
| 141 | elif oper == 'fattradd': |
---|
| 142 | ncvar.fattradd(var, opts.values, opts.ncfile) |
---|
| 143 | elif oper == 'fdimadd': |
---|
| 144 | ncvar.fdimadd(opts.values, opts.ncfile) |
---|
| 145 | elif oper == 'fgaddattr': |
---|
| 146 | ncvar.fgaddattr(opts.values, opts.ncfile) |
---|
[386] | 147 | elif oper == 'file_creation': |
---|
| 148 | ncvar.file_creation(opts.values, opts.ncfile, opts.varname) |
---|
[192] | 149 | elif oper == 'file_oper_alongdims': |
---|
| 150 | ncvar.file_oper_alongdims(opts.values, opts.ncfile, opts.varname) |
---|
| 151 | elif oper == 'field_stats': |
---|
| 152 | ncvar.field_stats(opts.values, opts.ncfile, opts.varname) |
---|
| 153 | elif oper == 'filter_2dim': |
---|
| 154 | ncvar.filter_2dim(opts.values, opts.ncfile, opts.varname) |
---|
| 155 | elif oper == 'flipdim': |
---|
| 156 | ncvar.flipdim(opts.values, opts.ncfile, opts.varname) |
---|
| 157 | elif oper == 'fvaradd': |
---|
| 158 | ncvar.fvaradd(opts.values, opts.ncfile) |
---|
| 159 | elif oper == 'gaddattrk': |
---|
| 160 | ncvar.gaddattrk(opts.values, opts.ncfile) |
---|
| 161 | elif oper == 'gaddattr': |
---|
| 162 | ncvar.gaddattr(opts.values, opts.ncfile) |
---|
| 163 | elif oper == 'get_namelist_vars': |
---|
| 164 | ncvar.get_namelist_vars(opts.values, opts.ncfile) |
---|
| 165 | elif oper == 'grattr': |
---|
| 166 | ncvar.grattr(opts.values, opts.ncfile) |
---|
| 167 | elif oper == 'grmattr': |
---|
| 168 | ncvar.grmattr(opts.values, opts.ncfile) |
---|
| 169 | elif oper == 'igattrs': |
---|
| 170 | ncvar.igattrs(opts.ncfile) |
---|
[404] | 171 | elif oper == 'increaseDimvar': |
---|
| 172 | ncvar.increaseDimvar(opts.values, opts.ncfile, opts.varname) |
---|
[192] | 173 | elif oper == 'isgattrs': |
---|
| 174 | ncvar.isgattrs(opts.values, opts.ncfile) |
---|
| 175 | elif oper == 'isvattrs': |
---|
| 176 | ncvar.isvattrs(opts.values, opts.ncfile, opts.varname) |
---|
| 177 | elif oper == 'ivars': |
---|
| 178 | ncvar.ivars(opts.ncfile) |
---|
| 179 | elif oper == 'ivattrs': |
---|
| 180 | ncvar.ivattrs(opts.ncfile, opts.varname) |
---|
| 181 | elif 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__ |
---|
| 188 | elif oper == 'maskvar': |
---|
| 189 | ncvar.maskvar(opts.values, opts.ncfile, opts.varname) |
---|
| 190 | elif oper == 'ncreplace': |
---|
| 191 | ncvar.ncreplace(opts.values, opts.ncfile, opts.varname) |
---|
[311] | 192 | elif oper == 'ncstepdiff': |
---|
| 193 | ncvar.ncstepdiff(opts.values, opts.ncfile, opts.varname) |
---|
[192] | 194 | elif oper == 'netcdf_concatenation': |
---|
| 195 | ncvar.netcdf_concatenation(opts.ncfile) |
---|
| 196 | elif oper == 'netcdf_fold_concatenation': |
---|
| 197 | ncvar.netcdf_fold_concatenation(opts.values, opts.ncfile, opts.varname) |
---|
| 198 | elif oper == 'opersvarsfiles': |
---|
| 199 | ncvar.compute_opersvarsfiles(opts.values, opts.varname) |
---|
[409] | 200 | elif oper == 'remapnn': |
---|
| 201 | ncvar.remapnn(opts.values, opts.ncfile, opts.varname) |
---|
[192] | 202 | elif oper == 'seasmean': |
---|
| 203 | ncvar.seasmean(timename, opts.ncfile, opts.varname) |
---|
| 204 | elif oper == 'sellonlatbox': |
---|
| 205 | ncvar.sellonlatbox(opts.values, opts.ncfile, opts.varname) |
---|
| 206 | elif oper == 'sellonlatlevbox': |
---|
| 207 | ncvar.sellonlatlevbox(opts.values, opts.ncfile, opts.varname) |
---|
[418] | 208 | elif oper == 'selvar': |
---|
| 209 | ncvar.selvar(opts.values, opts.ncfile, opts.varname) |
---|
[192] | 210 | elif oper == 'setvar_asciivalues': |
---|
| 211 | ncvar.setvar_asciivalues(opts.values, opts.ncfile, opts.varname) |
---|
| 212 | elif oper == 'sorttimesmat': |
---|
| 213 | ncvar.sorttimesmat(opts.ncfile, opts.varname) |
---|
| 214 | elif oper == 'spacemean': |
---|
| 215 | ncvar.spacemean(opts.ncfile, opts.varname) |
---|
| 216 | elif oper == 'statcompare_files': |
---|
| 217 | ncvar.statcompare_files(opts.values) |
---|
| 218 | elif oper == 'submns': |
---|
| 219 | ncvar.submns(opts.values, opts.ncfile, opts.varname) |
---|
| 220 | elif oper == 'subyrs': |
---|
| 221 | ncvar.subyrs(opts.values, opts.ncfile, opts.varname) |
---|
| 222 | elif oper == 'TimeInf': |
---|
| 223 | ncvar.TimeInf(opts.ncfile, opts.varname) |
---|
[533] | 224 | elif oper == 'TimeSplitmat': |
---|
| 225 | ncvar.TimeSplitmat(opts.values, opts.ncfile, opts.varname) |
---|
[192] | 226 | elif oper == 'timemean': |
---|
| 227 | ncvar.timemean(opts.values, opts.ncfile, opts.varname) |
---|
| 228 | elif oper == 'valmod': |
---|
| 229 | ncvar.valmod(opts.values, opts.ncfile, opts.varname) |
---|
| 230 | elif oper == 'valmod_dim': |
---|
| 231 | ncvar.valmod_dim(opts.values, opts.ncfile, opts.varname) |
---|
| 232 | elif oper == 'varaddattrk': |
---|
| 233 | ncvar.varaddattrk(opts.values, opts.ncfile, opts.varname) |
---|
| 234 | elif oper == 'varaddattr': |
---|
| 235 | ncvar.varaddattr(opts.values, opts.ncfile, opts.varname) |
---|
| 236 | elif oper == 'varaddref': |
---|
| 237 | ncvar.varaddref(opts.values, opts.ncfile, opts.varname) |
---|
[387] | 238 | elif oper == 'var_creation': |
---|
| 239 | ncvar.var_creation(opts.values, opts.ncfile, opts.varname) |
---|
[192] | 240 | elif oper == 'varout': |
---|
| 241 | ncvar.varout(opts.values, opts.ncfile, opts.varname) |
---|
| 242 | elif oper == 'varoutold': |
---|
| 243 | ncvar.varoutold(opts.values, opts.ncfile, opts.varname) |
---|
| 244 | elif oper == 'varrmattr': |
---|
| 245 | ncvar.varrmattr(opts.values, opts.ncfile, opts.varname) |
---|
| 246 | elif oper == 'varrm': |
---|
| 247 | ncvar.varrm(opts.ncfile, opts.varname) |
---|
| 248 | elif oper == 'vrattr': |
---|
| 249 | ncvar.vrattr(opts.values, opts.ncfile, opts.varname) |
---|
| 250 | elif oper == 'WRF_d0Nref': |
---|
| 251 | ncvar.WRF_d0Nref(opts.values, opts.ncfile) |
---|
| 252 | elif oper == 'WRF_CFlonlat_creation': |
---|
| 253 | ncvar.WRF_CFlonlat_creation(opts.values, opts.ncfile, opts.varname) |
---|
| 254 | elif oper == 'WRF_CFtime_creation': |
---|
| 255 | ncvar.WRF_CFtime_creation(opts.values, opts.ncfile, opts.varname) |
---|
| 256 | elif oper == 'WRF_CFxtime_creation': |
---|
| 257 | ncvar.WRF_CFxtime_creation(opts.values, opts.ncfile, opts.varname) |
---|
| 258 | else: |
---|
| 259 | print errormsg |
---|
| 260 | print ' The operation ' + oper + ' is not ready !!' |
---|
| 261 | print errormsg |
---|
| 262 | quit() |
---|