114 lines
4.0 KiB
Python
114 lines
4.0 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
|
|
import numpy.ma as ma
|
|
from heasarc.tdat import tDat
|
|
from heasarc.models import HeasarcTable, TableColumn, HeasarcSwiftBAT105m, 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
|
|
import math
|
|
|
|
def load_heasarc_table(filename):
|
|
|
|
|
|
data = astropy.table.Table.read(filename, delimiter='|',format='ascii.fast_no_header', data_start=2)
|
|
print(data.info)
|
|
|
|
tables = HeasarcSwiftBAT105m.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)
|
|
|
|
|
|
redshift=0.0
|
|
if not (ma.is_masked(item['col18'])):
|
|
redshift=float(item['col18'])
|
|
|
|
lum=0.0
|
|
if not (ma.is_masked(item['col19'])):
|
|
lum=float(item['col19'])
|
|
|
|
|
|
healpix = 0
|
|
ra = 0.0
|
|
dec = 0.0
|
|
lon = 0.0
|
|
lat = 0.0
|
|
if not (ma.is_masked(item['col3']) or ma.is_masked(item['col4'])):
|
|
ra = float(item['col3'])
|
|
dec = float(item['col4'])
|
|
crd = SkyCoord(ra, dec, frame=FK5(), unit="deg")
|
|
lon = crd.galactic.l.value
|
|
lat = crd.galactic.b.value
|
|
healpix = hp.skycoord_to_healpix(crd)
|
|
healpix_plate = hp_plate.skycoord_to_healpix(crd)
|
|
else:
|
|
continue
|
|
|
|
sign=item['col5']
|
|
error_radius = math.sqrt((30.5/sign)**2 + 0.1*0.1)*60
|
|
""" See Eq. 1 in 2018ApJS..235....4O """
|
|
"""
|
|
erf(1.65/sqrt(2)) corresponds to 90%
|
|
"""
|
|
#koeff=1.42
|
|
P=0.98
|
|
koeff=math.sqrt(-2*math.log(1-P))
|
|
|
|
#print(item['col1'],item['col2'],ra,dec,sign,error_radius)
|
|
#continue
|
|
|
|
obj = HeasarcSwiftBAT105m.objects.create(healpix=healpix,
|
|
healpix_plate=healpix_plate,
|
|
ra=ra,
|
|
dec=dec,
|
|
lii = lon,
|
|
bii = lat,
|
|
name=item['col2'],
|
|
error_radius = error_radius*koeff,
|
|
snr = sign,
|
|
counterpart_name = item['col6'],
|
|
flux = float(item['col10']),
|
|
flux_lo = float(item['col11']),
|
|
flux_hi = float(item['col12']),
|
|
redshift = redshift,
|
|
lum = lum,
|
|
class_id = int(item['col21']),
|
|
ref_id = int(item['col1']),
|
|
otype = item['col22'])
|
|
|
|
|
|
obj.save()
|
|
|
|
print('--> Successfully loaded "%s"' % filename)
|
|
pass
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Initiates data dase'
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
load_heasarc_table('/data/Swift/BAT_105m_catalog_07jul2019.txt')
|
|
|
|
self.stdout.write(self.style.SUCCESS('Done'))
|