将 10 分钟和 30 分钟 NETCDF 时间序列转换为每小时的函数 + 删除 Python 中的 NaN?
Function for converting 10 min and 30 min NETCDF time series to hourly + remove NaNs in Python?
我正在尝试解析 NOAA 浮标 NetCDF 文件,根据它们的启动时间,数据每 10 分钟、30 分钟或每小时记录一次。我需要他们所有人都在 Python 中始终保持每小时。因此,任何少于一个小时的时间都需要进行平均。我已经看到使用 cdo
和 resample
的方法,但我似乎无能为力。
我正在使用的 10 分钟 nc 文件示例:https://dods.ndbc.noaa.gov/thredds/fileServer/data/stdmet/41053/41053h9999.nc
我关心的三个变量是wave_height、average_wpd和mean_wave_dir。如果我能用像 9999 这样的数字替换 NaN 就好了。我需要一个可以将任何时间序列转换并平均为每小时的函数。
这在 CDO 中似乎工作正常,如下所示:
cdo hourmean 41053h9999.nc outfile.nc
我检查时输出看起来不错:
cdo showtime outfile.nc
你说,你试过cdo,但是没用。如果您尝试过上述方法,您可能需要检查您的 CDO 版本。
如果你想在python中这样做,你可以使用nctoolkit,它有一个调用CDO的方法:
import nctoolkit as nc
ds = nc.open_data("infile.nc")
ds.cdo_command("hourmean")
ds.to_nc("outfile.nc")
你可以尝试使用 xarray:
import xarray as xr
ds = xr.open_dataset("./41053h9999.nc")
ds_resampled = ds.resample(time='1H').mean() # or use other methods if you like see: http://xarray.pydata.org/en/stable/generated/xarray.Dataset.resample.html
# remove nans:
ds_resampled = ds_resampled.dropna('time')
我使用 xarray
和函数 ds.resample(time="H").mean()
解决了这个问题
将 ds
作为我的命名数据集读入后 ds = xr.open_dataset('filename.nc)
我正在尝试解析 NOAA 浮标 NetCDF 文件,根据它们的启动时间,数据每 10 分钟、30 分钟或每小时记录一次。我需要他们所有人都在 Python 中始终保持每小时。因此,任何少于一个小时的时间都需要进行平均。我已经看到使用 cdo
和 resample
的方法,但我似乎无能为力。
我正在使用的 10 分钟 nc 文件示例:https://dods.ndbc.noaa.gov/thredds/fileServer/data/stdmet/41053/41053h9999.nc
我关心的三个变量是wave_height、average_wpd和mean_wave_dir。如果我能用像 9999 这样的数字替换 NaN 就好了。我需要一个可以将任何时间序列转换并平均为每小时的函数。
这在 CDO 中似乎工作正常,如下所示:
cdo hourmean 41053h9999.nc outfile.nc
我检查时输出看起来不错:
cdo showtime outfile.nc
你说,你试过cdo,但是没用。如果您尝试过上述方法,您可能需要检查您的 CDO 版本。
如果你想在python中这样做,你可以使用nctoolkit,它有一个调用CDO的方法:
import nctoolkit as nc
ds = nc.open_data("infile.nc")
ds.cdo_command("hourmean")
ds.to_nc("outfile.nc")
你可以尝试使用 xarray:
import xarray as xr
ds = xr.open_dataset("./41053h9999.nc")
ds_resampled = ds.resample(time='1H').mean() # or use other methods if you like see: http://xarray.pydata.org/en/stable/generated/xarray.Dataset.resample.html
# remove nans:
ds_resampled = ds_resampled.dropna('time')
我使用 xarray
和函数 ds.resample(time="H").mean()
将 ds
作为我的命名数据集读入后 ds = xr.open_dataset('filename.nc)