Function for converting 10 min and 30 min NETCDF time series to hourly + remove NaNs in Python?

Viewed 164

I am trying to parse through NOAA buoy NetCDF files and depending on when they were launched, the data is recorded every 10 min, 30 min, or hourly. I need all of them to be consistently hourly in Python. So, anything less than an hour needs to be averaged. I've seen methods using cdo and resample but I can't seem to get much to work.

Sample 10 min nc file I'm working with: https://dods.ndbc.noaa.gov/thredds/fileServer/data/stdmet/41053/41053h9999.nc

The three variables I care about are wave_height, average_wpd, and mean_wave_dir. I'm fine if I can replace NaNs with a number like 9999. I need a function that can convert and average any time series into hourly.

3 Answers

you can try using 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')


This seems to work fine in CDO as follows:

cdo hourmean 41053h9999.nc outfile.nc

Output looks good when I check:

cdo showtime outfile.nc

You say, you have tried cdo, but it didn't work. If you tried the above, you may need to check your CDO version.

If you want to do this in python, you could use nctoolkit, which has a method for calling CDO:

import nctoolkit as nc
ds = nc.open_data("infile.nc")
ds.cdo_command("hourmean") 
ds.to_nc("outfile.nc")

I solved it with using xarray and the function ds.resample(time="H").mean()

With ds being my named dataset after reading it in as ds = xr.open_dataset('filename.nc)

Related