python-3.x - 从高分辨率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]
解决方案
既然你有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_
将它们应用于 中的 2D 数据Band1
。请参阅此处了解更多信息。
推荐阅读
- python - 决策树的简单递归[Python]
- asp.net-mvc - 在 mvc 中编辑后无法保留原始值
- c# - 在 hangfire jobfilter 生命周期中处理 Simple Injector 异步范围
- ruby-on-rails - ubuntu 服务器(nginx+passenger+capistrano)上的 Rails 应用程序日志在哪里?
- r - R中的Beta回归模型
- python - Django不呈现表单字段
- soap - 使用失眠症来打肥皂电话
- python - {Python} 如何将字典转换为坐标?
- python - 将绘图树状图应用于文本数据
- r - 确保使用 {mongolite} 读取 MongoDB 数据时数据帧变为 tibbles