- Timestamp:
- Apr 16, 2015, 6:51:28 PM (10 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r387 r397 20 20 'chgtimestep', 'chvarname', 'compute_deaccum', 'compute_opersvarsfiles', \ 21 21 'compute_opervaralltime', 'compute_opervartimes', 'compute_tevolboxtraj', \ 22 'DataSetSection', 'DataSetSection_multidims', 'dimVar_creation', 'fattradd', \ 22 'DataSetSection', 'DataSetSection_multidims', 'dimToUnlimited', 'dimVar_creation', \ 23 'fattradd', \ 23 24 'fdimadd', 'fgaddattr', 'field_stats', 'file_creation', 'file_oper_alongdims', \ 24 25 'filter_2dim', \ … … 121 122 elif oper == 'DataSetSection_multidims': 122 123 ncvar.DataSetSection_multidims(opts.values, opts.ncfile) 124 elif oper == 'dimToUnlimited': 125 ncvar.dimToUnlimited(opts.values, opts.ncfile) 123 126 elif oper == 'dimVar_creation': 124 127 ncvar.dimVar_creation(opts.values, opts.ncfile) -
trunk/tools/nc_var_tools.py
r396 r397 13824 13824 # newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 13825 13825 if kind == 'f' or kind == 'f4': 13826 newvar = onc.createVariable(varn, 'f4', tuple(dnames) )13826 newvar = onc.createVariable(varn, 'f4', tuple(dnames), fill_value=fillValue) 13827 13827 newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 13828 13828 elif kind == 'f8': 13829 newvar = onc.createVariable(varn, 'f8', tuple(dnames)) 13829 newvar = onc.createVariable(varn, 'f8', tuple(dnames), \ 13830 fill_value= np.float64(fillValue)) 13830 13831 newvar[:] = np.zeros(tuple(dsize), dtype=np.float64) 13831 13832 elif kind == 'i': 13832 newvar = onc.createVariable(varn, 'i', tuple(dnames) )13833 newvar = onc.createVariable(varn, 'i', tuple(dnames), fill_value=-999999) 13833 13834 newvar[:] = np.zeros(tuple(dsize), dtype=int) 13834 13835 else: … … 13911 13912 # newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 13912 13913 if kind == 'f' or kind == 'f4': 13913 newvar = onc.createVariable(varn, 'f4', tuple(dnames) )13914 newvar = onc.createVariable(varn, 'f4', tuple(dnames), fill_value=fillValue) 13914 13915 newvar[:] = np.zeros(tuple(dsize), dtype=np.float) 13915 13916 elif kind == 'f8': 13916 newvar = onc.createVariable(varn, 'f8', tuple(dnames)) 13917 newvar = onc.createVariable(varn, 'f8', tuple(dnames), \ 13918 fill_value=np.float64(fillValue)) 13917 13919 newvar[:] = np.zeros(tuple(dsize), dtype=np.float64) 13918 13920 elif kind == 'i': 13919 newvar = onc.createVariable(varn, 'i', tuple(dnames) )13921 newvar = onc.createVariable(varn, 'i', tuple(dnames), fill_value=-99999) 13920 13922 newvar[:] = np.zeros(tuple(dsize), dtype=int) 13921 13923 else: … … 13934 13936 13935 13937 #var_creation('dim1:12,sizes:24|time@time@seconds since 1949-12-01 00:00:00|f4', 'test.nc', 'var2') 13938 #quit() 13939 13940 def dimToUnlimited(values, ncfile): 13941 """ Operation to create an unlimited dimension from an existing one 13942 values= Name of the dimension to transform 13943 ncfile= name of the file 13944 """ 13945 fname = 'dimToUnlimited' 13946 13947 if values == 'h': 13948 print fname + '_____________________________________________________________' 13949 print dimToUnlimited.__doc__ 13950 quit() 13951 13952 dimn = values 13953 13954 ncobj = NetCDFFile(ncfile, 'r') 13955 dimnames = ncobj.dimensions.keys() 13956 variables = ncobj.variables.keys() 13957 13958 if not searchInlist(dimnames,dimn): 13959 print errormsg 13960 print ' ' + fname + ": dimension '" + dimn + "' does not exist in file '" + \ 13961 ncfile + "' !!" 13962 print ' existing ones are:',ncobj.dimensions.keys() 13963 quit(-1) 13964 13965 # Creation of a new file 13966 newnc = NetCDFFile('new_' + ncfile, 'w') 13967 13968 # Creation of dimensions 13969 for dn in dimnames: 13970 print 'adding dim:',dn 13971 if ncobj.dimensions[dn].isunlimited() or dn == dimn: 13972 newdim = newnc.createDimension(dn, None) 13973 else: 13974 newdim = newnc.createDimension(dn, len(ncobj.dimensions[dn])) 13975 13976 # ncobj.close() 13977 newnc.sync() 13978 # newnc.close() 13979 13980 # Adding variables 13981 for vn in variables: 13982 print 'adding variable:',vn 13983 vno = ncobj.variables[vn] 13984 vdim = vno.dimensions 13985 vtype = vno.dtype 13986 varatts = vno.ncattrs() 13987 13988 if searchInlist(varatts, '_FillValue'): 13989 fillv = vno.getncattr('_FillValue') 13990 newvar = newnc.createVariable(vn, vtype, vdim, fill_value=fillv) 13991 else: 13992 newvar = newnc.createVariable(vn, vtype, vdim) 13993 13994 newvar[:] = vno[:] 13995 for nattr in vno.ncattrs(): 13996 if not nattr == '_FillValue': 13997 nattrv = vno.getncattr(nattr) 13998 newattr = newvar.setncattr(nattr, nattrv) 13999 14000 # Adding attributes 14001 fgaddattr(ncfile, 'new_' + ncfile) 14002 sub.call('mv new_' + ncfile + ' ' + ncfile) 14003 14004 return 14005 14006 #dimToUnlimited('lon', 'test.nc') 13936 14007 #quit() 13937 14008
Note: See TracChangeset
for help on using the changeset viewer.