简体   繁体   English

将高分辨率的网格数据重新栅格化为低分辨率的较粗糙数据?

[英]Regridding high resolution gridded data to low resolution coarser resolution data?

I have a netcdf file with high resolution grided data (.1 x .1 deg) and plotting on the basemap using matplotlib. 我有一个带有高分辨率栅格数据(.1 x .1度)的netcdf文件,并使用matplotlib在底图上进行绘图。 The plot I am trying to do is contour lines. 我想做的图是轮廓线。 For a specific reason I would like to plot the data at the interval of 1 x 1 deg resolution. 由于特定的原因,我想以1 x 1度分辨率的间隔绘制数据。 For which I have used the following code sample example from here Regridding regular netcdf data . 为此,我使用了以下代码示例示例,从此处重新捕获常规的netcdf数据

See the update 1 for link to actual data. 有关实际数据的链接,请参见更新1。

For the sake of clearity following is the code I am trying to execute for regridding to lower resolution:- 为了清楚起见,以下是我尝试执行的代码以重新划分为较低的分辨率:-

from mpl_toolkits import basemap
from netCDF4 import Dataset

filename = 'path/to/netcdf/file.nc'
with Dataset(filename, mode='r') as fh:
   lons = fh.variables['LON'][:]
   lats = fh.variables['LAT'][:]
   data = fh.variables['Data'][:].squeeze()

lons_sub, lats_sub = np.meshgrid(lons[::4], lats[::4], sparse=True)

data_coarse = basemap.interp(data, lons, lats, lons_sub, lats_sub, order=1)

The code seems to be correct. 该代码似乎是正确的。 But when I execute the code I get the following error in the line data_coarse = basemap.interp(data, lons, lats, lons_sub, lats_sub, order=3) :- 但是,当我执行代码时,我在data_coarse = basemap.interp(data, lons, lats, lons_sub, lats_sub, order=3)行中收到以下错误:-

/__init__.py", line 4897, in interp
    if xin[-1]-xin[0] < 0 or yin[-1]-yin[0] < 0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I have not understood where the problem lies and how to solve this? 我不知道问题出在哪里,如何解决?

Any help is appreciated. 任何帮助表示赞赏。

Update 1 更新1

link to the actual data https://www.dropbox.com/s/ddlpbw5vvj5kz5w/mslp.txt?dl=0 链接到实际数据https://www.dropbox.com/s/ddlpbw5vvj5kz5w/mslp.txt?dl=0

link to lats https://www.dropbox.com/s/lpwjavxtwtt3r13/xlat.txt?dl=0 链接到经纬度https://www.dropbox.com/s/lpwjavxtwtt3r13/xlat.txt?dl=0

and the link to lons https://www.dropbox.com/s/1a0q49drfcd2o9h/xlon.txt?dl=0 以及指向lons的链接https://www.dropbox.com/s/1a0q49drfcd2o9h/xlon.txt?dl=0

In your code snippet, xin = lons and yin = lats . 在您的代码段中, xin = lonsyin = lats From the basemap.interp docs, basemap.interp文档中,

xin, yin: rank-1 arrays containing x and y of datain grid in increasing order. xin,yin:包含网格中data的x和y的等级1数组,以递增顺序排列。

meaning xin ( lons ) and yin ( lats ) have to be continuously increasing. 意思是xinlons )和yinlats )必须不断增加。 From your error traceback, it looks like that is not the case since either xin[-1]-xin[0] < 0 and/or yin[-1]-yin[0] < 0 . 从错误回溯来看,情况似乎并非如此,因为xin[-1]-xin[0] < 0和/或yin[-1]-yin[0] < 0

Without knowing what lons or lats are exactly (since your code does not run with the data you link to), it is tough to elaborate any further. 不知道确切的lonslats (因为您的代码未使用链接到的数据运行),因此很难进一步详细说明。

Edit - plotting with basemap 编辑-使用底图进行绘图

I think the main problem you were running into was the shape of your latitude and longitude data. 我认为您遇到的主要问题是纬度和经度数据的形状。 Since lons and lats were essentially stacks of the same arrays, I converted them both to 1d arrays as you'll see below. 由于lonslats本质上是同一数组的堆栈,因此我将它们都转换为1d数组,如下所示。

import numpy as np
from mpl_toolkits import basemap
import matplotlib.pyplot as plt

# load data
data = np.loadtxt('mslp.txt', delimiter=',')
lons = np.loadtxt('xlon.txt', delimiter=',')
lats = np.loadtxt('xlat.txt', delimiter=',')

# take 1 dimensional slice of lons and lats
longitude = lons[0]   # array([31.1, 31.4, 31.7,...,119.1, 119.3, 119.6])
latitude  = lats[:,0] # array([-2.3, -2.1, -1.8,...,39.4, 39.6, 39.8])

# subdivide longitude and latitude into ~1deg grid data
lons_sub, lats_sub = np.meshgrid(longitude[::4], latitude[::4])

# implement basemap.interp to interpolate along ~1deg grid data
data_course = basemap.interp(datain=data, xin=longitude, yin=latitude,
                             xout=lons_sub, yout=lats_sub, order=3)

m = basemap.Basemap(llcrnrlon=longitude[0],  llcrnrlat=latitude[0],
                    urcrnrlon=longitude[-1], urcrnrlat=latitude[-1],
                    projection='merc', resolution='c')

fig = plt.figure()
# ax1 -- contour plot of resampled data
ax1 = fig.add_subplot(211)
m.contour(lons_sub, lats_sub, data_course, linewidths=2.5, latlon=True)
m.fillcontinents(color=[0.7, 0.9, 0.8], lake_color=[0.3, 0.7, 0.9])
m.drawmapboundary(fill_color=[0.3, 0.7, 0.9])
m.drawcoastlines()

# ax2 -- imshow plot of resampled data
ax2 = fig.add_subplot(212)
m.drawcoastlines()
m.imshow(data_course, interpolation='nearest',
         extent=[lons_sub[0], lons_sub[-1], lats[0], lats[-1]])

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM