-
Notifications
You must be signed in to change notification settings - Fork 0
/
listOfWatersheds.py
220 lines (160 loc) · 6.67 KB
/
listOfWatersheds.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from lxml import etree
import xml.etree.ElementTree as ET
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 getCapabilities(uidOfDataset):
'''
getCapabilities function finds out the coverage name from the GetCapabilities response
'''
#GetCapabilities request
cap_url = 'http://gstore.unm.edu/apps/epscor/datasets/%s/services/ogc/wcs?VERSION=1.1.2&SERVICE=WCS&REQUEST=GetCapabilities&VERSION=1.0.0' %uidOfDataset
r_cap = requests.get(cap_url)
with open("capabilities.xml", "wb") as code:
code.write(r_cap.content)
tree = ET.parse('capabilities.xml')
root = tree.getroot()
#Coverage Name
coverageName = root[2][0][1].text
return coverageName
def describeCoverage(uidOfDataset, coverageName):
'''
describeCoverage function finds out the Supported format, CRS, BoundingBox coordinates from the DescribeCoverage response
'''
bboxValues = []
#DescribeCoverage request
desCoverage_url = 'http://gstore.unm.edu/apps/epscor/datasets/%s/services/ogc/wcs?VERSION=1.1.2&SERVICE=WCS&REQUEST=DescribeCoverage&VERSION=1.0.0&COVERAGE=%s' %(uidOfDataset, coverageName)
r_desCoverage = requests.get(desCoverage_url)
with open("coverage.xml", "wb") as code:
code.write(r_desCoverage.content)
tree = ET.parse('coverage.xml')
root = tree.getroot()
#Supported Format
supportedFormat = root[0][8][0].text
#CRS
CRS = root[0][7][0].text
#Bounding Box coordinates
boundingBox1 = root[0][5][0][1][0].text
fValues = boundingBox1.split()
for fvalue in fValues:
bboxValues.append(fvalue)
boundingBox2 = root[0][5][0][1][1].text
lValues = boundingBox2.split()
for lvalue in lValues:
bboxValues.append(lvalue)
coordinates = bboxValues[0]+","+ bboxValues[1]+","+ bboxValues[2]+","+ bboxValues[3]
return supportedFormat, coordinates, CRS
def downloadDataset(nameOfDataset, uidOfDataset, supportedFormat, coverageName, coordinates, CRS):
'''
downloadDataset function downloads the datset using GetCoverage request
'''
progress = progressbar.ProgressBar()
#GetCoverage request
getCoverage_url = 'http://gstore.unm.edu/apps/epscor/datasets/%s/services/ogc/wcs?SERVICE=WCS&VERSION=1.0.0&REQUEST=GetCoverage&FORMAT=%s&COVERAGE=%s&BBOX=%s&CRS=%s&RESPONSE_CRS=%s&WIDTH=500&HEIGHT=400' %(uidOfDataset, supportedFormat, coverageName, coordinates, CRS, CRS)
r_getCoverage = requests.get(getCoverage_url)
directoryName = "Data"
if not os.path.exists(directoryName):
os.makedirs(directoryName)
print "\nDownloading ..."
for i in progress(range(50)):
with open(os.path.join(directoryName, nameOfDataset), "w") as f:
f.write(r_getCoverage.content)
time.sleep(0.1)
print "\nDownloading completed\n"
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)
coverageName = getCapabilities(datasetDetails[1])
desCoverage = describeCoverage(datasetDetails[1], coverageName)
downloadDataset(datasetDetails[0], datasetDetails[1], desCoverage[0], coverageName, desCoverage[1], desCoverage[2])
multipleSelect(watershedDetails)
if __name__ == "__main__":
steps()