-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_usdm.py
115 lines (87 loc) · 3.53 KB
/
get_usdm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This is an example python file to automate the collection of shapefiles for 20
years from the USDM page using wget and a textfile.
Created on Thu Dec 15 14:59:16 2022
The link for GIS shapefile data:
https://droughtmonitor.unl.edu/DmData/GISData.aspx
@author: cwalker
"""
#to create the list of datetstamps for 20 years of shapefiles
# we need datestamps being incremented by a timedelta of 7 days
from datetime import datetime, timedelta
import os
import shutil
import sys
#datestamps and desired timedelta until the end date
starting_date = str(sys.argv[1]) #the first date in usdm database entered from command line
ending_date = str(sys.argv[2]) #the last date in usdm database for 2020 entered from command line
if len(starting_date) < 8:
print('Make sure starting date is yyyymmdd format!')
os.exit(1)
if len(ending_date) < 8:
print('Make sure ending date is yyyymmdd format!')
#delta we need to increment time by
day_delta = 7 #days
#convert the start and end date to timestamps
start_stamp = datetime.strptime(starting_date, '%Y%m%d').date()
end_stamp = datetime.strptime(ending_date, '%Y%m%d').date()
#list to collect the data
date_list = []
#while loop to create timestamps and append to list
while start_stamp != (end_stamp+timedelta(days=day_delta)):
date_list.append(start_stamp)
start_stamp = start_stamp+timedelta(days=day_delta)
#now we will create the link structures
#we will need a standard string and the append each of the
#correct dates to the end.
#standard string
standard_string = 'https://droughtmonitor.unl.edu/data/shapefiles_m/USDM_'
#list to collect strings
links = []
for i in range(len(date_list)):
#dateinformation for each string
dateyear = str(date_list[i].year)
datemonth = date_list[i].month
dateday = date_list[i].day
#if datemonth is less than 10, we will add a 0 to it so that its correct
#in the link
if datemonth < 10:
datemonth = "0" + str(datemonth)
else:
datemonth = str(datemonth)
if dateday < 10:
dateday = "0"+str(dateday)
else:
dateday = str(dateday)
#now make each string with correct information
link = standard_string + dateyear + datemonth + dateday + "_M.zip"
links.append(link)
#now we will add the strings to the textfile so that we
#can call wget on it and download all the files
#here we will delete the usdm_links.txt file for every iteration
#that this script is run so that you do not get
#errors where old lines are appended to the new lines
#we will also create a new shapefiles directory
#for each iteration so that you get the shapefiles for
#the selected dates only.
textfile_out = os.getcwd() + '/usdm_links.txt' #textfile for wget to operate on
shapefiles_dir = os.getcwd() + '/shapefiles' #directory for shapefiles
#delete the shapefiles dir if it exists
if os.path.exists(shapefiles_dir):
shutil.rmtree(shapefiles_dir)
#delete the textfile if it exists
if os.path.exists(textfile_out):
os.remove(textfile_out)
#create the usdm.txtfile and write to it
print('\n')
print('Appending HTTP links to download file: ')
with open(textfile_out, 'w') as f:
for link in links:
f.write("{}\n".format(link))
print(link + ' added to {}'.format(textfile_out[-14:]))
#create the data dir to dump USDM shapefiles into for selected dates
os.mkdir(shapefiles_dir)
print('\nDownload file is ready for wget!\n')
print('Run the following command:\n wget -i usdm_links.txt -P shapefiles/ --progress=bar:force:noscroll\n')