# Any function to download data and plot time series on graph

**URL:** <https://forum.ecmwf.int/t/any-function-to-download-data-and-plot-time-series-on-graph/1768>\
**Category:** Other - Software and tools\
**Tags:** c3s, toolbox\
**Created:** [16 January 2023 10:01 UTC](https://forum.ecmwf.int/t/any-function-to-download-data-and-plot-time-series-on-graph/1768 "2023-01-16T10:01:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![User\_akanksha\_dot\_ba](https://forum.ecmwf.int/letter_avatar_proxy/v4/letter/u/b5a626/32.png) [@User\_akanksha\_dot\_ba](https://forum.ecmwf.int/u/User_akanksha_dot_ba)\
**Post date:** [16 January 2023 10:01 UTC](https://forum.ecmwf.int/t/any-function-to-download-data-and-plot-time-series-on-graph/1768/1 "2023-01-16T10:01:51Z")

</div>

&nbsp;Hi All, I am working on surface albedo data. Is there any function using which I can download the data as well as extract a time series and plot the graph.&nbsp;

Thank you

---

<div class="post-metadata">

**Author:** ![Kevin\_Marsh](https://forum.ecmwf.int/user_avatar/forum.ecmwf.int/kevin_marsh/32/368_2.png) [@Kevin\_Marsh](https://forum.ecmwf.int/u/Kevin_Marsh)\
**Post date:** [2 February 2023 09:54 UTC](https://forum.ecmwf.int/t/any-function-to-download-data-and-plot-time-series-on-graph/1768/2 "2023-02-02T09:54:35Z")

</div>

Hi, Have you seen the examples in the Toolbox gallery? e.g.

[https://cds.climate.copernicus.eu/toolbox/doc/gallery/3\_extract\_time\_series\_and\_plot\_graph.html](https://cds.climate.copernicus.eu/toolbox/doc/gallery/3_extract_time_series_and_plot_graph.html)

Thanks,

Kevin

---

<div class="post-metadata">

**Author:** ![User\_akanksha\_dot\_ba](https://forum.ecmwf.int/letter_avatar_proxy/v4/letter/u/b5a626/32.png) [@User\_akanksha\_dot\_ba](https://forum.ecmwf.int/u/User_akanksha_dot_ba)\
**Post date:** [27 February 2023 15:02 UTC](https://forum.ecmwf.int/t/any-function-to-download-data-and-plot-time-series-on-graph/1768/3 "2023-02-27T15:02:04Z")

</div>

Hi [Kevin Marsh](https://confluence.ecmwf.int/display/~makm)&nbsp;

```
Yes I tried the one that you shared but I am getting the following error. Could you please help in resolving it.   
Also, I wish to know if I want to download the data instead of getting figure as an output, should I put the command 'return data' in The end?
```

```
Traceback (most recent call last): File "/opt/cdstoolbox/cdscompute/cdscompute/cdshandlers/services/handler.py", line 59, 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 124, in \_\_call\_\_ return p  
(\*args, \*\*kwargs) File "/opt/cdstoolbox/cdscompute/cdscompute/services.py", line 60, in \_\_call\_\_ return self.proc(context, \*args, \*\*kwargs) File   
"/home/cds/cdsservices/services/retrieve.py", line 200, in execute \*\*retrieve\_kwargs File "/opt/cdstoolbox/cdscompute/cdscompute/context.py",   
line 301, in call return c.call(service, \*args, \*\*kwargs).value File "/opt/cdstoolbox/cdsworkflows/cdsworkflows/future.py", line 76, in value   
raise self.\_result cdsworkflows.error.ClientError: None
```

---

<div class="post-metadata">

**Author:** ![Kevin\_Marsh](https://forum.ecmwf.int/user_avatar/forum.ecmwf.int/kevin_marsh/32/368_2.png) [@Kevin\_Marsh](https://forum.ecmwf.int/u/Kevin_Marsh)\
**Post date:** [24 March 2023 12:25 UTC](https://forum.ecmwf.int/t/any-function-to-download-data-and-plot-time-series-on-graph/1768/4 "2023-03-24T12:25:11Z")

</div>

Hi, The workflow is working for me&nbsp;

 ![](https://forum.ecmwf.int/uploads/default/original/1X/89dceb471e990c3398bbb3e081c2755557497d2d.jpeg)

and to download the data you would add @ct.output.download() at the start and return 'data\_daily' along with the figure:

import cdstoolbox as ct

# Defining an application layout with 3 input collumns locate above the outputs.  
 layout = {  
 &nbsp; &nbsp; 'input\_ncols': 3,  
 &nbsp; &nbsp; 'output\_align': 'bottom'  
 }

# Dictionary of selectable variables  
 variables = {  
 &nbsp; &nbsp; 'Near-Surface Air Temperature': '2m\_temperature',  
 &nbsp; &nbsp; 'Eastward Near-Surface Wind': '10m\_u\_component\_of\_wind',  
 &nbsp; &nbsp; 'Northward Near-Surface Wind': '10m\_v\_component\_of\_wind',  
 &nbsp; &nbsp; 'Sea Level Pressure': 'mean\_sea\_level\_pressure',  
 &nbsp; &nbsp; 'Sea Surface Temperature': 'sea\_surface\_temperature',  
 }

# Define the main application function  
 @ct.application(  
 &nbsp; &nbsp; title='Extract a time series and plot graph',  
 &nbsp; &nbsp; layout=layout,  
 &nbsp; &nbsp; description='Plot the the time series of a user selected variable at a user selected location.'  
 )  
 @ct.input.dropdown('var', label='Variable', values=variables.keys(), description='Sample variables')  
 @ct.input.text('lon', label='Longitude', type=float, default=75., description='Decimal degrees')  
 @ct.input.text('lat', label='Latitude', type=float, default=43., description='Decimal degrees')  
 @ct.output.livefigure()

**@ct.output.download()**

def plot\_time\_series(var, lon, lat):  
 &nbsp; &nbsp; """  
 &nbsp; &nbsp; Application main steps:

&nbsp; &nbsp; - set the application layout with 3 columns for the input and output at the bottom  
 &nbsp; &nbsp; - retrieve a variable over a defined time range  
 &nbsp; &nbsp; - select a location, defined by longitude and latitude coordinates  
 &nbsp; &nbsp; - compute the daily average  
 &nbsp; &nbsp; - show the result as a timeseries on an interactive chart

&nbsp; &nbsp; """

&nbsp; &nbsp; # Retrieve the selected variable for a predefined time range at a spatial resolution of 3' x 3'.  
 &nbsp; &nbsp; data = ct.catalogue.retrieve(  
 &nbsp; &nbsp; &nbsp; &nbsp; 'reanalysis-era5-single-levels',  
 &nbsp; &nbsp; &nbsp; &nbsp; {  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'variable': variables[var],  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'grid': ['3', '3'],  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'product\_type': 'reanalysis',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'year': [  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2008', '2009', '2010',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2011', '2012', '2013',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2014', '2015', '2016',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2017'  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'month': [  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '01', '02', '03', '04', '05', '06',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '07', '08', '09', '10', '11', '12'  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'day': [  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '01', '02', '03', '04', '05', '06',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '07', '08', '09', '10', '11', '12',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '13', '14', '15', '16', '17', '18',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '19', '20', '21', '22', '23', '24',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '25', '26', '27', '28', '29', '30',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '31'  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'time': ['00:00', '06:00', '12:00', '18:00'],  
 &nbsp; &nbsp; &nbsp; &nbsp; }  
 &nbsp; &nbsp; )

&nbsp; &nbsp; # Location selection

&nbsp; &nbsp; # Extract the closest point to selected lon/lat (no interpolation).  
 &nbsp; &nbsp; # If wrong number is set for latitude, the closest available one is chosen:  
 &nbsp; &nbsp; # e.g. if lat = 4000 -\> lat = 90.  
 &nbsp; &nbsp; # If wrong number is set for longitude, first a wrap in [-180, 180] is made,  
 &nbsp; &nbsp; # then the closest one present is chosen:  
 &nbsp; &nbsp; # e.g. if lon = 200 -\> lat = -160.  
 &nbsp; &nbsp; data\_sel = ct.geo.extract\_point(data, lon=lon, lat=lat)

&nbsp; &nbsp; # Daily mean on selection  
 &nbsp; &nbsp; data\_daily = ct.climate.daily\_mean(data\_sel)

&nbsp; &nbsp; # Plot the daily data  
 &nbsp; &nbsp; fig = ct.chart.line(data\_daily)

&nbsp; &nbsp; return fig, **data\_daily**
