首页 > 解决方案 > 从高分辨率netcdf文件python中提取区域

问题描述

我正在尝试按经度和纬度从 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]

标签: python-3.xnetcdfmatplotlib-basemap

解决方案


既然你有variables(dimensions): ..., int16 Band1(lat,lon),你可以应用np.where到变量latlon找到适当的索引,然后选择相应的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应用于latlon返回一维索引数组。用于np.ix_将它们应用于 中的 2D 数据Band1。请参阅此处了解更多信息。


推荐阅读