[Python] How to count the sum of at least two consecutive days with same the values, from...

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have an Xarray dataset with 3 dimensions (time, lon, lat) and a heat variable. The heat takes the values 1 or 0 (1 = more than 30degC, 0 = less than 30degC), and I want to calculate the number of events with at least three consecutive days with value 1 (time is in daily step)

    I want to create an xarray dataset with only one-time value (one day) where each pixel will contain the number of events with at least three consecutive days with value 1.

    time 155 = all July days for years 2026-2030

    xarray.DataArray 'heat' time: 155, rlat: 6, rlon: 4


    Do you have some ideas on how to do this?

    here you can find the netcdf data EC7_heatjulZA26_30.nc

    tm = 'C:/Path_to_NC/EC7_heatjulZA26_30.nc'
    dmax = xr.open_dataset(tm, decode_coords="all")

    da_max = dmax['tasmax']
    da_max

    *xarray.DataArray'tasmax'time: 155, rlat: 6, rlon: 4*


    I sum (the 1 and 0) the data to one 6 x 4 raster, but I want to sum only those which are consecutively behind each other for at least 3 days.

    HW=np.sum(da_max,axis = 0)
    HW

    array([[24, 20, 14, 11],
    [26, 21, 15, 11],
    [23, 23, 15, 12],
    [14, 15, 14, 11],
    [12, 12, 14, 11],
    [14, 12, 15, 13]])

    Continue reading...

Compartilhe esta Página