Commit ca7d721c authored by michitaro's avatar michitaro
Browse files

la2016

parent 95a4d11d
......@@ -16,40 +16,35 @@
'''
import PIL.Image
import numpy
import argparse
import tarfile
import subprocess
import tempfile
import getpass
import os
import os.path
import os, os.path
import contextlib
import logging
logging.basicConfig(level=logging.INFO)
import logging ; logging.basicConfig(level=logging.INFO)
try:
import pyfits
import pyfits # type: ignore
except:
import astropy.io.fits as pyfits
FLUXMAG0 = 10 ** (27 / 2.5)
import astropy.io.fits as pyfits # type: ignore
import numpy
import PIL.Image
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--outDir', '-o', required=True)
parser.add_argument('--user', '-u', required=True)
parser.add_argument('--filters', '-f',
nargs=3, default=['HSC-I', 'HSC-R', 'HSC-G'])
parser.add_argument('--filters', '-f', nargs=3, default=['HSC-I', 'HSC-R', 'HSC-G'])
parser.add_argument('--fov', default='30asec')
parser.add_argument('--rerun', default='any',
choices='la2014'.split())
choices='la2016 la2014'.split())
parser.add_argument('--color', choices='hsc sdss'.split(), default='hsc')
parser.add_argument('input', type=argparse.FileType('r'))
args = parser.parse_args()
password = getpass.getpass('Password: ')
password = os.environ.get('HSC_PASSWORD') or getpass.getpass('Password? ')
checkPassword(args.user, password)
coords, outs = loadCoords(args.input)
......@@ -67,13 +62,13 @@ def main():
makeColorPng(rgb, outFile, args.color)
TOP_PAGE = 'https://hscla.mtk.nao.ac.jp/das_cutout/la2014/'
API = 'https://hscla.mtk.nao.ac.jp/das_cutout/la2014/cgi-bin/cutout'
TOP_PAGE = 'https://hscla.mtk.nao.ac.jp/das_cutout/la2016/'
API = 'https://hscla.mtk.nao.ac.jp/das_cutout/la2016/cgi-bin/cutout'
def loadCoords(input):
import re
comment = re.compile('\s*(?:$|#)')
comment = re.compile(r'\s*(?:$|#)')
num = 1
coords = []
outs = []
......@@ -100,8 +95,7 @@ def requestFileFor(coords, filters, fov, rerun):
tmp.write('#? filter ra dec sw sh rerun\n'.encode('utf-8'))
for coord in coords:
for filterName in filters:
tmp.write('{} {} {} {} {} {}\n'.format(
filterName, coord[0], coord[1], fov, fov, rerun).encode())
tmp.write('{} {} {} {} {} {}\n'.format(filterName, coord[0], coord[1], fov, fov, rerun).encode())
tmp.flush()
yield tmp.name
......@@ -109,7 +103,7 @@ def requestFileFor(coords, filters, fov, rerun):
def batch(arr, n):
i = 0
while i < len(arr):
yield arr[i: i + n]
yield arr[i : i + n]
i += n
......@@ -129,7 +123,9 @@ def rgbBundle(files):
dst = locals()[ch]
copyFileObj(fileObj, dst)
rgb[ch] = dst.name
yield objNum, rgb
if len(rgb) > 0:
yield objNum, rgb
def copyFileObj(src, dst):
......@@ -140,8 +136,7 @@ def copyFileObj(src, dst):
def checkPassword(user, password):
with tempfile.NamedTemporaryFile() as netrc:
netrc.write('machine hscla.mtk.nao.ac.jp login {} password {}'.format(
user, password).encode('ascii'))
netrc.write('machine hscla.mtk.nao.ac.jp login {} password {}'.format(user, password).encode('ascii'))
netrc.flush()
httpCode = subprocess.check_output(
['curl', '--netrc-file', netrc.name, '-o', os.devnull, '-w', '%{http_code}', '-s', TOP_PAGE]).strip()
......@@ -151,13 +146,11 @@ def checkPassword(user, password):
def queryTar(user, password, requestFile):
with tempfile.NamedTemporaryFile() as netrc:
netrc.write(
'machine hscla.mtk.nao.ac.jp login {} password {}'.format(user, password).encode('ascii'))
netrc.write('machine hscla.mtk.nao.ac.jp login {} password {}'.format(user, password).encode('ascii'))
netrc.flush()
pipe = subprocess.Popen([
'curl', '--netrc-file', netrc.name,
'-L',
# '--dump-header', '-',
'--form', 'list=@{}'.format(requestFile),
'--silent',
API,
......@@ -165,9 +158,14 @@ def queryTar(user, password, requestFile):
with tarfile.open(fileobj=pipe.stdout, mode='r|*') as tar:
while True:
info = tar.next()
if info is None:
break
try:
info = tar.next()
except tarfile.StreamError as e:
if tar.offset == 0:
break
else:
raise
if info is None: break
logging.info('extracting {}...'.format(info.name))
f = tar.extractfile(info)
yield info, f
......@@ -187,7 +185,7 @@ def makeColorPng(rgb, out, color):
for i, ch in enumerate('rgb'):
if ch in rgb:
with pyfits.open(rgb[ch]) as hdul:
x = scale(hdul[1].data, FLUXMAG0)
x = scale(hdul[1].data, hdul[0].header['FLUXMAG0'])
layers[i] = x
if color == 'hsc':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment