0

I have 10 years output from the WRF climate model. I am looking for an efficient code which for every grid point in the xarray selects only those number of days where T>0 for more than 2 days. For my plots, I want for each month at each grid point the total number of days where T>2 for more than 2 days.

I am new to xarrays and looking at similar questions, I still couldn't find a proper loop or count function to apply for each grid point and month wise! Would really appreciate any help with this code.

Here is my current code:

import xarray as xr
import pandas as pd
import matplotlib.pyplot as plt
import netCDF4
from netCDF4 import Dataset
import numpy as np

#concatenate the 10year output
dataset=xr.open_mfdataset("\Python files for plotting wrfoutput\era5_1990-2000_output\*.nc",concat_dim='Time', combine='nested', compat='no_conflicts', preprocess=None, engine=None, data_vars='all', coords='all', parallel=False, join='outer', attrs_file=None,)

#dimensions are: Time, south_north, west_east
DS=dataset
DS = DS.assign_coords(Time=pd.to_datetime(DS['Time'].values))

#Select/extract only the mean 2m surface temperature (T2) from the large xarray 
DST2=DS.T2

#apply the where function to check at which grid points in each month the T2>0
T2threshold=DST2.groupby('Time.month').where(DST2>0)
dl.meteo
  • 1,658
  • 15
  • 25
MKM
  • 33
  • 5

1 Answers1

0

In general it is difficult to support you without a code that generates the issue you are running in.

Stackoverflow is not there to help you learn programming. It is there to help find solutions for edge cases and issues.

Never mind here are some thoughts for you. xarray is working similar as pandas. So if you can find a solution for pandas, try it with xarray.

ds['threshold_mask'] = ds.T2.where(dataset.T2>0)

Building a mask and then using groupby and cumsum:

ds.groupby((ds['threshold_mask'] == 0).cumsum().threshold_mask).cumsum()

No grants that this works, but I guess it will help you finding the right solution.

Seen here: Pandas : dataframe cumsum , reset if other column is false

dl.meteo
  • 1,658
  • 15
  • 25