-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasetsOfWatersheds.py
162 lines (121 loc) · 4.71 KB
/
datasetsOfWatersheds.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import requests
import json
import zipfile
import os
import progressbar
import time
import sys
def listWatersheds():
'''
listWatersheds function contacts GSTORE and returns a list of watersheds.
'''
wIndex = 1
collectionNames = []
collectionIds = []
rWatersheds = requests.get('http://gstore.unm.edu/apps/epscor/search/collections.json?version=3')
rData = rWatersheds.json()
watershedResults = rData['results']
#Get the length of Watershed list
countOfWatersheds = rData['subtotal']
#displays the name of all watersheds
print "\n\nName of watersheds available:\n\n"
for wResult in watershedResults:
print wIndex, ":", wResult['name']
wIndex += 1
collectionNames.append(wResult['name'])
collectionIds.append(wResult['uuid'])
return countOfWatersheds, collectionNames, collectionIds
def getWatershedDetails(userWatershedChoice, wDetails):
'''
getWatershedDetails function finds out the uid of the selected watershed
'''
nameOfWatershed = wDetails[1][userWatershedChoice - 1]
uidOfWatershed = wDetails[2][userWatershedChoice - 1]
return nameOfWatershed, uidOfWatershed
def listDatasets(nameOfWatershed, uidOfWatershed):
'''
listDatasets function contacts GSTORE and returns a list of datasets.
'''
rIndex = 1
datasetNames = []
datasetIds = []
rDatasets = requests.get('http://gstore.unm.edu/apps/epscor/search/collection/%s/datasets.json?version=3' %uidOfWatershed)
rrData = rDatasets.json()
dataResults = rrData['results']
#Get the length of Dataset list
countOfDatasets = rrData['subtotal']
#displays the name of all datasets
print "\n\nDatasets available for %s: " %nameOfWatershed, "\n\n"
for dResult in dataResults:
print rIndex, ":", dResult['name']
rIndex += 1
datasetNames.append(dResult['name'])
datasetIds.append(dResult['uuid'])
return countOfDatasets, datasetNames, datasetIds
def getDatasetDetails(userDatasetChoice, dDetails):
'''
getWatershedDetails function finds out the uid of the selected watershed
'''
nameOfDataset = dDetails[1][userDatasetChoice - 1]
uidOfDataset = dDetails[2][userDatasetChoice - 1]
return nameOfDataset, uidOfDataset
def downloadDataset(nameOfDataset, uidOfDataset):
'''
downloadDataset function contacts GSTORE and downloads dataset selected by the user
'''
progress = progressbar.ProgressBar()
url = "http://gstore.unm.edu/apps/epscor/datasets/%s/%s.original.tif" %(uidOfDataset, nameOfDataset)
response = requests.head(url)
print "Content length of "+nameOfDataset+ ": {:,}".format(int(response.headers['content-length']))
print "\nDownloading ..."
rDownload = requests.get(url)
for i in progress(range(100)):
with open("%s.zip" %nameOfDataset, "wb") as code:
code.write(rDownload.content)
time.sleep(0.01)
print "\nDownloading completed\n"
zip = zipfile.ZipFile(r'%s.zip' %nameOfDataset)
zip.extractall(r'%s' %nameOfDataset)
#List the name of files available from the downloaded file
print "Files available are:\n"
dirList = os.listdir(nameOfDataset)
for fname in dirList:
print fname
return
def multipleSelect(watershedDetails):
'''
multipleSelect function asks user for more selects
'''
while True:
userMDChoice = raw_input("\nDo you want to download more datasets? (Yes/No):\t")
if userMDChoice == "Yes":
dsteps(watershedDetails)
else:
userMWChoice = raw_input("\nDo you want to select another watershed? (Yes/No):\t")
if userMWChoice == "Yes":
steps()
else:
sys.exit()
def steps():
wDetails = listWatersheds()
while True:
userWatershedChoice = input("\n\nSelect an option: ")
if 1 <= userWatershedChoice <= wDetails[0]:
break
else:
print "Invalid option. Please try again."
watershedDetails = getWatershedDetails(userWatershedChoice, wDetails)
dsteps(watershedDetails)
def dsteps(watershedDetails):
dDetails = listDatasets(watershedDetails[0], watershedDetails[1])
while True:
userDatasetChoice = input("\n\nSelect an option: ")
if 1 <= userDatasetChoice <= dDetails[0]:
break
else:
print "Invalid option. Please try again."
datasetDetails = getDatasetDetails(userDatasetChoice, dDetails)
downloadDataset(datasetDetails[0], datasetDetails[1])
multipleSelect(watershedDetails)
if __name__ == "__main__":
steps()