source: dynamico_lmdz/guided/Data/vertical_interpolation_camelot.py @ 4159

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

guided: scripts to generate input data from CAMELOT datasets

File size: 3.5 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('2001.nc','r')
17dtfull = dw1.variables['t'][:,:,:,:]
18dpfulloned = dw1.variables['level'][:]
19dpfull = np.zeros(((len(dpfulloned),dtfull.shape[2],dtfull.shape[3])))
20for l in xrange(0, len(dpfulloned)):
21    dpfull[l,:,:]= dpfulloned[l]
22       
23print(dpfull)
24dtfull = dw1.variables['t'][:,:,:,:]
25#dPSfull = dw1.variables['PS'][:,:,:]
26time_counterfull = dw1.variables['time'][:]
27lat = dw1.variables['lat'][:]
28lon = dw1.variables['lon'][:]
29levels = [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
32levels = levels[::-1]
33print(levels)
34
35dim0=dtfull.shape[0]
36dim1=dtfull.shape[1]
37dim2=dtfull.shape[2]
38dim3=dtfull.shape[3]
39out3 = np.zeros((((dim0,len(levels),dim2,dim3))))
40
41def 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
50pfull = dpfull
51
52for 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
58del tfull,dtfull
59
60dufull = dw1.variables['u'][:,:,:,:]
61out4 = np.zeros((((dim0,len(levels),dim2,dim3))))
62for t in range(dim1):
63    print("time step=",t)
64    ufull = dufull[t,:,:,:]
65    out2 = vertical_int2(levels, pfull, ufull)
66    out4[t,:,:,:]=out2[:,:,:]
67del ufull, dufull
68
69dvfull = dw1.variables['v'][:,:,:,:]
70out5 = np.zeros((((dim0,len(levels),dim2,dim3))))
71for t in range(dim1):
72    print("time step=",t)
73    vfull = dvfull[t,:,:,:]
74    out2 = vertical_int2(levels, pfull, vfull)
75    out5[t,:,:,:]=out2[:,:,:]
76del vfull,dvfull
77
78print("-----------------------------writing NetCDF--------------------------------")
79print(out3)
80#sys.exit(0)
81try:
82    f = cdf.Dataset('interpolated_data.nc', 'w', format='NETCDF4')
83except:
84    print("Error occurred while opening new netCDF file, Error: ", sys.exc_info()[0])
85levels2 = [levels[i] / 100. for i in range(len(levels))]
86#levels2 = [int(i) for i in levels2]
87#print(levels2)
88f.createDimension('lon', len(lon))
89f.createDimension('lat', len(lat))
90f.createDimension('lev', len(levels2))
91f.createDimension('time_counter', len(time_counterfull))
92vlon = f.createVariable('lon', 'f4', 'lon')
93vlat = f.createVariable('lat', 'f4', 'lat') 
94vlev = f.createVariable('lev', 'f4', 'lev')
95#vlev = f.createVariable('lev', 'i4', 'lev')
96vtime = f.createVariable('time_counter', 'f8', 'time_counter')
97vt = f.createVariable('t', 'f4',('time_counter', 'lev', 'lat', 'lon'))
98vu = f.createVariable('u', 'f4',('time_counter', 'lev', 'lat', 'lon'))
99vv = f.createVariable('v', 'f4',('time_counter', 'lev', 'lat', 'lon'))
100#vPS = f.createVariable('PS', 'f4',('time_counter', 'lat', 'lon'))
101vlon[:] = lon
102vlat[:] = lat
103vlev[:] = levels2
104vtime[:] = time_counterfull
105vt[:] = out3
106vu[:] = out4
107vv[:] = out5
108#vPS[:] = dPSfull
109f.close()
110print("-----------------------done--------------------")
Note: See TracBrowser for help on using the repository browser.