112 lines
3.9 KiB
Python
112 lines
3.9 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, math
|
|
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, HeasarcMAXI7YR, 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 = HeasarcMAXI7YR.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)
|
|
"""
|
|
this is correct only for 1 dimention
|
|
|
|
erf(1.65/sqrt(2)) corresponds to 90%
|
|
|
|
Чтобы получить 90% из 1 сигма, надо использовать коэффициент 1.42 (sqrt(4.61/2.3))
|
|
|
|
"""
|
|
P=0.98
|
|
koeff=math.sqrt(-2*math.log(1-P))
|
|
sign = float(item['detsig_4_10'])
|
|
error_radius_1sigma=4800/sign
|
|
if not (ma.is_masked(item['error_radius'])):
|
|
error_radius_1sigma=float(item['error_radius'])
|
|
|
|
#if (ma.is_masked(item['error_radius'])):
|
|
print("{} {} {} --> {}".format(item['name'], item['error_radius'], item['catalog'], error_radius_1sigma*koeff))
|
|
|
|
"""
|
|
We save 90% confidence interval for error_radius
|
|
"""
|
|
obj = HeasarcMAXI7YR.objects.create(healpix=healpix,
|
|
healpix_plate=healpix_plate,
|
|
ra=item['ra'],
|
|
dec=item['dec'],
|
|
lii=item['lii'],
|
|
bii=item['bii'],
|
|
error_radius=error_radius_1sigma*koeff,
|
|
name=item['name'],
|
|
flux=item['flux_4_10'],sign=item['detsig_4_10'],)
|
|
|
|
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_maxigsc7yr.tdat.gz')
|
|
|
|
self.stdout.write(self.style.SUCCESS('Done'))
|