Skip to content

Commit 4a4a889

Browse files
authored
Minor improvements (#176)
- Improve astrometry parameters for blue filters. - Fix aperture photometry preview cutout size in admin. - Increase number of calibrators added from PanSTARRS. - Allow filtering by instruments and date with `--astrosource`.
1 parent 18fb22f commit 4a4a889

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

iop4admin/modeladmins/astrosource.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from astropy.coordinates import SkyCoord
1818

1919
# iop4lib imports
20-
from iop4lib.enums import BANDS
20+
from iop4lib.enums import BANDS, SRCTYPES
2121

2222
# logging
2323
import logging
@@ -169,16 +169,18 @@ def add_field_stars_from_panstarrs(self, request, queryset):
169169

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

172-
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}")
172+
n_valid = len(catalog_data)
173173

174-
if len(catalog_data) == 0:
174+
logger.info(f"Filtered down to {n_valid} PanSTARRS field stars with {MIN_R_MAG} <= R <= {MAX_R_MAG}, std < {MAX_MAG_STD} and n_obs > {MIN_N_OBS}")
175+
176+
if n_valid == 0:
175177
logger.error(f"No PanSTARRS field stars found for {main_src.name}, skipping")
176178

177179
# sort by number of observations in R, take top 10 only
178-
if len(catalog_data) > 10:
179-
logger.info("Keeping only top 10 field stars by number of R observations")
180+
if n_valid > 30:
181+
logger.info("Keeping only top 30 field stars by number of R observations")
180182
catalog_data.sort('rMeanApMagNpt')
181-
catalog_data = catalog_data[-10:]
183+
catalog_data = catalog_data[-30:]
182184

183185
field_stars = list()
184186

@@ -209,7 +211,7 @@ def add_field_stars_from_panstarrs(self, request, queryset):
209211
f"Autogenerated from PanSTARRS catalog query.\n"
210212
f"SDSS to Johnson-Cousins transformation by Lupton (2005).\n"
211213
f"Field `other_names` corresponds to PanSTARRS `objName`.\n"),
212-
srctype="star",
214+
srctype=SRCTYPES.STAR,
213215
mag_B=B, mag_B_err=err_B,
214216
mag_V=V, mag_V_err=err_V,
215217
mag_R=R, mag_R_err=err_R,
@@ -222,11 +224,11 @@ def add_field_stars_from_panstarrs(self, request, queryset):
222224
logger.error(f"Error with {row['objName']}: {e}")
223225
continue
224226

225-
logger.info(f"Added {len(field_stars)} PanSTARRS field stars for {main_src.name}")
227+
logger.info(f"Added {len(field_stars)} out of {n_valid} PanSTARRS field stars for {main_src.name}")
226228

227229
main_src.calibrators.add(*field_stars)
228230

229-
messages.success(request, f"Added {len(field_stars)} PanSTARRS field stars for {main_src.name}")
231+
messages.success(request, f"Added {len(field_stars)} out of {n_valid} PanSTARRS field stars for {main_src.name}")
230232

231233

232234
@admin.action(description='Remove all field stars from PanSTARRS')
@@ -243,4 +245,4 @@ def remove_field_stars_from_panstarrs(self, request, queryset):
243245
field_stars.delete()
244246
logger.info(f"Removed {n_field_stars} PanSTARRS field stars for {main_src.name}")
245247

246-
messages.success(request, f"Removed {n_field_stars} PanSTARRS field stars for {main_src.name}")
248+
messages.success(request, f"Removed {n_field_stars} PanSTARRS field stars for {main_src.name}")

iop4lib/db/aperphotresult.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ def get_img(self, force_rebuild=True, **kwargs):
103103

104104
wcs = self.reducedfit.wcs1 if self.pairs == 'O' else self.reducedfit.wcs2
105105

106+
cutout_size = np.ceil(2.2*self.r_out)
107+
106108
if self.reducedfit.has_pairs:
107-
cutout_size = np.ceil(2.2*np.linalg.norm(Instrument.by_name(self.reducedfit.instrument).disp_sign_mean))
108-
else:
109-
cutout_size = np.ceil(1.3*self.r_out)
109+
cutout_size = max(cutout_size, 2.2*np.ceil(np.linalg.norm(Instrument.by_name(self.reducedfit.instrument).disp_sign_mean)))
110110

111111
cutout = Cutout2D(self.reducedfit.mdata, (self.x_px, self.y_px), (cutout_size, cutout_size), wcs)
112112

@@ -124,6 +124,8 @@ def get_img(self, force_rebuild=True, **kwargs):
124124
with open(fpath, 'rb') as f:
125125
return f.read()
126126

127+
logger.info(f"Building image preview for AperPhotResult {self.id}.")
128+
127129
cmap = plt.cm.gray.copy()
128130
cmap.set_bad(color='red')
129131
cmap.set_under(color='black')
@@ -134,7 +136,6 @@ def get_img(self, force_rebuild=True, **kwargs):
134136
elif normtype == "logstretch":
135137
norm = ImageNormalize(stretch=LogStretch(a=a))
136138

137-
138139
buf = io.BytesIO()
139140

140141
fig = mplt.figure.Figure(figsize=(width/100, height/100), dpi=iop4conf.mplt_default_dpi)
@@ -165,4 +166,4 @@ def get_img(self, force_rebuild=True, **kwargs):
165166
with open(fpath, 'wb') as f:
166167
f.write(imgbytes)
167168

168-
return imgbytes
169+
return imgbytes

iop4lib/iop4.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,38 @@ def process_astrosource(args):
103103

104104
astrosource = AstroSource.objects.get(name=args.astrosource)
105105

106-
qs_all = ReducedFit.objects.filter(epoch__night__gte=args.date_start, epoch__night__lte=args.date_end)
106+
qs_redf = ReducedFit.objects.all()
107107

108-
logger.info(f"Found {qs_all.count()} reduced fits between {args.date_start} and {args.date_end}.")
108+
logger.debug(f"Found {qs_redf.count()} files in the DB")
109+
110+
if args.date_start:
111+
qs_redf = qs_redf.filter(epoch__night__gte=args.date_start)
112+
logger.debug(f"After {args.date_start}, {qs_redf.count()} files")
113+
114+
if args.date_end:
115+
qs_redf = qs_redf.filter(epoch__night__lte=args.date_end)
116+
logger.debug(f"Before {args.date_end}, {qs_redf.count()} files")
117+
118+
119+
if args.instruments:
120+
qs_redf = qs_redf.filter(instrument__in=args.instruments)
121+
logger.debug(f"For {args.instruments}, {qs_redf.count()} files")
122+
123+
logger.info(f"Found {qs_redf.count()} reduced fits.")
109124

110125
if args.only_sources_in_field:
111-
qs_all = qs_all.filter(sources_in_field__name=args.astrosource)
112-
redfL = list(qs_all)
126+
qs_redf = qs_redf.filter(sources_in_field__name=args.astrosource)
127+
redfL = list(qs_redf)
113128
else:
114129
# filter files that have identified this source (in sources_in_field) or have this source as header_hintobject
115130
redfL = list()
116-
for redf in qs_all:
131+
for redf in qs_redf:
117132
if redf.sources_in_field.filter(name=args.astrosource).exists() or redf.header_hintobject == astrosource:
118133
redfL.append(redf)
119134

120135
qs_redf = ReducedFit.objects.filter(pk__in=[redf.pk for redf in redfL])
121136

122-
logger.info(f"Found {len(redfL)} reduced fits for astrosource {args.astrosource}")
137+
logger.info(f"Selected {len(redfL)} reduced fits for astrosource {args.astrosource}")
123138

124139
if args.force_rebuild:
125140
logger.info("Forcing rebuild of reduced fits.")
@@ -174,6 +189,8 @@ def process_astrosource(args):
174189
except Exception as e:
175190
logger.exception(f"Error computing host galaxy correction for {result}.")
176191

192+
logger.info("Done.")
193+
177194
def list_local_epochnames() -> list[str]:
178195
"""List all local epochnames in local archives (by looking at the raw directory)."""
179196

@@ -363,6 +380,7 @@ def main():
363380

364381
# astrosource processing options
365382
parser.add_argument('--astrosource', type=str, default=None, help='<Optional> Select files only of this source')
383+
parser.add_argument('--instruments', nargs='+', help='<Optional> List of instruments')
366384
parser.add_argument('--recompute', action='store_true', help='<Optional> Recompute photometry and polarimetry results')
367385
parser.add_argument('--retry-failed', action='store_true', help='<Optional> Retry failed reduced fits')
368386
parser.add_argument('--only-sources-in-field', action='store_true', help='<Optional> Only process files that have this source in sources_in_field')
@@ -502,7 +520,7 @@ def main():
502520
if args.date_start is not None or args.date_end is not None:
503521
logger.info("Filtering files by date...")
504522
filelocs_to_process = filter_filelocs_by_date(filelocs_to_process, args.date_start, args.date_end)
505-
logger.info(f"Filtered to {len(filelocs_to_process)} filelocs_to_process between {args.date_start} and {args.date_end}.")
523+
logger.info(f"Filtered to {len(filelocs_to_process)} filelocs_to_process between {args.date_start} and {args.date_end}.")
506524

507525
filelocs_missing = set(filelocs_to_process).intersection(filelocs_missing)
508526

iop4lib/utils/astrometry.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from iop4lib.utils.sourcepairing import (get_pairs_d, get_pairs_dxy, get_best_pairs)
2727
from iop4lib.utils.sourcedetection import (get_bkg, get_segmentation, get_cat_sources_from_segment_map)
2828
from iop4lib.utils.plotting import build_astrometry_summary_images
29+
from iop4lib.enums import BANDS
2930

3031
# logging
3132

@@ -129,6 +130,12 @@ def build_wcs_params_shotgun(redf: 'ReducedFit', shotgun_params_kwargs : dict =
129130
params["npixels"] = [32, 8, 16]
130131
params["allsky"] = [False]
131132

133+
if redf.band == BANDS.B or redf.band == BANDS.U:
134+
params["n_rms_seg"] = [0.8, 0.66]
135+
params["npixels"] = [32, 16, 8]
136+
params["bkg_filter_size"] = [5, 7]
137+
params["bkg_box_size"] = [8]
138+
132139
## Substitute default params combinations with specified ones
133140

134141
if shotgun_params_kwargs is not None:

0 commit comments

Comments
 (0)