Below is an example script of retreiving the FWI dataset from 1940 to Present Day using Consolidated and Intermediate Datasets.


import cdsapi
import os
from datetime import datetime, timedelta

c = cdsapi.Client()
year_start=2023
year_end=2023
date_start=datetime(year_start,1,1)
date_end=datetime(year_end,12,31)
date=date_start

consolidated_end=False

DOWNLOAD_LOCATION=f'{os.getcwd()}/FWI/'
os.makedirs(DOWNLOAD_LOCATION,exist_ok=True)

def retrieve_CDS(year, month, day, dataset_type):
    c.retrieve(
    'cems-fire-historical-v1',
    {
        'product_type': 'reanalysis',
        'system_version': '4_1',
        'dataset_type': f'{dataset_type}_dataset',
        'variable': 'fire_weather_index',
        'year': year,
        'month': month,
        'day': day,
        'grid': '0.25/0.25',
        'format': 'netcdf',
    },
    f'{DOWNLOAD_LOCATION}/ECMWF_FWI_FWI_{year}{month}{day}_1200_hr_v4.1_{dataset_type[:3]}.nc')

while date <= date_end:
    year=date.strftime('%Y')
    month=date.strftime('%m')
    day=date.strftime('%d')
    print(f'Checking {year}-{month}-{day}')
    if not os.path.isfile(f'{DOWNLOAD_LOCATION}/ECMWF_FWI_FWI_{year}{month}{day}_1200_hr_v4.1_con.nc'):
        if consolidated_end==False:
            try:
                print(f'Attempting Retrievial of Consolidated {year}-{month}-{day}')
                retrieve_CDS(year,month,day,'consolidated')
                # Remove intermediate file for the same date to avoid duplication
                if os.path.exists(f'{DOWNLOAD_LOCATION}/ECMWF_FWI_FWI_{year}{month}{day}_1200_hr_v4.1_int.nc'):
                    os.remove(f'{DOWNLOAD_LOCATION}/ECMWF_FWI_FWI_{year}{month}{day}_1200_hr_v4.1_int.nc')
            except:
                print(f'Failed Retrievial of Consolidated {year}-{month}-{day}, Trying Intermediate')
                if not os.path.isfile(f'{DOWNLOAD_LOCATION}/ECMWF_FWI_FWI_{year}{month}{day}_1200_hr_v4.1_int.nc'):
                    retrieve_CDS(year,month,day,'intermediate')
                consolidated_end=True
        else:
            if not os.path.isfile(f'{DOWNLOAD_LOCATION}/ECMWF_FWI_FWI_{year}{month}{day}_1200_hr_v4.1_int.nc'):
                try:
                    print(f'Attempting Retrievial of Intermediate {year}-{month}-{day}')
                    retrieve_CDS(year,month,day,'intermediate')
                except:
                    print(f'Failed Retrievial of Intermediate {year}-{month}-{day}')
                    break
            else:
                print(f'Intermediate {year}-{month}-{day} exists')
                
    else:
        print(f'Consolidated {year}-{month}-{day} exists')
    date += timedelta(days=1)

print(f"Last Date retrieved {date.strftime('%Y-%m-%d')}")