forked from phaustin/netcdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_all.py
More file actions
40 lines (32 loc) · 1.1 KB
/
copy_all.py
File metadata and controls
40 lines (32 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""netcdf example that clones an ncfile
"""
from netCDF4 import Dataset
import glob
#
# assume we've got a single file that's
# uniquely identfied by this regexp
#
ncfile_in=glob.glob("toga*nc")[0]
run_num=1
ncfile_out="run_{0:d}_{1:s}".format(run_num,ncfile_in)
infile = Dataset(ncfile_in, 'r')
outfile = Dataset(ncfile_out,mode='w',format='NETCDF3_CLASSIC')
#first transfer the dimensions
for (name,value) in infile.dimensions.items():
outfile.createDimension(name,len(value))
#next tranfer the global attributes
for globalatt in infile.ncattrs():
print "copying global attribute: ",globalatt
outfile.setncattr(globalatt,infile.getncattr(globalatt))
#transfer each variable and its attributes
for (inname,invalue) in infile.variables.items():
print "copying variable: ",inname
#create the variable
outvar = outfile.createVariable(inname,invalue.dtype,invalue.dimensions)
#transfer the attributes
for varattr in invalue.ncattrs():
outvar.setncattr(varattr,invalue.getncattr(varattr))
## #finally, copy the data
outvar[...]=invalue[...]
infile.close()
outfile.close()