-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpaste_ncfiles.py
More file actions
66 lines (46 loc) · 1.89 KB
/
Copy pathpaste_ncfiles.py
File metadata and controls
66 lines (46 loc) · 1.89 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from __future__ import division,print_function
import matplotlib as mpl
import scipy as sp
import matplotlib.tri as mplt
import matplotlib.pyplot as plt
#from mpl_toolkits.basemap import Basemap
import os as os
import sys
np.set_printoptions(precision=8,suppress=True,threshold=sys.maxsize)
import netCDF4 as n4
fileone='ncfilewith_latlon'
filetwo='ncfilewith_xy'
fileout='newncfile_withboth'
#have to remove file first or errors...
os.system('rm '+fileout)
os.system('cp '+fileone+' '+fileout)
#new ncfile name
ncid = n4.Dataset(filetwo, 'r',format='NETCDF3_CLASSIC')
g = n4.Dataset(fileout, 'r+',format='NETCDF3_CLASSIC')
##Create new its.nc file
## To copy the global attributes of the netCDF file
#for attname in ncid.ncattrs():
#setattr(g,attname,getattr(ncid,attname))
## To copy the dimension of the netCDF file
#for dimname,dim in ncid.dimensions.iteritems():
## if you want to make changes in the dimensions of the new file
## you should add your own conditions here before the creation of the dimension.
#if dimname=='node':
#g.createDimension(dimname,len(data_new['nodell'][:,0]))
#elif dimname=='time':
#g.createDimension(dimname,None)
#else:
#g.createDimension(dimname,len(dim))
# To copy the variables of the netCDF file
for varname,ncvar in ncid.variables.iteritems():
#if you want to make changes in the variables of the new file
# you should add your own conditions here before the creation of the variable.
if varname in ['x', 'y']:
var = g.createVariable(varname,ncvar.dtype,ncvar.dimensions)
#Proceed to copy the variable attributes
for attname in ncvar.ncattrs():
setattr(var,attname,getattr(ncvar,attname))
#Finally copy the variable data to the new created variable
var[:] = ncvar[:]
ncid.close()
g.close()