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, HeasarcXMMSSC, HeasarcObjectClass from heasarc.models import NSIDE_SOURCES, ORDER 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'] 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 = HeasarcXMMSSC.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) obj = HeasarcXMMSSC.objects.create(healpix=healpix, ra=item['ra'], dec=item['dec'], lii=item['lii'], bii=item['bii'], error_radius=item['error_radius'], name=item['name'], detid=item['detid'], srcid=item['srcid'], time = item['time'], end_time = item['end_time'], ep_8_flux = item['ep_8_flux'], ep_8_flux_error = item['ep_8_flux_error'], pn_8_flux = item['pn_8_flux'], pn_8_flux_error = item['pn_8_flux_error'], m1_8_flux = item['m1_8_flux'], m1_8_flux_error = item['m1_8_flux_error'], m2_8_flux = item['m2_8_flux'], m2_8_flux_error = item['m2_8_flux_error'], ep_1_flux = item['ep_1_flux'], ep_1_flux_error = item['ep_1_flux_error'], ep_2_flux = item['ep_2_flux'], ep_2_flux_error = item['ep_2_flux_error'], ep_3_flux = item['ep_3_flux'], ep_3_flux_error = item['ep_3_flux_error'], sum_flag = item['sum_flag'], sc_extent = item['sc_extent'], sc_ext_ml = item['sc_ext_ml']) 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): # create sqlalchemy engine # engine = create_engine("mysql+pymysql://{user}:{pw}@localhost/{db}" # .format(user="heauser", # pw="srg2019@L2_heasarc", # db="heasarc_db")) # engine = create_engine("postgresql://{user}:{pw}@localhost/{db}" # .format(user="heauser", # pw="srg2019@L2_heasarc", # db="heasarc_db")) load_heasarc_table('heasarc_xmmssc.tdat.gz') return hp = HEALPix(nside=NSIDE_SOURCES, order=ORDER, frame=FK5()) # update healpix only srcs = HeasarcXMMSSC.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'))