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

Last change on this file since 1967 was 1959, checked in by lfita, 6 years ago

Adding:

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