HEASARC
This commit is contained in:
110
heasarc/management/commands/00_heasarc_intibisgal.py
Normal file
110
heasarc/management/commands/00_heasarc_intibisgal.py
Normal file
@@ -0,0 +1,110 @@
|
||||
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, HeasarcIntegral9, 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 = HeasarcIntegral9.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)
|
||||
print(item['name'], healpix_plate)
|
||||
obj = HeasarcIntegral9.objects.create(healpix=healpix,
|
||||
healpix_plate=healpix_plate,
|
||||
ra=item['ra'],
|
||||
dec=item['dec'],
|
||||
lii=item['lii'],
|
||||
bii=item['bii'],
|
||||
error_radius=240,
|
||||
name=item['name'],
|
||||
source_type=item['source_type'],
|
||||
flux=item['flux'],
|
||||
flux_error=item['flux'],
|
||||
class_id=class_id,
|
||||
object_class=object_class)
|
||||
|
||||
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_intibisgal.tdat.gz')
|
||||
return
|
||||
|
||||
hp = HEALPix(nside=NSIDE_SOURCES, order=ORDER, frame=FK5())
|
||||
# update healpix only
|
||||
srcs = HeasarcXrayMaster.objects.all()
|
||||
for src in srcs:
|
||||
crd = SkyCoord(src.ra, src.dec, frame=FK5(), unit="deg")
|
||||
src.healpix = hp.skycoord_to_healpix(crd)
|
||||
src.save()
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Done'))
|
||||
Reference in New Issue
Block a user