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, Heasarc2SXPS, HeasarcObjectClass from heasarc.models import NSIDE_SOURCES, ORDER from astropy.time import Time, TimezoneInfo 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): data = astropy.table.Table.read(filename, format='ascii.csv',encoding='latin1') """ keywords = data.meta['keywords'] 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 = Heasarc2SXPS.objects.all() tables.delete() utc_moscow = TimezoneInfo(utc_offset=3*u.hour) 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) tm = Time(item['FirstObsDate'], format='iso', scale='utc') FirstObsDate_dt = tm.to_datetime(timezone=utc_moscow) tm = Time(item['LastObsDate'], format='iso', scale='utc') LastObsDate_dt = tm.to_datetime(timezone=utc_moscow) tm = Time(item['FirstDetDate'], format='iso', scale='utc') FirstDetDate_dt = tm.to_datetime(timezone=utc_moscow) tm = Time(item['LastDetDate'], format='iso', scale='utc') LastDetDate_dt = tm.to_datetime(timezone=utc_moscow) healpix = 0 ra = 0.0 dec = 0.0 if not (ma.is_masked(item['RA']) or ma.is_masked(item['Decl'])): ra = float(item['RA']) dec = float(item['Decl']) crd = SkyCoord(ra, dec, frame=FK5(), unit="deg") healpix = hp.skycoord_to_healpix(crd) else: continue obj = Heasarc2SXPS.objects.create(healpix=healpix, ra=ra, dec=dec, lii=item['l'], bii=item['b'], error_radius=item['Err90'], name=item['IAUName'], Exposure = item['Exposure'], LastObsDate = LastObsDate_dt, FirstObsDate = FirstObsDate_dt, FirstDetDate = FirstDetDate_dt, LastDetDate = LastDetDate_dt, BestDetectionID = item['BestDetectionID'], DetFlag = item['DetFlag'], Rate_band0 = item['Rate_band0'], Rate_band0_pos = item['Rate_band0_pos'], Rate_band0_neg = item['Rate_band0_neg'], Rate_band1 = item['Rate_band1'], Rate_band1_pos = item['Rate_band1_pos'], Rate_band1_neg = item['Rate_band1_neg'], Rate_band2 = item['Rate_band2'], Rate_band2_pos = item['Rate_band2_pos'], Rate_band2_neg = item['Rate_band2_neg'], Rate_band3 = item['Rate_band3'], Rate_band3_pos = item['Rate_band3_pos'], Rate_band3_neg = item['Rate_band3_neg'], PeakRate_band0_pos = item['PeakRate_band0_pos'], PeakRate_band0_neg = item['PeakRate_band0_neg'], PeakRate_band1 = item['PeakRate_band1'], PeakRate_band1_pos = item['PeakRate_band1_pos'], PeakRate_band1_neg = item['PeakRate_band1_neg'], PeakRate_band2 = item['PeakRate_band2'], PeakRate_band2_pos = item['PeakRate_band2_pos'], PeakRate_band2_neg = item['PeakRate_band2_neg'], PeakRate_band3 = item['PeakRate_band3'], PeakRate_band3_pos = item['PeakRate_band3_pos'], PeakRate_band3_neg = item['PeakRate_band3_neg'], PowFlux =item['PowFlux'], PowFlux_pos= item['PowFlux_pos'], PowFlux_neg= item['PowFlux_neg'], APECFlux= item['APECFlux'], APECFlux_pos= item['APECFlux_pos'], APECFlux_neg= item['APECFlux_neg'], PowPeakFlux=item['PowPeakFlux'], PowPeakFlux_pos=item['PowPeakFlux_pos'], PowPeakFlux_neg=item['PowPeakFlux_neg'], FittedPowNH=item['FittedPowNH'], FittedPowRedChi=item['FittedPowReducedChi2'], FittedAPECNH=item['FittedAPECNH'], FittedAPECRedChi =item['FittedAPECReducedChi2']) obj.save() print('--> Successfully loaded "%s"' % filename) 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('/data/2SXPS/2SXPS_Sources.csv.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'))