-
Notifications
You must be signed in to change notification settings - Fork 3
/
oautils.py
82 lines (76 loc) · 3.65 KB
/
oautils.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
class dataCollector:
def __init__(self, beam=None, oaurl=None, track=None, date=None, latlims=None, lonlims=None):
if (beam is None) or ((oaurl is None) and (None in [track, date, latlims, lonlims])):
raise Exception('''Please specify a beam and
- either: an OpenAltimetry API url,
- or: a track, date, latitude limits and longitude limits.''')
else:
if oaurl is not None:
url = oaurl
tofind = '&beamName='
ids = url.find(tofind)
while ids>-1:
url = url.replace(url[ids:ids+len(tofind)+4],'')
ids = url.find(tofind)
iprod = url.find('/atl')
url = url.replace(url[iprod:iprod+6],'/atlXX')
url += tofind + beam + '&client=jupyter'
idate = url.find('date=') + len('date=')
date = url[idate:idate+10]
itrack = url.find('trackId=') + len('trackId=')
trackend = url[itrack:].find('&')
track = int(url[itrack:itrack+trackend])
bb = []
for s in ['minx=', 'maxx=', 'miny=', 'maxy=']:
ids = url.find(s) + len(s)
ide = url[ids:].find('&')
bb.append(float(url[ids:ids+ide]))
lonlims = bb[:2]
latlims = bb[2:]
elif None not in [track, date, latlims, lonlims]:
url = 'https://openaltimetry.org/data/api/icesat2/atlXX?'
url += 'date={date}&minx={minx}&miny={miny}&maxx={maxx}&maxy={maxy}&trackId={track}&beamName={beam}'.format(
date=date,minx=lonlims[0],miny=latlims[0],maxx=lonlims[1],maxy=latlims[1],track=track,beam=beam)
url += '&outputFormat=json&client=jupyter'
self.url = url
self.date = date
self.track = track
self.beam = beam
self.latlims = latlims
self.lonlims = lonlims
print('OpenAltimetry API URL:', self.url)
print('Date:', self.date)
print('Track:', self.track)
print('Beam:', self.beam)
print('Latitude limits:', self.latlims)
print('Longitude limits:', self.lonlims)
def requestData(self):
import pandas as pd
import json
import requests
print('---> requesting ATL03 data...',end='')
product = 'atl03'
request_url = self.url.replace('atlXX',product)
data = requests.get(request_url).json()
lat, lon, h, confs = [], [], [], []
for beam in data:
for confidence in beam['series']:
for p in confidence['data']:
confs.append(confidence['name'])
lat.append(p[0])
lon.append(p[1])
h.append(p[2])
self.atl03 = pd.DataFrame(list(zip(lat,lon,h,confs)), columns = ['lat','lon','h','conf'])
print(' Done.')
print('---> requesting ATL06 data...',end='')
product = 'atl06'
request_url = self.url.replace('atlXX',product)
data = requests.get(request_url).json()
self.atl06 = pd.DataFrame(data['series'][0]['lat_lon_elev'], columns = ['lat','lon','h'])
print(' Done.')
print('---> requesting ATL08 data...',end='')
product = 'atl08'
request_url = self.url.replace('atlXX',product)
data = requests.get(request_url).json()
self.atl08 = pd.DataFrame(data['series'][0]['lat_lon_elev_canopy'], columns = ['lat','lon','h','canopy'])
print(' Done.')