How to download a VIIRS L2 CLDPROP netcdf file from ladsweb using python ?

Published: May 09, 2019

DMCA.com Protection Status

Example of python script to download on your local machine a VIIRS L2 CLDPROP netcdf granule file from ladsweb using python.

Lets consider the following data for example:

year = 2018
month = 2
day = 15
hour = 14 
minute = 36

Note: VIIRS granules are every 6 minutes

Convert day to count of days

First step we need to get the count of days:

from datetime import date

d0 = date(year, 1, 1)
d1 = date(year, month, day)
delta = d1 - d0

count_of_days = delta.days + 1

Check existing files for the given date

To download a VIIRS granule file, it is necessary to get its full name. However the name of a VIIRS granule contains the date where the file has been created, which is impossible to know in advance. So, to get the names of VIIRS granule for a given day, LAADS DAAC provide a json file that can be accessed using urlopen and read using json.loads:

ladsweb_url = 'https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/5110/CLDPROP_L2_VIIRS_SNPP/{:04d}/{:03d}.json'.format(year,count_of_days)

with urllib.request.urlopen(ladsweb_url) as url:
    data = json.loads(url.read().decode())

pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)

returns:

[   {   'last-modified': '2019-03-10 19:33',
        'name': 'CLDPROP_L2_VIIRS_SNPP.A2018046.0000.001.2019068073503.nc',
        'size': 113431810},
    {   'last-modified': '2019-03-10 19:33',
        'name': 'CLDPROP_L2_VIIRS_SNPP.A2018046.0006.001.2019068073504.nc',
        'size': 112769587},
    {   'last-modified': '2019-03-10 19:33',
        'name': 'CLDPROP_L2_VIIRS_SNPP.A2018046.0012.001.2019068073506.nc',
        'size': 120314867},
    {   'last-modified': '2019-03-10 19:59',
        'name': 'CLDPROP_L2_VIIRS_SNPP.A2018046.0018.001.2019068073539.nc',
        'size': 155122070},

.
.
.

Now we can check if our file exists:

name_list = [data[i]['name'] for i in range(len(data))]

valid_name_list = [name for name in name_list if '{:04d}{:03d}.{:02d}{:02d}'.format(year,count_of_days,hour,minute) in name]

Download the file

Finally, the file can be downloaded using the python function urlretrieve:

if len(valid_name_list) == 1:

    ladsweb_url = 'https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/5110/CLDPROP_L2_VIIRS_SNPP/{:04d}/{:03d}/'.format(year,count_of_days)

    urllib.request.urlretrieve(ladsweb_url+valid_name_list[0], path_to_files + valid_name_list[0])

Python script

from datetime import date

import urllib.request
import urllib.request, json 
import pprint

year = 2018
month = 2
day = 15
hour = 14 
minute = 36

path_to_files = ''

#----------------------------------------------------------------------------------------#
# Convert day to count of days:

d0 = date(year, 1, 1)
d1 = date(year, month, day)
delta = d1 - d0

count_of_days = delta.days + 1

#----------------------------------------------------------------------------------------#
# Check existing files for the given date:

ladsweb_url = 'https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/5110/CLDPROP_L2_VIIRS_SNPP/{:04d}/{:03d}.json'.format(year,count_of_days)

with urllib.request.urlopen(ladsweb_url) as url:
    data = json.loads(url.read().decode())

pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)

#----------------------------------------------------------------------------------------#
# Download the file:

name_list = [data[i]['name'] for i in range(len(data))]

valid_name_list = [name for name in name_list if '{:04d}{:03d}.{:02d}{:02d}'.format(year,count_of_days,hour,minute) in name]

if len(valid_name_list) == 1:

    ladsweb_url = 'https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/5110/CLDPROP_L2_VIIRS_SNPP/{:04d}/{:03d}/'.format(year,count_of_days)

    urllib.request.urlretrieve(ladsweb_url+valid_name_list[0], path_to_files + valid_name_list[0])

References

Link WebSite
NASA LAADS DAAC ladsweb.modaps.eosdis.nasa.gov
datetime — Basic date and time types docs.python.org
urlopen docs.python.org
Reading and Writing JSON to a File in Python docs.python.org
urllib.request — Extensible library for opening URLs docs.python.org