111 lines
3.7 KiB
Python
111 lines
3.7 KiB
Python
from django.core.management.base import BaseCommand, CommandError
|
|
import math, sys
|
|
|
|
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
|
|
import numpy.ma as ma
|
|
|
|
from heasarc.tdat import tDat
|
|
from heasarc.models import HeasarcTable, TableColumn, Heasarc4FGL, 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)
|
|
|
|
|
|
#sys.exit()
|
|
tables = Heasarc4FGL.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:
|
|
crd = SkyCoord(item['ra'], item['dec'], frame=FK5(), unit="deg")
|
|
healpix = hp.skycoord_to_healpix(crd)
|
|
healpix_plate = hp_plate.skycoord_to_healpix(crd)
|
|
|
|
if not (ma.is_masked(item['semi_major_axis_68']) or ma.is_masked(item['semi_minor_axis_68'])):
|
|
sx=float(item['semi_major_axis_68'])*3600
|
|
sy=float(item['semi_minor_axis_68'])*3600
|
|
else:
|
|
continue
|
|
|
|
error_radius = max(sx,sy)
|
|
|
|
name=item['name']
|
|
|
|
koeff=1.42 # Paper I
|
|
|
|
R68=math.sqrt(-2*math.log(1-0.68))
|
|
R95=math.sqrt(-2*math.log(1-0.95))
|
|
R98=math.sqrt(-2*math.log(1-0.98))
|
|
|
|
obj = Heasarc4FGL.objects.create(healpix=healpix,
|
|
healpix_plate=healpix_plate,
|
|
ra=item['ra'],
|
|
dec=item['dec'],
|
|
lii=item['lii'],
|
|
bii=item['bii'],
|
|
error_radius=error_radius*R98,
|
|
name=item['name'],
|
|
flux=item['energy_flux'],sign=item['detection_significance'],)
|
|
|
|
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_fermilpsc.tdat.gz')
|
|
|
|
self.stdout.write(self.style.SUCCESS('Done'))
|