dataset time interpolation #6376
-
Good afternoon I asked this time ago in Stack Overflow but no answers still arrive! If anyone can help, will be great, thank you all in advance! https://stackoverflow.com/questions/71336717/xarray-dataset-time-interpolation I am trying to interpolate times from NetCDF with hourly mean values defined at specific date-times "YEAR-MONTH-DAY HOUR:30:00" to "YEAR-MONTH-DAY HOUR:00:00" That is the specific time when I need the information but I think that I am not using the correct method. import xarray as xr
from datetime import datetime
import matplotlib.pyplot as plt
url = "https://ihthredds.ihcantabria.com/thredds/dodsC/IBI_ANALYSISFORECAST_PHY_005_001/IBI/hourly/IBI_2021100400.nc"
ds = xr.open_dataset(url).squeeze(drop=True)
if ds.longitude.values.max() > 180:
ds = ds.assign_coords(longitude=(ds.longitude - 360))
ds = ds.sel(longitude=slice(-5, -1), latitude=slice(42.5, 45), time=slice(datetime(2021,10,3,0),datetime(2021,10,5,0)))
ds2 = ds.resample(time="1H").interpolate("linear") Can anyone tell me if this is the correct way?? Thank you in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Beta Was this translation helpful? Give feedback.
-
@spencerkclark, thank you for your answer! I've expected to find more infomarmation about such a simple operation, and I started to doubt if this operation was the correct one or standard method to do it. However I make a simple plots over the time and seems correct to me (figure attached). I want only to confirm that is the proper way to do this operation because it is quite recurrent in my workflows. Thank you very much! |
Beta Was this translation helpful? Give feedback.
ds.resample(time="1H").interpolate(kind="linear")
seems like a reasonable way to go about this, though perhaps I am misunderstanding your intentions. Could you say a little more about why you think this may not be the correct approach?