- Timestamp:
- Mar 31, 2015, 4:54:30 PM (10 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r386 r387 30 30 'sorttimesmat', 'spacemean', 'statcompare_files', 'submns', 'subyrs', 'TimeInf', \ 31 31 'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr', 'varaddref', \ 32 'varout', 'varoutold', 'varrmattr', 'varrm', 'vrattr', 'WRF_d0Nref', \ 32 'var_creation', 'varout', 'varoutold', 'varrmattr', 'varrm', 'vrattr', \ 33 'WRF_d0Nref', \ 33 34 'WRF_CFlonlat_creation', 'WRF_CFtime_creation', 'WRF_CFxtime_creation', \ 34 35 'list_operations'] … … 211 212 elif oper == 'varaddref': 212 213 ncvar.varaddref(opts.values, opts.ncfile, opts.varname) 214 elif oper == 'var_creation': 215 ncvar.var_creation(opts.values, opts.ncfile, opts.varname) 213 216 elif oper == 'varout': 214 217 ncvar.varout(opts.values, opts.ncfile, opts.varname) -
trunk/tools/nc_var_tools.py
r386 r387 13831 13831 #file_creation('time_counter:12,sizes:24|time@time@seconds since 1949-12-01 00:00:00|f8', 'test.nc', 't_instant') 13832 13832 13833 def var_creation(values, ncfile, varn): 13834 """ Operation to create a new variable in a file with a given set of dimensions 13835 values= [dimensions]|[varattributes]|[kind] 13836 [dimensions]: [dimn1]:[dsize1],...,[dimnN]:[dsizeN], ',' pairs of variable name [dimn] and [size] 13837 if [dsize] = 'None', give a third value with the real size 13838 [attributes]: [std_name]@[long_name]@[units], standard name, long name and units of the variable 13839 [kind]: type of variable (standard netCDF4/C-like values, 'c', 'i', 'f', 'f8',...) 13840 ncfile= name of the file 13841 varn= name of the variables 13842 """ 13843 fname = 'var_creation' 13844 13845 if values == 'h': 13846 print fname + '_____________________________________________________________' 13847 print var_creation.__doc__ 13848 quit() 13849 13850 expectargs = '[dimensions]|[varattributes]|[kind]' 13851 13852 check_arguments(fname,len(expectargs.split('|')),values,'|',expectargs) 13853 13854 dimensions = values.split('|')[0].split(',') 13855 attributes = values.split('|')[1] 13856 kind = values.split('|')[2] 13857 13858 onc = NetCDFFile(ncfile, 'a') 13859 13860 # Dimensions 13861 dnames = [] 13862 dsize = [] 13863 for dim in dimensions: 13864 # print " Adding: '" + dim + "' ..." 13865 dimn = dim.split(':')[0] 13866 dimv = dim.split(':')[1] 13867 if dimv == 'None': 13868 if len(dim.split(':')) != 3: 13869 print errormsg 13870 print ' ' + fname + ": dimension '" + dimn + "' is None but the " + \ 13871 'size is requried!!' 13872 quit(-1) 13873 else: 13874 dv = None 13875 dsize.append(int(dim.split(':')[2])) 13876 else: 13877 dv = int(dimv) 13878 dsize.append(dv) 13879 13880 dnames.append(dimn) 13881 13882 if not searchInlist(onc.dimensions, dimn): 13883 newdim = onc.createDimension(dimn, dv) 13884 13885 onc.sync() 13886 13887 # Variable 13888 if kind == 'c': 13889 newvar = onc.createVariable(varn, 'c', tuple(dnames)) 13890 # newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 13891 if kind == 'f4': 13892 newvar = onc.createVariable(varn, 'f4', tuple(dnames)) 13893 newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 13894 elif kind == 'f8': 13895 newvar = onc.createVariable(varn, 'f8', tuple(dnames)) 13896 newvar[:] = np.zeros(tuple(dsize), dtype=np.float64) 13897 elif kind == 'i': 13898 newvar = onc.createVariable(varn, 'i', tuple(dnames)) 13899 newvar[:] = np.zeros(tuple(dsize), dtype=int) 13900 else: 13901 print errormsg 13902 print ' ' + fname + ": variable kind '" + kind + "' not ready!!" 13903 quit(-1) 13904 13905 sname = attributes.split('@')[0] 13906 lname = attributes.split('@')[1] 13907 u = attributes.split('@')[2] 13908 13909 newattr = basicvardef(newvar, sname, lname, u) 13910 13911 onc.sync() 13912 onc.close() 13913 13914 #var_creation('dim1:12,sizes:24|time@time@seconds since 1949-12-01 00:00:00|f4', 'test.nc', 'var2') 13833 13915 #quit() 13834 13916
Note: See TracChangeset
for help on using the changeset viewer.