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('output_dcmip2016_regular_30days_d1.nc','r') |
---|
17 | dtfull = dw1.variables['T'][:,:,:,:] |
---|
18 | dpfull = dw1.variables['P'][:,:,:,:] |
---|
19 | dufull = dw1.variables['U'][:,:,:,:] |
---|
20 | dvfull = dw1.variables['V'][:,:,:,:] |
---|
21 | dPSfull = dw1.variables['PS'][:,:,:] |
---|
22 | time_counterfull = dw1.variables['time_counter'][:] |
---|
23 | lat = dw1.variables['lat'][:] |
---|
24 | lon = dw1.variables['lon'][:] |
---|
25 | levels = [100., 200., 300., 500., 700., 1000., 2000., 3000., 5000., 7000., 10000., 12500., 15000., 17500., 20000., 22500.,25000., 30000., 35000., 40000., |
---|
26 | 45000., 50000., 55000., 60000., 65000., 70000., 75000., 77500., 80000., 82500., 85000., 87500., 90000., 92500., 95000., 97500., 100000.] |
---|
27 | |
---|
28 | levels = levels[::-1] |
---|
29 | print(levels) |
---|
30 | out3 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3])))) |
---|
31 | out4 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3])))) |
---|
32 | out5 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3])))) |
---|
33 | |
---|
34 | def vertical_int2(levels, pfull, dataset): |
---|
35 | nl, ni, nj = len(levels), dataset.shape[1], dataset.shape[2] |
---|
36 | out = np.zeros((nl,ni,nj)) |
---|
37 | for i in range(dataset.shape[1]): |
---|
38 | for j in range(dataset.shape[2]): |
---|
39 | f = interp1d(pfull[:,i,j],dataset[:,i,j],kind='linear',fill_value="extrapolate") |
---|
40 | out[:,i,j] = f(levels) |
---|
41 | return out |
---|
42 | |
---|
43 | |
---|
44 | for t in range(dtfull.shape[1]): |
---|
45 | print("time step=",t) |
---|
46 | tfull = dtfull[t,:,:,:] |
---|
47 | pfull = dpfull[t,:,:,:] |
---|
48 | ufull = dufull[t,:,:,:] |
---|
49 | vfull = dvfull[t,:,:,:] |
---|
50 | out2 = vertical_int2(levels, pfull, tfull) |
---|
51 | out3[t,:,:,:]=out2[:,:,:] |
---|
52 | out2 = vertical_int2(levels, pfull, ufull) |
---|
53 | out4[t,:,:,:]=out2[:,:,:] |
---|
54 | out2 = vertical_int2(levels, pfull, vfull) |
---|
55 | out5[t,:,:,:]=out2[:,:,:] |
---|
56 | print("interpolated field:",out2) |
---|
57 | |
---|
58 | print("-----------------------------writing NetCDF--------------------------------") |
---|
59 | print(out3) |
---|
60 | #sys.exit(0) |
---|
61 | try: |
---|
62 | f = cdf.Dataset('interpolated_data.nc', 'w', format='NETCDF4') |
---|
63 | except: |
---|
64 | print("Error occurred while opening new netCDF file, Error: ", sys.exc_info()[0]) |
---|
65 | levels2 = [levels[i] / 100. for i in range(len(levels))] |
---|
66 | #levels2 = [int(i) for i in levels2] |
---|
67 | #print(levels2) |
---|
68 | f.createDimension('lon', len(lon)) |
---|
69 | f.createDimension('lat', len(lat)) |
---|
70 | f.createDimension('lev', len(levels2)) |
---|
71 | f.createDimension('time_counter', len(time_counterfull)) |
---|
72 | vlon = f.createVariable('lon', 'f4', 'lon') |
---|
73 | vlat = f.createVariable('lat', 'f4', 'lat') |
---|
74 | vlev = f.createVariable('lev', 'f4', 'lev') |
---|
75 | #vlev = f.createVariable('lev', 'i4', 'lev') |
---|
76 | vtime = f.createVariable('time_counter', 'f8', 'time_counter') |
---|
77 | vt = f.createVariable('t', 'f4',('time_counter', 'lev', 'lat', 'lon')) |
---|
78 | vu = f.createVariable('u', 'f4',('time_counter', 'lev', 'lat', 'lon')) |
---|
79 | vv = f.createVariable('v', 'f4',('time_counter', 'lev', 'lat', 'lon')) |
---|
80 | vPS = f.createVariable('PS', 'f4',('time_counter', 'lat', 'lon')) |
---|
81 | vlon[:] = lon |
---|
82 | vlat[:] = lat |
---|
83 | vlev[:] = levels2 |
---|
84 | vtime[:] = time_counterfull |
---|
85 | vt[:] = out3 |
---|
86 | vu[:] = out4 |
---|
87 | vv[:] = out5 |
---|
88 | vPS[:] = dPSfull |
---|
89 | f.close() |
---|
90 | print("-----------------------done--------------------") |
---|