# API Request Failed with Assertion error

**URL:** <https://forum.ecmwf.int/t/api-request-failed-with-assertion-error/1492>\
**Category:** API\
**Tags:** api, c3s\
**Created:** [10 March 2023 07:20 UTC](https://forum.ecmwf.int/t/api-request-failed-with-assertion-error/1492 "2023-03-10T07:20:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Som](https://forum.ecmwf.int/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@Abhishek\_Som](https://forum.ecmwf.int/u/Abhishek_Som)\
**Post date:** [10 March 2023 07:20 UTC](https://forum.ecmwf.int/t/api-request-failed-with-assertion-error/1492/1 "2023-03-10T07:20:36Z")

</div>

**So my code is failing at certain geo locations with below error can anyone help me to resolve this issue at earliest**

**My error**

2023-03-09 11:39:52,619 INFO Welcome to the CDS  
 2023-03-09 11:39:52,619 INFO Sending request to [https://cds.climate.copernicus.eu/api/v2/resources/reanalysis-era5-single-levels](https://cds.climate.copernicus.eu/api/v2/resources/reanalysis-era5-single-levels)  
 2023-03-09 11:39:52,832 INFO Request is queued  
 2023-03-09 11:39:53,996 INFO Request is running  
 2023-03-09 11:39:55,665 INFO Request is failed  
 2023-03-09 11:39:55,666 ERROR Message: the request you have submitted is not valid  
**2023-03-09 11:39:55,666 ERROR Reason: &nbsp;Exception: Assertion failed: bbox.north() \<= Latitude::NORTH\_POLE in check, line 34 of /home/cds/git/mars-client/mir/src/mir/util/[BoundingBox.cc](http://BoundingBox.cc); MIR: Assertion failed: bbox.north() \<= Latitude::NORTH\_POLE in check, line 34 of /home/cds/git/mars-client/mir/src/mir/util/[BoundingBox.cc](http://BoundingBox.cc); Interpolation failed (-2); Mars server task finished in error; Double buffer error: Assertion failed: length == buffers\_[i].length\_ in run, line 282 of /home/cds/git/mars-server/eckit/src/eckit/io/[DblBuffer.cc](http://DblBuffer.cc)(RemoteException from Connector[mvr0010,marsmvr-0010:9701]) [mars]; Error code is -2; Request failed; Some errors reported (last error -2)**  
 2023-03-09 11:39:55,666 ERROR &nbsp; Traceback (most recent call last):  
 2023-03-09 11:39:55,666 ERROR &nbsp; &nbsp; File "/opt/cdstoolbox/cdscompute/cdscompute/cdshandlers/services/handler.py", line 59, in handle\_request  
 2023-03-09 11:39:55,667 ERROR &nbsp; &nbsp; &nbsp; result = cached(context.method, proc, context, context.args, context.kwargs)  
 2023-03-09 11:39:55,667 ERROR &nbsp; &nbsp; File "/opt/cdstoolbox/cdscompute/cdscompute/caching.py", line 108, in cached  
 2023-03-09 11:39:55,667 ERROR &nbsp; &nbsp; &nbsp; result = proc(context, \*context.args, \*\*context.kwargs)  
 2023-03-09 11:39:55,667 ERROR &nbsp; &nbsp; File "/opt/cdstoolbox/cdscompute/cdscompute/services.py", line 124, in \_\_call\_\_  
 2023-03-09 11:39:55,667 ERROR &nbsp; &nbsp; &nbsp; return p(\*args, \*\*kwargs)  
 2023-03-09 11:39:55,668 ERROR &nbsp; &nbsp; File "/opt/cdstoolbox/cdscompute/cdscompute/services.py", line 60, in \_\_call\_\_  
 2023-03-09 11:39:55,668 ERROR &nbsp; &nbsp; &nbsp; return self.proc(context, \*args, \*\*kwargs)  
 2023-03-09 11:39:55,668 ERROR &nbsp; &nbsp; File "/home/cds/cdsservices/services/mars/mars.py", line 48, in internal  
 2023-03-09 11:39:55,668 ERROR &nbsp; &nbsp; &nbsp; return mars(context, request, \*\*kwargs)  
 2023-03-09 11:39:55,668 ERROR &nbsp; &nbsp; File "/home/cds/cdsservices/services/mars/mars.py", line 20, in mars  
 2023-03-09 11:39:55,668 ERROR &nbsp; &nbsp; &nbsp; execute\_mars(context, requests, info)  
 2023-03-09 11:39:55,669 ERROR &nbsp; &nbsp; File "/home/cds/cdsservices/services/mars/execute\_mars.py", line 20, in execute\_mars  
 2023-03-09 11:39:55,669 ERROR &nbsp; &nbsp; &nbsp; exception=MarsException)  
 2023-03-09 11:39:55,669 ERROR &nbsp; &nbsp; File "/opt/cdstoolbox/cdscompute/cdscompute/context.py", line 209, in run\_command  
 2023-03-09 11:39:55,669 ERROR &nbsp; &nbsp; &nbsp; raise exception(call, proc.returncode, output)  
 2023-03-09 11:39:55,669 ERROR &nbsp; home.cds.cdsservices.services.mars.\_\_init\_\_.py.exceptions.MarsException: Exception: Assertion failed: bbox.north() \<= Latitude::NORTH\_POLE in check, line 34 of /home/cds/git/mars-client/mir/src/mir/util/[BoundingBox.cc](http://BoundingBox.cc); MIR: Assertion failed: bbox.north() \<= Latitude::NORTH\_POLE in check, line 34 of /home/cds/git/mars-client/mir/src/mir/util/[BoundingBox.cc](http://BoundingBox.cc); Interpolation failed (-2); Mars server task finished in error; Double buffer error: Assertion failed: length == buffers\_[i].length\_ in run, line 282 of /home/cds/git/mars-server/eckit/src/eckit/io/[DblBuffer.cc](http://DblBuffer.cc)(RemoteException from Connector[mvr0010,marsmvr-0010:9701]) [mars]; Error code is -2; Request failed; Some errors reported (last error -2)  
**Error fetching data for area['35.14439832499524/33.90585241491622/35.14462956749524/33.90608365741622']: the request you have submitted is not valid. Exception: Assertion failed: bbox.north() \<= Latitude::NORTH\_POLE in check, line 34 of /home/cds/git/mars-client/mir/src/mir/util/[BoundingBox.cc](http://BoundingBox.cc); MIR: Assertion failed: bbox.north() \<= Latitude::NORTH\_POLE in check, line 34 of /home/cds/git/mars-client/mir/src/mir/util/[BoundingBox.cc](http://BoundingBox.cc); Interpolation failed (-2); Mars server task finished in error; Double buffer error: Assertion failed: length == buffers\_[i].length\_ in run, line 282 of /home/cds/git/mars-server/eckit/src/eckit/io/[DblBuffer.cc](http://DblBuffer.cc)(RemoteException from Connector[mvr0010,marsmvr-0010:9701]) [mars]; Error code is -2; Request failed; Some errors reported (last error -2).**

**My Code:**

#working for 92 logs

import cdsapi  
 import pandas as pd

df1 = pd.read\_csv('PX4\_logData\_skip\_na\_fromtimestamp\_v3.csv', usecols=['logNum', 'Time', 'Date'])  
 df3 = pd.read\_csv('timeconvdatav4.csv', usecols=['logNum'])  
 df1 = df1[:100]  
 path1 = 'D:/Abhishek/UBC/MASc/Thesis/Energy\_Modeling/Data/'  
 path2 = 'D:/Abhishek/UBC/MASc/Thesis/Energy\_Modeling/WeathERA5Single/'  
 logIds = df3['logNum'].tolist()  
 #batfile = '/log\_battery\_status\_0.csv'  
 notFoundList = []  
 #date = df1['Date'].tolist()  
 #time = df1['Time'].tolist()  
 month = int()  
 day = int()  
 year = int()  
 hr = int()  
 min = int()  
 i = 0  
 common\_df = pd.DataFrame()  
 &nbsp; &nbsp; #ht = df2['terrain\_alt']  
 &nbsp; &nbsp; #time = df1['Time']  
 #working till mean() function

for logId in logIds:  
 &nbsp; &nbsp; #picks the specific time and date from PX4\_logData\_skip\_na\_fromtimestamp\_v3.csv by comparing it with timeconvdatav4.csv log id  
 &nbsp; &nbsp; time1 = df1[df1['logNum'] == logId]['Time']  
 &nbsp; &nbsp; date1 = df1[df1['logNum'] == logId]['Date']  
 &nbsp; &nbsp; date0 = date1[i]  
 &nbsp; &nbsp; month, day, year = map(int,date0.split('/'))  
 &nbsp; &nbsp; print(month, day, year)  
 &nbsp; &nbsp; time0 = time1[i]  
 &nbsp; &nbsp; hr, min, sec = map(int,time0.split(':'))  
 &nbsp; &nbsp; i += 1  
 &nbsp; &nbsp; #for logId in logIds:  
 &nbsp; &nbsp; path = path1 + logId  
 &nbsp; &nbsp; gpsfile = '/log\_vehicle\_global\_position\_0.csv'  
 &nbsp; &nbsp; from os.path import exists  
 &nbsp; &nbsp; if not exists(path + gpsfile):  
 &nbsp; &nbsp; &nbsp; &nbsp; notFoundList.append(logId)  
 &nbsp; &nbsp; &nbsp; &nbsp; continue  
 &nbsp; &nbsp; df2 = pd.read\_csv(path + gpsfile)  
 &nbsp; &nbsp; lat = df2['lat']  
 &nbsp; &nbsp; long = df2['lon']  
 &nbsp; &nbsp; alt = df2["terrain\_alt"].mean()  
 &nbsp; &nbsp; #print (hr, min, sec)  
 &nbsp; &nbsp; &nbsp; &nbsp; # Create an empty list to store the area grid strings  
 &nbsp; &nbsp; #area\_grid = []  
 &nbsp; &nbsp; import math  
 &nbsp; &nbsp; #import matplotlib.pyplot as plt  
 &nbsp; &nbsp; import builtins

&nbsp; &nbsp; # Example usage:  
 &nbsp; &nbsp; latitudes = df2['lat'].tolist()  
 &nbsp; &nbsp; longitudes = df2['lon'].tolist()

&nbsp; &nbsp; def distance(lat1, lon1, lat2, lon2):  
 &nbsp; &nbsp; &nbsp; &nbsp; """  
 &nbsp; &nbsp; &nbsp; &nbsp; Calculates the great-circle distance between two points on the earth's surface  
 &nbsp; &nbsp; &nbsp; &nbsp; using the Haversine formula.  
 &nbsp; &nbsp; &nbsp; &nbsp; """  
 &nbsp; &nbsp; &nbsp; &nbsp; R = 6371.0 &nbsp;# radius of the earth in kilometers  
 &nbsp; &nbsp; &nbsp; &nbsp; dlat = math.radians(lat2 - lat1)  
 &nbsp; &nbsp; &nbsp; &nbsp; dlon = math.radians(lon2 - lon1)  
 &nbsp; &nbsp; &nbsp; &nbsp; a = math.sin(dlat / 2) \*\* 2 + math.cos(math.radians(lat1)) \* math.cos(math.radians(lat2)) \* math.sin(  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dlon / 2) \*\* 2  
 &nbsp; &nbsp; &nbsp; &nbsp; c = 2 \* math.atan2(math.sqrt(a), math.sqrt(1 - a))  
 &nbsp; &nbsp; &nbsp; &nbsp; dist = R \* c  
 &nbsp; &nbsp; &nbsp; &nbsp; return dist  
 &nbsp; &nbsp; def create\_grid(latitudes, longitudes, min\_lat=-90, max\_lat=90, min\_lon=-180, max\_lon=180):  
 &nbsp; &nbsp; &nbsp; &nbsp; """  
 &nbsp; &nbsp; &nbsp; &nbsp; Creates a grid of cells covering a larger area specified by the input latitudes and longitudes,  
 &nbsp; &nbsp; &nbsp; &nbsp; with each cell having a size determined by the distance between the minimum and maximum latitudes  
 &nbsp; &nbsp; &nbsp; &nbsp; and longitudes in the input data.

&nbsp; &nbsp; &nbsp; &nbsp; Args:  
 &nbsp; &nbsp; &nbsp; &nbsp; latitudes: list of latitude values  
 &nbsp; &nbsp; &nbsp; &nbsp; longitudes: list of longitude values  
 &nbsp; &nbsp; &nbsp; &nbsp; min\_lat: minimum latitude value for the grid, default -90  
 &nbsp; &nbsp; &nbsp; &nbsp; max\_lat: maximum latitude value for the grid, default 90  
 &nbsp; &nbsp; &nbsp; &nbsp; min\_lon: minimum longitude value for the grid, default -180  
 &nbsp; &nbsp; &nbsp; &nbsp; max\_lon: maximum longitude value for the grid, default 180

&nbsp; &nbsp; &nbsp; &nbsp; Returns:  
 &nbsp; &nbsp; &nbsp; &nbsp; grid\_cells: list of tuples representing grid cells, where each tuple contains (min\_lat, min\_lon, max\_lat, max\_lon)  
 &nbsp; &nbsp; &nbsp; &nbsp; grid\_size: size of each grid cell in kilometers  
 &nbsp; &nbsp; &nbsp; &nbsp; """  
 &nbsp; &nbsp; &nbsp; &nbsp; # Limit latitudes and longitudes to fall within the specified range  
 &nbsp; &nbsp; &nbsp; &nbsp; latitudes = [builtins.min(builtins.max(min\_lat, lat), max\_lat) for lat in latitudes]  
 &nbsp; &nbsp; &nbsp; &nbsp; longitudes = [builtins.min(builtins.max(min\_lon, lon), max\_lon) for lon in longitudes]

&nbsp; &nbsp; &nbsp; &nbsp; min\_latitude, max\_latitude = builtins.min(latitudes), builtins.max(latitudes)  
 &nbsp; &nbsp; &nbsp; &nbsp; min\_longitude, max\_longitude = builtins.min(longitudes), builtins.max(longitudes)

&nbsp; &nbsp; &nbsp; &nbsp; # Limit max\_latitude and max\_longitude to their respective maximum values  
 &nbsp; &nbsp; &nbsp; &nbsp; max\_latitude = builtins.min(max\_latitude, 90)  
 &nbsp; &nbsp; &nbsp; &nbsp; max\_longitude = builtins.min(max\_longitude, 180)  
 &nbsp; &nbsp; &nbsp; &nbsp; #max\_latitude = min(max\_latitude, 90)  
 &nbsp; &nbsp; &nbsp; &nbsp; #max\_longitude = min(max\_longitude, 180)

&nbsp; &nbsp; &nbsp; &nbsp; # Limit min\_longitude to its minimum value of -180  
 &nbsp; &nbsp; &nbsp; &nbsp; if min\_longitude \< -180:  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min\_longitude = -180

&nbsp; &nbsp; &nbsp; &nbsp; # Limit max\_longitude to its maximum value of 180  
 &nbsp; &nbsp; &nbsp; &nbsp; if max\_longitude \> 180:  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max\_longitude = 180

&nbsp; &nbsp; &nbsp; &nbsp; lat\_dist = distance(min\_latitude, min\_longitude, max\_latitude, min\_longitude)  
 &nbsp; &nbsp; &nbsp; &nbsp; lon\_dist = distance(min\_latitude, min\_longitude, min\_latitude, max\_longitude)  
 &nbsp; &nbsp; &nbsp; &nbsp; #changed the roundup issue to 10 it was failing due to grid size endingup as zero  
 &nbsp; &nbsp; &nbsp; &nbsp; grid\_size = round(builtins.min(lat\_dist, lon\_dist) / 5.0,10)  
 &nbsp; &nbsp; &nbsp; &nbsp; test = builtins.min(lat\_dist,lon\_dist)  
 &nbsp; &nbsp; &nbsp; &nbsp; grid\_cells = []  
 &nbsp; &nbsp; &nbsp; &nbsp; lat = min\_latitude  
 &nbsp; &nbsp; &nbsp; #debuging individual iteration use below format  
 &nbsp; &nbsp; &nbsp; &nbsp;# if i == 94:  
 &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp;print()

&nbsp; &nbsp; &nbsp; &nbsp; while lat \< max\_latitude:  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lon = min\_longitude  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while lon \< max\_longitude:  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell\_lat1, cell\_lon1 = lat, lon  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell\_lat2, cell\_lon2 = lat + grid\_size, lon + grid\_size  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid\_cells.append((cell\_lat1, cell\_lon1, cell\_lat2, cell\_lon2))  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lon += grid\_size  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lat += grid\_size  
 &nbsp; &nbsp; &nbsp; &nbsp; return grid\_cells, grid\_size  
 &nbsp; &nbsp; &nbsp; &nbsp; # Create a grid of cells covering the input data with a size based on the distance between the minimum and maximum latitudes and longitudes in the input data

&nbsp; &nbsp; grid\_cells, grid\_size = create\_grid(latitudes, longitudes)  
 &nbsp; &nbsp; print(f"Created {len(grid\_cells)} grid cells with a size of {grid\_size} km")

&nbsp; &nbsp; import json  
 &nbsp; &nbsp; # Define the request variable  
 &nbsp; &nbsp; request = {'area': []}  
 &nbsp; &nbsp; # Create an area grid string for the current latitude and longitude  
 &nbsp; &nbsp; area\_grid = []  
 &nbsp; &nbsp; #if len(grid\_cells) \> 0:  
 &nbsp; &nbsp; &nbsp; &nbsp; #print(f"Created {len(grid\_cells)} grid cells with sizes of {grid\_size} km each")  
 &nbsp; &nbsp; &nbsp; &nbsp; #rint(i)  
 &nbsp; &nbsp; for cell in grid\_cells:  
 &nbsp; &nbsp; &nbsp; &nbsp; lat, lon, \_, \_ = cell  
 &nbsp; &nbsp; &nbsp; &nbsp; area\_grid.append(f'{lat}/{lon}/{lat + grid\_size}/{lon + grid\_size}')  
 &nbsp; &nbsp; &nbsp; &nbsp; #break &nbsp;# add this line to restrict to the first element  
 &nbsp; &nbsp; &nbsp; &nbsp; # Add the area grid string to the request variable  
 &nbsp; &nbsp; &nbsp; &nbsp; #request['area'].append(area\_grid)  
 &nbsp; &nbsp; &nbsp; &nbsp; # Convert the request variable to a JSON-formatted string

&nbsp; &nbsp; &nbsp; &nbsp; #request\_string = json.dumps(request)  
 &nbsp; &nbsp; &nbsp; &nbsp; #print(request\_string)  
 &nbsp; &nbsp; &nbsp; &nbsp; # Print the resulting area grid list  
 &nbsp; &nbsp; &nbsp; &nbsp; print(area\_grid)  
 &nbsp; &nbsp; &nbsp; &nbsp; import psutil  
 &nbsp; &nbsp; &nbsp; &nbsp; #print(time)  
 &nbsp; &nbsp; &nbsp; &nbsp; # if grid\_size \>\< 4

&nbsp; &nbsp; &nbsp; &nbsp; try:  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = cdsapi.Client()

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # the below line fetches data T-1hr, T hr and T+1hr  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time = [str((hr - 1) % 24).zfill(2) + ":00", str(hr).zfill(2) + ":00",  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str((hr + 1) % 24).zfill(2) + ":00"]

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(time)  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # if grid\_size \>\< 4  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(grid\_size)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = c.retrieve(  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'reanalysis-era5-single-levels',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'product\_type': 'reanalysis',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ERA5 - single variables  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'variable': ['100m\_u\_component\_of\_wind', '10m\_u\_component\_of\_wind',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'10m\_v\_component\_of\_neutral\_wind',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'10m\_v\_component\_of\_wind', '10m\_wind\_gust\_since\_previous\_post\_processing',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'2m\_dewpoint\_temperature', '2m\_temperature', 'evaporation', 'friction\_velocity',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'instantaneous\_10m\_wind\_gust', 'k\_index',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'maximum\_2m\_temperature\_since\_previous\_post\_processing',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'minimum\_2m\_temperature\_since\_previous\_post\_processing', 'skin\_temperature',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'surface\_net\_thermal\_radiation', 'surface\_pressure', 'surface\_sensible\_heat\_flux',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'surface\_solar\_radiation\_downward\_clear\_sky', 'total\_cloud\_cover',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'total\_column\_water',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'total\_column\_water\_vapour', 'total\_precipitation', 'type\_of\_high\_vegetation',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'type\_of\_low\_vegetation'],  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 'temperature\_of\_snow\_layer', 'snowfall','snow\_density', 'snow\_evaporation',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'year': year,  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'month': month,  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'day': day,  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'time': time,  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'format': 'netcdf',  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'area': area\_grid,  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'grid': '{}/{}'.format(grid\_size, grid\_size)  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 'grid': '{}/{}'.format(grid\_size\_values, grid\_size\_values)  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 'grid': '0.05/0.05', #changing this will change the number of samples downloaded  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # step is not working  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 'step': '1/1440'

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '[download.nc](http://download.nc)')

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #print(data)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # process the downloaded data here

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; import xarray as xr

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ds = xr.open\_dataset('[download.nc](http://download.nc)')  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Check RAM usage  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mem = psutil.virtual\_memory()  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #if mem.used \> 14 \* 1024 \* 1024 \* 1024: &nbsp;# 14GB in bytes  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #print(mem.used)  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #print("Skipping iteration due to high RAM usage.")  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #continue  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ds.sel(time=ds.time[0], latitude=ds.latitude[0], longitude=ds.longitude[0])  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(ds.time)  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df = ds.to\_dataframe()  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df["logid"] = logId  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # set the time column  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df['time'] = f"{hr:02d}:{min:02d}:{sec:02d}"  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df["date"] = date0  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df["lat"] = lat  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df["lon"] = lon  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # below line not working  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # df["area\_grid"] = area\_grid

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; common\_df = pd.concat([common\_df, df], ignore\_index=True)  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newdf2 = path2 + "GridAct\_weatherdataTpnm1\_era5Mar08\_v24.csv"  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; common\_df.to\_csv(newdf2, index=False)  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # this works roll back if above concat doesn't work  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # newdf = path2 + logId + "\_weatherdata\_era5feb07.csv"  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Save the merged DataFrame to a new CSV file  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # df.to\_csv(newdf, index=False)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print(df)  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # df.to\_csv('weatherdata\_era5land\_feb06.csv')  
 &nbsp; &nbsp; &nbsp; &nbsp; except KeyError:  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Error fetching data from CDS API. Skipping to the next iteration.")  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue  
 &nbsp; &nbsp; &nbsp; &nbsp; except Exception as e:  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"Error fetching data for area{area\_grid}: {e}")  
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue

&nbsp; &nbsp; &nbsp; &nbsp; # use grid\_size values in API request  
 &nbsp; &nbsp; &nbsp; &nbsp; # ...  
 &nbsp; &nbsp; #else:  
 &nbsp; &nbsp; &nbsp; &nbsp; #print(f"Created {len(grid\_cells)} grid cell with a size of {grid\_size} km")
