160 lines
7.1 KiB
Python
160 lines
7.1 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, HeasarcRASS2RXS, 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',encoding='latin1')
|
|
|
|
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']
|
|
if 'default_search_radius' in keywords:
|
|
table.search_radius=int(keywords['default_search_radius'])
|
|
if 'frequency_regime' in keywords:
|
|
table.frequency_regime=keywords['frequency_regime']
|
|
table.observatory_name=keywords['observatory_name']
|
|
table.security=keywords['table_security']
|
|
if 'table_author' in keywords:
|
|
table.author=keywords['table_author']
|
|
if 'catalog_bibcode' in keywords:
|
|
table.bibcode=keywords['catalog_bibcode']
|
|
if 'declination' in keywords:
|
|
table.declination=keywords['declination'].replace('@', '')
|
|
if 'right_ascension' in keywords:
|
|
table.right_ascension=keywords['right_ascension'].replace('@', '')
|
|
table.observatory_name = keywords['observatory_name']
|
|
if 'parameter_defaults' in keywords:
|
|
table.parameter_defaults = keywords['parameter_defaults']
|
|
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)
|
|
|
|
# convert data frame to pandas
|
|
# df = data.to_pandas()
|
|
# sql = 'DROP TABLE IF EXISTS '+table_name+';'
|
|
# result = engine.execute(sql)
|
|
# Insert whole DataFrame into MySQL
|
|
# df.to_sql(table_name, con = engine, if_exists = 'append', chunksize = 200000)
|
|
|
|
tables = HeasarcRASS2RXS.objects.all()
|
|
tables.delete()
|
|
|
|
hp = HEALPix(nside=NSIDE_SOURCES, order=ORDER, frame=FK5())
|
|
for item in data:
|
|
|
|
#crd = SkyCoord(item['ra'], item['dec'], frame=FK5(), unit="deg")
|
|
#healpix = hp.skycoord_to_healpix(crd)
|
|
|
|
source_quality_flag=0
|
|
if not (ma.is_masked(item['source_quality_flag'])):
|
|
source_quality_flag=int(item['source_quality_flag'])
|
|
|
|
healpix = 0
|
|
ra = 0.0
|
|
dec = 0.0
|
|
if not (ma.is_masked(item['ra']) or ma.is_masked(item['dec'])):
|
|
ra = float(item['ra'])
|
|
dec = float(item['dec'])
|
|
crd = SkyCoord(ra, dec, frame=FK5(), unit="deg")
|
|
healpix = hp.skycoord_to_healpix(crd)
|
|
|
|
obj = HeasarcRASS2RXS.objects.create(healpix=healpix,
|
|
ra=ra,
|
|
dec=dec,
|
|
lii=item['lii'],
|
|
bii=item['bii'],
|
|
name=item['name'],
|
|
detection_likelihood = item['detection_likelihood'],
|
|
count_rate = item['count_rate'],
|
|
count_rate_error = item['count_rate_error'],
|
|
exposure = item['exposure'],
|
|
source_extent = item['source_extent'],
|
|
source_extent_prob = item['source_extent_prob'],
|
|
source_quality_flag = source_quality_flag,
|
|
hardness_ratio_1 = item['hardness_ratio_1'],
|
|
hardness_ratio_2 = item['hardness_ratio_2'],
|
|
plaw_flux = item['plaw_flux'],
|
|
plaw_chi2_reduced = item['plaw_chi2_reduced'],
|
|
plaw_nh = item['plaw_nh'],
|
|
mekal_flux = item['mekal_flux'],
|
|
mekal_chi2_reduced = item['mekal_chi2_reduced'],
|
|
mekal_nh = item['mekal_nh'],
|
|
bb_flux = item['bb_flux'],
|
|
bb_chi2_reduced = item['bb_chi2_reduced'],
|
|
bb_nh = item['bb_nh'],
|
|
x_pixel_error = item['x_pixel_error'],
|
|
y_pixel_error = item['y_pixel_error'],
|
|
time = item['time'])
|
|
obj.save()
|
|
|
|
print('--> Successfully loaded "%s"' % table_name)
|
|
pass
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Initiates data dase'
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
tables = HeasarcRASS2RXS.objects.all()
|
|
for src in tables:
|
|
print(src.error_radius)
|
|
return
|
|
|
|
|
|
#load_heasarc_table('heasarc_rass2rxs.tdat.gz')
|
|
|
|
# 14547279 | 28571450 | 180.63796 | 18.45888 | 247.99462296 | 75.7992302 | 11.33246061 | 2RXS J120233.1+182731 | 44 | 1.6219809792e-13 | 6975
|
|
|
|
hp_plate = HEALPix(nside=NSIDE_PLATES, order=ORDER, frame=FK5())
|
|
crd = SkyCoord(248.52271, -44.04758, frame=FK5(), unit="deg")
|
|
h = hp_plate.skycoord_to_healpix(crd)
|
|
print(h)
|
|
return
|
|
|
|
# update healpix only
|
|
srcs = HeasarcRASS2RXS.objects.all()
|
|
for src in srcs:
|
|
crd = SkyCoord(src.ra, src.dec, frame=FK5(), unit="deg")
|
|
src.healpix_plate = hp_plate.skycoord_to_healpix(crd)
|
|
src.save()
|
|
|
|
self.stdout.write(self.style.SUCCESS('Done'))
|