-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmars2020.py
186 lines (158 loc) · 5.82 KB
/
mars2020.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
import json
import requests
import sys
import os
import math
import datetime
from dataclasses import dataclass
camera_kinds = {
# Perseverance
# ============
# Engineering Cameras
"NAVCAM_LEFT": "Navigation Camera - Left",
"NAVCAM_RIGHT": "Navigation Camera - Right",
"FRONT_HAZCAM_LEFT_A|FRONT_HAZCAM_LEFT_B": "Front Hazcam - Left",
"FRONT_HAZCAM_RIGHT_A|FRONT_HAZCAM_LEFT_B": "Front Hazcam - Right",
"REAR_HAZCAM_LEFT": "Rear Hazcam - Left",
"REAR_HAZCAM_RIGHT": "Rear Hazcam - Right",
"CACHECAM": "Sample Caching System (CacheCam)",
# Science Cameras
"MCZ_LEFT": "Mastcam-Z - Left",
"MCZ_RIGHT": "Mastcam-Z - Right",
"SKYCAM": "MEDA SkyCam",
"PIXL_MCC": "PIXL Micro Context Camera",
"SHERLOC_WATSON": "SHERLOC - WATSON",
"SHERLOC_ACI": "SHERLOC Context Imager",
"SUPERCAM_RMI": "SuperCam Remote Micro Imager",
# Entry, Descent and Landing Cameras
"EDL_PUCAM1": "Parachute Up-Look Camera A",
"EDL_PUCAM2": "Parachute Up-Look Camera B",
"EDL_DDCAM": "Descent Stage Down-Look Camera",
"EDL_RUCAM": "Rover Up-Look Camera",
"EDL_RDCAM": "Rover Down-Look Camera",
"LCAM": "Lander Vision System Camera",
# Ingenuity
# =========
"HELI_NAV": "Ingenuity Navigation Camera",
"HELI_RTE": "Ingenuity Color Camera",
}
@dataclass
class Image:
sol: int
attitude: tuple[float]
caption: str
sample_type: str
date_taken_mars: str
credit: str
date_taken_utc: datetime.datetime
link: str
drive: int
title: str
site: int
date_received: datetime.datetime
mast_azimuth: float
mast_elevation: float
sclk: float # no idea what this is
scale_factor: int
xyz: tuple[float]
subframe_rect: tuple[int]
dimension: tuple[int]
image_small: str
image_medium: str
image_large: str
image_fullres: str
camera_filter_name: str
camera_vector: tuple[float]
camera_model_component_list: str
camera_position: tuple[float]
instrument: str
camera_model_type: str
@dataclass
class QueryResults:
total_results: int
total_images: int
class Mars2020:
def __init__(self):
pass
def get_data(self, results=100, page=1, cameras=["MCZ_LEFT", "MCZ_RIGHT"], sort="newest"):
page = page - 1
for c in cameras:
if not c in camera_kinds.keys():
print(f"error: camera '{c}' not found.")
sys.exit(1)
if results > 100 or results < 1:
print(f"error: number of results must be either 1, 100 or somewhere in between.")
sys.exit(1)
if sort == "newest":
sort = "sol+desc"
else:
sort = "sol+asc"
joined_cameras = "|".join(cameras)
# I like to err on the side of.. well no error handling I guess... what's the worst that could happen?
api_req = requests.get(f"https://mars.nasa.gov/rss/api/?feed=raw_images&category=mars2020,ingenuity&feedtype=json&ver=1.2&num={results}&page={page}&order={sort}&search={joined_cameras}&")
api_res = json.loads(api_req.text)
total_results = api_res["total_results"]
total_images = api_res["total_images"]
images = []
for image in api_res["images"]:
attitude = tuple(image["attitude"].strip("()").split(","))
date_taken_utc = datetime.datetime.fromisoformat(image["date_taken_utc"])
date_received = datetime.datetime.fromisoformat(image["date_received"])
drive = image["drive"]
sol = image["sol"]
caption = image["caption"]
sample_type = image["sample_type"]
date_taken_mars = image["date_taken_mars"]
credit = image["credit"]
link = image["link"]
title = image["title"]
site = image["site"]
mast_azimuth = image["extended"]["mastAz"]
mast_elevation = image["extended"]["mastEl"]
sclk = image["extended"]["sclk"]
scale_factor = int(image["extended"]["scaleFactor"])
xyz = image["extended"]["xyz"]
subframe_rect = tuple(image["extended"]["subframeRect"].strip("()").split(","))
dimension = tuple(image["extended"]["dimension"].strip("()").split(","))
image_small = image["image_files"]["small"]
image_medium = image["image_files"]["medium"]
image_large = image["image_files"]["large"]
image_fullres = image["image_files"]["full_res"]
camera_filter_name = image["camera"]["filter_name"]
camera_vector = tuple(image["camera"]["camera_vector"].strip("()").split(","))
camera_model_component_list = image["camera"]["camera_model_component_list"]
camera_position = tuple(image["camera"]["camera_position"].strip("()").split(","))
instrument = image["camera"]["instrument"]
camera_model_type = image["camera"]["camera_model_type"]
images.append(Image(
sol,
attitude,
caption,
sample_type,
date_taken_mars,
credit,
date_taken_utc,
link,
drive,
title,
site,
date_received,
mast_azimuth,
mast_elevation,
sclk,
scale_factor,
xyz,
subframe_rect,
dimension,
image_small,
image_medium,
image_large,
image_fullres,
camera_filter_name,
camera_vector,
camera_model_component_list,
camera_position,
instrument,
camera_model_type
))
return images, QueryResults(total_results, total_images)