-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtfits.py
77 lines (56 loc) · 1.89 KB
/
htfits.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
import matplotlib
import matplotlib.image
import io
from io import StringIO
from astropy.io import fits
import astropy.io.fits as pyfits
import numpy as np
from urllib.request import urlopen, urlparse
import matplotlib
import matplotlib.image
import io
import urllib.request as urllib2
from urllib.parse import urlparse
from astropy.io import fits
import numpy as np
ALLOWED_SCHEMES = ['http']
ALLOWED_NETLOCS = ['space.astro.cz']
def fits_to_png(environ, start_response):
url = environ['QUERY_STRING']
print(url)
urlp = urlparse(url)
if not (urlp.netloc in ALLOWED_NETLOCS and urlp.scheme in ALLOWED_SCHEMES):
start_response('403 Forbidden', [('Content-type', 'text/plain')])
return "FITS URL is bad"
try:
fits_file = fits.open(io.BytesIO(urllib2.urlopen(url).read()))
sio = io.BytesIO()
imunit = None
for unit in fits_file:
if unit.data is not None:
imunit = unit
print("GEN")
matplotlib.image.imsave(sio, imunit.data[::-1,:], format='png', cmap='gnuplot')
sio.seek(0)
except Exception:
start_response('500 Internal Server Error', [('Content-type', 'text/plain')])
return "Error"
start_response('200 OK', [('Content-type', 'image/png')])
return [sio.getvalue()]
def main():
from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server
import static # https://github.com/lukearno/static
static = static.Cling('.')
def app(environ, start_response):
if environ['PATH_INFO'] == '/f.png':
return fits_to_png(environ, start_response)
return static(environ, start_response)
port = 80
httpd = make_server('', port, app)
print("Serving on port {}...".format(port))
httpd.serve_forever()
print("End...")
if __name__ == "__main__":
main()
application = fits_to_png