1 | from __future__ import division |
---|
2 | import argparse |
---|
3 | import numpy as np |
---|
4 | from netCDF4 import Dataset |
---|
5 | import matplotlib.pyplot as plt |
---|
6 | from mpl_toolkits.basemap import Basemap |
---|
7 | import matplotlib.mlab as mlab |
---|
8 | import matplotlib.colors |
---|
9 | import sys |
---|
10 | import os |
---|
11 | import math |
---|
12 | from scipy.interpolate import interp1d |
---|
13 | from matplotlib.ticker import AutoMinorLocator |
---|
14 | import netCDF4 as cdf |
---|
15 | |
---|
16 | dw1 = Dataset('2001.nc','r') |
---|
17 | dtfull = dw1.variables['t'][:,:,:,:] |
---|
18 | dpfulloned = dw1.variables['level'][:] |
---|
19 | dpfull = np.zeros(((len(dpfulloned),dtfull.shape[2],dtfull.shape[3]))) |
---|
20 | for l in xrange(0, len(dpfulloned)): |
---|
21 | dpfull[l,:,:]= dpfulloned[l] |
---|
22 | |
---|
23 | print(dpfull) |
---|
24 | dtfull = dw1.variables['t'][:,:,:,:] |
---|
25 | #dPSfull = dw1.variables['PS'][:,:,:] |
---|
26 | time_counterfull = dw1.variables['time'][:] |
---|
27 | lat = dw1.variables['lat'][:] |
---|
28 | lon = dw1.variables['lon'][:] |
---|
29 | levels = [100., 200., 300., 500., 700., 1000., 2000., 3000., 5000., 7000., 10000., 12500., 15000., 17500., 20000., 22500.,25000., 30000., 35000., 40000., |
---|
30 | 45000., 50000., 55000., 60000., 65000., 70000., 75000., 77500., 80000., 82500., 85000., 87500., 90000., 92500., 95000., 97500., 100000.] |
---|
31 | |
---|
32 | levels = levels[::-1] |
---|
33 | print(levels) |
---|
34 | |
---|
35 | dim0=dtfull.shape[0] |
---|
36 | dim1=dtfull.shape[1] |
---|
37 | dim2=dtfull.shape[2] |
---|
38 | dim3=dtfull.shape[3] |
---|
39 | out3 = np.zeros((((dim0,len(levels),dim2,dim3)))) |
---|
40 | |
---|
41 | def vertical_int2(levels, pfull, dataset): |
---|
42 | nl, ni, nj = len(levels), dataset.shape[1], dataset.shape[2] |
---|
43 | out = np.zeros((nl,ni,nj)) |
---|
44 | for i in range(dataset.shape[1]): |
---|
45 | for j in range(dataset.shape[2]): |
---|
46 | f = interp1d(pfull[:,i,j],dataset[:,i,j],kind='linear',fill_value="extrapolate") |
---|
47 | out[:,i,j] = f(levels) |
---|
48 | return out |
---|
49 | |
---|
50 | pfull = dpfull |
---|
51 | |
---|
52 | for t in range(dim1): |
---|
53 | print("time step=",t) |
---|
54 | tfull = dtfull[t,:,:,:] |
---|
55 | out2 = vertical_int2(levels, pfull, tfull) |
---|
56 | out3[t,:,:,:]=out2[:,:,:] |
---|
57 | |
---|
58 | del tfull,dtfull |
---|
59 | |
---|
60 | dufull = dw1.variables['u'][:,:,:,:] |
---|
61 | out4 = np.zeros((((dim0,len(levels),dim2,dim3)))) |
---|
62 | for t in range(dim1): |
---|
63 | print("time step=",t) |
---|
64 | ufull = dufull[t,:,:,:] |
---|
65 | out2 = vertical_int2(levels, pfull, ufull) |
---|
66 | out4[t,:,:,:]=out2[:,:,:] |
---|
67 | del ufull, dufull |
---|
68 | |
---|
69 | dvfull = dw1.variables['v'][:,:,:,:] |
---|
70 | out5 = np.zeros((((dim0,len(levels),dim2,dim3)))) |
---|
71 | for t in range(dim1): |
---|
72 | print("time step=",t) |
---|
73 | vfull = dvfull[t,:,:,:] |
---|
74 | out2 = vertical_int2(levels, pfull, vfull) |
---|
75 | out5[t,:,:,:]=out2[:,:,:] |
---|
76 | del vfull,dvfull |
---|
77 | |
---|
78 | print("-----------------------------writing NetCDF--------------------------------") |
---|
79 | print(out3) |
---|
80 | #sys.exit(0) |
---|
81 | try: |
---|
82 | f = cdf.Dataset('interpolated_data.nc', 'w', format='NETCDF4') |
---|
83 | except: |
---|
84 | print("Error occurred while opening new netCDF file, Error: ", sys.exc_info()[0]) |
---|
85 | levels2 = [levels[i] / 100. for i in range(len(levels))] |
---|
86 | #levels2 = [int(i) for i in levels2] |
---|
87 | #print(levels2) |
---|
88 | f.createDimension('lon', len(lon)) |
---|
89 | f.createDimension('lat', len(lat)) |
---|
90 | f.createDimension('lev', len(levels2)) |
---|
91 | f.createDimension('time_counter', len(time_counterfull)) |
---|
92 | vlon = f.createVariable('lon', 'f4', 'lon') |
---|
93 | vlat = f.createVariable('lat', 'f4', 'lat') |
---|
94 | vlev = f.createVariable('lev', 'f4', 'lev') |
---|
95 | #vlev = f.createVariable('lev', 'i4', 'lev') |
---|
96 | vtime = f.createVariable('time_counter', 'f8', 'time_counter') |
---|
97 | vt = f.createVariable('t', 'f4',('time_counter', 'lev', 'lat', 'lon')) |
---|
98 | vu = f.createVariable('u', 'f4',('time_counter', 'lev', 'lat', 'lon')) |
---|
99 | vv = f.createVariable('v', 'f4',('time_counter', 'lev', 'lat', 'lon')) |
---|
100 | #vPS = f.createVariable('PS', 'f4',('time_counter', 'lat', 'lon')) |
---|
101 | vlon[:] = lon |
---|
102 | vlat[:] = lat |
---|
103 | vlev[:] = levels2 |
---|
104 | vtime[:] = time_counterfull |
---|
105 | vt[:] = out3 |
---|
106 | vu[:] = out4 |
---|
107 | vv[:] = out5 |
---|
108 | #vPS[:] = dPSfull |
---|
109 | f.close() |
---|
110 | print("-----------------------done--------------------") |
---|