将选择GFSensemble openDAP数据加载到内存(Python)

2024-06-16 14:51:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过netCDF和xarray从OpenDAP服务器下载GFS集成数据的一个子集。但是,当尝试将子选择加载到内存中时,程序会在返回运行时错误(netCDF:I/O失败)后崩溃。在

我希望获得的数据点数量是13650,因此数据大小应该可以用Python轻松处理。在

奇怪的是,我在下载GFS数据或NCEP再分析数据时没有遇到这个问题。这使我相信这个问题可能与数据维度的数量有关,因为集合数据有5个维度,而再分析和操作(GFS)数据只有4个维度。在

我也尝试过在只使用netCDF4模块的情况下下载数据,但这导致了相同的错误。因此,我不认为这个问题与xarray有关。在

以下是下载数据所需的代码:

from netCDF4 import Dataset
import numpy as np
import pandas as pd
import xarray as xr
import time as tm

# Set time to download data from (this is always the 00UTC run of the present day)
time_year = str(tm.localtime()[0])
time_month = str(tm.localtime()[1])
time_day = str(tm.localtime()[2])

if len(time_month)== 1:
    time_month = '0' + time_month
datestr = time_year + time_month + time_day
print('The run chosen is the 00 UTC run of ' + time_day + '-' + time_month + '-' + time_year)

# Define server information
serverstring='http://nomads.ncep.noaa.gov:9090/dods/gens_bc/gens' + datestr + '/gep_all_00z'
print(serverstring)

# Load data 
dataset = xr.open_dataset(serverstring)
time = dataset.variables['time']  
lat = dataset.variables['lat'][:]
lon = dataset.variables['lon'][:]
lev = dataset.variables['lev'][:]
ens = dataset.variables['ens'][:]

# Select user settings to plot (in this case all timesteps for all (20) members for a box around the Netherlands near the surface)
time_toplot = time  # select all available timesteps
lat_toplot = np.arange(50, 55, 0.5)
lon_toplot = np.arange(2, 8, 0.5)
lev_toplot = np.array([1000])
ens_toplot = ens  # select all available ensemble members

# Select required data via xarray
dataset = dataset.sel(ens=ens_toplot, time=time_toplot, lev=lev_toplot, lon=lon_toplot, lat=lat_toplot)

# Loading the data into memory finally results in the error
u = dataset.variables["ugrdprs"].values

谢谢!在


Tags: the数据importtimeasvariablesalldataset