0

I have a daily netcdf dataset. I want to get each year maximum value, and return the season of the maximum value is in.

I have tried the xarray.DataArray.idxmax, below is the example, it can return the coordinate label of the maximum value along a dimension.

IN:

import xarray as xr

array = xr.DataArray(
        [
            [2.0, 1.0, 2.0, 0.0, -2.0],
            [-4.0, np.NaN, 2.0, np.NaN, -2.0],
            [np.NaN, np.NaN, 1.0, np.NaN, np.NaN],
        ],
        dims=["y", "x"],
        coords={"y": [-1, 0, 1], "x": np.arange(5.0) ** 2},
    )
array.max(dim="x")

OUT:

<xarray.DataArray (y: 3)>
array([2., 2., 1.])
    Coordinates:
      * y        (y) int64 -1 0 1

IN:

array.argmax(dim="x")

OUT:

<xarray.DataArray (y: 3)>
array([0, 2, 2])
    Coordinates:
      * y        (y) int64 -1 0 1

I know xarray.DataArray.idxmax can return the day of the all years. but i can't turn this day to each year's season is. The most difficult thing for me is winter, because winter spans between in two years.

How can I achieve that?

hpchavaz
  • 1,368
  • 10
  • 16
shen159876
  • 11
  • 1

1 Answers1

1

You tagged the question with cdo, and the cdo answer to the first part is fairly straightforward, getting the maximum in the year is just

cdo yearmax in.nc out.nc 

That is the first part of your question. But in terms of the season it occurs in, How are you defining your seasons? DJF, MAM, JJA, and SON? Can you clarify this in your question? Or would knowing the month be adequate (or even the date itself?).

Assuming it is these standard seasons as defined by CDO, then you could try something along these lines where you make a mask of zero for all days below the maximum, 1=day of maximum. You then do a seasonal sum, and then mask again for non-zero seasons... But this method requires you to split the file into years using yearsplit:

# split the files into years, year2010.nc year2011.nc 
cdo splityear t2m_daymean.nc year

# loop over the years, 
for y in $(seq 2010 2020) ; do 
  # 1 for day of max and 0 otherwise
  cdo gec,0 -sub year${y}.nc -yearmax year${y}.nc mask${y}.nc 
  # Now do the seasonal sum, will be >0 for seasonal with max,
  # then convert this to 1, 0 otherwise
  cdo -gt,0 -seassum mask${y}.nc seas_max${y}.nc
done 

now the files seas_max2010 etc will have 4 time slices for the 4 seasons, with dates like: 2019-01-30 2019-04-15 2019-07-16 2019-10-16 and 0 for all seasons except the one with the max...

You can merge the files back together with cdo merge or cdo mergetime if you want.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86