def missing_interp(lon, lat, arr): grid0 = create_grid(lon[0, :], lat[:, 0]) varri = MV2.asarray(arr) varri = set_grid(varri, grid0) tempf = fill2d(varri, method='carg') lon = np.ma.masked_where(tempf == tempf.fill_value, lon) lat = np.ma.masked_where(tempf == tempf.fill_value, lat) arri = griddata(lon.compressed(), lat.compressed(), tempf.compressed(), (lon[0, :], lat[:, 0]), method='nat', ext=True, sub=10) return arri
from vcmq import P, N, MV2, code_file_name, os, add_grid, rotate_grid, set_grid, \ create_grid, rc, rcdefaults, plot2d from vacumm.misc.grid.regridding import regrid2d # Input grid and data nxi = 20 nyi = 15 # - rect xi = N.arange(nxi*1.) yi = N.arange(nyi*1.) gridri = create_grid(xi, yi) xxri, yyri = N.meshgrid(xi, yi) zzri = N.ma.array(yyri) zzri[int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked varri = MV2.asarray(zzri) set_grid(varri, gridri) # - curv gridci = rotate_grid(gridri, 30) xxci = gridci.getLongitude().getValue() yyci = gridci.getLatitude().getValue() zzci = N.ma.array(yyci) zzci[int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked varci = MV2.asarray(zzci) set_grid(varci, gridci) # Output positions nxo = 25 nyo = 18 # - rect dxi = xi[-1]-xi[0]
# Input grid and data nxi = 20 nyi = 15 nt = 5 # - rect xi = N.arange(nxi*1.) yi = N.arange(nyi*1.) ti = create_time((nt, ), 'years since 2000') gridri = create_grid(xi, yi) xxri, yyri = N.meshgrid(xi, yi) zzri = N.ma.resize(yyri, (nt, nyi, nxi)) zzri[:, int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked zzri[1] = N.ma.masked varri = MV2.asarray(zzri) varri.setAxis(0, ti) set_grid(varri, gridri) # - curv gridci = rotate_grid(gridri, 30) xxci = gridci.getLongitude().getValue() yyci = gridci.getLatitude().getValue() zzci = N.ma.resize(yyci, (nt, nyi, nxi)) zzci[:, int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked zzci[1] = N.ma.masked varci = MV2.asarray(zzci) varci.setAxis(0, ti) set_grid(varci, gridci) # Output positions nxo = 25
P.subplot(132, sharex=ax, sharey=ax) P.pcolormesh(xbo, ybo, vol, **kw) P.title('Linear') P.subplot(133, sharex=ax, sharey=ax) P.pcolormesh(xbo, ybo, voc, **kw) P.ylim(ymin=min(ybi.min(), ybo.min()), ymax=max(ybi.max(), ybo.max())) P.title('Cellave') P.tight_layout() P.savefig(figfile) # 1d->1d depi1d = create_dep(N.arange(-4500., 1, 500)) depo1d = create_dep(N.arange(-4000., 1, 333.33)) nzi = depi1d.shape[0] vari = MV2.asarray( N.ma.resize(depi1d[:], (nt, ny, nx, nzi)).transpose([0, 3, 1, 2])) vari.setAxis(1, depi1d) varol1 = regrid1d(vari, depo1d, method='linear') varol2 = regrid1d(vari, depo1d, method='linear', iaxi=0, iaxo=0, axi=depi1d) result.append(('assertEqual', [(varol1 - varol2).std(), 0])) varoc = regrid1d(vari, depo1d, method='cellave') myplot(vari, depi1d, varol1, varoc, depo1d, code_file_name(ext='_0.png')) # 4d->1d depi1d = N.arange(-4500., 1, 500) nzi = depi1d.shape[0] depi4d = N.resize(N.resize(depi1d, (nx, ny, nzi)).T, (nt, nzi, ny, nx)) depi4d += 500 * (N.random.random(depi4d.shape) - 0.5) depo1d = create_dep(N.arange(-4000., 1, 333.33)) vari = MV2.array(depi4d, fill_value=1e20) vari.getAxis(1).designateLevel()
P.subplot(132, sharex=ax, sharey=ax) P.pcolormesh(xbo, ybo, vol, **kw) P.title('Linear') P.subplot(133, sharex=ax, sharey=ax) P.pcolormesh(xbo, ybo, voc, **kw) P.ylim(ymin=min(ybi.min(), ybo.min()), ymax=max(ybi.max(), ybo.max())) P.title('Cellave') P.tight_layout() P.savefig(figfile) # 1d->1d depi1d = create_dep(N.arange(-4500., 1, 500)) depo1d = create_dep(N.arange(-4000., 1, 333.33)) nzi = depi1d.shape[0] vari = MV2.asarray(N.ma.resize(depi1d[:], (nt, ny, nx, nzi)).transpose([0, 3, 1, 2])) vari.setAxis(1, depi1d) varol1 = regrid1d(vari, depo1d, method='linear') varol2 = regrid1d(vari, depo1d, method='linear', iaxi=0, iaxo=0, axi=depi1d) result.append(('assertEqual', [(varol1-varol2).std(), 0])) varoc = regrid1d(vari, depo1d, method='cellave') myplot(vari, depi1d, varol1, varoc, depo1d, code_file_name(ext='_0.png')) # 4d->1d depi1d = N.arange(-4500., 1, 500) nzi = depi1d.shape[0] depi4d = N.resize(N.resize(depi1d, (nx, ny, nzi)).T, (nt, nzi, ny, nx)) depi4d += 500*(N.random.random(depi4d.shape)-0.5) depo1d = create_dep(N.arange(-4000., 1, 333.33)) vari = MV2.array(depi4d, fill_value=1e20)