how add extra parameters in a custom function in Python

148 Views Asked by At

I am fairly new to python. I am trying to use some codes that are on the copernicus website. The codes are already written and I will not use them as my own. I am only interested in understanding how they work so I can change the parameters and obtain results from different years, areas etc. So the function is the following

    data = ct.catalogue.retrieve(
        'reanalysis-era5-single-levels',
        {
            'variable': variables[var],
            'grid': ['3', '3'],
            'product_type': 'reanalysis',
            'year': [
                '2008', '2009', '2010',
                '2011', '2012', '2013',
                '2014', '2015', '2016',
                '2017'
            ],
            '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': ['00:00', '06:00', '12:00', '18:00'],
        }
    )

My question is, how can I add extra years without having to write them one by one? For example here we have from 2008-2017. How can I write if I want from 1900 to 2100?

1

There are 1 best solutions below

3
On

You can use python's range function inside a list comprehension to crate a list of all years from 1900 to 2100 like this:

data = ct.catalogue.retrieve(
    'reanalysis-era5-single-levels',
    {
        'variable': variables[var],
        'grid': ['3', '3'],
        'product_type': 'reanalysis',
        'year': [str(i) for i in range(1900, 2101)],
        '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': ['00:00', '06:00', '12:00', '18:00'],
    }
)