【问题标题】:Extract area from high resolution netcdf file python从高分辨率netcdf文件python中提取区域
【发布时间】:2019-10-09 13:17:53
【问题描述】:

我正在尝试按经度和纬度从 netcdf 文件中提取区域。 然而,分辨率远高于 1x1 度。 那么你将如何提取一个区域,例如经度:30-80 纬度: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]

【问题讨论】:

  • 您已经不远了,我认为 fh.variables['Band1'][:30:80,30:40] 中的索引并没有达到您的预期...[30:80,30:40] 将是索引范围,而不是值范围。

标签: python-3.x netcdf matplotlib-basemap


【解决方案1】:

由于您有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_ 将它们应用于Band1 中的二维数据。请参阅here 了解更多信息。

【讨论】:

    猜你喜欢
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2021-07-22
    • 2017-06-06
    • 2020-06-04
    • 1970-01-01
    相关资源
    最近更新 更多