Skip to content

Instantly share code, notes, and snippets.

@mbjoseph
Created June 13, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbjoseph/9c5b1a4f40275b1aa02292e67a2d9e4a to your computer and use it in GitHub Desktop.
Save mbjoseph/9c5b1a4f40275b1aa02292e67a2d9e4a to your computer and use it in GitHub Desktop.
Python script to grab modus data
#===============================================================================
# Date: May 22, 2016
# Author: Lindy Nelson
# Purpose: This script downloads monthly MODIS burn data from
#
#
# FTP Server: fuoco.geog.umd.edu
#
#
# Python FTP User Guide:
#
# https://docs.python.org/2.7/library/ftplib.html
#
# Modus User Guide:
#
# http://modis-fire.umd.edu/files/MODIS_Burned_Area_Collection51_User_Guide_3.1.0.pdf
#
# Example Run:
#
# python modis_download.py h01v10 /Users/lindynelson/Desktop 201304 201306
#
#
# Arguments
#
# Arg 1: h01v10 - tile to download
# Arg 2: download location
# Arg 3: begin month and year (inclusive) YYYYMM
# Arg 4: end month and year (inclusive) YYYYMM
#
#
#===============================================================================
import ftplib
import datetime
import sys
import os
#===============================================================================
#function get_all_files_for_tile
#
#input: the tile that was entered as arg 1
#output: the list of files that are in the data directory
#===============================================================================
def get_all_files_for_tile(tile):
username = 'fire'
password = 'burnt'
modis_ftp_url = 'fuoco.geog.umd.edu'
ftp_data_directory = '/db/MCD64A1/%s/' %tile
ftp = ftplib.FTP(modis_ftp_url, username, password)
global ftp
ftp.cwd(ftp_data_directory)
file_list = ftp.nlst(ftp_data_directory)
return file_list
#===============================================================================
#function download
#
#input: filename to download
# directory to download
#desc: download given filename to given local data_dir
#===============================================================================
def download(filename, data_dir):
'''
params: filename to download
directory for download
desc: download given filename to given local data_dir
'''
assert os.path.isdir(data_dir), 'FTP download failed. Not a directory: %s' %data_dir
retr_cmd = 'RETR %s' %filename
local_file = os.path.join(data_dir, filename)
fp = open(local_file, 'wb')
ftp.retrbinary(retr_cmd, fp.write)
fp.close()
#===============================================================================
#function download_files
#
#input: filename to download
# directory to download
#desc: downloads all files from start_month to end_month for given tile
#===============================================================================
def download_files(tile, data_dir, start_month, end_month):
"""
download all files from start_month to end_month(inclusive) for given tile
"""
def filename_is_between_months(filename, start_month_arg, end_month_arg):
filename_parts = filename.split('.')
tile_id = filename_parts[2]
julian_day = filename_parts[1][1:]
year = int(julian_day[:4])
day = int(julian_day[4:])
date = datetime.datetime(year, 1, 1) + datetime.timedelta(day -1)
start_dt_obj = datetime.datetime.strptime(start_month, '%Y%m')
end_dt_obj = datetime.datetime.strptime(end_month, '%Y%m')
bool_ret_val = (date >= start_dt_obj and date <= end_dt_obj)
return bool_ret_val
file_list = get_all_files_for_tile(tile)
files_to_download = [filename for filename in file_list if filename_is_between_months(filename, start_month, end_month)]
for filename in files_to_download:
print('downloading file: %s' %filename )
download(filename, data_dir)
#===============================================================================
#function main
#===============================================================================
def main():
assert len(sys.argv) is 5, 'Usage: download_modis.py tile data_dir start_month end_month'
tile, data_dir, start_month, end_month = sys.argv[1:]
assert len(start_month) is 6, 'Invalid month format %s: start_month must be in format of YYYYMM, e.g. 201308' %start_month
assert os.path.isdir(data_dir), 'Please specify an existing directory. Not a directory: %s' %data_dir
assert len(end_month) is 6, 'Invalid month format %s: end_month must be in format of YYYYMM, e.g. 201308' %end_month
assert int(start_month), 'Invalid month format %s: start_month must be in format of YYYYMM, e.g. 201308' %start_month
assert int(end_month), 'Invalid month format %s: end_month must be in format of YYYYMM, e.g. 201308' %end_month
download_files(tile, data_dir, start_month, end_month)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment