1 | #!/usr/bin/python |
---|
2 | # Python to manage netCDF files. |
---|
3 | # From L. Fita work in different places: CCRC (Australia), LMD (France) |
---|
4 | # More information at: http://www.xn--llusfb-5va.cat/python/PyNCplot |
---|
5 | # |
---|
6 | # pyNCplot and its component nc_var.py comes with ABSOLUTELY NO WARRANTY. |
---|
7 | # This work is licendes under a Creative Commons |
---|
8 | # Attribution-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-sa/4.0) |
---|
9 | # |
---|
10 | |
---|
11 | ## e.g. # nc_var.py -o lonlat_polygon -f wrfout_d01_1995-01-01_00:00:00 -S star.dat -v XLONG,XLAT |
---|
12 | ## e.g. # nc_var.py -o maskvar -f SouthAm_Climzones_masked.nc -S 'geo_em.d01.nc:LANDMASK:Time|0:0' -v region:lon,lat |
---|
13 | ## e.g. # nc_var.py -o addDim -S 128 -f SouthAm_Climzones.nc -v Lstring |
---|
14 | ## e.g. # nc_var.py -o addVar -S 'reg,Lstring|region_name@name!of!the!regions@-|c' -f SouthAm_Climzones.nc -v regionn |
---|
15 | ## e.g. # nc_var.py -o varaddattr -f SouthAm_Climzones.nc -S 'grid_mapping_name|Mercator' -v Mercator |
---|
16 | ## e.g. # nc_var.py -o varaddattrk -f SouthAm_Climzones.nc -S 'longitude_of_central_meridian|-60.0|R' -v Mercator |
---|
17 | ## e.g. # nc_var.py -o setvar_asciivalues -S 'nombres_zonas.dat' -f SouthAm_Climzones.nc -v regionn |
---|
18 | ## e.g. # nc_var.py -o WRF_to_newCF -f wrfout_d01_1995-01-01_00\:00\:00 -S 'XLONG:XLAT:Times:19491201000000:minutes' -v QVAPOR,T2,Q2 |
---|
19 | ## e.g. # nc_var.py -o reconstruct_matrix_from_vector -f cruncep_halfdeg_1958.nc -S 'nav_lon:nav_lat:-90.:-7.:-67.:15.:latlon:0.5:0.5:0.05' -v all |
---|
20 | |
---|
21 | |
---|
22 | ## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1 |
---|
23 | ## e.g. ccrc468-17 # ./nc_var.py -v prac -f xyz/CCRC_NARCliM_Sydney_DAM_1990-1999_prac.nc -o mname -S pluja |
---|
24 | ## 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.' |
---|
25 | ## 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' |
---|
26 | ## 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' |
---|
27 | ## 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' |
---|
28 | ## 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' |
---|
29 | ## e.g. # nc_var.py -o field_stats -f ~/etudes/domains/Polynesie/geo_em.d03.nc -S full -v HGT_M |
---|
30 | ## e.g. # nc_var.py -o filter_2dim -S '80,y,x,lon,lat' -f 'tahiti_5m_ll.grd' -v 'z' |
---|
31 | ## 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 |
---|
32 | ## e.g. # nc_var.py -o 'Partialmap_Entiremap' -f carteveg5km.nc -S 'longitude,latitude,std,5000.,Goode,Goode_5km.nc' -v vegetation_map |
---|
33 | ## e.g. # nc_var.py -o subbasin -f routing.nc -S Caceres,-57.75,-16.25,left |
---|
34 | ## e.g. # nc_var.py -o computevar_model -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S hurs |
---|
35 | ## e.g. # nc_var.py -o model_characteristics -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S WRF |
---|
36 | ## e.g. # nc_var.py -o WRF_toCF -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S XLONG:XLAT:19491201000000:minutes |
---|
37 | ## e.g. # nc_var.py -o cleaning_varsfile -f ~/PY/wrfout_d01_2001-11-11_00:00:00 -S T2,XLONG,XLAT,Times |
---|
38 | ## 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' |
---|
39 | ## e.g. # nc_var.py -o CDO_toCF -f LMDZ/AR40/hfss_reproj-histins.nc |
---|
40 | ## 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 |
---|
41 | ## 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 |
---|
42 | ## e.g. # nc_var.py -o reproject -f analysis/LMDZ/AR40/hurs_histins.nc -S 'lon,lat,analysis/WRF/current/hurs_wrfout.nc,lon,lat,npp,time@time' -v hurs |
---|
43 | ## e.g. # nc_var.py -o field_stats_dim -f /home/lluis/PY/wrfout_d01_2001-11-11_00:00:00 -S 'full,None,None,west_east,XLONG,False' -v 'T2' |
---|
44 | ## e.g. # nc_var.py -o compute_opersvarsfiles -S 'west_east|XLONG|-1;south_north|XLAT|-1;Time|Times|3@add|wrfout_d01_2001-11-11_00:00:00|T2%west_east|XLONG|-1;south_north|XLAT|-1;Time|Times|3@subc,273.15|wrfout_d01_2001-11-11_00:00:00|None' -v 'tempC,air!temperature,C' |
---|
45 | ## e.g. # nc_var.py -o compute_opersvarsfiles -S 'lon|lon|-1;lat|lat|-1;time_counter|time_counter|-1@forwrdderiv,1,1,2|/ccc/store/cont003/gen7593/fitaborl/etudes/DynamicoESM/aquaplanet/AR40/19800101000000-19810101000000/histday.nc|t2m' -v 'tasderiv,x-derivative|of|air|temperature,K |
---|
46 | ## e.g. # nc_var.py -o getvars_tofile -S soils_param_Zobler_textXIOS.nc -f soils_param_colorXIOS.nc -v 'soiltext,soiltexttypes' |
---|
47 | ## e.g. # nc_var.py -o addVar -S 'lon,lat|mask@mask@1|f' -f new_ORCHIDEE_13PFTmap_2010_cmpi6_LUH2v2h.nc -v mask |
---|
48 | ## e.g. # nc_var.py -o setvar_nc -S 'new_ORCHIDEE_13PFTmap_2010_cmpi6_LUH2v2h.nc,maxvegetfrac,veget;0' -f new_ORCHIDEE_13PFTmap_2010_cmpi6_LUH2v2h.nc -v mask |
---|
49 | ## e.g. $ nc_var.py -o VarVal_FillValue -S '0.,eq,std' -f new_lai2D.nc -v LAI |
---|
50 | ## e.g. # nc_var.py -o rm_FillValue -f new_ORCHIDEE_13PFTmap_2010_cmpi6_LUH2v2h.nc -S 0 -v mask |
---|
51 | ## e.g. # nc_var.py -o curve_section -f /home/lluis/PY/test.nc -S 'gridline,x,y,8.,8.,16.,16.,32' -v all |
---|
52 | ## e.g. # nc_var.py -o merge_files -S 'plev|plev,time|time:merged.nc' -f 'ncobs/AliceSprings/snd_94326_198201-198201.nc,ncobs/AliceSprings/snd_94326_198202-198202.nc' -v 'plev,time' |
---|
53 | |
---|
54 | from optparse import OptionParser |
---|
55 | import numpy as np |
---|
56 | from netCDF4 import Dataset as NetCDFFile |
---|
57 | import os |
---|
58 | import re |
---|
59 | import nc_var_tools as ncvar |
---|
60 | # Using 'generic_tools.py' |
---|
61 | import generic_tools as gen |
---|
62 | |
---|
63 | # addDim: Operation to add a new dimension in a file |
---|
64 | # addVar: Operation to add a new variable in a file using exisintg dimensions |
---|
65 | # addvals: Function to add values to a given variable at a given dimension |
---|
66 | # CDO_toCF: Function to pass a CDO output file to CF-conventions |
---|
67 | # chdimname: Changing the name of the dimension |
---|
68 | # changevartype: Function to change the type of a variable (when possible) |
---|
69 | # checkallvars: Function to check all variables of a file |
---|
70 | # checkAllValues: Function to check for variables with along all their dimensions with the same value in a file |
---|
71 | # checkNaNs: Function to check for NaN values over all variables in a file |
---|
72 | # chgtimestep: Function to change the values of a given time-step of a variable inside a netCDF for values from a nother file |
---|
73 | # chvarname: Changing the name of the variable |
---|
74 | # cleaning_varsfile: Function to keep a list of varibales from a file |
---|
75 | # compute_deaccum: Function to compute deaccum: deaccumulation of a variable (VAR[t+1]-VAR[t]) |
---|
76 | # compute_opersvarsfiles: Function to compute opersvarfiles: operation of variables from different files |
---|
77 | # compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps |
---|
78 | # compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps |
---|
79 | # compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory |
---|
80 | # computevar_model: Function to provide the way to compute a CF-variable providing its name |
---|
81 | # curve_section: Function to provide a section of a file following a given curve |
---|
82 | # DatesFiles: Function to find different time values on a series of WRF files in a folder |
---|
83 | # DataSetSection: Function to get a section (values along a dimension) of a given data-set |
---|
84 | # DataSetSection_multidims: Function to get a section (values along multiple dimensions) of a given data-set |
---|
85 | # DataSetSection_multivars: Function to get a section (values along multiple variables) of a given data-set |
---|
86 | # DYNAMICO_toCF: Function to pass a DYNAMICO original file to CF-conventions |
---|
87 | # dimToUnlimited: Operation to create an unlimited dimension from an existing one |
---|
88 | # dimVar_creation: Function to add a 1D variable with the size of a given dimension in a file |
---|
89 | # fattradd: Adding attributes from a reference file |
---|
90 | # fdimadd: Adding dimension from another reference file |
---|
91 | # fgaddattr: Adding global attributes from a reference file |
---|
92 | # field_stats: Function to retrieve statistics from a field |
---|
93 | # field_stats_dim: Function to retrieve statistics from a field along dimensions |
---|
94 | # file_creation: Operation to create a file with one variable with a given set of dimensions |
---|
95 | # file_oper_alongdims: Function to operate a file along different dimensions of a variable |
---|
96 | # filter_2dim: Function to filter along 2 dimensions (moving grid-box means of a given size) values of the netCDF file |
---|
97 | # flipdim: flips the value following one dimension |
---|
98 | # fvaradd: Adding variable (and all its attributes and dimensions) from a reference file to a file |
---|
99 | # gaddattrk: Add a global attribute to a netCDF caring about the type. Removes previous attribute if it exist |
---|
100 | # gaddattr: Add a global attribute to a netCDF. Removes previous attribute if it exist |
---|
101 | # get_attribute: Function to get an attribute from a netCDF file |
---|
102 | # get_namelist_vars: Function to get namelist-like values ([varname] = [value]) |
---|
103 | # get_Variables: Function to get a list of variables from an existing file |
---|
104 | # getvalues_lonlat: Function to retrieve the values from the closest grid point to a set of longitude, latitude values |
---|
105 | # getvars_tofile: Function to get variables from a file and pass them to another one |
---|
106 | # grattr: Function to read a global atribute |
---|
107 | # grmattr: Removing a global attribute |
---|
108 | # idims: Give all the dimensions names of a file |
---|
109 | # igattrs: Give all the global attributes of a file |
---|
110 | # increaseDimvar: Function to increase with 1 dimension an existing variable within a netcdf file. Values of |
---|
111 | # the variable will be repeated along the new dimension |
---|
112 | # isgattrs: Give a single global attribute of a file and its type |
---|
113 | # isvattrs: Give a single attribute of a variable |
---|
114 | # ivars: Give all the variable names of a file |
---|
115 | # ivattrs: Give all the attributes of a variable and its type |
---|
116 | # LMDZ_toCF: Function to pass a LMDZ original file to CF-conventions |
---|
117 | # lonlat_polygon: Function to define a lon/lat region giving the coordinates of the vertexs of a given polygon |
---|
118 | # maskvar: Function to mask a variable using another variable to mask it. Only need to share at least 1 dimension with the same size (no need same name) |
---|
119 | # merge_files: Function to merge variables from two files |
---|
120 | # model_characteristics: Function to provide major characterisitcs of a given model output |
---|
121 | # mthDYNAMICO_toCF: Function to pass a mthDYNAMICO original file to CF-conventions |
---|
122 | # ncreplace: Function to replace something from a netCDF file |
---|
123 | # ncstepdiff: Function to compute differencies between time-steps (deacumulate) a netCDF file |
---|
124 | # netcdf_concatenation: Function to concatenate netCDF files for a given set of variables |
---|
125 | # netcdf_fold_concatenation: Function to concatenate netCDF files in a given folder for a given set of variables |
---|
126 | # netcdf_fold_concatenation_HMT: Function to concatenate netCDF files in a given folder for a given set of variables |
---|
127 | # giving Header, Middle, Tail for the name files |
---|
128 | # reconstruct_matrix_from_vector: Function to reconstruct a 2D matrix from a pair of syncronized vectors with the positions on x and y coordinates |
---|
129 | # reproject: Function to reproject values to another one |
---|
130 | # rm_FillValue: Operation to remove the _FillValue from a variable inside a netCDF file |
---|
131 | # Partialmap_Entiremap: Function to transform from a partial global map (e.g.: only land points) to an entire one |
---|
132 | # Coincidence of points is done throughout a first guess from fractions of the total domain of search |
---|
133 | # Partialmap_EntiremapFor: Function to transform from a partial global map (e.g.: only land points) to an entire one |
---|
134 | # using Fortran code Coincidence of points is done throughout a first guess from fractions of the total domain of search |
---|
135 | # Partialmap_EntiremapForExact: Function to transform from a partial global map (e.g.: only land points) to an entire |
---|
136 | # one using Fortran code with exact location Coincidence of points is done throughout a first guess from fractions of |
---|
137 | # the total domain of search |
---|
138 | # pinterp: Function to vertically interpolate using subroutines from the p_interp.F90 NCAR program |
---|
139 | # remapnn: Function to remap to the nearest neightbor a variable using projection from another file |
---|
140 | # seasmean: Function to compute the seasonal mean of a variable |
---|
141 | # sellonlatbox: Function to select a lotlan box from a data-set |
---|
142 | # sellonlatlevbox: Function to select a lotlan box and a given level from a data-set |
---|
143 | # selvar: Function to select a series of variables from a netcdf file. Variables with |
---|
144 | # setvar_asciivalues: Function to set de values of a variable with an ASCII file (common Fortran-like format) |
---|
145 | # setvar_nc: Operation to set values of a variable from values of an other one |
---|
146 | # sorttimesmat: Function to sort the time values of a given file |
---|
147 | # spacemean: Function to retrieve a space mean series from a multidimensional variable of a file |
---|
148 | # SpatialWeightedMean: Function to compute the spatial mean using weights from a netCDF file |
---|
149 | # statcompare_files: Python script to statistically compare two different files |
---|
150 | # subbasin: Function to retrieve the subbasin (all the sub-flows untila a given lon,lat) |
---|
151 | # submns: Function to retrieve a series of months from a file |
---|
152 | # subyrs: Function to retrieve a series of years from a file |
---|
153 | # TimeInf: Function to print all the information from the variable time |
---|
154 | # time_reset: Function to re-set the time axis of a file |
---|
155 | # TimeSplitmat: Function to transform a file with CFtimes to a matrix [Nyear,Nmonth,Nday,Nhour,Nminute,Nsecond] |
---|
156 | # timemean: Function to retrieve a time mean series from a multidimensional variable of a file |
---|
157 | # valmod: Function to modify the value of a variable |
---|
158 | # valmod_dim: Function to modify the value of a variable at a given dimension and value |
---|
159 | # varaddattrk: Add an attribute to a variable caring about the type |
---|
160 | # varaddattr: Add an attribute to a variable. Removes previous attribute if it exists |
---|
161 | # varaddref: Function to add a variable in an existing file copying characteristics from an existing one |
---|
162 | # var_creation: Operation to create a new variable in a file with a given set of dimensions |
---|
163 | # varout: Function when we want to output variable values |
---|
164 | # varrmattr: Removing an attribute from a variable |
---|
165 | # varrm: Removing a variable from a file |
---|
166 | # VarVal_FillValue: Function to transform a given value from a given variable to _FillValue in a netCDF file |
---|
167 | # vrattr: Function to remove an atribute from a variable |
---|
168 | # WRF_d0Nref: Function for the generation of an extra WRF domain from a given one |
---|
169 | # WRF_CFlonlat_creation: Function to add a CF-convention longitude/latitude variables in a WRF file |
---|
170 | # WRF_CFtime_creation: Function to add a CF-convention time unit in a WRF file |
---|
171 | # WRF_CFxtime_creation: Function to add a CF-convention time unit in a WRF file using variable 'XTIME' |
---|
172 | # WRF_toCF: Function to pass a WRF original file to CF-conventions |
---|
173 | # WRF_to_newCF: Function to pass a WRF original file to new CF-conventions file |
---|
174 | |
---|
175 | operations=['addDim', 'addVar', 'addvals', 'CDO_toCF', 'chdimname', 'changevartype', \ |
---|
176 | 'checkallvars', 'checkAllValues', 'checkNaNs', \ |
---|
177 | 'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compute_deaccum', \ |
---|
178 | 'compute_opersvarsfiles', \ |
---|
179 | 'compute_opervaralltime', 'compute_opervartimes', 'compute_tevolboxtraj', \ |
---|
180 | 'computevar_model', 'curve_section', 'DatesFiles', \ |
---|
181 | 'DataSetSection', 'DataSetSection_multidims', 'DataSetSection_multivars', \ |
---|
182 | 'DYNAMICO_toCF', 'dimToUnlimited', 'dimVar_creation', \ |
---|
183 | 'fattradd', \ |
---|
184 | 'fdimadd', 'fgaddattr', 'field_stats', 'field_stats_dim', 'file_creation', \ |
---|
185 | 'file_oper_alongdims', 'filter_2dim', \ |
---|
186 | 'flipdim', 'fvaradd', 'gaddattrk', 'gaddattr', 'get_attribute', \ |
---|
187 | 'get_namelist_vars', 'get_Variables', \ |
---|
188 | 'getvalues_lonlat', 'getvars_tofile', 'grattr', \ |
---|
189 | 'grmattr', 'idims', 'igattrs', 'increaseDimvar', 'isgattrs', \ |
---|
190 | 'isvattrs', 'ivars', 'ivattrs', 'LMDZ_toCF', 'lonlat_polygon', 'maskvar', \ |
---|
191 | 'merge_files', 'model_characteristics', \ |
---|
192 | 'mthDYNAMICO_toCF', 'ncreplace', 'ncstepdiff', 'netcdf_concatenation', \ |
---|
193 | 'netcdf_fold_concatenation', \ |
---|
194 | 'netcdf_fold_concatenation_HMT', 'reproject', 'Partialmap_Entiremap', \ |
---|
195 | 'Partialmap_EntiremapFor', 'Partialmap_EntiremapForExact', \ |
---|
196 | 'pinterp', 'reconstruct_matrix_from_vector', 'remapnn', 'rm_FillValue', \ |
---|
197 | 'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues', \ |
---|
198 | 'setvar_nc', 'sorttimesmat', 'spacemean', 'SpatialWeightedMean', \ |
---|
199 | 'statcompare_files', \ |
---|
200 | 'subbasin', 'submns', 'subyrs', 'TimeInf', 'time_reset', \ |
---|
201 | 'TimeSplitmat', 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr', \ |
---|
202 | 'varaddref', \ |
---|
203 | 'var_creation', 'varout', 'varoutold', 'varrmattr', 'varrm', 'VarVal_FillValue', \ |
---|
204 | 'vrattr', 'WRF_d0Nref', \ |
---|
205 | 'WRF_CFlonlat_creation', 'WRF_CFtime_creation', 'WRF_CFxtime_creation', \ |
---|
206 | 'list_operations', 'WRF_toCF', 'WRF_to_newCF'] |
---|
207 | |
---|
208 | ### Options |
---|
209 | ##string_operation="operation to make: " + '\n' + " out, output values -S inidim1,[inidim2,...]:enddim1,[enddim2,...]" |
---|
210 | string_operation="""operation to make: |
---|
211 | addgattr, add a new global attribute: addatr -S [attrname]|[attrvalue] |
---|
212 | addvattr, add a new attribute to any given variable: addatr -S [attrname]|[attrvalue] |
---|
213 | 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 |
---|
214 | checkallvalrs: Function to check all variables of a file: -S [dimn1],[[dimn2],...,[dimnN]]:[dim1],[[dim2],...,[dimN]] |
---|
215 | mname, modify name -S newname |
---|
216 | out, output values -S inidim1,[inidim2,...]:enddim1,[enddim2,...] |
---|
217 | valmod, modifiy values of variable -S [modification]: |
---|
218 | sumc,[constant]: add [constant] to variables values |
---|
219 | subc,[constant]: substract [constant] to variables values |
---|
220 | mulc,[constant]: multipy by [constant] to variables values |
---|
221 | divc,[constant]: divide by [constant] to variables values |
---|
222 | rmgattr, remove a global attribute: rmgattr -S [attrname] |
---|
223 | rmvattr, remove an attribute to any given variable: rmvattr -S [attrname] |
---|
224 | """ |
---|
225 | |
---|
226 | #print string_operation |
---|
227 | |
---|
228 | operationnames = "'" + gen.numVector_String(operations, "', '") + "'" |
---|
229 | |
---|
230 | parser = OptionParser() |
---|
231 | parser.add_option("-f", "--netCDF_file", dest="ncfile", help="file to use", |
---|
232 | metavar="FILE") |
---|
233 | parser.add_option("-o", "--operation", type='choice', dest="operation", |
---|
234 | choices=operations, help="operation to make: " + operationnames, metavar="OPER") |
---|
235 | parser.add_option("-S", "--valueS", dest="values", |
---|
236 | help="values to use according to the operation (when applicable); '-h' specific help of the operation", metavar="VALUES") |
---|
237 | parser.add_option("-v", "--variable", dest="varname", |
---|
238 | help="variable to use (when applicable)", metavar="VAR") |
---|
239 | |
---|
240 | (opts, args) = parser.parse_args() |
---|
241 | |
---|
242 | #if opts.help: |
---|
243 | # parser.print_help() |
---|
244 | # print string_operation |
---|
245 | # sys.exit() |
---|
246 | |
---|
247 | ####### ####### |
---|
248 | ## MAIN |
---|
249 | ####### |
---|
250 | |
---|
251 | # Operations which file name is not a real file |
---|
252 | NotCheckingFile = ['DatesFiles', 'compute_opersvarsfiles', 'file_creation', \ |
---|
253 | 'list_operations', 'merge_files', \ |
---|
254 | 'model_characteristics', 'netcdf_concatenation', 'netcdf_fold_concatenation', \ |
---|
255 | 'netcdf_fold_concatenation_HMT'] |
---|
256 | |
---|
257 | ####### ###### ##### #### ### ## # |
---|
258 | errormsg='ERROR -- error -- ERROR -- error' |
---|
259 | |
---|
260 | varn=opts.varname |
---|
261 | oper=opts.operation |
---|
262 | |
---|
263 | if opts.operation is None: |
---|
264 | print errormsg |
---|
265 | print ' No operation provided !!' |
---|
266 | print " an operation must be provided as '-o [operationname]' " |
---|
267 | quit(-1) |
---|
268 | |
---|
269 | if opts.ncfile is not None and not os.path.isfile(opts.ncfile) and \ |
---|
270 | not gen.searchInlist(NotCheckingFile,oper): |
---|
271 | print errormsg |
---|
272 | print ' File ' + opts.ncfile + ' does not exist !!' |
---|
273 | quit(-1) |
---|
274 | elif opts.ncfile is None and not gen.searchInlist(NotCheckingFile,oper) and \ |
---|
275 | opts.values != 'h': |
---|
276 | print errormsg |
---|
277 | print ' No file provided !!' |
---|
278 | print " a file must be provided as '-f [filename]' " |
---|
279 | quit(-1) |
---|
280 | |
---|
281 | if oper == 'addDim': |
---|
282 | ncvar.addDim(opts.values, opts.ncfile, opts.varname) |
---|
283 | elif oper == 'addVar': |
---|
284 | ncvar.addVar(opts.values, opts.ncfile, opts.varname) |
---|
285 | elif oper == 'addvals': |
---|
286 | ncvar.addvals(opts.values, opts.ncfile, opts.varname) |
---|
287 | elif oper == 'CDO_toCF': |
---|
288 | ncvar.CDO_toCF(opts.ncfile) |
---|
289 | elif oper == 'chdimname': |
---|
290 | ncvar.chdimname(opts.values, opts.ncfile) |
---|
291 | elif oper == 'changevartype': |
---|
292 | ncvar.changevartype(opts.values, opts.ncfile, opts.varname) |
---|
293 | elif oper == 'checkallvars': |
---|
294 | ncvar.checkallvars(opts.values, opts.ncfile) |
---|
295 | elif oper == 'checkAllValues': |
---|
296 | ncvar.checkAllValues(opts.values, opts.ncfile) |
---|
297 | elif oper == 'checkNaNs': |
---|
298 | ncvar.checkNaNs(opts.values, opts.ncfile) |
---|
299 | elif oper == 'chgtimestep': |
---|
300 | ncvar.chgtimestep(opts.values, opts.ncfile, opts.varname) |
---|
301 | elif oper == 'chvarname': |
---|
302 | ncvar.chvarname(opts.values, opts.ncfile, opts.varname) |
---|
303 | elif oper == 'cleaning_varsfile': |
---|
304 | ncvar.cleaning_varsfile(opts.values, opts.ncfile) |
---|
305 | elif oper == 'compute_deaccum': |
---|
306 | ncvar.compute_deaccum(opts.values, opts.ncfile, opts.varname) |
---|
307 | elif oper == 'compute_opersvarsfiles': |
---|
308 | ncvar.compute_opersvarsfiles(opts.values, opts.varname) |
---|
309 | elif oper == 'compute_opervaralltime': |
---|
310 | ncvar.compute_opervaralltime(opts.values, opts.ncfile, opts.varname) |
---|
311 | elif oper == 'compute_opervartimes': |
---|
312 | ncvar.compute_opervartimes(opts.values, opts.ncfile, opts.varname) |
---|
313 | elif oper == 'compute_tevolboxtraj': |
---|
314 | ncvar.compute_tevolboxtraj(opts.values, opts.ncfile, opts.varname) |
---|
315 | elif oper == 'computevar_model': |
---|
316 | ncvar.computevar_model(opts.values, opts.ncfile) |
---|
317 | elif oper == 'curve_section': |
---|
318 | ncvar.curve_section(opts.values, opts.ncfile, opts.varname) |
---|
319 | elif oper == 'DataSetSection': |
---|
320 | ncvar.DataSetSection(opts.values, opts.ncfile) |
---|
321 | elif oper == 'DataSetSection_multidims': |
---|
322 | ncvar.DataSetSection_multidims(opts.values, opts.ncfile, opts.varname) |
---|
323 | elif oper == 'DataSetSection_multivars': |
---|
324 | ncvar.DataSetSection_multivars(opts.values, opts.ncfile, opts.varname) |
---|
325 | elif oper == 'DatesFiles': |
---|
326 | ncvar.DatesFiles(opts.values, opts.ncfile, opts.varname) |
---|
327 | elif oper == 'DYNAMICO_toCF': |
---|
328 | ncvar.DYNAMICO_toCF(opts.values, opts.ncfile) |
---|
329 | elif oper == 'dimToUnlimited': |
---|
330 | ncvar.dimToUnlimited(opts.values, opts.ncfile) |
---|
331 | elif oper == 'dimVar_creation': |
---|
332 | ncvar.dimVar_creation(opts.values, opts.ncfile) |
---|
333 | elif oper == 'fattradd': |
---|
334 | ncvar.fattradd(var, opts.values, opts.ncfile) |
---|
335 | elif oper == 'fdimadd': |
---|
336 | ncvar.fdimadd(opts.values, opts.ncfile) |
---|
337 | elif oper == 'fgaddattr': |
---|
338 | ncvar.fgaddattr(opts.values, opts.ncfile) |
---|
339 | elif oper == 'file_creation': |
---|
340 | ncvar.file_creation(opts.values, opts.ncfile, opts.varname) |
---|
341 | elif oper == 'file_oper_alongdims': |
---|
342 | ncvar.file_oper_alongdims(opts.values, opts.ncfile, opts.varname) |
---|
343 | elif oper == 'field_stats': |
---|
344 | ncvar.field_stats(opts.values, opts.ncfile, opts.varname) |
---|
345 | elif oper == 'field_stats_dim': |
---|
346 | ncvar.field_stats_dim(opts.values, opts.ncfile, opts.varname) |
---|
347 | elif oper == 'filter_2dim': |
---|
348 | ncvar.filter_2dim(opts.values, opts.ncfile, opts.varname) |
---|
349 | elif oper == 'flipdim': |
---|
350 | ncvar.flipdim(opts.values, opts.ncfile, opts.varname) |
---|
351 | elif oper == 'fvaradd': |
---|
352 | ncvar.fvaradd(opts.values, opts.ncfile) |
---|
353 | elif oper == 'gaddattrk': |
---|
354 | ncvar.gaddattrk(opts.values, opts.ncfile) |
---|
355 | elif oper == 'gaddattr': |
---|
356 | ncvar.gaddattr(opts.values, opts.ncfile) |
---|
357 | elif oper == 'get_attribute': |
---|
358 | ncvar.get_attribute(opts.values, opts.ncfile, opts.varname) |
---|
359 | elif oper == 'get_namelist_vars': |
---|
360 | ncvar.get_namelist_vars(opts.values, opts.ncfile) |
---|
361 | elif oper == 'get_Variables': |
---|
362 | ncvar.get_Variables(opts.values, opts.ncfile, opts.varname) |
---|
363 | elif oper == 'getvalues_lonlat': |
---|
364 | ncvar.getvalues_lonlat(opts.values, opts.ncfile) |
---|
365 | elif oper == 'getvars_tofile': |
---|
366 | ncvar.getvars_tofile(opts.values, opts.ncfile, opts.varname) |
---|
367 | elif oper == 'grattr': |
---|
368 | ncvar.grattr(opts.values, opts.ncfile) |
---|
369 | elif oper == 'grmattr': |
---|
370 | ncvar.grmattr(opts.values, opts.ncfile) |
---|
371 | elif oper == 'idims': |
---|
372 | ncvar.idims(opts.ncfile) |
---|
373 | elif oper == 'igattrs': |
---|
374 | ncvar.igattrs(opts.ncfile) |
---|
375 | elif oper == 'increaseDimvar': |
---|
376 | ncvar.increaseDimvar(opts.values, opts.ncfile, opts.varname) |
---|
377 | elif oper == 'isgattrs': |
---|
378 | ncvar.isgattrs(opts.values, opts.ncfile) |
---|
379 | elif oper == 'isvattrs': |
---|
380 | ncvar.isvattrs(opts.values, opts.ncfile, opts.varname) |
---|
381 | elif oper == 'ivars': |
---|
382 | ncvar.ivars(opts.ncfile) |
---|
383 | elif oper == 'ivattrs': |
---|
384 | ncvar.ivattrs(opts.ncfile, opts.varname) |
---|
385 | elif oper == 'list_operations': |
---|
386 | # From: http://www.diveintopython.net/power_of_introspection/all_together.html |
---|
387 | object = ncvar |
---|
388 | for opern in operations: |
---|
389 | if opern != 'list_operations': |
---|
390 | print opern + '_______ ______ _____ ____ ___ __ _' |
---|
391 | print getattr(object, opern).__doc__ |
---|
392 | elif oper == 'LMDZ_toCF': |
---|
393 | ncvar.LMDZ_toCF(opts.values, opts.ncfile) |
---|
394 | elif oper == 'lonlat_polygon': |
---|
395 | ncvar.lonlat_polygon(opts.values, opts.ncfile, opts.varname) |
---|
396 | elif oper == 'maskvar': |
---|
397 | ncvar.maskvar(opts.values, opts.ncfile, opts.varname) |
---|
398 | elif oper == 'merge_files': |
---|
399 | ncvar.merge_files(opts.values, opts.ncfile, opts.varname) |
---|
400 | elif oper == 'model_characteristics': |
---|
401 | ncvar.model_characteristics(opts.values, opts.ncfile) |
---|
402 | elif oper == 'mthDYNAMICO_toCF': |
---|
403 | ncvar.mthDYNAMICO_toCF(opts.values, opts.ncfile) |
---|
404 | elif oper == 'ncreplace': |
---|
405 | ncvar.ncreplace(opts.values, opts.ncfile, opts.varname) |
---|
406 | elif oper == 'ncstepdiff': |
---|
407 | ncvar.ncstepdiff(opts.values, opts.ncfile, opts.varname) |
---|
408 | elif oper == 'netcdf_concatenation': |
---|
409 | ncvar.netcdf_concatenation(opts.ncfile) |
---|
410 | elif oper == 'netcdf_fold_concatenation': |
---|
411 | ncvar.netcdf_fold_concatenation(opts.values, opts.ncfile, opts.varname) |
---|
412 | elif oper == 'netcdf_fold_concatenation_HMT': |
---|
413 | ncvar.netcdf_fold_concatenation_HMT(opts.values, opts.ncfile, opts.varname) |
---|
414 | elif oper == 'opersvarsfiles': |
---|
415 | ncvar.compute_opersvarsfiles(opts.values, opts.varname) |
---|
416 | elif oper == 'pinterp': |
---|
417 | ncvar.pinterp(opts.values, opts.ncfile, opts.varname) |
---|
418 | elif oper == 'remapnn': |
---|
419 | ncvar.remapnn(opts.values, opts.ncfile, opts.varname) |
---|
420 | elif oper == 'Partialmap_Entiremap': |
---|
421 | ncvar.Partialmap_Entiremap(opts.values, opts.ncfile, opts.varname) |
---|
422 | elif oper == 'Partialmap_EntiremapFor': |
---|
423 | ncvar.Partialmap_EntiremapFor(opts.values, opts.ncfile, opts.varname) |
---|
424 | elif oper == 'Partialmap_EntiremapForExact': |
---|
425 | ncvar.Partialmap_EntiremapForExact(opts.values, opts.ncfile, opts.varname) |
---|
426 | elif oper == 'reconstruct_matrix_from_vector': |
---|
427 | ncvar.reconstruct_matrix_from_vector(opts.values, opts.ncfile, opts.varname) |
---|
428 | elif oper == 'reproject': |
---|
429 | ncvar.reproject(opts.values, opts.ncfile, opts.varname) |
---|
430 | elif oper == 'rm_FillValue': |
---|
431 | ncvar.rm_FillValue(opts.values, opts.ncfile, opts.varname) |
---|
432 | elif oper == 'seasmean': |
---|
433 | ncvar.seasmean(timename, opts.ncfile, opts.varname) |
---|
434 | elif oper == 'sellonlatbox': |
---|
435 | ncvar.sellonlatbox(opts.values, opts.ncfile, opts.varname) |
---|
436 | elif oper == 'sellonlatlevbox': |
---|
437 | ncvar.sellonlatlevbox(opts.values, opts.ncfile, opts.varname) |
---|
438 | elif oper == 'selvar': |
---|
439 | ncvar.selvar(opts.values, opts.ncfile, opts.varname) |
---|
440 | elif oper == 'setvar_asciivalues': |
---|
441 | ncvar.setvar_asciivalues(opts.values, opts.ncfile, opts.varname) |
---|
442 | elif oper == 'setvar_nc': |
---|
443 | ncvar.setvar_nc(opts.values, opts.ncfile, opts.varname) |
---|
444 | elif oper == 'sorttimesmat': |
---|
445 | ncvar.sorttimesmat(opts.ncfile, opts.varname) |
---|
446 | elif oper == 'spacemean': |
---|
447 | ncvar.spacemean(opts.ncfile, opts.varname) |
---|
448 | elif oper == 'SpatialWeightedMean': |
---|
449 | ncvar.SpatialWeightedMean(opts.values, opts.ncfile, opts.varname) |
---|
450 | elif oper == 'statcompare_files': |
---|
451 | ncvar.statcompare_files(opts.values) |
---|
452 | elif oper == 'subbasin': |
---|
453 | ncvar.subbasin(opts.values, opts.ncfile) |
---|
454 | elif oper == 'submns': |
---|
455 | ncvar.submns(opts.values, opts.ncfile, opts.varname) |
---|
456 | elif oper == 'subyrs': |
---|
457 | ncvar.subyrs(opts.values, opts.ncfile, opts.varname) |
---|
458 | elif oper == 'TimeInf': |
---|
459 | ncvar.TimeInf(opts.ncfile, opts.varname) |
---|
460 | elif oper == 'time_reset': |
---|
461 | ncvar.time_reset(opts.values, opts.ncfile, opts.varname) |
---|
462 | elif oper == 'TimeSplitmat': |
---|
463 | ncvar.TimeSplitmat(opts.values, opts.ncfile, opts.varname) |
---|
464 | elif oper == 'timemean': |
---|
465 | ncvar.timemean(opts.values, opts.ncfile, opts.varname) |
---|
466 | elif oper == 'valmod': |
---|
467 | ncvar.valmod(opts.values, opts.ncfile, opts.varname) |
---|
468 | elif oper == 'valmod_dim': |
---|
469 | ncvar.valmod_dim(opts.values, opts.ncfile, opts.varname) |
---|
470 | elif oper == 'varaddattrk': |
---|
471 | ncvar.varaddattrk(opts.values, opts.ncfile, opts.varname) |
---|
472 | elif oper == 'varaddattr': |
---|
473 | ncvar.varaddattr(opts.values, opts.ncfile, opts.varname) |
---|
474 | elif oper == 'varaddref': |
---|
475 | ncvar.varaddref(opts.values, opts.ncfile, opts.varname) |
---|
476 | elif oper == 'var_creation': |
---|
477 | ncvar.var_creation(opts.values, opts.ncfile, opts.varname) |
---|
478 | elif oper == 'varout': |
---|
479 | ncvar.varout(opts.values, opts.ncfile, opts.varname) |
---|
480 | elif oper == 'varoutold': |
---|
481 | ncvar.varoutold(opts.values, opts.ncfile, opts.varname) |
---|
482 | elif oper == 'varrmattr': |
---|
483 | ncvar.varrmattr(opts.values, opts.ncfile, opts.varname) |
---|
484 | elif oper == 'varrm': |
---|
485 | ncvar.varrm(opts.ncfile, opts.varname) |
---|
486 | elif oper == 'VarVal_FillValue': |
---|
487 | ncvar.VarVal_FillValue(opts.values, opts.ncfile, opts.varname) |
---|
488 | elif oper == 'vrattr': |
---|
489 | ncvar.vrattr(opts.values, opts.ncfile, opts.varname) |
---|
490 | elif oper == 'WRF_d0Nref': |
---|
491 | ncvar.WRF_d0Nref(opts.values, opts.ncfile) |
---|
492 | elif oper == 'WRF_CFlonlat_creation': |
---|
493 | ncvar.WRF_CFlonlat_creation(opts.values, opts.ncfile, opts.varname) |
---|
494 | elif oper == 'WRF_CFtime_creation': |
---|
495 | ncvar.WRF_CFtime_creation(opts.values, opts.ncfile, opts.varname) |
---|
496 | elif oper == 'WRF_CFxtime_creation': |
---|
497 | ncvar.WRF_CFxtime_creation(opts.values, opts.ncfile, opts.varname) |
---|
498 | elif oper == 'WRF_toCF': |
---|
499 | ncvar.WRF_toCF(opts.values, opts.ncfile) |
---|
500 | elif oper == 'WRF_to_newCF': |
---|
501 | ncvar.WRF_to_newCF(opts.values, opts.ncfile, opts.varname) |
---|
502 | else: |
---|
503 | print errormsg |
---|
504 | print ' The operation ' + oper + ' is not ready !!' |
---|
505 | print errormsg |
---|
506 | quit() |
---|