from django.shortcuts import render from django.db.models import F from django.db.models import Q from django.db.models import DecimalField, Value from django.db.models import Max, Subquery, OuterRef, Min, Case, When, Min, Func from django.db.models.functions.math import Sqrt, Cos, Power, Abs from django.shortcuts import get_object_or_404 from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden from astropy import units as u from astropy.coordinates import SkyCoord from astropy.coordinates import ICRS, Galactic, FK5 from astropy.coordinates import BarycentricTrueEcliptic from astropy.coordinates import HeliocentricTrueEcliptic from astropy.coordinates import GeocentricTrueEcliptic from astropy_healpix import HEALPix from heasarc.utils import healpix_usercat from heasarc.utils import match_usercat_all from heasarc.models import HeasarcTable from heasarc.models import TableColumn, HeasarcObjectClass, HeasarcBase from heasarc.models import HeasarcXrayMaster, HeasarcXMMSSC from heasarc.models import HeasarcUserCat, HeasarcUserCatForm from heasarc.models import HeasarcALLWISEAGN, HeasarcCHANMASTER from heasarc.models import HeasarcCSC, HeasarcRASS2RXS, Heasarc2SXPS from heasarc.models import Heasarc4XMMDR9 from heasarc.models import Heasarc4XMMDR12 from heasarc.models import HeasarcXMMSL2 from heasarc.models import Heasarc3MAXI from heasarc.models import HeasarcMAXIGSCHGL from srgcat.models import ArtCat from srglib.utils import make_source_name from astrobasis.models import GAIADR3 from astrobasis.models import VLASS from astrobasis.models import GLIMPSE from astrobasis.models import AllWise from astrobasis.models import TwoMASS from artsurvey.models import ArtSurveySource # Create your views here. def index(request): tables = HeasarcTable.objects.all() xmmssc = HeasarcBase.objects.instance_of(HeasarcXMMSSC) xray = HeasarcBase.objects.instance_of(HeasarcXrayMaster) chanmaster = HeasarcBase.objects.instance_of(HeasarcCHANMASTER) csc = HeasarcBase.objects.instance_of(HeasarcCSC) rass2rxs = HeasarcBase.objects.instance_of(HeasarcRASS2RXS) twosxps = HeasarcBase.objects.instance_of(Heasarc2SXPS) allwiseagn = HeasarcBase.objects.instance_of(HeasarcALLWISEAGN) xmmdr9 = HeasarcBase.objects.instance_of(Heasarc4XMMDR9) xmmssc_count = xmmssc.count() xmmdr9_count = xmmdr9.count() chanmaster_count = chanmaster.count() csc_count = csc.count() rass2rxs_count = rass2rxs.count() xray_count = xray.count() twosxps_count = twosxps.count() allwiseagn_count = allwiseagn.count() return render(request,'heasarc/index.html', {'tables': tables, 'xmmdr9_count':xmmdr9_count, 'xmmssc_count':xmmssc_count, 'chanmaster_count':chanmaster_count, 'csc_count':csc_count,'twosxps_count':twosxps_count, 'allwiseagn_count':allwiseagn_count, 'rass2rxs_count':rass2rxs_count,'xray_count':xray_count,}) def show_object_classes(request): classes = HeasarcObjectClass.objects.all().order_by('class_id') #.exclude(class_name='UNCLASSIFIED').distinct('class_name') return render(request,'heasarc/show_object_classes.html', {'classes': classes,}) def show_table(request, table_id): try: table=HeasarcTable.objects.get(pk=table_id) except: return HttpResponse("This table was not found") columns = table.tablecolumn_set.all() tables = HeasarcTable.objects.all() return render(request,'heasarc/index.html', {'tables': tables,'table':table,'columns':columns}) def show_heasarc_xray(request, pk_id): try: heasarc=HeasarcXrayMaster.objects.get(pk=pk_id) except: return HttpResponse("This HeasarcXrayMaster was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() return render(request,'heasarc/show_heasarc_xray.html', {'heasarc':heasarc, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def show_heasarc_xmmssc(request, pk_id): try: heasarc=HeasarcXMMSSC.objects.get(pk=pk_id) except: return HttpResponse("This HeasarcXMMSSC was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() return render(request,'heasarc/show_heasarc_xmmssc.html', {'heasarc':heasarc, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def show_heasarc_4xmmdr12(request, pk_id): try: heasarc=Heasarc4XMMDR12.objects.get(pk=pk_id) except: return HttpResponse("Object was not found") return render(request,'heasarc/show_heasarc_4xmmdr12.html', {'heasarc':heasarc,}) def show_heasarc_3maxi(request, pk_id): try: heasarc=Heasarc3MAXI.objects.get(pk=pk_id) except: return HttpResponse("This Heasarc3MAXI was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() return render(request,'heasarc/show_heasarc_3maxi.html', {'heasarc':heasarc, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def show_heasarc_xmmsl2(request, pk_id): try: src=HeasarcXMMSL2.objects.get(pk=pk_id) except: return HttpResponse("This HeasarcXMMSL2 was not found") crd = SkyCoord(src.ra, src.dec, frame="fk5", unit="deg") gaia = src.gaia3.all() gaia_ex = gaia.exclude(Q(pmra__abs__gt=F("pmra_error")*Value(4.)) | Q(pmdec__abs__gt=F("pmdec_error")*Value(4.)) | Q(parallax__abs__gt=F("parallax_error")*Value(4.))) gaia_ex = gaia_ex.annotate(separation=Value(0.0, output_field=DecimalField())) for gaiasrc in gaia_ex: gaia_crd=SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg") gaiasrc.separation=crd.separation(SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg")).arcsecond gaia_pm = gaia.filter(Q(pmra__abs__gt=F("pmra_error")*Value(4.)) | Q(pmdec__abs__gt=F("pmdec_error")*Value(4.))) gaia_pm = gaia_pm.annotate(separation=Value(0.0, output_field=DecimalField())) for gaiasrc in gaia_pm: gaia_crd=SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg") gaiasrc.separation=crd.separation(gaia_crd).arcsecond gaia_plx = gaia.filter(Q(parallax__abs__gt=F("parallax_error")*Value(4.))) gaia_plx = gaia_plx.annotate(separation=Value(0.0, output_field=DecimalField())) for gaiasrc in gaia_plx: gaia_crd=SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg") gaiasrc.separation=crd.separation(SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg")).arcsecond return render(request,'heasarc/show_heasarc_xmmsl2.html', {'src':src,'gaia_plx':gaia_plx,'gaia_pm':gaia_pm,'gaia_ex':gaia_ex}) def annotate_separation(srcs, source_crd): srcs = srcs.annotate(separation=Value(0.0, output_field=DecimalField())) for src in srcs: crd = SkyCoord(src.ra, src.dec, frame="fk5", unit="deg") src.separation=source_crd.separation(crd).arcsecond return srcs def show_heasarc_base(request, source_id): src = get_object_or_404(HeasarcBase, pk=source_id) crd = SkyCoord(src.ra, src.dec, frame="fk5", unit="deg") artsurveysources_listed = src.artsurveysource_set.all() artsurveysources_linked = src.artsurveysource_linked.all() gaia = src.gaia3.all() usercat = annotate_separation(src.usercat.all(), crd) vlass = annotate_separation(src.vlass.all(), crd) glimpse = annotate_separation(src.glimpse.all(), crd) twomass = annotate_separation(src.twomass.all(), crd) allwise = annotate_separation(src.allwise.all(), crd) gaia_ex = gaia.exclude(Q(pmra__abs__gt=F("pmra_error")*Value(4.)) | Q(pmdec__abs__gt=F("pmdec_error")*Value(4.)) | Q(parallax__abs__gt=F("parallax_error")*Value(4.))) gaia_ex = gaia_ex.annotate(separation=Value(0.0, output_field=DecimalField())) for gaiasrc in gaia_ex: gaia_crd=SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg") gaiasrc.separation=crd.separation(SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg")).arcsecond gaia_pm = gaia.filter(Q(pmra__abs__gt=F("pmra_error")*Value(4.)) | Q(pmdec__abs__gt=F("pmdec_error")*Value(4.))) gaia_pm = gaia_pm.annotate(separation=Value(0.0, output_field=DecimalField())) for gaiasrc in gaia_pm: gaia_crd=SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg") gaiasrc.separation=crd.separation(gaia_crd).arcsecond gaia_plx = gaia.filter(Q(parallax__abs__gt=F("parallax_error")*Value(4.))) gaia_plx = gaia_plx.annotate(separation=Value(0.0, output_field=DecimalField())) for gaiasrc in gaia_plx: gaia_crd=SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg") gaiasrc.separation=crd.separation(SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg")).arcsecond return render(request,'heasarc/show_heasarc_base.html', {'src':src,'usercat':usercat, 'vlass':vlass, 'glimpse':glimpse, 'twomass':twomass, 'allwise':allwise, 'artsurveysources_listed':artsurveysources_listed, 'artsurveysources_linked':artsurveysources_linked, 'gaia_plx':gaia_plx,'gaia_pm':gaia_pm,'gaia_ex':gaia_ex}) def show_heasarc_4xmm(request, pk_id): try: heasarc=Heasarc4XMMDR9.objects.get(pk=pk_id) except: return HttpResponse("This Heasarc4XMMDR9 was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() return render(request,'heasarc/show_heasarc_4xmm.html', {'src':heasarc, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def show_heasarc_chanmaster(request, pk_id): try: heasarc=HeasarcCHANMASTER.objects.get(pk=pk_id) except: return HttpResponse("This HeasarcCHANMASTER was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() return render(request,'heasarc/show_heasarc_chanmaster.html', {'heasarc':heasarc, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def show_heasarc_csc(request, pk_id): try: heasarc=HeasarcCSC.objects.get(pk=pk_id) except: return HttpResponse("This HeasarcCSC was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() gaia = heasarc.gaia3.all() gaia_plx = gaia.filter(Q(parallax__abs__gt=F("parallax_error")*Value(4.))) gaia_plx = gaia_plx.annotate(separation=Value(0.0, output_field=DecimalField())) gaia_plx = gaia_plx.annotate(erosita_separation=Value(0.0, output_field=DecimalField())) for gaiasrc in gaia_plx: gaia_crd=SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg") gaiasrc.separation=c.separation(SkyCoord(gaiasrc.ra, gaiasrc.dec, frame="fk5", unit="deg")).arcsecond if not erosita is None: gaiasrc.erosita_separation=erosita_crd.separation(gaia_crd).arcsecond return render(request,'heasarc/show_heasarc_csc.html', {'heasarc':heasarc, 'gaia_plx':gaia_plx, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def show_heasarc_rass2rxs(request, pk_id): try: heasarc=HeasarcRASS2RXS.objects.get(pk=pk_id) except: return HttpResponse("This HeasarcRASS2RXS was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() return render(request,'heasarc/show_heasarc_rass2rxs.html', {'heasarc':heasarc, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def show_heasarc_maxi_gsc(request, pk_id): try: heasarc=HeasarcMAXIGSCHGL.objects.get(pk=pk_id) except: return HttpResponse("This HeasarcRASS2RXS was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() return render(request,'heasarc/show_heasarc_maxi_gsc.html', {'heasarc':heasarc, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def show_heasarc_allwiseagn(request, pk_id): try: heasarc=HeasarcALLWISEAGN.objects.get(pk=pk_id) except: return HttpResponse("This HeasarcALLWISEAGN was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() return render(request,'heasarc/show_heasarc_allwiseagn.html', {'heasarc':heasarc, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def show_heasarc_2sxps(request, pk_id): try: heasarc=Heasarc2SXPS.objects.get(pk=pk_id) except: return HttpResponse("This Heasarc2SXPS was not found") erotrans_set = heasarc.erotranssource_set.all() erosurvey_set = heasarc.erosurveysource_set.all() artcat_set = heasarc.artsource_set.all() return render(request,'heasarc/show_heasarc_2sxps.html', {'heasarc':heasarc, 'artcat_set':artcat_set, 'erosurvey_set':erosurvey_set, 'erotrans_set':erotrans_set,}) def set_gaia(request,source_id,gaia_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.gaia3_primary = get_object_or_404(GAIADR3, pk=gaia_id) src.save() return HttpResponseRedirect(src.get_base_url()) def clean_gaia(request,source_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.gaia3_primary=None src.save() return HttpResponseRedirect(src.get_base_url()) def set_vlass(request,source_id,vlass_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.vlass_primary = get_object_or_404(VLASS, pk=vlass_id) src.save() return HttpResponseRedirect(src.get_base_url()) def clean_vlass(request,source_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.vlass_primary=None src.save() return HttpResponseRedirect(src.get_base_url()) def set_glimpse(request,source_id,glimpse_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.glimpse_primary = get_object_or_404(GLIMPSE, pk=glimpse_id) src.save() return HttpResponseRedirect(src.get_base_url()) def clean_glimpse(request,source_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.glimpse_primary=None src.save() return HttpResponseRedirect(src.get_base_url()) def set_twomass(request,source_id,twomass_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.twomass_primary = get_object_or_404(TwoMASS, pk=twomass_id) src.save() return HttpResponseRedirect(src.get_base_url()) def clean_twomass(request,source_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.twomass_primary=None src.save() return HttpResponseRedirect(src.get_base_url()) def set_allwise(request,source_id,allwise_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.allwise_primary = get_object_or_404(AllWise, pk=allwise_id) src.save() return HttpResponseRedirect(src.get_base_url()) def clean_allwise(request,source_id): src = get_object_or_404(HeasarcBase, pk=source_id) src.allwise_primary=None src.save() return HttpResponseRedirect(src.get_base_url()) def update_heasarc_usercat(request,pk_id,source_id): src = get_object_or_404(ArtSurveySource, pk=source_id) obj = get_object_or_404(HeasarcUserCat, pk=pk_id) srcs = obj.artsurveysource_set.all() srcs_linked = obj.artsurveysource_linked.all() if request.method == 'POST': # If the form has been submitted... form = HeasarcUserCatForm(request.POST,instance=obj) # A form bound to the POST data if form.is_valid(): # All validation rules pass new_form=form.save(commit=False) ra=float(form.cleaned_data['ra']) dec=float(form.cleaned_data['dec']) name = form.cleaned_data['name'] if (form.cleaned_data['name']) else 'NONAME' autoname=form.cleaned_data['autoname'] if(autoname): new_form.name=make_source_name(name,ra,dec) new_form.save() healpix_usercat(new_form) match_usercat_all(new_form) #category = form.cleaned_data['category'] #new_form.category=category #new_form.save() return HttpResponseRedirect(src.get_absolute_url()) else: return render(request,'artsurvey/usercat.html', {'form':form,'src':src,'srcs':srcs,'srcs_linked':srcs_linked,}) else: form = HeasarcUserCatForm(instance=obj) return render(request,'artsurvey/usercat.html', {'form':form,'obj':obj,'src':src,'srcs':srcs,'srcs_linked':srcs_linked,}) def delete_heasarc_usercat(request,pk_id,source_id): src = get_object_or_404(ArtSurveySource, pk=source_id) obj = get_object_or_404(HeasarcUserCat, pk=pk_id) obj.delete() return HttpResponseRedirect(src.get_absolute_url())