How to compute the warm spell days index (ECAHWFI)

The Climate Change Service API contains an example that computes consecutive summer days index per time period (‘eca_csu’), see this link.

The associated Python code for the calculation of 'eca_scu' is as follows: 


eca_csu = ct.climate.compute_extreme_index(data, 'eca_csu', [26.0, 5])


I ran this code in the toolbox and it works like a charm.

Question: how does this code look like for the calculation  of the warm spell days index (ECAHWFI)?

According to the reference manual, the syntax is as follows:


cdo eca_hwfi tgfile tgn90file outfile


The API requires this syntax for the data item:


  • data (data object or list of data objects) – Data or list of data objects required to compute the cdo_index


which would translate in Python to the following line, where p90data was obtained from cdstoolbox.climate.climatology_perc:


eca_csu = ct.climate.compute_extreme_index([data, p90data], 'eca_hwfi', [6, 5])

However, when it use this line of code in the toolbox, then I get an AttributeError: 'list' object has no attribute 'name'.

Dear Martien,

The full traceback and workflow would help but my feeling is that p90data is a list. The way you call the cdo index looks right but cdstoolbox.climate.climatology_perc returns a list so if you have not selected the right element when declaring p90data it might sill be a list.

Let me know if that was the issue and if not try to share your workflow here.

Regards.

Vivien

Hi Vivien,

Thank you for your reply. Running the script in the toolbox works, except for the penultimate line: (eca_hwfi = ct.climate.compute_extreme_index([data_daily, clim_perc], 'eca_hwfi', [6, 5]))

I hope this helps.

Martien


import cdstoolbox as ct

@ct.application(title='Extract')
@ct.output.download()
def plot_time_series():
data = ct.catalogue.retrieve(
'reanalysis-era5-single-levels',
{
'variable': '2m_temperature',
'grid': ['1', '1'],
'product_type': 'reanalysis',
'year': ['2018', '2019'],
'month': ['01', '02', '03', '04', '05', '06','07', '08', '09', '10', '11', '12'],
'day': [
'01', '02', '03', '04', '05', '06',
'07', '08', '09', '10', '11', '12',
'13', '14', '15', '16', '17', '18',
'19', '20', '21', '22', '23', '24',
'25', '26', '27', '28', '29', '30',
'31'
],
'time': ['11:00', '12:00', '13:00', '14:00', '15:00'],
}
)

# Daily mean on selection
data_daily = ct.climate.daily_max(data)

# This works:
# eca_csu = ct.climate.compute_extreme_index(data_daily, 'eca_csu', [26.0, 5])

# For eca_hwfi I need two inputs: see CDO handbook: cdo eca_hwfi tgfile tgn90file outfile
# Hence this:
clim_perc = ct.climate.climatology_perc(data_daily, percentiles=[90]) # <- This works too

# eca_hwfi = ct.climate.compute_extreme_index([data_daily, clim_perc], 'eca_hwfi', [6, 5]) # <- This throws an error

return clim_perc


The error message when using eca_hwfi = ct.climate.compute_extreme_index([data_daily, clim_perc], 'eca_hwfi', [6, 5])


Traceback (most recent call last):
  File "/opt/cdstoolbox/cdscompute/cdscompute/cdshandlers/services/handler.py", line 49, in handle_request
    result = cached(context.method, proc, context, *context.args, **context.kwargs)
  File "/opt/cdstoolbox/cdscompute/cdscompute/caching.py", line 108, in cached
    result = proc(context, *context.args, **context.kwargs)
  File "/opt/cdstoolbox/cdscompute/cdscompute/services.py", line 118, in __call__
    return p(*args, **kwargs)
  File "/opt/cdstoolbox/cdscompute/cdscompute/services.py", line 59, in __call__
    return self.proc(context, *args, **kwargs)
  File "/home/cds/cdsservices/services/python_service.py", line 32, in execute
    raise exceptions.InternalError(logging + traceback, '')
cdsclient.exceptions.InternalError: Traceback (most recent call last):
  File "/opt/cdstoolbox/jsonrequest/jsonrequest/requests.py", line 71, in jsonrequestcall
    resp = coding.encode(req.callable(*req.args, **req.kwargs), register=encoders, **context)
  File "/opt/cdstoolbox/cdscdo/cdscdo/cdo.py", line 462, in _compute_extreme_index
    infiles_list = [io.dataarray_to_netcdf(infile, remove_atexit=True) for infile in data]
  File "/opt/cdstoolbox/cdscdo/cdscdo/cdo.py", line 462, in <listcomp>
    infiles_list = [io.dataarray_to_netcdf(infile, remove_atexit=True) for infile in data]
  File "/opt/cdstoolbox/cdscdm/cdscdm/io.py", line 128, in dataarray_to_netcdf
    if data.name is None:
AttributeError: 'list' object has no attribute 'name'