srg/artsurvey/management/commands/00_artsurvey_transfer.py
2024-04-26 12:43:00 +03:00

189 lines
7.0 KiB
Python

from django.core.management.base import BaseCommand, CommandError
from django.db.models import F, Q
from datetime import date
import datetime
from django.utils import timezone
import astropy
from astropy.io import ascii
import pandas as pd
import pymysql
import numpy as np
from sqlalchemy import create_engine
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.coordinates import ICRS, Galactic, FK5
from astropy_healpix import HEALPix, neighbours
from heasarc.models import HeasarcBase
from artsurvey.models import ArtSurveySource, ArtSurvey, EnergyBand
from astrobasis.models import GAIADR2
from srglib.utils import find_counterparts
from srglib.utils import crossmatch
from srglib.utils import transfer
from heasarc.models import NSIDE_SOURCES, ORDER
def do_transfer(parent, child_version, child_name, slug, minrad=30, maxdist=500):
""" the same band assumed for parent and child """
try:
band = EnergyBand.objects.get(slug=slug)
except EnergyBand.DoesNotExist:
print("EnergyBand bot found")
return
print("use EnergyBand {}".format(band))
try:
s1 = ArtSurvey.objects.get(Q(version=parent)) # & Q(archived=True))
except ArtSurvey.DoesNotExist:
print("ArtSurvey {} not found".format(parent))
return
try:
s2 = ArtSurvey.objects.get(Q(version=child_version) & Q(name=child_name))
except ArtSurvey.DoesNotExist:
print("ArtSurvey {} not found".format(child_version))
return
srcs1=s1.artsurveysource_set.all().filter(band=band)
srcs2=s2.artsurveysource_set.all().filter(band=band)
print("[{}] use parent ArtSurvey {} nrows={}".format(slug, s1, srcs1.count()))
print("[{}] use child ArtSurvey {} nrows={}".format(slug, s2, srcs2.count()))
match_count = transfer(srcs1, srcs2, minrad=minrad, maxdist=maxdist)
print("Total matched {}".format(match_count))
def do_transfer_band(parent, parent_band, child_version, child_name, child_band, minrad=30, maxdist=500):
""" the same band assumed for parent and child """
try:
band1 = EnergyBand.objects.get(slug=parent_band)
except EnergyBand.DoesNotExist:
print("EnergyBand not found")
return
try:
band2 = EnergyBand.objects.get(slug=child_band)
except EnergyBand.DoesNotExist:
print("EnergyBand not found")
return
print("use EnergyBand {} --> {}".format(band1, band2))
try:
s1 = ArtSurvey.objects.get(Q(version=parent)) # & Q(archived=True))
except ArtSurvey.DoesNotExist:
print("ArtSurvey {} not found".format(parent))
return
try:
s2 = ArtSurvey.objects.get(Q(version=child_version) & Q(name=child_name))
except ArtSurvey.DoesNotExist:
print("ArtSurvey {} not found".format(child_version))
return
srcs1=s1.artsurveysource_set.all().filter(band=band1)
srcs2=s2.artsurveysource_set.all().filter(band=band2)
print("[{}] use parent ArtSurvey {} nrows={}".format(child_band, s1, srcs1.count()))
print("[{}] use child ArtSurvey {} nrows={}".format(child_band, s2, srcs2.count()))
match_count = transfer(srcs1, srcs2, minrad=minrad, maxdist=maxdist)
print("Total matched {}".format(match_count))
def do_transfer_by_name(parent_version, parent_name, child_version, child_name, slug, minrad=30, maxdist=500):
try:
band = EnergyBand.objects.get(slug=slug)
except EnergyBand.DoesNotExist:
print("EnergyBand bot found")
return
print("use EnergyBand {}".format(band))
try:
s1 = ArtSurvey.objects.get(Q(version=parent_version) & Q(name=parent_name))
except ArtSurvey.DoesNotExist:
print("*** parent ArtSurvey {} {} not found".format(parent_name,parent_version))
return
try:
s2 = ArtSurvey.objects.get(Q(version=child_version) & Q(name=child_name))
except ArtSurvey.DoesNotExist:
print("*** child ArtSurvey {} not found".format(child_version))
return
srcs1=s1.artsurveysource_set.all().filter(band=band)
srcs2=s2.artsurveysource_set.all().filter(band=band)
print("[{}] use parent ArtSurvey {} nrows={}".format(slug, s1, srcs1.count()))
print("[{}] use child ArtSurvey {} nrows={}".format(slug, s2, srcs2.count()))
match_count = transfer(srcs1, srcs2, minrad=minrad, maxdist=maxdist)
print("Total matched {}".format(match_count))
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):
""" arcsecond """
minrad=30.0
maxdist=500.0
"""
bands = ['E0','E1','E2','E3']
for slug in bands:
print (slug)
do_transfer(12.4, 12.5, slug, minrad=minrad, maxdist=maxdist)
do_transfer(1.6, 1.7, 'E0', minrad=minrad, maxdist=maxdist)
do_transfer(2.6, 2.7, 'E0', minrad=minrad, maxdist=maxdist)
"""
#do_transfer(12.5, 1234.0, 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer(12.5, 3.0, 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer(12.5, 34.0,"34", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer(12.5, 5.0,"5", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer(1234.0, 12345.0,"1-5", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer(12345.0, 12345.1,"1-5", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer(12345.0, 12345.11,"1-5", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer(12345.11, 12345.12,"1-5", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer_band(12345.11, "E0", 12345.12,"1-5", 'E5', minrad=minrad, maxdist=maxdist)
#do_transfer_band(12345.12, "E5", 12345.13,"1-5", 'E5', minrad=minrad, maxdist=maxdist)
do_transfer_band(12345.13, "E5", 12345.14,"1-5", 'E5', minrad=minrad, maxdist=maxdist)
#names=['GP025','GP020','GP015','GP010','GP005','GP000','GP355','GP350','GP345',]
#names=['GP055','GP060','GP065']
names=['GP070','GP075','GP080','GP085','GP090', 'GP270','GP275','GP280','GP285','GP290','GP295','GP300','GP305','GP310','GP215','GP320','GP325']
"""
for name in names:
do_transfer(12.5, 0.11, name, 'E0', minrad=minrad, maxdist=maxdist)
"""
"""
names=['GP025','GP020','GP015','GP010','GP005','GP000','GP355','GP350','GP345',]
for name in names:
do_transfer_by_name(0.10, name, 0.11, name, 'E0', minrad=minrad, maxdist=maxdist)
"""
#do_transfer_by_name(0.10, "L20", 0.12, "L20", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer_by_name(0.10, "L20", 0.12, "L20p1", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer_by_name(0.10, "L20", 0.12, "L20p2", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer_by_name(0.10, "L20", 0.12, "L20p3", 'E0', minrad=minrad, maxdist=maxdist)
#do_transfer_by_name(0.10, "L20", 0.12, "L20p4", 'E0', minrad=minrad, maxdist=maxdist)
self.stdout.write(self.style.SUCCESS('Done'))