[4146] | 1 | from __future__ import division |
---|
| 2 | import numpy as np |
---|
| 3 | from netCDF4 import Dataset |
---|
| 4 | import math |
---|
| 5 | from scipy.interpolate import interp1d |
---|
| 6 | import netCDF4 as cdf |
---|
| 7 | |
---|
[4165] | 8 | dw1 = Dataset('../Experiments/LOWRES_DCMIP41/netcdf/output_dcmip2016_regular.nc','r') |
---|
[4146] | 9 | dtfull = dw1.variables['T'][:,:,:,:] |
---|
| 10 | dpfull = dw1.variables['P'][:,:,:,:] |
---|
| 11 | dufull = dw1.variables['U'][:,:,:,:] |
---|
| 12 | dvfull = dw1.variables['V'][:,:,:,:] |
---|
| 13 | dPSfull = dw1.variables['PS'][:,:,:] |
---|
| 14 | time_counterfull = dw1.variables['time_counter'][:] |
---|
| 15 | lat = dw1.variables['lat'][:] |
---|
| 16 | lon = dw1.variables['lon'][:] |
---|
| 17 | levels = [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 | |
---|
| 20 | levels = levels[::-1] |
---|
| 21 | print(levels) |
---|
| 22 | out3 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3])))) |
---|
| 23 | out4 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3])))) |
---|
| 24 | out5 = np.zeros((((dpfull.shape[0],len(levels),dpfull.shape[2],dpfull.shape[3])))) |
---|
| 25 | |
---|
| 26 | def 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 | |
---|
[4169] | 36 | for t in range(dtfull.shape[0]): |
---|
[4146] | 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 | |
---|
| 50 | print("-----------------------------writing NetCDF--------------------------------") |
---|
| 51 | print(out3) |
---|
| 52 | #sys.exit(0) |
---|
| 53 | try: |
---|
[4165] | 54 | f = cdf.Dataset('DYNAMICO.nc', 'w', format='NETCDF4') |
---|
[4146] | 55 | except: |
---|
| 56 | print("Error occurred while opening new netCDF file, Error: ", sys.exc_info()[0]) |
---|
| 57 | levels2 = [levels[i] / 100. for i in range(len(levels))] |
---|
| 58 | #levels2 = [int(i) for i in levels2] |
---|
| 59 | #print(levels2) |
---|
| 60 | f.createDimension('lon', len(lon)) |
---|
| 61 | f.createDimension('lat', len(lat)) |
---|
| 62 | f.createDimension('lev', len(levels2)) |
---|
| 63 | f.createDimension('time_counter', len(time_counterfull)) |
---|
| 64 | vlon = f.createVariable('lon', 'f4', 'lon') |
---|
| 65 | vlat = f.createVariable('lat', 'f4', 'lat') |
---|
| 66 | vlev = f.createVariable('lev', 'f4', 'lev') |
---|
| 67 | #vlev = f.createVariable('lev', 'i4', 'lev') |
---|
| 68 | vtime = f.createVariable('time_counter', 'f8', 'time_counter') |
---|
| 69 | vt = f.createVariable('t', 'f4',('time_counter', 'lev', 'lat', 'lon')) |
---|
| 70 | vu = f.createVariable('u', 'f4',('time_counter', 'lev', 'lat', 'lon')) |
---|
| 71 | vv = f.createVariable('v', 'f4',('time_counter', 'lev', 'lat', 'lon')) |
---|
| 72 | vPS = f.createVariable('PS', 'f4',('time_counter', 'lat', 'lon')) |
---|
| 73 | vlon[:] = lon |
---|
| 74 | vlat[:] = lat |
---|
| 75 | vlev[:] = levels2 |
---|
| 76 | vtime[:] = time_counterfull |
---|
| 77 | vt[:] = out3 |
---|
| 78 | vu[:] = out4 |
---|
| 79 | vv[:] = out5 |
---|
| 80 | vPS[:] = dPSfull |
---|
| 81 | f.close() |
---|
| 82 | print("-----------------------done--------------------") |
---|