Skip to content

Commit d5f3d3e

Browse files
committed
simplified ancillary record handling
1 parent a96faa3 commit d5f3d3e

File tree

4 files changed

+117
-156
lines changed

4 files changed

+117
-156
lines changed

sliderule/icesat2.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2828
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30-
30+
import os
31+
import time
3132
import itertools
3233
import copy
3334
import json
@@ -43,8 +44,6 @@
4344
from sklearn.cluster import KMeans
4445
import sliderule
4546
from sliderule import version
46-
import os
47-
import time
4847

4948
###############################################################################
5049
# GLOBALS
@@ -100,20 +99,6 @@
10099
# gps-based epoch for delta times
101100
ATLAS_SDP_EPOCH = datetime.datetime(2018, 1, 1)
102101

103-
# ancillary list types
104-
ATL03_GEOLOCATION = 0
105-
ATL03_GEOCORRECTION = 1
106-
ATL03_HEIGHT = 2
107-
ATL08_SIGNAL_PHOTON = 3
108-
109-
# ancillary list type matching
110-
ancillary_lists = {
111-
ATL03_GEOLOCATION: "atl03_geolocation_fields",
112-
ATL03_GEOCORRECTION: "atl03_geocorrection_fields",
113-
ATL03_HEIGHT: "atl03_height_fields",
114-
ATL08_SIGNAL_PHOTON: "atl08_signal_photon_fields"
115-
}
116-
117102
###############################################################################
118103
# NSIDC UTILITIES
119104
###############################################################################
@@ -767,19 +752,18 @@ def atl06p(parm, asset=DEFAULT_ASSET, version=DEFAULT_ICESAT2_SDP_VERSION, callb
767752
if 'atl06rec' in rsp['__rectype']:
768753
elevation_records += rsp,
769754
num_elevations += len(rsp['elevation'])
770-
elif 'atlxxrec' in rsp['__rectype']:
771-
if rsp['list_type'] == ATL03_GEOLOCATION or rsp['list_type'] == ATL03_GEOCORRECTION:
772-
field_name = parm[ancillary_lists[rsp['list_type']]][rsp['field_index']]
773-
if field_name not in field_dictionary:
774-
field_dictionary[field_name] = {"extent_id": [], field_name: []}
775-
# Parse Ancillary Data
776-
data = __get_values(rsp['data'], rsp['data_type'], len(rsp['data']))
777-
# Add Left Pair Track Entry
778-
field_dictionary[field_name]['extent_id'] += rsp['extent_id'] | 0x2,
779-
field_dictionary[field_name][field_name] += data[0],
780-
# Add Right Pair Track Entry
781-
field_dictionary[field_name]['extent_id'] += rsp['extent_id'] | 0x3,
782-
field_dictionary[field_name][field_name] += data[1],
755+
elif 'ga3rec' == rsp['__rectype']:
756+
field_name = parm['atl03_geo_fields'][rsp['field_index']]
757+
if field_name not in field_dictionary:
758+
field_dictionary[field_name] = {"extent_id": [], field_name: []}
759+
# Parse Ancillary Data
760+
data = __get_values(rsp['data'], rsp['data_type'], len(rsp['data']))
761+
# Add Left Pair Track Entry
762+
field_dictionary[field_name]['extent_id'] += rsp['extent_id'] | 0x2,
763+
field_dictionary[field_name][field_name] += data[0],
764+
# Add Right Pair Track Entry
765+
field_dictionary[field_name]['extent_id'] += rsp['extent_id'] | 0x3,
766+
field_dictionary[field_name][field_name] += data[1],
783767
# Build Elevation Columns
784768
if num_elevations > 0:
785769
# Initialize Columns

utils/query_elevations.py

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,31 @@
66
import logging
77
import time
88
from sliderule import icesat2
9-
from utils import parse_command_line
9+
from utils import initialize_client
1010

1111
###############################################################################
1212
# MAIN
1313
###############################################################################
1414

1515
if __name__ == '__main__':
1616

17-
# Set Script Defaults
18-
cfg = {
19-
"url": 'localhost',
20-
"organization": None,
21-
"asset": 'atlas-local',
22-
"region": 'examples/grandmesa.geojson',
23-
"resource": 'ATL03_20181017222812_02950102_005_01.h5',
24-
"srt": icesat2.SRT_LAND,
25-
"cnf": icesat2.CNF_SURFACE_HIGH,
26-
"ats": 10.0,
27-
"cnt": 10,
28-
"len": 40.0,
29-
"res": 20.0,
30-
"maxi": 1,
31-
"atl03_geolocation_fields": [],
32-
"atl03_geocorrection_fields": [],
33-
"atl03_height_fields": [],
34-
"atl08_signal_photon_fields": [],
35-
"profile": True
36-
}
37-
38-
# Parse Configuration Parameters
39-
parse_command_line(sys.argv, cfg)
40-
41-
# Configure Logging #
17+
# Configure Logging
4218
logging.basicConfig(level=logging.INFO)
4319

44-
# Region of Interest #
45-
region = icesat2.toregion(cfg["region"])
46-
47-
# Configure SlideRule #
48-
icesat2.init(cfg["url"], True, organization=cfg["organization"])
49-
50-
# Build ATL06 Request #
51-
parms = {
52-
"poly": region['poly'],
53-
"raster": region['raster'],
54-
"srt": cfg['srt'],
55-
"cnf": cfg['cnf'],
56-
"ats": cfg['ats'],
57-
"cnt": cfg['cnt'],
58-
"len": cfg['len'],
59-
"res": cfg['res'],
60-
"maxi": cfg['maxi'],
61-
"atl03_geolocation_fields": cfg['atl03_geolocation_fields'],
62-
"atl03_geocorrection_fields": cfg['atl03_geocorrection_fields'],
63-
"atl03_height_fields": cfg['atl03_height_fields'],
64-
"atl08_signal_photon_fields": cfg['atl08_signal_photon_fields']
65-
}
20+
# Initialize Client
21+
parms, cfg = initialize_client(sys.argv)
6622

6723
# Request ATL06 Data
68-
perf_start = time.perf_counter()
24+
tstart = time.perf_counter()
6925
gdf = icesat2.atl06p(parms, asset=cfg["asset"], resources=[cfg["resource"]])
70-
perf_stop = time.perf_counter()
26+
perf_duration = time.perf_counter() - tstart
7127

7228
# Display Statistics
73-
num_elevations = len(gdf)
74-
perf_duration = perf_stop - perf_start
7529
print("Completed in {:.3f} seconds of wall-clock time".format(perf_duration))
76-
if num_elevations > 0:
30+
if len(gdf) > 0:
7731
print("Reference Ground Tracks: {}".format(gdf["rgt"].unique()))
7832
print("Cycles: {}".format(gdf["cycle"].unique()))
79-
print("Received {} elevations".format(num_elevations))
33+
print("Received {} elevations".format(len(gdf)))
8034
else:
8135
print("No elevations were returned")
8236

utils/region_of_interest.py

Lines changed: 21 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,94 +20,43 @@
2020
import geopandas as gpd
2121
import matplotlib.pyplot as plt
2222
from sliderule import icesat2
23-
from utils import parse_command_line
24-
25-
###############################################################################
26-
# LOCAL FUNCTIONS
27-
###############################################################################
28-
29-
def process_atl06_algorithm(parms, asset):
30-
31-
# Latch Start Time
32-
perf_start = time.perf_counter()
33-
34-
# Request ATL06 Data
35-
gdf = icesat2.atl06p(parms, asset)
36-
37-
# Latch Stop Time
38-
perf_stop = time.perf_counter()
39-
40-
# Build DataFrame of SlideRule Responses
41-
num_elevations = len(gdf)
42-
43-
# Display Statistics
44-
perf_duration = perf_stop - perf_start
45-
print("Completed in {:.3f} seconds of wall-clock time".format(perf_duration))
46-
if num_elevations > 0:
47-
print("Reference Ground Tracks: {}".format(gdf["rgt"].unique()))
48-
print("Cycles: {}".format(gdf["cycle"].unique()))
49-
print("Received {} elevations".format(num_elevations))
50-
else:
51-
print("No elevations were returned")
52-
53-
# Return DataFrame
54-
return gdf
23+
from utils import initialize_client
5524

5625
###############################################################################
5726
# MAIN
5827
###############################################################################
5928

6029
if __name__ == '__main__':
6130

62-
# Set Script Defaults
63-
cfg = {
64-
"url": 'localhost',
65-
"organization": None,
66-
"asset": 'atlas-local',
67-
"region": 'examples/grandmesa.geojson',
68-
"verbose": True
69-
}
70-
71-
# Parse Configuration Parameters
72-
parse_command_line(sys.argv, cfg)
73-
7431
# Configure Logging
7532
logging.basicConfig(level=logging.INFO)
7633

77-
# Region of Interest #
78-
region = icesat2.toregion(cfg["region"])
79-
80-
# Configure SlideRule #
81-
icesat2.init(cfg["url"], cfg["verbose"], organization=cfg["organization"])
82-
83-
# Build ATL06 Request #
84-
parms = {
85-
"poly": region["poly"],
86-
"raster": region["raster"],
87-
"srt": icesat2.SRT_LAND,
88-
"cnf": icesat2.CNF_SURFACE_HIGH,
89-
"atl08_class": ["atl08_ground"],
90-
"ats": 10.0,
91-
"cnt": 10,
92-
"len": 40.0,
93-
"res": 20.0,
94-
"maxi": 1
95-
}
96-
97-
# Parse Algorithm Parameters
98-
parse_command_line(sys.argv, parms)
34+
# Initialize Client #
35+
parms, cfg = initialize_client(sys.argv)
9936

10037
# Get ATL06 Elevations
101-
atl06 = process_atl06_algorithm(parms, cfg["asset"])
38+
tstart = time.perf_counter()
39+
atl06 = icesat2.atl06p(parms, cfg["asset"])
40+
perf_duration = time.perf_counter() - tstart
10241

103-
# Check Results Present
104-
if len(atl06) == 0:
105-
print("No ATL06 data available")
42+
# Display Statistics
43+
print("Completed in {:.3f} seconds of wall-clock time".format(perf_duration))
44+
if len(atl06) > 0:
45+
print("Reference Ground Tracks: {}".format(atl06["rgt"].unique()))
46+
print("Cycles: {}".format(atl06["cycle"].unique()))
47+
print("Received {} elevations".format(len(atl06)))
48+
else:
49+
print("No elevations were returned")
10650
sys.exit()
10751

52+
# Display Profile
53+
print("\nTiming Profiles")
54+
for key in icesat2.profiles:
55+
print("{:16}: {:.6f} secs".format(key, icesat2.profiles[key]))
56+
10857
# Calculate Extent
109-
lons = [p["lon"] for p in region["poly"]]
110-
lats = [p["lat"] for p in region["poly"]]
58+
lons = [p["lon"] for p in parms["poly"]]
59+
lats = [p["lat"] for p in parms["poly"]]
11160
lon_margin = (max(lons) - min(lons)) * 0.1
11261
lat_margin = (max(lats) - min(lats)) * 0.1
11362
extent = (min(lons) - lon_margin, max(lons) + lon_margin, min(lats) - lat_margin, max(lats) + lat_margin)

utils/utils.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
from sliderule import icesat2
12

3+
#
4+
# Parse Command Line
5+
#
26
def parse_command_line(args, cfg):
37
i = 1
48
while i < len(args):
@@ -17,6 +21,76 @@ def parse_command_line(args, cfg):
1721
cfg[entry] = l
1822
elif type(cfg[entry]) is int:
1923
cfg[entry] = int(args[i + 1])
24+
elif type(cfg[entry]) is bool:
25+
if args[i + 1] == "True" or args[i + 1] == "true":
26+
cfg[entry] = True
27+
elif args[i + 1] == "False" or args[i + 1] == "false":
28+
cfg[entry] = False
2029
i += 1
2130
i += 1
2231
return cfg
32+
33+
#
34+
# Initialize Client
35+
#
36+
def initialize_client(args):
37+
38+
# Set Script Defaults
39+
cfg = {
40+
"url": 'localhost',
41+
"organization": None,
42+
"asset": 'atlas-local',
43+
"region": 'examples/grandmesa.geojson',
44+
"resource": 'ATL03_20181017222812_02950102_005_01.h5',
45+
"raster": True,
46+
"atl08_class": [],
47+
"srt": icesat2.SRT_LAND,
48+
"cnf": icesat2.CNF_SURFACE_HIGH,
49+
"ats": 10.0,
50+
"cnt": 10,
51+
"len": 40.0,
52+
"res": 20.0,
53+
"maxi": 1,
54+
"atl03_geo_fields": [],
55+
"atl03_photon_fields": [],
56+
"profile": True,
57+
"verbose": True
58+
}
59+
60+
# Parse Configuration Parameters #
61+
parse_command_line(args, cfg)
62+
63+
# Region of Interest #
64+
region = icesat2.toregion(cfg["region"])
65+
66+
# Configure SlideRule #
67+
icesat2.init(cfg["url"], cfg["verbose"], organization=cfg["organization"])
68+
69+
# Build Initial Parameters #
70+
parms = {
71+
"poly": region['poly'],
72+
"srt": cfg['srt'],
73+
"cnf": cfg['cnf'],
74+
"ats": cfg['ats'],
75+
"cnt": cfg['cnt'],
76+
"len": cfg['len'],
77+
"res": cfg['res'],
78+
"maxi": cfg['maxi'],
79+
}
80+
81+
# Add Raster #
82+
if cfg["raster"]:
83+
parms["raster"] = region['raster']
84+
85+
# Add Ancillary Fields #
86+
if len(cfg['atl03_geo_fields']) > 0:
87+
parms['atl03_geo_fields'] = cfg['atl03_geo_fields']
88+
if len(cfg['atl03_photon_fields']) > 0:
89+
parms['atl03_photon_fields'] = cfg['atl03_photon_fields']
90+
91+
# Add ATL08 Classification #
92+
if len(cfg['atl08_class']) > 0:
93+
parms['atl08_class'] = cfg['atl08_class']
94+
95+
# Return Parameters and Configuration #
96+
return parms, cfg

0 commit comments

Comments
 (0)