source: dynamico_lmdz/guided/Data/vertical_interpolation.py

Last change on this file was 4169, checked in by jisesh, 5 years ago

guided: fixed index error in vertical_interpolation

File size: 3.1 KB
Line 
1from __future__ import division
2import numpy as np
3from netCDF4 import Dataset
4import math
5from scipy.interpolate import interp1d
6import netCDF4 as cdf
7
8dw1 = Dataset('../Experiments/LOWRES_DCMIP41/netcdf/output_dcmip2016_regular.nc','r')
9dtfull = dw1.variables['T'][:,:,:,:]
10dpfull = dw1.variables['P'][:,:,:,:]
11dufull = dw1.variables['U'][:,:,:,:]
12dvfull = dw1.variables['V'][:,:,:,:]
13dPSfull = dw1.variables['PS'][:,:,:]
14time_counterfull = dw1.variables['time_counter'][:]
15lat = dw1.variables['lat'][:]
16lon = dw1.variables['lon'][:]
17levels = [100., 200., 300., 500., 700., 1000., 2000., 3000., 5000., 7000., 10000., 12500., 15000., 17500., 20000., 22500.,25000., 30000., 35000., 40000., 
18    45000., 50000., 55000., 60000., 65000., 70000., 75000., 77500., 80000., 82500., 85000., 87500., 90000., 92500., 95000., 97500., 100000.]
19
20levels = levels[::-1]
21print(levels)
22out3 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3]))))
23out4 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3]))))
24out5 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3]))))
25
26def vertical_int2(levels, pfull, dataset):
27    nl, ni, nj = len(levels), dataset.shape[1], dataset.shape[2]
28    out = np.zeros((nl,ni,nj))
29    for i in range(dataset.shape[1]):
30        for j in range(dataset.shape[2]):
31            f = interp1d(pfull[:,i,j],dataset[:,i,j],kind='linear',fill_value="extrapolate")
32            out[:,i,j] = f(levels)
33    return out
34
35
36for t in range(dtfull.shape[0]):
37    print("time step=",t)
38    tfull = dtfull[t,:,:,:]
39    pfull = dpfull[t,:,:,:]
40    ufull = dufull[t,:,:,:]
41    vfull = dvfull[t,:,:,:]
42    out2 = vertical_int2(levels, pfull, tfull)
43    out3[t,:,:,:]=out2[:,:,:]
44    out2 = vertical_int2(levels, pfull, ufull)
45    out4[t,:,:,:]=out2[:,:,:]
46    out2 = vertical_int2(levels, pfull, vfull)
47    out5[t,:,:,:]=out2[:,:,:]
48    print("interpolated field:",out2)
49
50print("-----------------------------writing NetCDF--------------------------------")
51print(out3)
52#sys.exit(0)
53try:
54    f = cdf.Dataset('DYNAMICO.nc', 'w', format='NETCDF4')
55except:
56    print("Error occurred while opening new netCDF file, Error: ", sys.exc_info()[0])
57levels2 = [levels[i] / 100. for i in range(len(levels))]
58#levels2 = [int(i) for i in levels2]
59#print(levels2)
60f.createDimension('lon', len(lon))
61f.createDimension('lat', len(lat))
62f.createDimension('lev', len(levels2))
63f.createDimension('time_counter', len(time_counterfull))
64vlon = f.createVariable('lon', 'f4', 'lon')
65vlat = f.createVariable('lat', 'f4', 'lat') 
66vlev = f.createVariable('lev', 'f4', 'lev')
67#vlev = f.createVariable('lev', 'i4', 'lev')
68vtime = f.createVariable('time_counter', 'f8', 'time_counter')
69vt = f.createVariable('t', 'f4',('time_counter', 'lev', 'lat', 'lon'))
70vu = f.createVariable('u', 'f4',('time_counter', 'lev', 'lat', 'lon'))
71vv = f.createVariable('v', 'f4',('time_counter', 'lev', 'lat', 'lon'))
72vPS = f.createVariable('PS', 'f4',('time_counter', 'lat', 'lon'))
73vlon[:] = lon
74vlat[:] = lat
75vlev[:] = levels2
76vtime[:] = time_counterfull
77vt[:] = out3
78vu[:] = out4
79vv[:] = out5
80vPS[:] = dPSfull
81f.close()
82print("-----------------------done--------------------")
Note: See TracBrowser for help on using the repository browser.