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

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

guided: Improved input data preperation from CAMELOT data sets

File size: 3.6 KB
Line 
1from __future__ import division
2import numpy as np
3from netCDF4 import Dataset
4import sys 
5import os
6import math
7from scipy.interpolate import interp1d
8import 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.]
13levels = [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.]
15levels = levels[::-1]
16print(levels)
17
18dw1 = Dataset('2001.nc','r')
19dtfull = dw1.variables['t'][:,:,:,:]
20dpfulloned = dw1.variables['level'][:]
21dpfull = np.zeros(((len(dpfulloned),dtfull.shape[2],dtfull.shape[3])))
22#------ mb conversion
23#dpfulloned = dpfulloned *100.
24for l in range(0, len(dpfulloned)):
25    dpfull[l,:,:]= dpfulloned[l]
26print(dpfull)
27
28dtfull = dw1.variables['t'][:,:,:,:]
29#dPSfull = dw1.variables['PS'][:,:,:]
30time_counterfull = dw1.variables['time'][:]
31lat = dw1.variables['lat'][:]
32lon = dw1.variables['lon'][:]
33
34dim0=dtfull.shape[0]
35dim1=dtfull.shape[1]
36dim2=dtfull.shape[2]
37dim3=dtfull.shape[3]
38out3 = np.zeros((((dim0,len(levels),dim2,dim3))))
39
40def 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
49pfull = dpfull
50
51for t in range(dim0):
52    print("time step=",t)
53    tfull = dtfull[t,:,:,:]
54    out2 = vertical_int2(levels, pfull, tfull)
55    out3[t,:,:,:]=out2[:,:,:]
56del tfull,dtfull
57
58dufull = dw1.variables['u'][:,:,:,:]
59out4 = np.zeros((((dim0,len(levels),dim2,dim3))))
60for t in range(dim0):
61    print("time step=",t)
62    ufull = dufull[t,:,:,:]
63    out2 = vertical_int2(levels, pfull, ufull)
64    out4[t,:,:,:]=out2[:,:,:]
65del ufull, dufull
66
67dvfull = dw1.variables['v'][:,:,:,:]
68out5 = np.zeros((((dim0,len(levels),dim2,dim3))))
69for t in range(dim0):
70    print("time step=",t)
71    vfull = dvfull[t,:,:,:]
72    out2 = vertical_int2(levels, pfull, vfull)
73    out5[t,:,:,:]=out2[:,:,:]
74del vfull,dvfull
75
76
77print("-----------------------------writing NetCDF--------------------------------")
78print(out3)
79try:
80    f = cdf.Dataset('interpolated_data.nc', 'w', format='NETCDF4')
81except:
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))]
84levels2 = levels
85#levels2 = [int(i) for i in levels2]
86f.createDimension('lon', len(lon))
87f.createDimension('lat', len(lat))
88f.createDimension('lev', len(levels2))
89f.createDimension('time_counter', len(time_counterfull))
90vlon = f.createVariable('lon', 'f4', 'lon')
91vlat = f.createVariable('lat', 'f4', 'lat') 
92vlev = f.createVariable('lev', 'f4', 'lev')
93#vlev = f.createVariable('lev', 'i4', 'lev')
94vtime = f.createVariable('time_counter', 'f8', 'time_counter')
95vt = f.createVariable('t', 'f4',('time_counter', 'lev', 'lat', 'lon'))
96vu = f.createVariable('u', 'f4',('time_counter', 'lev', 'lat', 'lon'))
97vv = f.createVariable('v', 'f4',('time_counter', 'lev', 'lat', 'lon'))
98#vPS = f.createVariable('PS', 'f4',('time_counter', 'lat', 'lon'))
99vlon[:] = lon
100vlat[:] = lat
101vlev[:] = levels2
102vtime[:] = time_counterfull
103vt[:] = out3
104vu[:] = out4
105vv[:] = out5
106#vPS[:] = dPSfull
107f.close()
108print("-----------------------done--------------------")
Note: See TracBrowser for help on using the repository browser.