122 lines
4.6 KiB
Python
122 lines
4.6 KiB
Python
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from datetime import date
|
|
from heasarc.models import INPUT_DATA_DIR
|
|
import datetime
|
|
from django.utils import timezone
|
|
import astropy
|
|
from astropy.io import ascii
|
|
import pandas as pd
|
|
import pymysql
|
|
from sqlalchemy import create_engine
|
|
|
|
from heasarc.tdat import tDat
|
|
from heasarc.models import HeasarcTable, TableColumn, HeasarcXMMSL2, HeasarcObjectClass
|
|
from heasarc.models import NSIDE_SOURCES, ORDER
|
|
from monthplan.models import NSIDE_PLATES
|
|
from astropy_healpix import HEALPix
|
|
from astropy.coordinates import SkyCoord # High-level coordinates
|
|
from astropy.coordinates import ICRS, Galactic, FK4, FK5 # Low-level frames
|
|
from astropy.coordinates import Angle, Latitude, Longitude # Angles
|
|
import astropy.units as u
|
|
|
|
def load_heasarc_table(filename):
|
|
tdatfile=INPUT_DATA_DIR+'/dump/'+filename
|
|
data = astropy.table.Table.read(tdatfile, format='ascii.tdat')
|
|
|
|
keywords = data.meta['keywords']
|
|
for key, value in keywords.items():
|
|
print(key, '->', value)
|
|
|
|
try:
|
|
table=HeasarcTable.objects.get(name__exact=keywords['table_name'])
|
|
table.delete()
|
|
except:
|
|
pass
|
|
|
|
table_name=keywords['table_name']
|
|
table = HeasarcTable(name=keywords['table_name'])
|
|
table.description=keywords['table_description'].replace('\"', '')
|
|
table.document_url=keywords['table_document_url']
|
|
table.save()
|
|
|
|
cols = data.meta['cols']
|
|
for key, value in cols.items():
|
|
column = TableColumn(table=table)
|
|
column.name=key
|
|
column.tdat_type=value['type']
|
|
column.description=value['description']
|
|
column.save()
|
|
print(key, '->', value)
|
|
|
|
|
|
|
|
tables = HeasarcXMMSL2.objects.all()
|
|
tables.delete()
|
|
|
|
|
|
hp = HEALPix(nside=NSIDE_SOURCES, order=ORDER, frame=FK5())
|
|
hp_plate = HEALPix(nside=NSIDE_PLATES, order=ORDER, frame=FK5())
|
|
|
|
for item in data:
|
|
class_id=9999
|
|
if item['class'] != '':
|
|
class_id = int(item['class'])
|
|
try:
|
|
object_class = HeasarcObjectClass.objects.get(class_id=class_id)
|
|
except:
|
|
print("HeasarcObjectClass not found for class_id=", class_id)
|
|
object_class=None
|
|
|
|
crd = SkyCoord(item['ra'], item['dec'], frame=FK5(), unit="deg")
|
|
healpix = hp.skycoord_to_healpix(crd)
|
|
healpix_plate = hp_plate.skycoord_to_healpix(crd)
|
|
|
|
""" Skip if source with the same name exists """
|
|
try:
|
|
src=HeasarcXMMSL2.objects.get(name__exact=item['xmmslew_name'])
|
|
#print("*** Skip {}".format(item['xmmslew_name']))
|
|
|
|
continue
|
|
except:
|
|
pass
|
|
#print("Create {}".format(item['xmmslew_name']))
|
|
|
|
obj = HeasarcXMMSL2.objects.create(healpix=healpix,
|
|
healpix_plate=healpix_plate,
|
|
ra=item['ra'],
|
|
dec=item['dec'],
|
|
lii=item['lii'],
|
|
bii=item['bii'],
|
|
error_radius=float(item['radec_error'])*1.42,
|
|
name="{}".format(item['xmmslew_name']),
|
|
class_id=class_id,object_class=object_class,
|
|
flux_b8=item['flux_b8'],
|
|
flux_b7=item['flux_b7'],
|
|
obsid=item['obsid'],
|
|
ver_inext = item['ver_inext'],
|
|
ver_halo = item['ver_halo'],
|
|
ver_hibgnd = item['ver_hibgnd'],
|
|
ver_nredg = item['ver_nredg'],
|
|
ver_psusp = item['ver_psusp'],
|
|
ver_false = item['ver_false'],
|
|
flag_comment = item['flag_comment'],
|
|
sourcenum=item['sourcenum'])
|
|
|
|
obj.save()
|
|
|
|
print('--> Successfully loaded "%s"' % table_name)
|
|
pass
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Initiates data dase'
|
|
|
|
# def add_arguments(self, parser):
|
|
# parser.add_argument('poll_id', nargs='+', type=int)
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
load_heasarc_table('heasarc_xmmslewcln.tdat.gz')
|
|
|
|
self.stdout.write(self.style.SUCCESS('Done'))
|