This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastro-basic.py
120 lines (94 loc) · 3.22 KB
/
astro-basic.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
# astro.py
try:
import unzip_requirements
except ImportError:
pass
import astropy.units as u
import astropy.coordinates as coord
import json, logging, time, os, decimal, requests
import numpy as np
from astropy.io import fits
from astropy.stats import sigma_clipped_stats
logger = logging.getLogger("handler_logger")
logger.setLevel(logging.DEBUG)
###################################
######### Helper Functions ########
###################################
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
def _get_response(status_code, body):
if not isinstance(body, str):
body = json.dumps(body)
return {
"statusCode": status_code,
'headers': {
# Required for CORS support to work
'Access-Control-Allow-Origin': '*',
# Required for cookies, authorization headers with HTTPS
'Access-Control-Allow-Credentials': 'true',
},
"body": body}
def _get_body(event):
try:
return json.loads(event.get("body", ""))
except:
logger.debug("event body could not be JSON decoded.")
return {}
###################################
###### Cache the Fits Files #######
###################################
fits_cache = {}
def _get_fits(site, base_filename, EX01orEX13):
"""
EX01orEX13 should have a value in ["01", "13"]
"""
file_id = f"{base_filename}-{EX01orEX13}"
print(f"Fits cache keys: {fits_cache.keys()}")
# Only download the file if it's not cached already
if file_id not in fits_cache:
print("Fits file not cached; downloading file.")
# Get the url for the file
api_url = f"https://api.photonranch.org/api/fits{EX01orEX13}_url/{base_filename}"
print(f"API url: {api_url}")
file_url = requests.get(api_url).text
print(f"File url: {file_url}")
# Load the file from the url
with fits.open(fileURL) as f:
fits_cache[file_id] = f
# check that it is a legit file
print("data shape: ")
print(f[0].data.shape)
else:
print("Fits file already cached. No download needed.")
return fits_cache[file_id]
###################################
###$$$### Lambda Handler ##########
###################################
def getRegionStats(event, context):
body = _get_body(event)
site = body.get("site")
base_filename = body.get("base_filename")
EXversion = "01" # we want the full sized fits file
fitsfile = _get_fits(site, base_filename, EXversion)
header = fitsfile[0].header
data = fitsfile[0].data
mean, median, std = sigma_clipped_stats(data, sigma=3.0)
print((mean, median, std))
return_data = {
"mean": mean,
"median": median,
"std": std
}
return _get_response(200, return_data)
def hello(event,context):
icrs = coord.ICRS(ra=258.58356362*u.deg, dec=14.55255619*u.deg, radial_velocity=-16.1*u.km/u.s)
print("icrs:")
print(icrs)
return icrs