176 lines
6.8 KiB
Plaintext
176 lines
6.8 KiB
Plaintext
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from datetime import date
|
|
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 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
|
|
from astrobasis.models import GAIADR2
|
|
from srglib.utils import find_counterparts
|
|
from heasarc.models import NSIDE_SOURCES, ORDER
|
|
|
|
def transfer_data(psrc,child):
|
|
print("--> Transfer data from {} (S{} E{}) to {} (S{} E{})".format(psrc,psrc.survey, psrc.band, child, child.survey, child.band))
|
|
|
|
child.owner=psrc.owner
|
|
child.notes=psrc.notes
|
|
child.redshift=psrc.redshift
|
|
child.class_id=psrc.class_id
|
|
child.object_class=psrc.object_class
|
|
child.cname=psrc.cname
|
|
child.refid=psrc.refid
|
|
child.erosita_data=psrc.erosita_data
|
|
child.gaia_primary=psrc.gaia_primary
|
|
child.class_tentative=psrc.class_tentative
|
|
child.category=psrc.category
|
|
|
|
for a in psrc.ads.all():
|
|
child.ads.add(a)
|
|
|
|
""" clone OtherName """
|
|
for o in psrc.othername_set.all():
|
|
o.source=child
|
|
o.pk=None
|
|
o.save()
|
|
|
|
child.save()
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'DEPRECATED'
|
|
|
|
# def add_arguments(self, parser):
|
|
# parser.add_argument('poll_id', nargs='+', type=int)
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
""" arcsecond """
|
|
tolerance=10
|
|
parent_version=1.1
|
|
children_version=1.2
|
|
|
|
try:
|
|
parent = ArtSurvey.objects.get(version=parent_version)
|
|
except ArtSurvey.DoesNotExist:
|
|
print("parent ArtSurvey {} not found".format(parent_version))
|
|
return
|
|
|
|
try:
|
|
children = ArtSurvey.objects.get(version=children_version)
|
|
except ArtSurvey.DoesNotExist:
|
|
print("children ArtSurvey {} not found".format(children_version))
|
|
return
|
|
|
|
print("use parent ArtSurvey {}".format(parent))
|
|
print("use children ArtSurvey {}".format(children))
|
|
|
|
""" Reset parent in children's relations """
|
|
|
|
for child in children.artsurveysource_set.all():
|
|
child.parent=None
|
|
child.owner=None
|
|
child.notes=''
|
|
child.redshift=0.0
|
|
child.class_id=0
|
|
child.object_class=None
|
|
child.cname=''
|
|
child.refid=0
|
|
child.erosita_data=False
|
|
child.gaia_primary=None
|
|
child.class_tentative=False
|
|
child.category=None
|
|
child.ads.clear()
|
|
for o in child.othername_set.all():
|
|
print("Delete {} in {}".format(o,child))
|
|
o.delete()
|
|
child.save()
|
|
|
|
|
|
for psrc in parent.artsurveysource_set.all():
|
|
c = SkyCoord(psrc.ra, psrc.dec, frame="fk5", unit="deg")
|
|
|
|
#print("{} {}".format(psrc,psrc.healpix))
|
|
try:
|
|
csrcs=children.artsurveysource_set.all().filter(healpix=psrc.healpix)
|
|
except:
|
|
#self.stdout.write(self.style.ERROR("Missed childrens for {}".format(psrc)))
|
|
print("[cent] Missed childrens for {}".format(psrc))
|
|
continue
|
|
#print("{} {} --> {} {}".format(psrc,psrc.healpix,csrc, csrc.healpix))
|
|
for csrc in csrcs:
|
|
c0 = SkyCoord(csrc.ra, csrc.dec, frame="fk5", unit="deg")
|
|
sep=c.separation(c0).arcsecond
|
|
if(sep <= tolerance):
|
|
print("[cent] Found children {} (S{} E{}) for parent {} (S{} E{}) at {:.1f}''".format(csrc,csrc.survey,csrc.band,psrc,psrc.survey,psrc.band,sep))
|
|
csrc.parent=psrc
|
|
csrc.save()
|
|
transfer_data(psrc,csrc)
|
|
else:
|
|
#print("[cent] Possible children {} for parent {} is too far away {:.1f}'' (tol {}'')".format(csrc,psrc,sep,tolerance))
|
|
pass
|
|
|
|
neib = neighbours(psrc.healpix, NSIDE_SOURCES, order=ORDER)
|
|
for heal in neib:
|
|
try:
|
|
csrcs=children.artsurveysource_set.all().filter(healpix=heal)
|
|
except:
|
|
#print("neib: Missed children for {}".format(psrc))
|
|
continue
|
|
for csrc in csrcs:
|
|
c0 = SkyCoord(csrc.ra, csrc.dec, frame="fk5", unit="deg")
|
|
sep=c.separation(c0).arcsecond
|
|
if(sep <= tolerance):
|
|
self.stdout.write(self.style.SUCCESS("[neib] Found children {} (S{} E{}) for parent {} (S{} E{}) at {:1f}''".format(csrc,csrc.survey,csrc.band,psrc,psrc.survey,psrc.band,sep)))
|
|
csrc.parent=psrc
|
|
csrc.save()
|
|
transfer_data(psrc,csrc)
|
|
else:
|
|
print("[neib] Possible children {} for parent {} is too far away {:.1f}'' (tol {}'')".format(csrc,psrc,sep,tolerance))
|
|
|
|
self.stdout.write(self.style.SUCCESS('Done'))
|
|
return
|
|
""" dalshe ne rabotaet """
|
|
""" print warning for missed sources """
|
|
for psrc in parent.artsurveysource_set.all():
|
|
if (psrc.children.all().count() == 0):
|
|
self.stdout.write(self.style.ERROR("Missed children for {}".format(psrc)))
|
|
if (psrc.children.all().count() > 1):
|
|
self.stdout.write(self.style.ERROR("Too many ({}) children for {}".format(psrc.children.all().count(), psrc)))
|
|
if (psrc.children.all().count() == 1):
|
|
|
|
""" transfer catalog information """
|
|
child = psrc.children.all()[0]
|
|
print("Transfer data from {} (S{} E{}) to {} (S{} E{})".format(psrc,psrc.survey, psrc.band, child, child.survey, child.band))
|
|
child.owner=psrc.owner
|
|
child.notes=psrc.notes
|
|
child.redshift=psrc.redshift
|
|
child.class_id=psrc.class_id
|
|
child.object_class=psrc.object_class
|
|
child.cname=psrc.cname
|
|
child.refid=psrc.refid
|
|
child.erosita_data=psrc.erosita_data
|
|
child.gaia_primary=psrc.gaia_primary
|
|
child.class_tentative=psrc.class_tentative
|
|
child.category=psrc.category
|
|
for a in psrc.ads.all():
|
|
child.ads.add(a)
|
|
""" clone OtherName """
|
|
for o in psrc.othername_set.all():
|
|
o.source=child
|
|
o.pk=None
|
|
o.save()
|
|
child.save()
|
|
|
|
self.stdout.write(self.style.SUCCESS('Done'))
|