-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertOldCStoNewCS.py
executable file
·161 lines (134 loc) · 5.66 KB
/
convertOldCStoNewCS.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
#-------------
# Load modules
#-------------
from netCDF4 import Dataset
import numpy
import argparse
def parse_args():
p = argparse.ArgumentParser(description='Flatten a lat-lon to 1D')
p.add_argument('-i','--input',type=str,help='input file',default=None)
p.add_argument('-e','--example',type=str,help='example file',default=None)
p.add_argument('-o','--output',type=str,help='output file',default=None)
return vars(p.parse_args())
#------------------
# Opening the file
#------------------
comm_args = parse_args()
Input_file = comm_args['input']
Output_file = comm_args['output']
Example_file = comm_args['example']
ncFid = Dataset(Input_file, mode='r')
ncFidEx = Dataset(Example_file, mode='r')
ncFidOut = Dataset(Output_file, mode='w', format='NETCDF4')
#---------------------
# Extracting variables
#---------------------
haveLev = False
for dim in ncFid.dimensions:
if dim == 'lev':
haveLev = True
levSize = len(ncFid.dimensions['lev'])
haveTime = False
for dim in ncFid.dimensions:
if dim == 'time':
haveTime = True
if haveTime:
time = ncFid.variables['time'][:]
if haveLev:
lev = ncFid.variables['lev'][:]
cRes = len(ncFid.dimensions['Xdim'])
print cRes
Xdim = ncFidOut.createDimension('Xdim',cRes)
Ydim = ncFidOut.createDimension('Ydim',cRes)
nf = ncFidOut.createDimension('nf',6)
ncontact = ncFidOut.createDimension('contact',4)
if haveLev:
levOut = ncFidOut.createDimension('lev',levSize)
timeOut = ncFidOut.createDimension('time',1)
vXdim = ncFidOut.createVariable('Xdim','f8',('Xdim'))
vYdim = ncFidOut.createVariable('Ydim','f8',('Ydim'))
setattr(ncFidOut.variables['Xdim'],'units','degrees_east')
setattr(ncFidOut.variables['Ydim'],'units','degrees_north')
setattr(ncFidOut.variables['Xdim'],'long_name','Fake Longitude for GrADS Compatibility')
setattr(ncFidOut.variables['Ydim'],'long_name','Fake Latitude for GrADS Compatibility')
vXdim[:]=range(1,cRes+1)
vYdim[:]=range(1,cRes+1)
vnf = ncFidOut.createVariable('nf','i4',('nf'))
vnf[:]=range(1,7)
setattr(ncFidOut.variables['nf'],'long_name','cubed-sphere face')
setattr(ncFidOut.variables['nf'],'axis','e')
setattr(ncFidOut.variables['nf'],'grads_dim','e')
if haveLev:
vLevOut= ncFidOut.createVariable('lev','f8',('lev'))
for att in ncFid.variables['lev'].ncattrs():
setattr(ncFidOut.variables['lev'],att,getattr(ncFid.variables['lev'],att))
vLevOut[:] = range(1,levSize+1)
if haveTime:
vtimeOut = ncFidOut.createVariable('time','i4',('time'))
vchar = ncFidOut.createVariable('cubed_sphere','S1')
setattr(ncFidOut.variables['cubed_sphere'],'grid_mapping_name','gnomonic cubed-sphere')
setattr(ncFidOut.variables['cubed_sphere'],'file_format_version','2.90')
setattr(ncFidOut.variables['cubed_sphere'],'additional_vars','contacts,orientation,anchor')
Exclude_Var = ['Xdim','Ydim','time','lev']
for var in ncFid.variables:
if var not in Exclude_Var:
temp = ncFid.variables[var][:]
dim_size =len(temp.shape)
if haveTime:
dim_size = dim_size -1
if dim_size == 3:
if haveTime:
tout = ncFidOut.createVariable(var,'f4',('time','lev','nf','Ydim','Xdim'),fill_value=1.0e15)
else:
tout = ncFidOut.createVariable(var,'f4',('lev','nf','Ydim','Xdim'),fill_value=1.0e15)
for att in ncFid.variables[var].ncattrs():
if att != "_FillValue":
setattr(ncFidOut.variables[var],att,getattr(ncFid.variables[var],att))
setattr(ncFidOut.variables[var],'grid_mapping','cubed_sphere')
setattr(ncFidOut.variables[var],'coordinates','lons lats')
for i in range(6):
il = cRes*i
iu = cRes*(i+1)
if haveTime:
tout[:,:,i,:,:] = temp[:,:,il:iu,:]
else:
tout[:,i,:,:] = temp[:,il:iu,:]
elif dim_size == 2:
if haveTime:
tout = ncFidOut.createVariable(var,'f4',('time','nf','Ydim','Xdim'),fill_value=1.0e15)
else:
tout = ncFidOut.createVariable(var,'f4',('nf','Ydim','Xdim'),fill_value=1.0e15)
for att in ncFid.variables[var].ncattrs():
if att != "_FillValue":
setattr(ncFidOut.variables[var],att,getattr(ncFid.variables[var],att))
setattr(ncFidOut.variables[var],'grid_mapping','cubed_sphere')
setattr(ncFidOut.variables[var],'coordinates','lons lats')
for i in range(6):
il = cRes*i
iu = cRes*(i+1)
if haveTime:
tout[:,i,:,:] = temp[:,il:iu,:]
else:
tout[i,:,:] = temp[il:iu,:]
center_lons = ncFidOut.createVariable('lons','f8',('nf','Ydim','Xdim'))
setattr(ncFidOut.variables['lons'],'long_name','longitude')
setattr(ncFidOut.variables['lons'],'units','degrees_east')
center_lats = ncFidOut.createVariable('lats','f8',('nf','Ydim','Xdim'))
setattr(ncFidOut.variables['lats'],'long_name','latitude')
setattr(ncFidOut.variables['lats'],'units','degrees_north')
center_lons[:,:,:] = ncFidEx.variables['lons'][:,:,:]
center_lats[:,:,:] = ncFidEx.variables['lats'][:,:,:]
corner_lons = ncFidOut.createVariable('corner_lons','f8',('nf','Ydim','Xdim'))
setattr(ncFidOut.variables['corner_lons'],'long_name','longitude')
setattr(ncFidOut.variables['corner_lons'],'units','degrees_east')
corner_lats = ncFidOut.createVariable('corner_lats','f8',('nf','Ydim','Xdim'))
setattr(ncFidOut.variables['corner_lats'],'long_name','latitude')
setattr(ncFidOut.variables['corner_lats'],'units','degrees_north')
corner_lons[:,:,:] = ncFidEx.variables['lons'][:,:,:]
corner_lats[:,:,:] = ncFidEx.variables['lats'][:,:,:]
#-----------------
# Closing the file
#-----------------
ncFidOut.close()
ncFid.close()