diff --git a/iop4lib/iop4.py b/iop4lib/iop4.py index 014ea280..90d743a8 100644 --- a/iop4lib/iop4.py +++ b/iop4lib/iop4.py @@ -100,6 +100,73 @@ def process_epochs(epochname_list: Iterable[str], args): logger.info("Done.") +def process_astrosource(args): + from iop4lib.db import ReducedFit, AstroSource, PhotoPolResult, Epoch + + astrosource = AstroSource.objects.get(name=args.astrosource) + qs_all = ReducedFit.objects.filter(epoch__night__gte=args.date_start, epoch__night__lte=args.date_end) + + logger.info(f"Found {qs_all.count()} reduced fits between {args.date_start} and {args.date_end}.") + + # filter files that have identified this source (in sources_in_field) or have this source as header_hintobject + + redfL = list() + for redf in qs_all: + if redf.sources_in_field.filter(name=args.astrosource).exists() or redf.header_hintobject == astrosource: + redfL.append(redf) + + qs_redf = ReducedFit.objects.filter(pk__in=[redf.pk for redf in redfL]) + + logger.info(f"Found {len(redfL)} reduced fits for astrosource {args.astrosource}") + + if args.force_rebuild: + logger.info("Forcing rebuild of reduced fits.") + Epoch.reduce_reducedfits(redfL) + + if args.retry_failed: + logger.info("Retrying failed reduced fits.") + redfL_failed = [redf for redf in redfL if redf.has_flag(ReducedFit.FLAGS.ERROR_ASTROMETRY)] + Epoch.reduce_reducedfits(redfL_failed) + + if args.recompute: + qs_res = PhotoPolResult.objects.filter(reducedfits__in=redfL) + n_res_before = qs_res.count() + + logger.info(f"Recomputing {n_res_before} results.") + + deletion = qs_res.delete() + + logger.info(f"Deleted {deletion[0]} results.") + + epoch_L = set([redf.epoch for redf in redfL]) + + for epoch in epoch_L: + epoch.compute_relative_photometry(qs_redf) + epoch.compute_relative_polarimetry(qs_redf) + + qs_res = PhotoPolResult.objects.filter(reducedfits__in=redfL) + n_res_after = qs_res.count() + + logger.info(f"Recomputed {n_res_after} ({n_res_after-n_res_before} new).") + + logger.info("Auto-flagging points.") + + for result in PhotoPolResult.objects.filter(epoch__in=epoch_L).all(): + if result.p is not None and not (0 <= result.p <= 1): + result.set_flag(PhotoPolResult.FLAGS.BAD_POLARIMETRY) + result.save() + + logger.info("Applying corrections.") + + for epoch in epoch_L: + results = PhotoPolResult.objects.filter(epoch=epoch).all() + for result in results: + try: + result.compute_host_galaxy_correction() + except PhotoPolResult.NoHostCorrectionAvailable: + pass + except Exception as e: + logger.exception(f"Error computing host galaxy correction for {result}.") def list_local_epochnames() -> list[str]: """List all local epochnames in local archives (by looking at the raw directory).""" @@ -288,6 +355,11 @@ def main(): parser.add_argument('--list-files-only', action='store_true', help=' If given, the built list of filelocs will be printed but not processed') parser.add_argument('--no-check-db-files', dest='keep_files_in_db', action='store_true', help=' Process discovered files even if they existed in archive') + # astrosource processing options + parser.add_argument('--astrosource', type=str, default=None, help=' Select files only of this source') + parser.add_argument('--recompute', action='store_true', help=' Recompute photometry and polarimetry results') + parser.add_argument('--retry-failed', action='store_true', help=' Retry failed reduced fits') + # other options parser.add_argument('--skip-remote-file-list', action='store_true', help=' Skip remote file list check') parser.add_argument("--force-rebuild", action="store_true", help=" Force re-building of files (pass force_rebuild=True)") @@ -443,6 +515,11 @@ def main(): else: logger.info("Invoked with --list-files-only") + # Astrosource + + if args.astrosource is not None: + process_astrosource(args) + # Start interactive shell if indicated if args.interactive: