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