Search code examples
timeslicepython-xarraynetcdfhour

NetCDF XArray time slice 12 midday 12 midnight NARR reanalysis data


stack overflow (think it's been over 10 years) but I am well and truly stuck!

I am trying to reduce the NARR reanalysis data to pass through a relatively simple calculation to replicate this analysis

The analysis states reduced the data set size by selecting the second half of the day 12 - 12 to identify the max value in that range. Xarray has the time slice function but I can't see anything about slicing a subset of hours per day just a date range?

is Xarray what I want to be using if so, how? If not, is there another package/approach which I should be utilising?

Time Array below:

array(['2022-01-01T00:00:00.000000000', '2022-01-01T03:00:00.000000000',
   '2022-01-01T06:00:00.000000000', ..., '2022-12-31T15:00:00.000000000',
   '2022-12-31T18:00:00.000000000', '2022-12-31T21:00:00.000000000'],
  dtype='datetime64[ns]')

Any help would be very much appreciated!


Solution

  • You can use Xarray's where to do more custom subsetting/slicing:

    import xarray as xr
    
    ds = xr.tutorial.open_dataset('air_temperature')
    ds_subset = ds.where(ds.time.dt.hour > 12, drop=True)
    # 👆 is what you want