source: dynamico_lmdz/guided/Data/vertical_interpolation.py @ 4163

Last change on this file since 4163 was 4146, checked in by jisesh, 5 years ago

guided: interpolate input data to pressure levels

File size: 3.3 KB
Line 
1from __future__ import division
2import argparse
3import numpy as np
4from netCDF4 import Dataset
5import matplotlib.pyplot as plt 
6from mpl_toolkits.basemap import Basemap
7import matplotlib.mlab as mlab
8import matplotlib.colors
9import sys 
10import os
11import math
12from scipy.interpolate import interp1d
13from matplotlib.ticker import AutoMinorLocator
14import netCDF4 as cdf
15
16dw1 = Dataset('output_dcmip2016_regular_30days_d1.nc','r')
17dtfull = dw1.variables['T'][:,:,:,:]
18dpfull = dw1.variables['P'][:,:,:,:]
19dufull = dw1.variables['U'][:,:,:,:]
20dvfull = dw1.variables['V'][:,:,:,:]
21dPSfull = dw1.variables['PS'][:,:,:]
22time_counterfull = dw1.variables['time_counter'][:]
23lat = dw1.variables['lat'][:]
24lon = dw1.variables['lon'][:]
25levels = [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
28levels = levels[::-1]
29print(levels)
30out3 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3]))))
31out4 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3]))))
32out5 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3]))))
33
34def 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
44for 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
58print("-----------------------------writing NetCDF--------------------------------")
59print(out3)
60#sys.exit(0)
61try:
62    f = cdf.Dataset('interpolated_data.nc', 'w', format='NETCDF4')
63except:
64    print("Error occurred while opening new netCDF file, Error: ", sys.exc_info()[0])
65levels2 = [levels[i] / 100. for i in range(len(levels))]
66#levels2 = [int(i) for i in levels2]
67#print(levels2)
68f.createDimension('lon', len(lon))
69f.createDimension('lat', len(lat))
70f.createDimension('lev', len(levels2))
71f.createDimension('time_counter', len(time_counterfull))
72vlon = f.createVariable('lon', 'f4', 'lon')
73vlat = f.createVariable('lat', 'f4', 'lat') 
74vlev = f.createVariable('lev', 'f4', 'lev')
75#vlev = f.createVariable('lev', 'i4', 'lev')
76vtime = f.createVariable('time_counter', 'f8', 'time_counter')
77vt = f.createVariable('t', 'f4',('time_counter', 'lev', 'lat', 'lon'))
78vu = f.createVariable('u', 'f4',('time_counter', 'lev', 'lat', 'lon'))
79vv = f.createVariable('v', 'f4',('time_counter', 'lev', 'lat', 'lon'))
80vPS = f.createVariable('PS', 'f4',('time_counter', 'lat', 'lon'))
81vlon[:] = lon
82vlat[:] = lat
83vlev[:] = levels2
84vtime[:] = time_counterfull
85vt[:] = out3
86vu[:] = out4
87vv[:] = out5
88vPS[:] = dPSfull
89f.close()
90print("-----------------------done--------------------")
Note: See TracBrowser for help on using the repository browser.