Page tree
Skip to end of metadata
Go to start of metadata

Dear CDS Team, 

I am trying to reproduce the following rolling plot:

Using 'reanalysis-era5-pressure-levels-monthly-means'-data by plotting the relative humidity (%) against the time. The code I used can be found in the bottom of this post.

1. Approach

  • Tried using Chart Line plot
  • Extracting from specific geographical point using ct.geo.extract_point

Received error with no plot appearing: 

An error occurred consuming the image:
"Unknown error"


The hyperlink ("the image") gives me the following information:

{"data": [{"mode": "lines+markers", "x": ["2016-12-31", "2017-01-31", "2017-02-28", "2017-03-31", "2017-04-30", "2017-05-31", "2017-06-30", "2017-07-31", "2017-08-31", "2017-09-30", "2017-10-31", "2017-11-30", "2017-12-31", "2018-01-31", "2018-02-28", "2018-03-31", "2018-04-30", "2018-05-31", "2018-06-30", "2018-07-31", "2018-08-31", "2018-09-30", "2018-10-31", "2018-11-30"], "y": [23.415220260620117, 19.917795181274414, 17.67203140258789, 13.655970573425293, 11.731369972229004, 15.640883445739746, 19.300086975097656, 22.90304946899414, 15.036171913146973, 23.61700439453125, 26.047481536865234, 33.30152893066406, 32.64613342285156, 20.09418296813965, 12.759061813354492, 10.235821723937988, 11.96240234375, 16.967960357666016, 17.566856384277344, 37.258033752441406, 21.444129943847656, 21.325790405273438, 24.212051391601562, 37.59654235839844], "type": "scatter", "uid": "1e77ff61-af9d-4e34-87a2-297722c44724"}], "layout": {"yaxis": {"title": {"text": "Relative humidity (%)"}}, "xaxis": {"title": {"text": "time"}}}}


It seems like the time-axis is in strings and, thusly, cannot be plotted. How do I fix this?

2. Approach

  • Using data from the whole area '20/-180/-20/180' (North/West/South/East)
  • Still using Chart Line plot

Received error message:

Traceback (most recent call last):
File "/src/jsonrequest/jsonrequest/requests.py", line 71, in jsonrequestcall
resp = coding.encode(req.callable(*req.args, **req.kwargs), register=encoders, **context)
File "/src/cdsworkflows-image/cdsworkflows/submit_workflow.py", line 20, in submit_workflow
return workflow_bare_func(**kwargs)
File "/workflows/internal/code/55d332d3156350482c2965421ef3c492e2541566c350ce79f80443fc/workflows.py", line 38, in humidity
fig = ct.chart.line(data)
File "/src/cdsworkflows-image/cdsworkflows/local.py", line 27, in wrapped
return func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsplotly.py", line 98, in _default_fig_decorator
fig = func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 237, in conversion_decorator
return func(source, *args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 257, in select_dimensions
return func(source, *args, **kwargs)
File "/src/cdsplot/cdsplot/cdsplotly.py", line 56, in xlabel_decorator
fig = func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsplotly.py", line 75, in ylabel_decorator
fig = func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsplotly.py", line 419, in line
% (len(source.dims), source.dims)
AssertionError: data must have single dimension, found 3 valid dimensions instead: ('time', 'lat', 'lon').

From the error above, it looks like there are too many dimensions in the data array. So I try to take the average across the dimension "lat" of the data, using ct.cube.average, and receive another error:

AssertionError: data must have single dimension, found 2 valid dimensions instead: ('time', 'lon'). 

This time it wants me to have even fewer dimensions, which I don't completely understand.


3. Approach

  • This time using ct.cdsplot.rolling_plot
  • Tried using data from the whole area '20/-180/-20/180' (North/West/South/East)
  • Tried using data from specific geographical location

I received the following error message:

Traceback (most recent call last):
File "/src/jsonrequest/jsonrequest/requests.py", line 71, in jsonrequestcall
resp = coding.encode(req.callable(*req.args, **req.kwargs), register=encoders, **context)
File "/src/cdsworkflows-image/cdsworkflows/submit_workflow.py", line 20, in submit_workflow
return workflow_bare_func(**kwargs)
File "/workflows/internal/code/93f1df5918957bb31ee16bc34f77e3880ae37188486fc5006325ea06/workflows.py", line 39, in humidity
fig = ct.cdsplot.rolling_plot(data_sel)
File "/src/cdsworkflows-image/cdsworkflows/local.py", line 27, in wrapped
return func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 237, in conversion_decorator
return func(source, *args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 257, in select_dimensions
return func(source, *args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 267, in xlabel_decorator
fig = func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 288, in ylabel_decorator
fig = func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 403, in ticks_decorator
fig = func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 351, in legend_decorator
fig = func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 366, in title_decorator
fig = func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 507, in _subplot_decorator
fig = func(*args, **kwargs)
File "/src/cdsplot/cdsplot/cdsmatplotlib.py", line 955, in rolling_plot
step = int(year - years_data.group[0])
AttributeError: 'DataArrayGroupBy' object has no attribute 'group'

Trying to reduce the dimensions of the data array by taking the average over the dimension "lat", using ct.cube.average again, produces the same error as above.


My code for all three approaches:

import cdstoolbox as ct

@ct.application(title='Humidity')
@ct.output.figure()
def humidity():
data = ct.catalogue.retrieve(
'reanalysis-era5-pressure-levels-monthly-means',
{
'product_type': 'monthly_averaged_reanalysis',
'variable': 'relative_humidity',
'pressure_level': '1000',
'year': [
'2017', '2018',
],
'month': [
'01', '02', '03',
'04', '05', '06',
'07', '08', '09',
'10', '11', '12',
],
'area':'20/-180/-20/180',
'time': '00:00',
'grid': [3, 3]
}
)

#extent = {'area':[-20,20,-180,180]}
#data_select = ct.cube.select(data, extent=extent['area'])

data_sel = ct.geo.extract_point(data, lon=20, lat=180)

fig = ct.chart.line(data_sel)

#data_plot = ct.cube.average(data,dim='lat')
#data_plot2 = ct.cube.average(data_plot,dim='lon')

#fig = ct.chart.line(data_plot)

#fig = ct.cdsplot.rolling_plot(data_plot)

#fig = ct.cdsplot.rolling_plot(data_plot2)

#fig = ct.cdsplot.rolling_plot(data_sel)

return fig


I hope that you can help.


Kind regards,

Huayuan

2 Comments

  1. Dear Huayuan,

    To solve your first approach the output type need to be a livefigure and not a figure (@ct.output.livefigure()). The ct.chart.* library outputs livefiures indeed. Here is a working workflow: https://cds.climate.copernicus.eu/toolbox-editor/168/forum_chart_plot

    For your second approach you can only plot 1 dimensional data with ct.chart.line.

    I have not had a look at you third approach yet.

    Regards.

    Vivien

    1. Hi, Vivien
      thank you for your help!

      Best wishes,
      Huayuan