Skip to content

Commit

Permalink
Merge branch 'main' into fix_non_editable
Browse files Browse the repository at this point in the history
  • Loading branch information
juanep97 authored Oct 10, 2024
2 parents 00dc3c5 + 858052a commit 61d5c77
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 2 deletions.
133 changes: 132 additions & 1 deletion iop4admin/modeladmins/astrosource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
iop4conf = iop4lib.Config(config_db=False)

# django imports
from django.contrib import admin
from django.contrib import admin, messages
from django.utils.html import format_html
from django.urls import path, reverse
from django.db.models import Avg
Expand All @@ -12,6 +12,10 @@
from iop4api.models import AstroSource, ReducedFit
from iop4admin import views

import numpy as np
from astroquery.mast import Catalogs
from astropy.coordinates import SkyCoord

# iop4lib imports
from iop4lib.enums import BANDS

Expand All @@ -24,6 +28,7 @@ class AdminAstroSource(admin.ModelAdmin):
list_display = ['name', 'other_names', 'ra_hms', 'dec_dms', 'srctype', 'get_last_reducedfit', 'get_last_mag_R', 'get_calibrates', 'get_comment_firstline', 'get_details']
search_fields = ['name', 'other_names', 'ra_hms', 'dec_dms', 'srctype', 'comment']
list_filter = ('srctype',)
actions = ['add_field_stars_from_panstarrs', 'remove_field_stars_from_panstarrs']

@admin.display(description='CALIBRATES')
def get_calibrates(self, obj):
Expand Down Expand Up @@ -74,3 +79,129 @@ def get_urls(self):
return my_urls + urls


@admin.action(description='Automatically add field stars from PanSTARRS')
def add_field_stars_from_panstarrs(self, request, queryset):

if queryset.count() > 1:
messages.error(request, "Only one source at a time, please.")
return

main_src = queryset.first()

# quality constraints
MIN_R_MAG = 11 # minimum R magnitude
MAX_R_MAG = 16 # maximum R magnitude
MAX_MAG_STD = 0.01 # maximum standard deviation in the required bands (we want our calibrators to be stable)
MIN_N_OBS = 5 # minimum number of observations in the required bands

logger.info(f"Querying PanSTARRS around {main_src.name} ({main_src.coord.ra.deg} {main_src.coord.dec.deg})")

try:
catalog_data = Catalogs.query_criteria(coordinates=f"{main_src.coord.ra.deg} {main_src.coord.dec.deg}", catalog="PANSTARRS", version=1, radius="0.12 deg")
except Exception as e:
logger.error(f"Error querying PanSTARRS around {main_src.name} ({main_src.coord.ra.deg} {main_src.coord.dec.deg}): {e}")
messages.error(request, f"Error querying PanSTARRS around {main_src.name}: {e}")
return

logger.info(f"Got {len(catalog_data)} PanSTARRS field stars around {main_src.name}")

column_names = ['objName', 'raMean', 'decMean',
'rMeanApMag', 'rMeanApMagErr', 'rMeanApMagStd', 'rMeanApMagNpt',
'gMeanApMag', 'gMeanApMagErr', 'gMeanApMagStd', 'gMeanApMagNpt',
'iMeanApMag', 'iMeanApMagErr', 'iMeanApMagStd', 'iMeanApMagNpt',
'yMeanApMag', 'yMeanApMagErr', 'yMeanApMagStd', 'yMeanApMagNpt']

for col in column_names:

if np.issubdtype(catalog_data.dtype[col], np.number):
idx = np.isfinite(catalog_data[col])
idx = idx.data & idx.mask
catalog_data = catalog_data[~idx]

idx = (MIN_R_MAG <= catalog_data["rMeanApMag"]) & (catalog_data["rMeanApMag"] <= MAX_R_MAG)
idx = idx & (catalog_data["rMeanApMagStd"] < MAX_MAG_STD)
idx = idx & (catalog_data["rMeanApMagNpt"] > MIN_N_OBS)
idx = idx & (catalog_data["gMeanApMagStd"] < MAX_MAG_STD)
idx = idx & (catalog_data["gMeanApMagNpt"] > MIN_N_OBS)
idx = idx & (catalog_data["iMeanApMagStd"] < MAX_MAG_STD)
idx = idx & (catalog_data["iMeanApMagNpt"] > MIN_N_OBS)
idx = idx & (catalog_data["yMeanApMagStd"] < MAX_MAG_STD)
idx = idx & (catalog_data["yMeanApMagNpt"] > MIN_N_OBS)
catalog_data = catalog_data[idx]

catalog_data.remove_columns([col for col in catalog_data.columns if col not in column_names])

logger.info(f"Filtered down to {len(catalog_data)} PanSTARRS field stars with {MIN_R_MAG} <= R <= {MAX_R_MAG}, std < {MAX_MAG_STD} and n_obs > {MIN_N_OBS}")

if len(catalog_data) == 0:
logger.error(f"No PanSTARRS field stars found for {main_src.name}, skipping")

# sort by number of observations in R, take top 10 only
if len(catalog_data) > 10:
logger.info("Keeping only top 10 field stars by number of R observations")
catalog_data.sort('rMeanApMagNpt')
catalog_data = catalog_data[-10:]

field_stars = list()

for row in catalog_data:
try:
cat_ra, cat_dec = row["raMean"], row["decMean"]
ra_hms, dec_dms = SkyCoord(cat_ra, cat_dec, unit="deg").to_string("hmsdms").split()

# Transform SDSS to Johnson-Cousins (Lupton 2005)

B = row["gMeanApMag"] + 0.3130*(row["gMeanApMag"] - row["rMeanApMag"]) + 0.2271
err_B = np.sqrt((1.313*row["gMeanApMagErr"])**2+(0.313*row["rMeanApMagErr"])**2+0.0107**2)

V = row["gMeanApMag"] - 0.5784*(row["gMeanApMag"] - row["rMeanApMag"]) - 0.0038
err_V = np.sqrt((0.4216*row["gMeanApMagErr"])**2+(0.5784*row["rMeanApMagErr"])**2+0.0054**2)

R = row["rMeanApMag"] - 0.2936*(row["rMeanApMag"] - row["iMeanApMag"]) - 0.1439
err_R = np.sqrt((0.7064*row["rMeanApMagErr"])**2+(0.2936*row["iMeanApMagErr"])**2+0.0072**2)

I = row["rMeanApMag"] - 1.2444*(row["rMeanApMag"] - row["iMeanApMag"]) - 0.3820;
err_I = np.sqrt((0.2444*row["rMeanApMagErr"])**2+(1.2444*row["iMeanApMagErr"])**2+0.0078**2)

field_star = AstroSource(name=f"PanSTARRS {cat_ra:.5f} {cat_dec:.5f}",
other_names=row["objName"],
ra_hms=ra_hms, dec_dms=dec_dms,
comment=(f"PanSTARRS field star for {main_src.name}.\n"
f"Object name: `{row['objName']}`\n"
f"Autogenerated from PanSTARRS catalog query.\n"
f"SDSS to Johnson-Cousins transformation by Lupton (2005).\n"
f"Field `other_names` corresponds to PanSTARRS `objName`.\n"),
srctype="star",
mag_B=B, mag_B_err=err_B,
mag_V=V, mag_V_err=err_V,
mag_R=R, mag_R_err=err_R,
mag_I=I, mag_I_err=err_I)

field_star.save()

field_stars.append(field_star)
except Exception as e:
logger.error(f"Error with {row['objName']}: {e}")
continue

logger.info(f"Added {len(field_stars)} PanSTARRS field stars for {main_src.name}")

main_src.calibrators.add(*field_stars)

messages.success(request, f"Added {len(field_stars)} PanSTARRS field stars for {main_src.name}")


@admin.action(description='Remove all field stars from PanSTARRS')
def remove_field_stars_from_panstarrs(self, request, queryset):

if queryset.count() > 1:
messages.error(request, "Only one source at a time, please.")
return

main_src = queryset.first()

field_stars = main_src.calibrators.filter(name__startswith="PanSTARRS")
field_stars.delete()
logger.info(f"Removed {len(field_stars)} PanSTARRS field stars for {main_src.name}")

messages.success(request, f"Removed {len(field_stars)} PanSTARRS field stars for {main_src.name}")
40 changes: 39 additions & 1 deletion iop4api/templates/iop4api/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <h2><a href="https://vhega.iaa.es/">VHEGA@IAA-CSIC</a></h2>
selectedTabs: [C1selectedTab_0, C2selectedTab_0],
// log
logEntries: [],
// plot
// plot (also uses input_astrosource in query vars)
showPlot: false,
plot_config_fast: true,
enable_full_lc: false,
Expand All @@ -121,6 +121,8 @@ <h2><a href="https://vhega.iaa.es/">VHEGA@IAA-CSIC</a></h2>
use_hostcorrected: false,
plot_config_band: 'R',
plot_config_band_options: ['R', 'V', 'B', 'U', 'I'],
input_date_start: '',
input_date_end: '',
// plot (flagging)
selected_plot_idx: [],
show_selected_plot_pts: false,
Expand Down Expand Up @@ -331,6 +333,9 @@ <h2><a href="https://vhega.iaa.es/">VHEGA@IAA-CSIC</a></h2>
},
methods: {
updateURL() {
// Get the current query parameters
const currentParams = window.location.search;

// Start forming the new URL based on the selected tabs
let newURL = '/iop4/';

Expand All @@ -354,6 +359,9 @@ <h2><a href="https://vhega.iaa.es/">VHEGA@IAA-CSIC</a></h2>
}
}

// Append the original query parameters to the new URL
newURL += currentParams;

window.history.pushState({}, '', newURL);
},
updateSelectedTabFromPath() {
Expand Down Expand Up @@ -404,6 +412,21 @@ <h2><a href="https://vhega.iaa.es/">VHEGA@IAA-CSIC</a></h2>
}
return Number(value).toFixed(precision);
},
// auto load plot from url (e.g. /iop4/explore/plot/?srcname=2200%2B420&from=2024-01-01&to=2024-09-01)
autoSubmitPlotForm() {
load_source_plot(document.getElementById('plot_astrosource_form'));
},
getPlotQueryParams() {
const params = new URLSearchParams(window.location.search);
return {
srcname: params.get('srcname') || '',
date1: params.get('from') || '',
date2: params.get('to') || '',
band: params.get('band') || 'R',
fast: params.get('fast') || 'false',
errors: params.get('errors') || 'true',
};
},
},
beforeMount() {
this.updateSelectedTabFromPath();
Expand All @@ -412,6 +435,21 @@ <h2><a href="https://vhega.iaa.es/">VHEGA@IAA-CSIC</a></h2>
window.addEventListener('popstate', () => {
this.updateSelectedTabFromPath();
});
// auto load plot from url
const params = this.getPlotQueryParams();
this.input_astrosource = params.srcname;
this.input_date_start = params.date1;
this.input_date_end = params.date2;
this.plot_config_band = params.band;
this.plot_config_fast = params.fast === 'true';
this.enable_errorbars = params.errors === 'true';

if (this.input_astrosource) { // && this.input_date_start && this.input_date_end) {
console.log('auto plotting from url with params', params);
Vue.nextTick(() => {
this.autoSubmitPlotForm();
});
}
}
}).use(Quasar).mount('#app')

Expand Down

0 comments on commit 61d5c77

Please sign in to comment.