-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandalone_convert.py
executable file
·85 lines (72 loc) · 2.79 KB
/
standalone_convert.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
#!/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('-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']
ncFid = Dataset(Input_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
time = ncFid.variables['time'][:]
if haveLev:
lev = ncFid.variables['lev'][:]
ydim = ncFid.variables['Xdim'][:]
xdim = ncFid.variables['Ydim'][:]
nf = ncFid.variables['nf'][:]
for att in ncFid.ncattrs():
setattr(ncFidOut,att,getattr(ncFid,att))
ncols = len(xdim)*len(ydim)*len(nf)
ncolOut = ncFidOut.createDimension('ncol', ncols)
ncolsOut = ncFidOut.createVariable('ncol','f4',('ncol',))
ncolsOut[:] = range(ncols)
if haveLev:
levOut = ncFidOut.createDimension('lev', len(lev))
levsOut = ncFidOut.createVariable('lev','f4',('lev',))
for att in ncFid.variables['lev'].ncattrs():
setattr(ncFidOut.variables['lev'],att,getattr(ncFid.variables['lev'],att))
levsOut[:] = lev
timeOut = ncFidOut.createDimension('time', len(time))
timesOut = ncFidOut.createVariable('time','f4',('time',))
for att in ncFid.variables['time'].ncattrs():
setattr(ncFidOut.variables['time'],att,getattr(ncFid.variables['time'],att))
timesOut[:] = time
Exclude_Var = ['Xdim','Ydim','time','lev','nf','ncontact','cubed_sphere','contacts','orientation','anchor']
for var in ncFid.variables:
if var not in Exclude_Var:
temp = ncFid.variables[var][:]
dim_size =len(temp.shape)
if dim_size == 5:
Tout = ncFidOut.createVariable(var,'f4',('time','lev','ncol',),fill_value=1.0e15)
for att in ncFid.variables[var].ncattrs():
if att != "_FillValue" and att != "grid_mapping":
setattr(ncFidOut.variables[var],att,getattr(ncFid.variables[var],att))
Tout[:,:,:] = temp.reshape([ len(time),len(lev),ncols])
elif dim_size == 4:
Tout = ncFidOut.createVariable(var,'f4',('time','ncol',),fill_value=1.0e15)
for att in ncFid.variables[var].ncattrs():
if att != "_FillValue" and att != "grid_mapping":
setattr(ncFidOut.variables[var],att,getattr(ncFid.variables[var],att))
Tout[:,:] = temp.reshape([ len(time),ncols])
ncFidOut.close()
#-----------------
# Closing the file
#-----------------
ncFid.close()