我正在尝试通过经度和纬度从netcdf文件中提取区域。 但是,分辨率远远高于1x1度。 那么您将如何提取区域,例如lon:30-80,lat:30-40。 可以在以下位置找到该文件:https://drive.google.com/open?id=1zX-qYBdXT_GuktC81NoQz9xSxSzM-CTJ
键和形状如下:
odict_keys(['crs', 'lat', 'lon', 'Band1'])
crs ()
lat (25827,)
lon (35178,)
Band1 (25827, 35178)
我已经尝试过了,但是在高分辨率下,它并不代表实际的经度/纬度。
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
file = path + '20180801-ESACCI-L3S_FIRE-BA-MODIS-AREA_3-fv5.1-JD.nc'
fh = Dataset(file)
longitude = fh.variables['lon'][:]
latitude = fh.variables['lat'][:]
band1 = fh.variables['Band1'][:30:80,30:40]
答案 0 :(得分:1)
由于您拥有variables(dimensions): ..., int16 Band1(lat,lon)
,因此可以将np.where
应用于变量lat
和lon
以找到适当的索引,然后选择相应的Band1
数据作为sel_band1
:
import numpy as np
from netCDF4 import Dataset
file = '20180801-ESACCI-L3S_FIRE-BA-MODIS-AREA_3-fv5.1-JD.nc'
with Dataset(file) as nc_obj:
lat = nc_obj.variables['lat'][:]
lon = nc_obj.variables['lon'][:]
sel_lat, sel_lon = [30, 40], [30, 80]
sel_lat_idx = np.where((lat >= sel_lat[0]) & (lat <= sel_lat[1]))
sel_lon_idx = np.where((lon >= sel_lon[0]) & (lon <= sel_lon[1]))
sel_band1 = nc_obj.variables['Band1'][:][np.ix_(sel_lat_idx[0], sel_lon_idx[0])]
请注意,将np.where
应用于lat
和lon
会返回一维索引数组。使用np.ix_
将它们应用于Band1
中的2D数据。有关更多信息,请参见here。