## relphot outline

- load data images: match photcode and time range, reset flags measure: select subset matching restritions on: photcode, time, dM, Minst, Mag, dophot == 1
- iterate to find Mcal, image.flags:

  ## relphot overview

  relphot has two primary purposes:

  - calculate Mcal for images / measures
  - calculate Mrel for stars

  relphot can also be used to determine the mosaic grid used to generate photometrically corrected flats (-grid option).

  ## data exclusion

  relphot uses only a subset of the photometry data to calculate Mcal and Mrel. In the first stage, calculation of the Mcal values, relphot loads the photometry data from each relevant catalog and creates an internal subset catalog with the function bcatalog, excluding some of the irrelevant data. In addition, it uses flags to mark some of the data as invalid for the processing. bcatalog exclusions

  - measure.photcode not equivalent to requested photcode
  - measure.dophot != 1
  - measure.Mcat > MAG_LIM
  - measure.dM > SIGMA_LIM
  - measure.Minst out of range (ImagMin - ImagMax) [optional]
  - measure.t out of range (TSTART, TSTOP) flagged data flagged image data images can be flagged by setting bits of image.code stars can be flagged by setting bits of average.code measures can be flagged by setting bits of measure.flag image.code ID_IMAGE_NOCAL : ignore, irrelevant ID_IMAGE_POOR : image measured bad ID_IMAGE_SKIP : externally known bad dMcal > VALUE FLAG_IMAGE_SCATTER clean_images fabs(Mcal) > VALUE FLAG_IMAGE_ZEROPT clean_images dMcal > VALUE FLAG_IMAGE_SCATTER clean_mosaics fabs(Mcal) > VALUE FLAG_IMAGE_ZEROPT clean_mosaics mark_images does not seem to do anything useful? average.code Ngood < MEAS_TOOFEW setMrel Ngood < MEAS_TOOFEW clean_measures ChiSq > STAR_CHISQ clean_stars dM > STAR_SCATTER clean_stars average.code (STAR_BAD) is not saved by relphot: it is set by clean_stars, clean_measures, and setMrel, but not setMrelOutput. STAR_BAD should only be internal since it depends on the photcode, but is not associated with a specific photcode in the data. Just in case, it is reset to 0 in setMrelFinal. measure.flag X,Y out of range setExclusions 3 sigma clipping clean_measures

    ## setting Mrel final value

    setMrelFinal is used to set the final average.Mrel values. We do this in 4 stages. In each stage, we set the Mrel values for stars which have not already been set, based on the current exclusion settings. At successive stages, we relax the exclusions, allowing the more spurious objects to have a valid Mrel value to be set. In this loop, we actually run setMrelOutput twice: once to get the approximate Mrel value, then we flag the outlier measurements with clean_measure, then we redetermine the Mrel values on this basis, and mark the stars for exclusion from the next iteration. exclude on photcode 0 1 2 3 time range 0 1 2 3 MEAS_POOR 0 1 2 3 MEAS_TOOFEW 0 1 2 3 dophot == 10 0 1 2 inst mag 0 1 2 dophot != 1,2 0 1 ID_IMAGE_POOR 0 1 ID_IMAGE_SKIP 0 1 dophot != 1 0 measure.dM 0 for all relphot runs, Mrel is re-calculated, and measures are marked at least if they are outliers in mag or ccd area. setMrel.output needs to do a few things differently from setMrel:
    - set measure.Mcal (skipped in setMrel.basic)
    - set average.Mrel if N < TOO_FEW (not STAR_BAD) (optional!)
    - use MAX (stats.error, stats.sigma) (optionally)
    - allow STAR_BAD?

    ## imphotset

    imphotset allows you to set certain phot.image table entries. here are the options: imphotset [-photcode code] [-name foo] [-trange (start) (stop)] -flag and value