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