我正在使用siphon从Unidata Thredds服务器下载GFS数据,因此可以使用MetPy对其进行绘制。我写了一个脚本来做到这一点,昨天效果很好:
#Get data using siphon
best_gfs = TDSCatalog('http://thredds.ucar.edu/thredds/catalog/grib/NCEP/GFS/Global_0p25deg/catalog.xml?dataset=grib/NCEP/GFS/Global_0p25deg/Best')
best_ds = best_gfs.datasets[0]
ncss = best_ds.subset()
query = ncss.query()
query.lonlat_box(north=55, south=20, east=-60, west=-120).time(datetime.utcnow())
query.accept('netcdf4')
query.variables('Geopotential_height_isobaric')
data = ncss.get_data(query)
#Parse data using MetPy
ds = xr.open_dataset(NetCDF4DataStore(data))
data = ds.metpy.parse_cf()
time_of_run = data['reftime'][0].dt.strftime('%Y%m%d_%H%MZ').values
print(time_of_run)
当我在美国东部时间下午2点运行时,此代码输出了2020-03-29 12:00Z,一切都很好。
当我今天早上运行它时,出现一个错误:
Traceback (most recent call last):
File "C:\Users\jacks\Anaconda3\envs\metpy_test\lib\site-packages\xarray\core\dataset.py", line 1155, in _construct_dataarray
variable = self._variables[name]
KeyError: 'reftime'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "h5_rh_wind_gph_temp.py", line 51, in <module>
time_of_run = data['reftime'][0].dt.strftime('%Y%m%d_%H%MZ').values
File "C:\Users\jacks\Anaconda3\envs\metpy_test\lib\site-packages\xarray\core\dataset.py", line 1245, in __getitem__
return self._construct_dataarray(key)
File "C:\Users\jacks\Anaconda3\envs\metpy_test\lib\site-packages\xarray\core\dataset.py", line 1158, in _construct_dataarray
self._variables, name, self._level_coords, self.dims
File "C:\Users\jacks\Anaconda3\envs\metpy_test\lib\site-packages\xarray\core\dataset.py", line 165, in _get_virtual_variable
ref_var = variables[ref_name]
KeyError: 'reftime'
这表明对“ reftime”键的引用无效。为了进行调查,我随后打印了“数据” xarray:
<xarray.Dataset>
Dimensions: (isobaric6: 34, lat: 141, lon: 241, time1: 1)
Coordinates:
reftime1 (time1) datetime64[ns] ...
* time1 (time1) datetime64[ns] 2020-03-30T12:00:00
* isobaric6 (isobaric6) float32 40.0 100.0 ... 100000.0
* lat (lat) float32 55.0 54.75 54.5 ... 20.25 20.0
* lon (lon) float32 240.0 240.25 ... 299.75 300.0
crs object Projection: latitude_longitude
Data variables:
Geopotential_height_isobaric (time1, isobaric6, lat, lon) float32 ...
LatLon_Projection int32 ...
Attributes:
Originating_or_generating_Center: ...
Originating_or_generating_Subcenter: ...
GRIB_table_version: ...
Type_of_generating_process: ...
Analysis_or_forecast_generating_process_identifier_defined_by_originating...
Conventions: ...
history: ...
featureType: ...
History: ...
geospatial_lat_min: ...
geospatial_lat_max: ...
geospatial_lon_min: ...
geospatial_lon_max: ...
这表明我想要的信息(模型的运行时间)现在存储为“ reftime1”。为什么1突然出现在reftime键的末尾?它的出现有规律吗?我希望最终将这个脚本作为cron作业运行,以自动生成图,因此,最好能找到一种方法来预期名称的更改或完全避开键名。
从reftime
到的变化来自reftime1
THREDDS和netCDF-java处理基础GRIB数据的netCDF表示形式的方式。从根本上来说,GRIB是作为单独的2D数据切片到达的。为了创建时间,参考时间和各种垂直尺寸,netCDF-java正在查看可用于给定字段(例如Geopotential_height_isobaric
)的GRIB消息集。如果字段有不同的套次/垂直尺寸,然后单独尺寸与唯一的名称如创建reftime
,reftime1
,reftime2
。哪个字段以哪个维度名称结尾取决于netCDF-java在集合中遇到特定GRIB消息的顺序。
解决此问题的方法是避免依赖名称,而是使用元数据来确定要查找的内容。MetPy可以通过为您提供各种尺寸/坐标的适当别名来做到这一点:
# Will point to appropriate time1, time2, etc.
time = data.Geopotential_height_isobaric.metpy.time
这适用于特定变量的坐标。对于reftime
,由于它不是变量的坐标,因此您还可以通过查找气候和预报(CF)元数据标准名称来进行查询forecast_reference_time
:
filtered_ds = data_.filter_by_attrs(standard_name='forecast_reference_time')
仍然留下了一个xarray Dataset
,您需要找到某种方法来提取内部的唯一变量-我不确定从那里下来的最佳方法是什么。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句