IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 13, 2017, 10:53:48 AM (9 years ago)
Author:
eugene
Message:

merge EAM development branch changes for DR2 into trunk (add PS1_V6 dvo format; change Mcal to McalPSF, McalAPER; change opihi int vectors to 64bit)

Location:
trunk/Ohana
Files:
28 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana

  • trunk/Ohana/src/relphot/include/relphot.h

    r39926 r40291  
    7777  unsigned int stop;
    7878  short photcode;
    79   float Mcal;
     79  float McalPSF;
     80  float McalAPER;
    8081  float dMcal;
    8182  float dMsys;
    82   unsigned short nFitPhotom;
    83   short Xm;
     83  float McalChiSq;
    8484  float secz;
    8585  float ubercalDist;
     86  unsigned short nFitPhotom;
    8687  unsigned int flags;
    8788  char skipCal;               // if TRUE, this mosaic is incomplete and should not be calibrated
     
    201202
    202203typedef struct {
    203   float Mcal;
     204  float McalPSF;
     205  float McalAPER;
    204206  float dMcal;
    205207  float dMagSys;
    206   short Xm;
     208  float McalChiSq;
    207209  int nFitPhotom;
    208210  int flags;
     
    220222
    221223typedef struct {
    222   float Mcal;
     224  float McalPSF;
     225  float McalAPER;
    223226  float dMcal;
    224227  unsigned int imageID;
     
    336339int    SyntheticPhotometry;
    337340
     341int    USE_MCAL_PSF_FOR_STACK_APER;
     342
    338343char  *PhotcodeList;
    339344
     
    404409int           findMosaics         PROTO((Catalog *catalog, int Ncatalog, int doMosaicList));
    405410
     411void clearImages (void);
     412void checkImages (char *name);
     413int dumpMags (FILE *fout, Catalog *catalog, int Ncatalog);
     414
    406415void makeMosaics (Image *image, off_t Nimage, int mergeMcal);
    407416Mosaic *getMosaicForImage (off_t im);
     
    422431off_t         getImageEntry       PROTO((off_t meas, int cat));
    423432
    424 float         getMcal_alt         PROTO((off_t meas, int cat, FlatCorrectionTable *flatcorr, float Xccd, float Yccd));
    425 float         getMcal             PROTO((off_t meas, int cat, FlatCorrectionTable *flatcorr, Catalog *catalog));
     433float         getMcal             PROTO((off_t meas, int cat, dvoMagClassType class));
    426434float         getMflat            PROTO((off_t meas, int cat, FlatCorrectionTable *flatcorr, Catalog *catalog));
    427435float         getMgrid            PROTO((off_t meas, int cat));
    428436float         getMmos             PROTO((off_t meas, int cat));
    429 float         getMrel             PROTO((Catalog *catalog, off_t meas, int cat));
     437float         getMrel             PROTO((Catalog *catalog, off_t meas, int cat, dvoMagClassType class, dvoMagSourceType source));
    430438short         getUbercalDist      PROTO((off_t meas, int cat));
    431439float         getCenterOffset     PROTO((off_t meas, int cat, Measure *measure, unsigned int *myID));
  • trunk/Ohana/src/relphot/src/BrightCatalog.c

    r39478 r40291  
    6666    GET_COLUMN(D,         "DEC",        double);
    6767    GET_COLUMN(M,         "MAG_SYS",    float);
    68     GET_COLUMN(Mcal,      "MAG_CAL",    float);
     68    GET_COLUMN(Mkron,     "MAG_KRON",   float);
     69    GET_COLUMN(McalPSF,   "MCAL_PSF",   float);
     70    GET_COLUMN(McalAPER,  "MCAL_APER",  float);
    6971    GET_COLUMN(Mflat,     "MAG_FLAT",   float);
    7072    GET_COLUMN(dM,        "MAG_ERR",    float);
     
    8991      measure[i].D         = D[i];
    9092      measure[i].M         = M[i];
    91       measure[i].Mcal      = Mcal[i];
     93      measure[i].Mkron     = Mkron[i];
     94      measure[i].McalPSF   = McalPSF[i];
     95      measure[i].McalAPER  = McalAPER[i];
    9296      measure[i].Mflat     = Mflat[i];
    9397      measure[i].dM        = dM[i];
     
    110114    free (D       );
    111115    free (M       );
    112     free (Mcal    );
     116    free (Mkron   );
     117    free (McalPSF );
     118    free (McalAPER);
    113119    free (Mflat   );
    114120    free (dM      );
     
    186192    // need to create and assign to flat-field correction
    187193    GET_COLUMN(M,      "MAG",      float);
     194    GET_COLUMN(Mkron,  "MAG_KRON", float);
    188195    GET_COLUMN(dM,     "MAG_ERR",  float);
    189196    GET_COLUMN(Mchisq, "MAG_CHI",  float);
     
    199206    ALLOCATE (secfilt, SecFilt, Nrow);
    200207    for (i = 0; i < Nrow; i++) {
    201       secfilt[i].M      = M[i];         
    202       secfilt[i].dM     = dM[i];
    203       secfilt[i].Mchisq = Mchisq[i];
    204       secfilt[i].flags  = flags[i];
    205       secfilt[i].Ncode  = Ncode[i];
    206       secfilt[i].Nused  = Nused[i];
    207       secfilt[i].Mmin   = Mmin[i];
    208       secfilt[i].Mmax   = Mmax[i];
     208      secfilt[i].MpsfChp  = M[i];         
     209      secfilt[i].MkronChp = Mkron[i];         
     210      secfilt[i].dMpsfChp = dM[i];
     211      secfilt[i].Mchisq   = Mchisq[i];
     212      secfilt[i].flags    = flags[i];
     213      secfilt[i].Ncode    = Ncode[i];
     214      secfilt[i].Nused    = Nused[i];
     215      secfilt[i].Mmin     = Mmin[i];
     216      secfilt[i].Mmax     = Mmax[i];
    209217    }
    210218    fprintf (stderr, "loaded data for %lld secfilt\n", (long long) Nrow);
    211219
    212220    free (M     );
     221    free (Mkron );
    213222    free (dM    );
    214223    free (Mchisq);
     
    271280    gfits_define_bintable_column (&theader, "D", "DEC",      "dec",                        "degree", 1.0, 0.0);
    272281    gfits_define_bintable_column (&theader, "E", "MAG_SYS",  "magnitude (sys)",             NULL,    1.0, 0.0);
    273     gfits_define_bintable_column (&theader, "E", "MAG_CAL",  "magnitude (cal)",             NULL,    1.0, 0.0);
     282    gfits_define_bintable_column (&theader, "E", "MAG_KRON", "magnitude (sys,kron)",        NULL,    1.0, 0.0);
     283    gfits_define_bintable_column (&theader, "E", "MCAL_PSF", "magnitude (cal)",             NULL,    1.0, 0.0);
     284    gfits_define_bintable_column (&theader, "E", "MCAL_APER","magnitude (cal)",             NULL,    1.0, 0.0);
    274285    gfits_define_bintable_column (&theader, "E", "MAG_FLAT", "magnitude (flat)",            NULL,    1.0, 0.0);
    275286    gfits_define_bintable_column (&theader, "E", "MAG_ERR",  "magnitude (err)",             NULL,    1.0, 0.0);
     
    293304    double *D         ; ALLOCATE (D        ,  double, catalog->Nmeasure);
    294305    float  *M         ; ALLOCATE (M        ,  float,  catalog->Nmeasure);
    295     float  *Mcal      ; ALLOCATE (Mcal     ,  float,  catalog->Nmeasure);
     306    float  *Mkron     ; ALLOCATE (Mkron    ,  float,  catalog->Nmeasure);
     307    float  *McalPSF   ; ALLOCATE (McalPSF  ,  float,  catalog->Nmeasure);
     308    float  *McalAPER  ; ALLOCATE (McalAPER ,  float,  catalog->Nmeasure);
    296309    float  *Mflat     ; ALLOCATE (Mflat    ,  float,  catalog->Nmeasure);
    297310    float  *dM        ; ALLOCATE (dM       ,  float,  catalog->Nmeasure);
     
    311324    MeasureTiny *measure = catalog->measure;
    312325    for (i = 0; i < catalog->Nmeasure; i++) {
    313       R[i]        = measure[i].R       ;
    314       D[i]        = measure[i].D       ;
     326      R[i]        = measure[i].R        ;
     327      D[i]        = measure[i].D        ;
    315328      M[i]        = measure[i].M        ;
    316       Mcal[i]     = measure[i].Mcal     ;
     329      Mkron[i]    = measure[i].Mkron    ;
     330      McalPSF[i]  = measure[i].McalPSF  ;
     331      McalAPER[i] = measure[i].McalAPER ;
    317332      Mflat[i]    = measure[i].Mflat    ;
    318333      dM[i]       = measure[i].dM       ;
     
    334349    gfits_set_bintable_column (&theader, &ftable, "DEC",        D,         catalog->Nmeasure);
    335350    gfits_set_bintable_column (&theader, &ftable, "MAG_SYS",    M,         catalog->Nmeasure);
    336     gfits_set_bintable_column (&theader, &ftable, "MAG_CAL",    Mcal,      catalog->Nmeasure);
     351    gfits_set_bintable_column (&theader, &ftable, "MAG_KRON",   Mkron,     catalog->Nmeasure);
     352    gfits_set_bintable_column (&theader, &ftable, "MCAL_PSF",   McalPSF,   catalog->Nmeasure);
     353    gfits_set_bintable_column (&theader, &ftable, "MCAL_APER",  McalAPER,  catalog->Nmeasure);
    337354    gfits_set_bintable_column (&theader, &ftable, "MAG_FLAT",   Mflat,     catalog->Nmeasure);
    338355    gfits_set_bintable_column (&theader, &ftable, "MAG_ERR",    dM,        catalog->Nmeasure);
     
    352369    free (D       );
    353370    free (M       );
    354     free (Mcal    );
     371    free (Mkron   );
     372    free (McalPSF );
     373    free (McalAPER);
    355374    free (Mflat   );
    356375    free (dM      );
     
    436455    gfits_create_table_header (&theader, "BINTABLE", "SECFILT");
    437456
    438     gfits_define_bintable_column (&theader, "E", "MAG",      "",              "arcsec", 1.0, 0.0);
    439     gfits_define_bintable_column (&theader, "E", "MAG_ERR",  "",              "arcsec", 1.0, 0.0);
     457    gfits_define_bintable_column (&theader, "E", "MAG",      "",              "mag",   1.0, 0.0);
     458    gfits_define_bintable_column (&theader, "E", "MAG_KRON", "",              "mag",   1.0, 0.0);
     459    gfits_define_bintable_column (&theader, "E", "MAG_ERR",  "",              "mag",   1.0, 0.0);
    440460    gfits_define_bintable_column (&theader, "E", "MAG_CHI",  "",              NULL,    1.0, 0.0);
    441461    gfits_define_bintable_column (&theader, "J", "FLAGS",    "",              NULL,    1.0, 0.0);
     
    454474    // create intermediate storage arrays
    455475    float *M        ; ALLOCATE (M      ,  float, Nsec);
     476    float *Mkron    ; ALLOCATE (Mkron  ,  float, Nsec);
    456477    float *dM       ; ALLOCATE (dM     ,  float, Nsec);
    457478    float *Mchisq   ; ALLOCATE (Mchisq ,  float, Nsec);
     
    465486    SecFilt *secfilt = catalog->secfilt;
    466487    for (i = 0; i < Nsec; i++) {
    467       M     [i]       = secfilt[i].M      ;
    468       dM    [i]       = secfilt[i].dM     ;
    469       Mchisq[i]       = secfilt[i].Mchisq ;
    470       flags [i]       = secfilt[i].flags  ;
    471       Ncode [i]       = secfilt[i].Ncode  ;
    472       Nused [i]       = secfilt[i].Nused  ;
    473       Mmin  [i]       = secfilt[i].Mmin   ;
    474       Mmax  [i]       = secfilt[i].Mmax   ;
     488      M     [i]       = secfilt[i].MpsfChp ;
     489      Mkron [i]       = secfilt[i].MkronChp;
     490      dM    [i]       = secfilt[i].dMpsfChp;
     491      Mchisq[i]       = secfilt[i].Mchisq  ;
     492      flags [i]       = secfilt[i].flags   ;
     493      Ncode [i]       = secfilt[i].Ncode   ;
     494      Nused [i]       = secfilt[i].Nused   ;
     495      Mmin  [i]       = secfilt[i].Mmin    ;
     496      Mmax  [i]       = secfilt[i].Mmax    ;
    475497    }
    476498
    477499    // add the columns to the output array
    478500    gfits_set_bintable_column (&theader, &ftable, "MAG",      M     , Nsec);
     501    gfits_set_bintable_column (&theader, &ftable, "MAG_KRON", Mkron , Nsec);
    479502    gfits_set_bintable_column (&theader, &ftable, "MAG_ERR",  dM    , Nsec);
    480503    gfits_set_bintable_column (&theader, &ftable, "MAG_CHI",  Mchisq, Nsec);
     
    486509
    487510    free (M      );
     511    free (Mkron  );
    488512    free (dM     );
    489513    free (Mchisq );
  • trunk/Ohana/src/relphot/src/GridOps.c

    r39632 r40291  
    401401
    402402        // skip images marked as BAD
    403         Mcal = getMcal  (m, c, flatcorr, catalog);
     403        Mcal = getMcal  (m, c, MAG_CLASS_PSF);
    404404        if (isnan(Mcal)) {
    405405          Ncal ++;
     
    558558        continue;
    559559      }
    560       Mcal = getMcal  (m, c, flatcorr, catalog);
     560      Mcal = getMcal  (m, c, MAG_CLASS_PSF);
    561561      if (isnan(Mcal)) {
    562562        Ncal ++;
     
    568568        continue;
    569569      }
    570       Mrel  = getMrel  (catalog, m, c);
     570      Mrel  = getMrel  (catalog, m, c, MAG_CLASS_PSF, MAG_SRC_CHP);
    571571      if (isnan(Mrel)) {
    572572        Nrel ++;
     
    645645        continue;
    646646      }
    647       Mcal  = getMcal  (m, c, flatcorr, catalog);
     647      Mcal  = getMcal  (m, c, MAG_CLASS_PSF);
    648648      if (isnan(Mcal)) continue;
    649649      Mmos  = getMmos  (m, c);
    650650      if (isnan(Mmos)) continue;
    651       Mrel  = getMrel  (catalog, m, c);
     651      Mrel  = getMrel  (catalog, m, c, MAG_CLASS_PSF, MAG_SRC_CHP);
    652652      if (isnan(Mrel)) continue;
    653653
  • trunk/Ohana/src/relphot/src/ImageMagIO.c

    r37037 r40291  
    5858  char type[16];
    5959
    60   GET_COLUMN (Mcal,              "MCAL",           float);
     60  GET_COLUMN (McalPSF,           "MCAL_PSF",       float);
     61  GET_COLUMN (McalAPER,          "MCAL_APER",      float);
    6162  GET_COLUMN (dMcal,             "MCAL_ERR",       float);
    6263  GET_COLUMN (dMagSys,           "MCAL_SYSERR",    float);
    63   GET_COLUMN (nFitPhotom,        "NFIT",             int);
    64   GET_COLUMN (flags,             "FLAGS",            int);
    65   GET_COLUMN (ubercalDist,       "UDIST",            int);
    66   GET_COLUMN (imageID,           "ID",               int);
    67   GET_COLUMN (Xm,                "CHISQ",          short);
     64  GET_COLUMN (McalChiSq,         "MCAL_CHISQ",     float);
     65  GET_COLUMN (nFitPhotom,        "NFIT",           int);
     66  GET_COLUMN (flags,             "FLAGS",          int);
     67  GET_COLUMN (ubercalDist,       "UDIST",          int);
     68  GET_COLUMN (imageID,           "ID",             int);
    6869
    6970  // free the memory associated with the FITS files
     
    7576  ALLOCATE (image_mags, ImageMag, Nrow);
    7677  for (i = 0; i < Nrow; i++) {
    77     image_mags[i].Mcal                      = Mcal       [i];
     78    image_mags[i].McalPSF                   = McalPSF    [i];
     79    image_mags[i].McalAPER                  = McalAPER   [i];
    7880    image_mags[i].dMcal                     = dMcal      [i];
    7981    image_mags[i].dMagSys                   = dMagSys    [i];
    80     image_mags[i].Xm                        = Xm         [i];
     82    image_mags[i].McalChiSq                 = McalChiSq  [i];
    8183    image_mags[i].nFitPhotom                = nFitPhotom [i];
    8284    image_mags[i].flags                     = flags      [i];
     
    8688  fprintf (stderr, "loaded data for %lld images\n", (long long) Nrow);
    8789
    88   free (Mcal       );
     90  free (McalPSF    );
     91  free (McalAPER   );
    8992  free (dMcal      );
    9093  free (dMagSys    );
    91   free (Xm         );
     94  free (McalChiSq  );
    9295  free (nFitPhotom );
    9396  free (flags      );
     
    121124  gfits_create_table_header (&theader, "BINTABLE", "IMAGE_MAGS");
    122125
    123   gfits_define_bintable_column (&theader, "E", "MCAL",           "cal offset",                 "magnitudes", 1.0, 0.0);
     126  gfits_define_bintable_column (&theader, "E", "MCAL_PSF",       "PSF cal offset",             "magnitudes", 1.0, 0.0);
     127  gfits_define_bintable_column (&theader, "E", "MCAL_APER",      "APER cal offset",            "magnitudes", 1.0, 0.0);
    124128  gfits_define_bintable_column (&theader, "E", "MCAL_ERR",       "cal error",                  "magnitudes", 1.0, 0.0);
    125129  gfits_define_bintable_column (&theader, "E", "MCAL_SYSERR",    "systematic error",           "magnitudes", 1.0, 0.0);
     130  gfits_define_bintable_column (&theader, "E", "MCAL_CHISQ",     "cal chisq",                  "unitless",   1.0, 0.0);
    126131  gfits_define_bintable_column (&theader, "J", "NFIT",           "number of fitted stars",     "unitless",   1.0, 0.0);
    127132  gfits_define_bintable_column (&theader, "J", "FLAGS",          "analysis flags",             "unitless",   1.0, 0.0);
    128133  gfits_define_bintable_column (&theader, "J", "UDIST",          "distance to ubercal images", "images",     1.0, 0.0);
    129134  gfits_define_bintable_column (&theader, "J", "ID",             "image ID",                   "unitless",   1.0, 0.0);
    130   gfits_define_bintable_column (&theader, "I", "CHISQ",          "cal chisq",                  "unitless",   1.0, FT_BZERO_INT16);
    131135
    132136  // generate the output array that carries the data
    133137  gfits_create_table (&theader, &ftable);
    134138
    135   float *Mcal        ;
     139  float *McalPSF     ;
     140  float *McalAPER    ;
    136141  float *dMcal       ;
    137142  float *dMagSys     ;
    138   float *Xm          ;
     143  float *McalChiSq   ;
    139144  int   *nFitPhotom  ;
    140145  int   *flags       ;
     
    143148
    144149  // create intermediate storage arrays
    145   ALLOCATE (Mcal        ,         float,          Nimage_mags);
     150  ALLOCATE (McalPSF     ,         float,          Nimage_mags);
     151  ALLOCATE (McalAPER    ,         float,          Nimage_mags);
    146152  ALLOCATE (dMcal       ,         float,          Nimage_mags);
    147153  ALLOCATE (dMagSys     ,         float,          Nimage_mags);
    148   ALLOCATE (Xm          ,         float,          Nimage_mags);
     154  ALLOCATE (McalChiSq   ,         float,          Nimage_mags);
    149155  ALLOCATE (nFitPhotom  ,           int,          Nimage_mags);
    150156  ALLOCATE (flags       ,           int,          Nimage_mags);
     
    154160  // assign the storage arrays
    155161  for (i = 0; i < Nimage_mags; i++) {
    156     Mcal       [i]   = image_mags[i].Mcal       ;
     162    McalPSF    [i]   = image_mags[i].McalPSF    ;
     163    McalAPER   [i]   = image_mags[i].McalAPER   ;
    157164    dMcal      [i]   = image_mags[i].dMcal      ;
    158165    dMagSys    [i]   = image_mags[i].dMagSys    ;
    159     Xm         [i]   = image_mags[i].Xm         ;
     166    McalChiSq  [i]   = image_mags[i].McalChiSq  ;
    160167    nFitPhotom [i]   = image_mags[i].nFitPhotom ;
    161168    flags      [i]   = image_mags[i].flags      ;
     
    165172
    166173  // add the columns to the output array
    167   gfits_set_bintable_column (&theader, &ftable, "MCAL",           Mcal       ,         Nimage_mags);
     174  gfits_set_bintable_column (&theader, &ftable, "MCAL_PSF",       McalPSF    ,         Nimage_mags);
     175  gfits_set_bintable_column (&theader, &ftable, "MCAL_APER",      McalAPER   ,         Nimage_mags);
    168176  gfits_set_bintable_column (&theader, &ftable, "MCAL_ERR",       dMcal      ,         Nimage_mags);
    169177  gfits_set_bintable_column (&theader, &ftable, "MCAL_SYSERR",    dMagSys    ,         Nimage_mags);
    170   gfits_set_bintable_column (&theader, &ftable, "CHISQ",          Xm         ,         Nimage_mags);
     178  gfits_set_bintable_column (&theader, &ftable, "MCAL_CHISQ",     McalChiSq  ,         Nimage_mags);
    171179  gfits_set_bintable_column (&theader, &ftable, "NFIT",           nFitPhotom ,         Nimage_mags);
    172180  gfits_set_bintable_column (&theader, &ftable, "FLAGS",          flags      ,         Nimage_mags);
     
    174182  gfits_set_bintable_column (&theader, &ftable, "ID",             imageID    ,         Nimage_mags);
    175183
    176   free (Mcal       );
     184  free (McalPSF    );
     185  free (McalAPER   );
    177186  free (dMcal      );
    178187  free (dMagSys    );
    179   free (Xm         );
     188  free (McalChiSq  );
    180189  free (nFitPhotom );
    181190  free (flags      );
  • trunk/Ohana/src/relphot/src/ImageOps.c

    r39643 r40291  
    143143    image[i].photom_map_id = input[i].photom_map_id;
    144144    image[i].flags         = input[i].flags        ;
    145     image[i].Mcal          = input[i].Mcal         ;
     145    image[i].McalPSF       = input[i].McalPSF      ;
     146    image[i].McalAPER      = input[i].McalAPER     ;
    146147    image[i].dMcal         = input[i].dMcal        ;
    147148    image[i].tzero         = input[i].tzero        ;
     
    179180    subset[i].photom_map_id = image[i].photom_map_id;
    180181    subset[i].flags         = image[i].flags        ;
    181     subset[i].Mcal          = image[i].Mcal         ;
     182    subset[i].McalPSF       = image[i].McalPSF      ;
     183    subset[i].McalAPER      = image[i].McalAPER     ;
    182184    subset[i].dMcal         = image[i].dMcal        ;
    183185    subset[i].tzero         = image[i].tzero        ;
     
    430432}
    431433
    432 // returns image.Mcal
     434// returns image.McalPSF or image.McalAPER
    433435// NOTE: static flat-field component is included in measure.Mflat
    434 float getMcal (off_t meas, int cat, FlatCorrectionTable *flatcorr, Catalog *catalog) {
    435   OHANA_UNUSED_PARAM(flatcorr);
    436   OHANA_UNUSED_PARAM(catalog);
    437 
    438   off_t i;
    439   float value;
     436float getMcal (off_t meas, int cat, dvoMagClassType class) {
     437
     438  off_t i;
    440439
    441440  i = MeasureToImage[cat][meas];
    442   if (i == -1) return (NAN);
    443 
    444   if (image[i].flags & IMAGE_BAD) return (NAN); 
    445   value = image[i].Mcal;
    446 
    447   // to do this, I need to pass in the catalog and flatcorr pointers
    448   // int flat_id = image[i].photom_map_id;
    449   // float offset = 0.0;
    450   // if (flat_id) {
    451   //   offset = FlatCorrectionOffset (flatcorr, flat_id, catalog[cat].measureT[meas].Xccd, catalog[cat].measureT[meas].Yccd);
    452   // }
    453   // value -= offset;
    454 
    455   return (value);
    456 }
    457 
    458 // returns image.Mcal - ff(x,y)
    459 // NOTE: static flat-field component is included in measure.Mflat
    460 float getMcal_alt (off_t meas, int cat, FlatCorrectionTable *flatcorr, float Xccd, float Yccd) {
    461   OHANA_UNUSED_PARAM(flatcorr);
    462   OHANA_UNUSED_PARAM(Xccd);
    463   OHANA_UNUSED_PARAM(Yccd);
    464 
    465   off_t i;
    466   float value;
    467 
    468   i = MeasureToImage[cat][meas];
    469   if (i == -1) return (NAN);
    470 
    471   if (image[i].flags & IMAGE_BAD) return (NAN); 
    472   value = image[i].Mcal;
    473 
    474   // to do this, I need to pass in the catalog and flatcorr pointers
    475   // int flat_id = image[i].photom_map_id;
    476   // float offset = 0.0;
    477   // if (flat_id) {
    478   //   offset = FlatCorrectionOffset (flatcorr, flat_id, Xccd, Yccd);
    479   // }
    480   // value -= offset;
    481 
    482   return (value);
    483 }
    484 
    485 // returns image.Mcal - ff(x,y)
     441  if (i == -1) return NAN;
     442
     443  if (image[i].flags & IMAGE_BAD) return NAN; 
     444
     445  switch (class) {
     446    case MAG_CLASS_PSF:
     447      return image[i].McalPSF;
     448    case MAG_CLASS_APER:
     449    case MAG_CLASS_KRON:
     450      return image[i].McalAPER;
     451    default:
     452      return NAN;
     453  }
     454  return NAN; // should not be able to reach here
     455}
     456
    486457short getUbercalDist (off_t meas, int cat) {
    487458
     
    499470}
    500471
    501 // returns image.Mcal - ff(x,y)
    502472float getCenterOffset (off_t meas, int cat, Measure *measure, unsigned int *myID) {
    503473
     
    519489}
    520490
    521 // returns image.Mcal - ff(x,y)
    522491int MatchImageName (off_t meas, int cat, char *name) {
    523492
     
    542511}
    543512
    544 // returns image.Mcal - ff(x,y)
    545513int MatchImageSkycellID (off_t meas, int cat, int myTessID, int myProjectionID, int mySkycellID) {
    546514
     
    563531}
    564532
    565 // returns image.Mcal - ff(x,y)
    566533int FindImageSkycellID (off_t meas, int cat, int *myTessID, int *myProjectionID, int *mySkycellID) {
    567534
     
    618585void setMcal (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
    619586
    620   off_t i, j, m, c, n, Nmax;
     587  off_t i, j, m, c, n;
    621588  int mark, bad, Nfew, Nbad, Nmos, Nrel, Ngrid, Nsys;
    622   float Msys, Mrel, Mmos, Mgrid, Mflat;
    623   // double *list, *dlist, *Mlist, *dMlist;
    624589
    625590  StatType stats;
     
    638603  }
    639604
    640   Nmax = 0;
     605  off_t Nmax = 0;
    641606  for (i = 0; i < Nimage; i++) {
    642607    Nmax = MAX (Nmax, N_onImage[i]);
    643608  }
    644   StatDataSet *refStars = StatDataSetAlloc (1, Nmax);
     609
     610  StatDataSet *kronStars   = StatDataSetAlloc (1, Nmax);
     611  StatDataSet *psfStars    = StatDataSetAlloc (1, Nmax);
    645612  StatDataSet *brightStars = StatDataSetAlloc (1, Nmax);
    646613
     
    676643    int minUbercalDist = 1000;
    677644   
    678     off_t Nref = 0;  // total number of reference stars on the image
    679     int Nbright = 0; // number of stars to measure the bright-end scatter
     645    off_t Nref = 0;    // number of stars used to measure McalPSF
     646    int   Nkron = 0;   // number of stars to measure McalAPER
     647    int   Nbright = 0; // number of stars to measure the bright-end scatter
    680648
    681649    if (N_onImage[i] == 0) {
     
    693661          continue;
    694662      }
    695       Mmos  = getMmos  (m, c);
     663      float Mmos  = getMmos  (m, c);
    696664      if (isnan(Mmos)) {
    697665          Nmos ++;
    698666          continue;
    699667      }
    700       Mgrid = getMgrid (m, c);
     668      float Mgrid = getMgrid (m, c);
    701669      if (isnan(Mgrid)) {
    702670          Ngrid++;
    703671          continue;
    704672      }
    705       Mrel  = getMrel  (catalog, m, c);
    706       if (isnan(Mrel)) {
     673
     674      // Mrel* is the average magnitude for this star.  For PS1 stacks, we have too much
     675      // PSF variability.  We need to calibrate the PSF magnitudes separately from the
     676      // Aperture-like magnitues.  (We have an option to use the kron magnitudes or the
     677      // other apertures here).  I basically need to do this analysis separately for each
     678      // magnitude type
     679
     680      float MrelPSF  = getMrel  (catalog, m, c, MAG_CLASS_PSF, MAG_SRC_CHP);
     681      if (isnan(MrelPSF)) {
    707682          Nrel ++;
    708683          continue;
     
    714689      // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal - Mflat)
    715690
    716       Mflat = getMflat (m, c, flatcorr, catalog);
     691      float Mflat = getMflat (m, c, flatcorr, catalog);
    717692
    718693      n = catalog[c].measureT[m].averef;
    719       Msys = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_PSF);
    720       if (isnan(Msys)) {
     694      float MsysPSF = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_PSF);
     695      if (isnan(MsysPSF)) {
    721696        Nsys++;
    722697        continue;
     
    731706
    732707    skip:
    733       refStars->flxlist[Nref] = Msys - Mrel - Mmos - Mgrid + Mflat;
    734       refStars->errlist[Nref] = MAX (catalog[c].measureT[m].dM, MIN_ERROR);
    735       refStars->wgtlist[Nref] = 1;
    736       refStars->msklist[Nref] = 0;
    737       if (fabs(refStars->flxlist[Nref]) > 0.03) {
     708      psfStars->flxlist[Nref] = MsysPSF - MrelPSF - Mmos - Mgrid + Mflat;
     709      psfStars->errlist[Nref] = MAX (catalog[c].measureT[m].dM, MIN_ERROR);
     710      psfStars->wgtlist[Nref] = 1;
     711      psfStars->msklist[Nref] = 0;
     712      if (fabs(psfStars->flxlist[Nref]) > 0.03) {
    738713        // fprintf (stderr, "deviant\n");
    739714      }
    740715
     716      float MrelKron = getMrel  (catalog, m, c, MAG_CLASS_KRON, MAG_SRC_CHP);
     717      float MsysKron = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_KRON);
     718
     719      if (isfinite(MrelKron) && isfinite(MsysKron)) {
     720        kronStars->flxlist[Nkron] = MsysKron - MrelKron - Mmos - Mgrid + Mflat;
     721        kronStars->errlist[Nkron] = MAX (catalog[c].measureT[m].dM, MIN_ERROR);
     722        kronStars->wgtlist[Nkron] = 1;
     723        kronStars->msklist[Nkron] = 0;
     724        Nkron ++;
     725      }
     726
    741727      if ((image[i].imageID == TEST_IMAGE1) || (image[i].imageID == TEST_IMAGE2)) {
    742         fprintf (stderr, "%1d, %3d : %3d, %3d : %10.6f %10.6f : %6.3f  %6.3f  %6.3f  %6.3f  %6.3f : %6.3f\n", (int) i, (int) j, (int) c, (int) m, catalog[c].averageT[n].R, catalog[c].averageT[n].D, Msys, Mrel, Mmos, Mgrid, Mflat, refStars->flxlist[Nref]);
     728        fprintf (stderr, "%1d, %3d : %3d, %3d : %10.6f %10.6f : %6.3f  %6.3f  %6.3f  %6.3f  %6.3f : %6.3f\n", (int) i, (int) j, (int) c, (int) m, catalog[c].averageT[n].R, catalog[c].averageT[n].D, MsysKron, MrelKron, Mmos, Mgrid, Mflat, kronStars->flxlist[Nref]);
    743729      }
    744730
    745731      if (catalog[c].measureT[m].dM < IMFIT_SYS_SIGMA_LIM) {
    746         brightStars->flxlist[Nbright] = refStars->flxlist[Nref];
    747         brightStars->errlist[Nbright] = refStars->errlist[Nref];
     732        brightStars->flxlist[Nbright] = psfStars->flxlist[Nref];
     733        brightStars->errlist[Nbright] = psfStars->errlist[Nref];
    748734        brightStars->wgtlist[Nbright] = 1;
    749735        brightStars->msklist[Nbright] = 0;
     
    770756    // no additional weight modification (we treat all stars on an image equally -- note an image is either ubercal-tied or not)
    771757# if (BASIC_STATS)
    772     liststats (refStars->flxlist, refStars->errlist, NULL, Nref, &stats);
     758    liststats (psfStars->flxlist, psfStars->errlist, NULL, Nref, &stats);
    773759# else
    774     liststats_irls (refStars, Nref, &stats);
     760    liststats_irls (psfStars, Nref, &stats);
    775761# endif
    776     image[i].Mcal       = stats.mean;
     762    image[i].McalPSF    = stats.mean;
    777763    image[i].dMcal      = stats.error;
    778764    image[i].nFitPhotom = Nref;
    779     image[i].Xm         = 100.0*log10(stats.chisq);
     765    image[i].McalChiSq  = stats.chisq;
    780766    Ncalibrated ++;
     767
     768    // no additional weight modification (we treat all stars on an image equally -- note an image is either ubercal-tied or not)
     769# if (BASIC_STATS)
     770    liststats (kronStars->flxlist, kronStars->errlist, NULL, Nkron, &stats);
     771# else
     772    liststats_irls (kronStars, Nref, &stats);
     773# endif
     774    image[i].McalAPER   = stats.mean;
    781775
    782776    if ((image[i].imageID == TEST_IMAGE1) || (image[i].imageID == TEST_IMAGE2)) {
    783777      for (j = 0; j < Nref; j++) {
    784         fprintf (stderr, "%1d, %8d : %6.3f  %6.3f  %6.3f  %d\n", (int) i, (int) image[i].imageID, refStars->flxlist[j], refStars->errlist[j], refStars->wgtlist[j], refStars->msklist[j]);
     778        fprintf (stderr, "%1d, %8d : %6.3f  %6.3f  %6.3f  %d\n", (int) i, (int) image[i].imageID, kronStars->flxlist[j], kronStars->errlist[j], kronStars->wgtlist[j], kronStars->msklist[j]);
    785779      }
    786780    }
    787781
    788782    if ((image[i].imageID == TEST_IMAGE1) || (image[i].imageID == TEST_IMAGE2)) {
    789       fprintf (stderr, "Mcal for : %s : %7.4f %7.4f\n", image[i].name, image[i].Mcal, image[i].dMcal);
     783      fprintf (stderr, "Mcal for : %s : %7.4f %7.4f\n", image[i].name, image[i].McalAPER, image[i].dMcal);
    790784    }
    791785
    792786    if (!mark && VERBOSE_IMAGE) {
    793       fprintf (stderr, "Mcal for : %s : %7.4f %7.4f\n", image[i].name, image[i].Mcal, image[i].dMcal);
     787      fprintf (stderr, "Mcal for : %s : %7.4f %7.4f\n", image[i].name, image[i].McalAPER, image[i].dMcal);
    794788    }
    795789
    796790    if (PLOTSTUFF) {
    797       fprintf (stderr, "Mcal for : %s : %7.4f %7.4f\n", image[i].name, image[i].Mcal, image[i].dMcal);
    798       plot_setMcal (refStars->flxlist, Nref, &stats, CLOUD_TOLERANCE);
     791      fprintf (stderr, "Mcal for : %s : %7.4f %7.4f\n", image[i].name, image[i].McalAPER, image[i].dMcal);
     792      plot_setMcal (psfStars->flxlist, Nref, &stats, CLOUD_TOLERANCE);
    799793    }
    800794
     
    803797    image[i].dMagSys = stats.sigma;
    804798
    805     if (image[i].Mcal < -CLOUD_TOLERANCE) {
    806       image[i].Mcal = 0.0;
     799    if (image[i].McalPSF < -CLOUD_TOLERANCE) {
     800      image[i].McalPSF = 0.0;
    807801    }
    808802
     
    813807
    814808  StatDataSetFree (brightStars, 1);
    815   StatDataSetFree (refStars, 1);
     809  StatDataSetFree (kronStars, 1);
     810  StatDataSetFree (psfStars, 1);
    816811
    817812  fprintf (stderr, "%d images calibrated\n", Ncalibrated);
     
    848843    if (FREEZE_IMAGES && isGPC1chip(image[i].photcode)) continue;
    849844
    850     mlist[N] = image[i].Mcal;
     845    mlist[N] = image[i].McalPSF;
    851846    slist[N] = image[i].dMcal;
    852847    dlist[N] = 1;
     
    873868    mark = FALSE;
    874869    image[i].flags &= ~ID_IMAGE_PHOTOM_POOR;
    875     mark = (image[i].dMcal > MaxScatter) || (fabs(image[i].Mcal - MedOffset) > MaxOffset);
     870    mark = (image[i].dMcal > MaxScatter) || (fabs(image[i].McalPSF - MedOffset) > MaxOffset);
    876871    if (mark) {
    877872      Nmark ++;
     
    961956    if (FREEZE_IMAGES && isGPC1chip(image[i].photcode)) continue;
    962957
    963     Mlist[Nplot] = image[i].Mcal;
     958    Mlist[Nplot] = image[i].McalPSF;
    964959    dlist[Nplot] = image[i].dMcal;
    965960    xlist[Nplot] = image[i].secz;
    966961    minAirmass = MIN (image[i].secz, minAirmass);
    967962    maxAirmass = MAX (image[i].secz, maxAirmass);
    968     minMcal = MIN (image[i].Mcal, minMcal);
    969     maxMcal = MAX (image[i].Mcal, maxMcal);
     963    minMcal = MIN (image[i].McalPSF, minMcal);
     964    maxMcal = MAX (image[i].McalPSF, maxMcal);
    970965    mindMcal = MIN (image[i].dMcal, mindMcal);
    971966    maxdMcal = MAX (image[i].dMcal, maxdMcal);
     
    10511046      c = ImageToCatalog[i][j];
    10521047
    1053       Mcal  = getMcal  (m, c, flatcorr, catalog);
     1048      Mcal  = getMcal  (m, c, MAG_CLASS_PSF);
    10541049      if (isnan(Mcal)) continue;
    10551050      Mmos  = getMmos  (m, c);
     
    10921087    if (FREEZE_IMAGES && isGPC1chip(image[i].photcode)) continue;
    10931088
    1094     list[n] = pow (10.0, 0.01*image[i].Xm);
     1089    list[n] = image[i].McalChiSq;
    10951090    dlist[n] = 1;
    10961091    n++;
     
    11251120    if (FREEZE_IMAGES && isGPC1chip(image[i].photcode)) continue;
    11261121
    1127     list[n] = image[i].Mcal;
     1122    list[n] = image[i].McalPSF;
    11281123    dlist[n] = 1;
    11291124    n++;
     
    11701165  return (stats);
    11711166}
     1167
     1168void clearImages (void) {
     1169  image = NULL;
     1170}
     1171
     1172void checkImages (char *name) {
     1173
     1174  off_t i;
     1175  fprintf (stderr, "--- %s ---\n", name);
     1176 
     1177  OhanaMemblock *ref = (OhanaMemblock *) image - 1;
     1178  fprintf (stderr, "file: %s\n", ref->file);
     1179
     1180  fprintf (stderr, "0x%08llx \n", (unsigned long long) image);
     1181
     1182  for (i = 0; i < Nimage; i++) {
     1183    fprintf (stderr, "%s : 0x%08x : %6.3f  %6.3f\n", image[i].name, image[i].flags, image[i].McalPSF, image[i].McalAPER);
     1184  }
     1185}
  • trunk/Ohana/src/relphot/src/ImageSubset.c

    r39478 r40291  
    6363  char type[16];
    6464
    65   GET_COLUMN (Mcal,      "MCAL",         float);
     65  GET_COLUMN (McalPSF,   "MCAL_PSF",     float);
     66  GET_COLUMN (McalAPER,  "MCAL_APER",    float);
    6667  GET_COLUMN (dMcal,     "MCAL_ERR",     float);
    6768  GET_COLUMN (imageID,   "IMAGE_ID",     int);
     
    7980  ALLOCATE (image, ImageSubset, Nrow);
    8081  for (i = 0; i < Nrow; i++) {
    81     image[i].Mcal          = Mcal[i];
     82    image[i].McalPSF       = McalPSF[i];
     83    image[i].McalAPER      = McalAPER[i];
    8284    image[i].dMcal         = dMcal[i];
    8385    image[i].imageID       = imageID[i];
     
    9395  fprintf (stderr, "loaded data for %lld images\n", (long long) Nrow);
    9496
    95   free (Mcal);
     97  free (McalPSF);
     98  free (McalAPER);
    9699  free (dMcal);
    97100  free (imageID);
     
    136139  gfits_create_table_header (&theader, "BINTABLE", "IMAGE_SUBSET");
    137140
    138   gfits_define_bintable_column (&theader, "E", "MCAL",         "zero point offset", "magnitudes", 1.0, 0.0);
     141  gfits_define_bintable_column (&theader, "E", "MCAL_PSF",     "zero point offset", "magnitudes", 1.0, 0.0);
     142  gfits_define_bintable_column (&theader, "E", "MCAL_APER",    "zero point offset", "magnitudes", 1.0, 0.0);
    139143  gfits_define_bintable_column (&theader, "E", "MCAL_ERR",     "zero point error",  "magnitudes", 1.0, 0.0);
    140144  gfits_define_bintable_column (&theader, "J", "IMAGE_ID",     "image ID",          NULL,         1.0, FT_BZERO_INT32);
     
    151155  gfits_create_table (&theader, &ftable);
    152156
    153   float *Mcal, *dMcal;
     157  float *McalPSF, *McalAPER, *dMcal;
    154158  unsigned int *imageID, *map, *flags, *tzero;
    155159  int *tessID, *projID, *skycellID;
     
    158162
    159163  // create intermediate storage arrays
    160   ALLOCATE (Mcal,      float,          Nimage);
     164  ALLOCATE (McalPSF,   float,          Nimage);
     165  ALLOCATE (McalAPER,  float,          Nimage);
    161166  ALLOCATE (dMcal,     float,          Nimage);
    162167  ALLOCATE (imageID,   unsigned int,   Nimage);
     
    172177  // assign the storage arrays
    173178  for (i = 0; i < Nimage; i++) {
    174     Mcal[i]      = image[i].Mcal;
     179    McalPSF[i]   = image[i].McalPSF;
     180    McalAPER[i]  = image[i].McalAPER;
    175181    dMcal[i]     = image[i].dMcal;
    176182    imageID[i]   = image[i].imageID;
     
    186192
    187193  // add the columns to the output array
    188   gfits_set_bintable_column (&theader, &ftable, "MCAL",         Mcal,      Nimage);
     194  gfits_set_bintable_column (&theader, &ftable, "MCAL_PSF",     McalPSF,   Nimage);
     195  gfits_set_bintable_column (&theader, &ftable, "MCAL_APER",    McalAPER,  Nimage);
    189196  gfits_set_bintable_column (&theader, &ftable, "MCAL_ERR",     dMcal,     Nimage);
    190197  gfits_set_bintable_column (&theader, &ftable, "IMAGE_ID",     imageID,   Nimage);
     
    198205  gfits_set_bintable_column (&theader, &ftable, "UBERCAL_DIST", ucdist,    Nimage);
    199206
    200   free (Mcal);
     207  free (McalPSF);
     208  free (McalAPER);
    201209  free (dMcal);
    202210  free (imageID);
  • trunk/Ohana/src/relphot/src/MosaicOps.c

    r39632 r40291  
    111111   
    112112    /* a new mosaic, define ranges -- preserve the original values incase this image is not used */
    113     mosaic[Nmosaic].start  = start;
    114     mosaic[Nmosaic].stop   = stop;
    115     mosaic[Nmosaic].Mcal   = 0.0; // note : mosaic stores only offsets relative to the original image values
    116     mosaic[Nmosaic].dMcal  = 0.0; // note : at the end, mosaic.Mcal is added back to the input images
    117     mosaic[Nmosaic].dMsys  = 0.0;
    118     mosaic[Nmosaic].Xm = 0.0;
    119     mosaic[Nmosaic].flags  = image[i].flags;
    120     mosaic[Nmosaic].secz   = image[i].secz;
    121     mosaic[Nmosaic].photcode = GetPhotcodeEquivCodebyCode (image[i].photcode);
     113    mosaic[Nmosaic].start     = start;
     114    mosaic[Nmosaic].stop      = stop;
     115    mosaic[Nmosaic].McalPSF   = 0.0; // note : at the end, mosaic.Mcal is added back to the input images
     116    mosaic[Nmosaic].McalAPER  = 0.0; // note : mosaic stores only offsets relative to the original image values
     117    mosaic[Nmosaic].dMcal     = 0.0; // note : at the end, mosaic.Mcal is added back to the input images
     118    mosaic[Nmosaic].dMsys     = 0.0;
     119    mosaic[Nmosaic].McalChiSq = 0.0;// NAN or 0.0?
     120    mosaic[Nmosaic].flags     = image[i].flags;
     121    mosaic[Nmosaic].secz      = image[i].secz;
     122    mosaic[Nmosaic].photcode  = GetPhotcodeEquivCodebyCode (image[i].photcode);
    122123
    123124    // XXX do we need to do something about flag consistency across a mosaic?
     
    208209  for (i = 0; i < Nmosaic; i++) {
    209210    /* a new mosaic, define ranges */
    210     mosaic[i].start    = startTimesMosaic[i];
    211     mosaic[i].stop     = 0;
    212     mosaic[i].Mcal     = 0.0;
    213     mosaic[i].dMcal    = 0.0;
    214     mosaic[i].dMsys    = 0.0;
    215     mosaic[i].Xm       = 0.0;
    216     mosaic[i].flags    = 0;
    217     mosaic[i].secz     = NAN;
    218     mosaic[i].photcode = 0;
    219     mosaic[i].skipCal  = FALSE;
     211    mosaic[i].start     = startTimesMosaic[i];
     212    mosaic[i].stop      = 0;
     213    mosaic[i].McalPSF   = 0.0; // note : at the end, mosaic.Mcal is added back to the input images
     214    mosaic[i].McalAPER  = 0.0; // note : mosaic stores only offsets relative to the original image values
     215    mosaic[i].dMcal     = 0.0; // note : at the end, mosaic.Mcal is added back to the input images
     216    mosaic[i].dMsys     = 0.0;
     217    mosaic[i].McalChiSq = 0.0;// NAN or 0.0?
     218    mosaic[i].flags     = 0;
     219    mosaic[i].secz      = NAN;
     220    mosaic[i].photcode  = 0;
     221    mosaic[i].skipCal   = FALSE;
    220222   
    221223    memset (&mosaic[i].coords, 0, sizeof(Coords));
     
    294296      abort();
    295297    }
    296     mosaic[j].stop     = stop;
    297     mosaic[j].Mcal     = 0.0;
    298     mosaic[j].dMcal    = 0.0;
    299     mosaic[j].Xm       = 0.0;
    300     mosaic[j].dMsys    = subset[i].flags;
    301     mosaic[j].flags    = subset[i].flags;
    302     mosaic[j].secz     = subset[i].secz;
    303     mosaic[j].photcode = GetPhotcodeEquivCodebyCode (subset[i].photcode);
     298    mosaic[j].stop      = stop;
     299
     300    mosaic[j].McalPSF   = 0.0;
     301    mosaic[j].McalAPER  = 0.0;
     302    mosaic[j].dMcal     = 0.0;
     303    mosaic[j].McalChiSq = 0.0;
     304
     305    mosaic[j].dMsys     = subset[i].flags;
     306    mosaic[j].flags     = subset[i].flags;
     307    mosaic[j].secz      = subset[i].secz;
     308    mosaic[j].photcode  = GetPhotcodeEquivCodebyCode (subset[i].photcode);
    304309  }
    305310
     
    377382  // init the mosaic array values
    378383  for (i = 0; i < Nmosaic; i++) {
    379     mosaic[i].start    = startTimesMosaic[i];
    380     mosaic[i].stop     = 0;
    381     mosaic[i].Mcal     = 0.0;
    382     mosaic[i].dMcal    = 0.0;
    383     mosaic[i].dMsys    = 0.0;
    384     mosaic[i].Xm       = 0.0;
    385     mosaic[i].flags    = 0;
    386     mosaic[i].secz     = NAN;
    387     mosaic[i].photcode = 0;
    388     mosaic[i].skipCal  = FALSE;
     384    mosaic[i].start     = startTimesMosaic[i];
     385    mosaic[i].stop      = 0;
     386    mosaic[i].McalPSF   = 0.0; // note : at the end, mosaic.Mcal is added back to the input images
     387    mosaic[i].McalAPER  = 0.0; // note : mosaic stores only offsets relative to the original image values
     388    mosaic[i].dMcal     = 0.0; // note : at the end, mosaic.Mcal is added back to the input images
     389    mosaic[i].dMsys     = 0.0;
     390    mosaic[i].McalChiSq = 0.0;// NAN or 0.0?
     391    mosaic[i].flags     = 0;
     392    mosaic[i].secz      = NAN;
     393    mosaic[i].photcode  = 0;
     394    mosaic[i].skipCal   = FALSE;
    389395   
    390396    memset (&mosaic[i].coords, 0, sizeof(Coords));
     
    438444    }
    439445    mosaic[j].stop     = stop;
    440     mosaic[j].Mcal     = 0.0;
    441     mosaic[j].dMcal    = 0.0;
    442     mosaic[j].Xm       = 0.0;
    443     mosaic[j].dMsys    = image[i].flags;
    444     mosaic[j].flags    = image[i].flags;
    445     mosaic[j].secz     = image[i].secz;
    446     mosaic[j].photcode = GetPhotcodeEquivCodebyCode (image[i].photcode);
     446    mosaic[j].McalPSF   = 0.0;
     447    mosaic[j].McalAPER  = 0.0;
     448    mosaic[j].dMcal     = 0.0;
     449    mosaic[j].McalChiSq = 0.0;
     450    mosaic[j].dMsys     = image[i].flags;
     451    mosaic[j].flags     = image[i].flags;
     452    mosaic[j].secz      = image[i].secz;
     453    mosaic[j].photcode  = GetPhotcodeEquivCodebyCode (image[i].photcode);
    447454  }
    448455  MARKTIME("assign images to mosaic: %f sec\n", dtime);
     
    594601    mosaic[i].coords.cdelt2 = 1.0 / 3600.0;
    595602
    596     mosaic[i].Mcal   = 0.0;
    597     mosaic[i].dMcal  = 0.0;
    598     mosaic[i].Xm     = 0.0;
     603    mosaic[i].McalPSF   = 0.0;
     604    mosaic[i].McalAPER  = 0.0;
     605    mosaic[i].dMcal     = 0.0;
     606    mosaic[i].dMsys     = 0.0;
     607    mosaic[i].McalChiSq = 0.0;
    599608  }
    600609  return;
     
    612621  double dS, dX, dY;
    613622  double R, D, Rmin, Rmax, Dmin, Dmax;
    614   double Mcal, dMcal, Xm;
     623  double McalPSF, McalAPER, dMcal, McalChiSq;
    615624
    616625  fprintf (stderr, "*** moving Mcal from image.Mcal to mosaic.Mcal ***\n");
     
    621630    Dmax = Rmax = -360.0;
    622631    dS = 0.0;
    623     Mcal = dMcal = Xm = 0;
     632    McalPSF = McalAPER = dMcal = McalChiSq = 0;
    624633    for (j = 0; j < MosaicN_Image[i]; j++) {
    625634      m = MosaicToImage[i][j];
     
    678687      // if it helps, note that ubercal uses a single zp per exposure, so the mean of those values is the same as the value
    679688
    680       Mcal  += image[m].Mcal;
    681       dMcal += image[m].dMcal;
    682       Xm    += image[m].Xm;
    683 
    684       image[m].Mcal   = 0.0;
    685       image[m].dMcal  = NAN;
    686       image[m].Xm     = NAN_S_SHORT;
     689      McalPSF   += image[m].McalPSF;
     690      McalAPER  += image[m].McalAPER;
     691      dMcal     += image[m].dMcal;
     692      McalChiSq += image[m].McalChiSq;
     693
     694      image[m].McalPSF   = 0.0;
     695      image[m].McalAPER  = 0.0;
     696      image[m].dMcal     = NAN;
     697      image[m].McalChiSq = NAN;
    687698    }
    688699    dS /= MosaicN_Image[i];
     
    696707    RD_to_XY (&dX, &dY, Rmax, Dmax, &mosaic[i].coords);
    697708
    698     mosaic[i].Mcal   = Mcal / MosaicN_Image[i];
    699     mosaic[i].dMcal  = dMcal / MosaicN_Image[i];
    700     mosaic[i].Xm     = Xm / MosaicN_Image[i];
     709    mosaic[i].McalPSF   = McalPSF / MosaicN_Image[i];
     710    mosaic[i].McalAPER  = McalAPER / MosaicN_Image[i];
     711    mosaic[i].dMcal     = dMcal / MosaicN_Image[i];
     712    mosaic[i].McalChiSq = McalChiSq / MosaicN_Image[i];
    701713  }
    702714  if (!USE_GRID) return;
     
    724736    for (j = 0; j < MosaicN_Image[i]; j++) {
    725737      im = MosaicToImage[i][j];
    726       image[im].Mcal += mosaic[i].Mcal;
    727       image[im].dMcal = mosaic[i].dMcal;
    728       image[im].Xm = mosaic[i].Xm;
     738      image[im].McalPSF    += mosaic[i].McalPSF;
     739      image[im].McalAPER   += mosaic[i].McalAPER;
     740      image[im].dMcal       = mosaic[i].dMcal;
     741      image[im].McalChiSq   = mosaic[i].McalChiSq;
    729742      image[im].ubercalDist = mosaic[i].ubercalDist;
    730       image[im].dMagSys = mosaic[i].dMsys;
    731       image[im].nFitPhotom = mosaic[i].nFitPhotom;
    732       image[im].flags |= (mosaic[i].flags & ID_IMAGE_PHOTOM_FEW);
    733       image[im].flags |= (mosaic[i].flags & ID_IMAGE_PHOTOM_POOR);
    734     }
    735     mosaic[i].Mcal = 0.0;
     743      image[im].dMagSys     = mosaic[i].dMsys;
     744      image[im].nFitPhotom  = mosaic[i].nFitPhotom;
     745      image[im].flags      |= (mosaic[i].flags & ID_IMAGE_PHOTOM_FEW);
     746      image[im].flags      |= (mosaic[i].flags & ID_IMAGE_PHOTOM_POOR);
     747    }
     748    mosaic[i].McalPSF  = 0.0;
     749    mosaic[i].McalAPER = 0.0;
    736750  }     
    737751}
     
    886900
    887901  if (mosaic[i].flags & IMAGE_BAD) return (NAN); 
    888   value = mosaic[i].Mcal;
     902  value = mosaic[i].McalPSF;
    889903  return (value);
    890904}
     
    911925  off_t Nmax;
    912926  int PoorImages;
    913   double *list;
    914   double *dlist;
    915   double *Mlist;
    916   double *dMlist;
     927  double *psfMagList;
     928  double *psfErrList;
     929  double *brightMagList;
     930  double *brightErrList;
     931  double *kronMagList;
     932  double *kronErrList;
    917933} SetMmosInfo;
    918934
     
    945961
    946962  if (allocLists) {
    947     ALLOCATE (info->list, double, Nmax);
    948     ALLOCATE (info->dlist, double, Nmax);
    949     ALLOCATE (info->Mlist, double, Nmax);
    950     ALLOCATE (info->dMlist, double, Nmax);
     963    ALLOCATE (info->psfMagList, double, Nmax);
     964    ALLOCATE (info->psfErrList, double, Nmax);
     965    ALLOCATE (info->kronMagList, double, Nmax);
     966    ALLOCATE (info->kronErrList, double, Nmax);
     967    ALLOCATE (info->brightMagList, double, Nmax);
     968    ALLOCATE (info->brightErrList, double, Nmax);
    951969  }
    952970}
    953971
    954972void SetMmosInfoFree (SetMmosInfo *info) {
    955   free (info->list);
    956   free (info->dlist);
    957   free (info->Mlist);
    958   free (info->dMlist);
     973  free (info->psfMagList   );
     974  free (info->psfErrList   );
     975  free (info->kronMagList  );
     976  free (info->kronErrList  );
     977  free (info->brightMagList);
     978  free (info->brightErrList);
    959979}
    960980
     
    10571077  liststats_setmode (&stats, "INNER_WTMEAN");
    10581078
    1059   double *list   = info->list;
    1060   double *dlist  = info->dlist;
    1061   double *Mlist  = info->Mlist;
    1062   double *dMlist = info->dMlist;
     1079  double *psfMagList    = info->psfMagList;
     1080  double *psfErrList    = info->psfErrList;
     1081  double *kronMagList   = info->kronMagList;
     1082  double *kronErrList   = info->kronErrList;
     1083  double *brightMagList = info->brightMagList;
     1084  double *brightErrList = info->brightErrList;
    10631085
    10641086  assert (Nmos >= 0);
     
    11251147  int N = 0;
    11261148  for (j = 0; j < N_onMosaic[Nmos]; j++) {
    1127     float Msys, Mrel, Mcal, Mgrid, Mflat;
    11281149     
    11291150    off_t m = MosaicToMeasure[Nmos][j];
    11301151    off_t c = MosaicToCatalog[Nmos][j];
    11311152     
     1153    // NOTE : we are only using Mcal == McalPSF in this function; we set McalAPER to McalPSF
    11321154    if (fout) {
    1133       Mcal  = getMcal  (m, c, flatcorr, catalog);
    1134       Mgrid = getMgrid (m, c);
    1135       Mrel  = getMrel  (catalog, m, c);
    1136       Mflat = getMflat (m, c, flatcorr, catalog);
     1155      float Mcal     = getMcal  (m, c, MAG_CLASS_PSF);
     1156      float Mgrid    = getMgrid (m, c);
     1157      float MrelPSF  = getMrel  (catalog, m, c, MAG_CLASS_PSF, MAG_SRC_CHP);
     1158      float Mflat    = getMflat (m, c, flatcorr, catalog);
    11371159
    11381160      off_t n = catalog[c].measureT[m].averef;
    1139       Msys = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_PSF);
    1140 
    1141       float delta = Msys - Mrel - Mcal - Mgrid + Mflat;
     1161      float MsysPSF = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_PSF);
     1162
     1163      float delta = MsysPSF - MrelPSF - Mcal - Mgrid + Mflat;
    11421164
    11431165      int isBad = (catalog[c].measureT[m].dbFlags & MEAS_BAD);
    11441166
    1145       fprintf (fout, "%f %f : %f %f %f %f %f  : %f %d\n", catalog[c].averageT[n].R, catalog[c].averageT[n].D, Msys, Mrel, Mcal, Mgrid, Mflat, delta, isBad);
     1167      fprintf (fout, "%f %f : %f %f %f %f %f  : %f %d\n", catalog[c].averageT[n].R, catalog[c].averageT[n].D, MsysPSF, MrelPSF, Mcal, Mgrid, Mflat, delta, isBad);
    11461168    }
    11471169
     
    11501172      continue;
    11511173    }
    1152     Mcal  = getMcal  (m, c, flatcorr, catalog);
     1174    float Mcal = getMcal  (m, c, MAG_CLASS_PSF);
    11531175    if (isnan(Mcal)) {
    11541176      info->Ncal++;
    11551177      continue;
    11561178    }
    1157     Mgrid = getMgrid (m, c);
     1179    float Mgrid = getMgrid (m, c);
    11581180    if (isnan(Mgrid)) {
    11591181      info->Ngrid ++;
    11601182      continue;
    11611183    }
    1162     Mrel  = getMrel  (catalog, m, c);
    1163     if (isnan(Mrel)) {
     1184
     1185    // Mrel* is the average magnitude for this star.  For PS1 stacks, we have too much
     1186    // PSF variability.  We need to calibrate the PSF magnitudes separately from the
     1187    // Aperture-like magnitues.  (We have an option to use the kron magnitudes or the
     1188    // other apertures here).  I basically need to do this analysis separately for each
     1189    // magnitude type
     1190   
     1191    float MrelPSF = getMrel  (catalog, m, c, MAG_CLASS_PSF, MAG_SRC_CHP);
     1192    if (isnan(MrelPSF)) {
    11641193      info->Nrel ++;
    11651194      continue;
    11661195    }
     1196    float MrelKron = getMrel  (catalog, m, c, MAG_CLASS_KRON, MAG_SRC_CHP);
    11671197     
    11681198    // image.Mcal is not supposed to include the flat-field correction, so we need to
     
    11711201    // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal - Mflat)
    11721202
    1173     Mflat = getMflat (m, c, flatcorr, catalog);
     1203    float Mflat = getMflat (m, c, flatcorr, catalog);
    11741204
    11751205    off_t n = catalog[c].measureT[m].averef;
    1176     Msys = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_PSF);
    1177     if (isnan(Msys)) {
     1206    float MsysPSF = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_PSF);
     1207    if (isnan(MsysPSF)) {
    11781208      info->Nsys++;
    11791209      continue;
    11801210    }
     1211    float MsysKron = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt], MAG_CLASS_KRON);
    11811212
    11821213    PhotCode *code = GetPhotcodebyCode (catalog[c].measureT[m].photcode);
     
    11931224    assert (Nbright >= 0);
    11941225
    1195     list[N]  = Msys - Mrel - Mcal - Mgrid + Mflat;
    1196     dlist[N] = MAX (catalog[c].measureT[m].dM, MIN_ERROR);
     1226    psfMagList[N]  = MsysPSF - MrelPSF - Mcal - Mgrid + Mflat;
     1227    psfErrList[N]  = MAX (catalog[c].measureT[m].dM, MIN_ERROR);
     1228    kronMagList[N] = MsysKron - MrelKron - Mcal - Mgrid + Mflat;
     1229    kronErrList[N] = psfErrList[N];
    11971230    if (catalog[c].measureT[m].dM < IMFIT_SYS_SIGMA_LIM) {
    1198       Mlist[Nbright] = list[N];
    1199       dMlist[Nbright] = dlist[N];
     1231      brightMagList[Nbright] = psfMagList[N];
     1232      brightErrList[Nbright] = psfErrList[N];
    12001233      Nbright ++;
    12011234    }
     
    12231256  }
    12241257
    1225   liststats (list, dlist, NULL, N, &stats);
     1258  liststats (psfMagList, psfErrList, NULL, N, &stats);
    12261259  if (VERBOSE2 && info->PoorImages) fprintf (stderr, "Mmos: %f %f %d %d\n", stats.mean, stats.sigma, stats.Nmeas, N);
    12271260
    1228   myMosaic[0].Mcal  = stats.mean;
    1229   myMosaic[0].dMcal = stats.error;
     1261  // for now, I have no reason to measure these separately for camera-level images
     1262  myMosaic[0].McalPSF    = stats.mean;
     1263  myMosaic[0].dMcal      = stats.error;
     1264  myMosaic[0].McalChiSq  = stats.chisq;
    12301265  myMosaic[0].nFitPhotom = N;
    1231   myMosaic[0].Xm    = 100.0*log10(stats.chisq);
     1266
     1267  liststats (kronMagList, kronErrList, NULL, N, &stats);
     1268  myMosaic[0].McalAPER   = stats.mean;
    12321269
    12331270  if (testImage) {
     
    12371274  if (PLOTSTUFF) {
    12381275    fprintf (stderr, "Mmos: %6.3f %6.3f +/- %6.3f %5d %5d | %s\n", stats.mean, stats.median, stats.sigma, stats.Nmeas, N, image[MosaicToImage[Nmos][0]].name);
    1239     plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
     1276    plot_setMcal (psfMagList, N, &stats, CLOUD_TOLERANCE);
    12401277  }
    12411278
    12421279  // bright end scatter
    1243   liststats (Mlist, dMlist, NULL, Nbright, &stats);
     1280  liststats (brightMagList, brightErrList, NULL, Nbright, &stats);
    12441281  myMosaic[0].dMsys = stats.sigma;
    12451282
    1246   if (myMosaic[0].Mcal < -CLOUD_TOLERANCE) {
    1247     myMosaic[0].Mcal = 0.0;
     1283  if (myMosaic[0].McalPSF < -CLOUD_TOLERANCE) {
     1284    myMosaic[0].McalPSF = 0.0;
    12481285  }
    12491286
    12501287  if (testImage) {
    1251     fprintf (stderr, "%f %f  :  %f\n", myMosaic[0].Mcal, myMosaic[0].dMsys, pow (10.0, 0.01*myMosaic[0].Xm));
     1288    fprintf (stderr, "%f %f  :  %f\n", myMosaic[0].McalPSF, myMosaic[0].dMsys, myMosaic[0].McalChiSq);
    12521289  }
    12531290
     
    14271464    imageOffset[i] = 0.0;
    14281465
    1429     if (VERBOSE2 && (fabs(mosaic[i].Mcal) < CLOUD_TOLERANCE)) {
    1430       fprintf (stderr, "cloud-free: %s : %f\n", image[MosaicToImage[i][0]].name, mosaic[i].Mcal);
     1466    if (VERBOSE2 && (fabs(mosaic[i].McalPSF) < CLOUD_TOLERANCE)) {
     1467      fprintf (stderr, "cloud-free: %s : %f\n", image[MosaicToImage[i][0]].name, mosaic[i].McalPSF);
    14311468    }
    1432     if (VERBOSE2 && (mosaic[i].Mcal < -CLOUD_TOLERANCE)) {
    1433       imageOffset[i] = -mosaic[i].Mcal;
    1434       // NOTE the negative sign: down below, we are going to add in the negative of Mcal
     1469    if (VERBOSE2 && (mosaic[i].McalPSF < -CLOUD_TOLERANCE)) {
     1470      imageOffset[i] = -mosaic[i].McalPSF;
     1471      // NOTE the negative sign: down below, we are going to add in the negative of McalPSF
    14351472      // to this image, and the propagated mean values for other images
    1436       fprintf (stderr, "anti-clouds: %s : %f\n", image[MosaicToImage[i][0]].name, mosaic[i].Mcal);
     1473      fprintf (stderr, "anti-clouds: %s : %f\n", image[MosaicToImage[i][0]].name, mosaic[i].McalPSF);
    14371474    }
    1438     if (VERBOSE2 && (mosaic[i].Mcal > CLOUD_TOLERANCE)) {
    1439       fprintf (stderr, "cloudy    : %s : %f\n", image[MosaicToImage[i][0]].name, mosaic[i].Mcal);
     1475    if (VERBOSE2 && (mosaic[i].McalPSF > CLOUD_TOLERANCE)) {
     1476      fprintf (stderr, "cloudy    : %s : %f\n", image[MosaicToImage[i][0]].name, mosaic[i].McalPSF);
    14401477    }
    14411478  }
     
    15401577    }
    15411578
    1542     // a negative cloud image (cloud: Mcal > 0; anti-clouds: Mcal < 0; imageOffset = -Mcal)
     1579    // a negative cloud image (cloud: McalPSF > 0; anti-clouds: McalPSF < 0; imageOffset = -McalPSF)
    15431580    if (imageOffset[i] > 0.0) continue;
    15441581
     
    15641601  // find the images / mosaics with negative clouds and save their offset
    15651602  for (i = 0; i < Nmosaic; i++) {
    1566     mosaic[i].Mcal += imageOffset[i];
     1603    mosaic[i].McalPSF += imageOffset[i];
    15671604  }
    15681605
     
    16051642    if (KEEP_UBERCAL && (mosaic[i].flags & ID_IMAGE_PHOTOM_UBERCAL)) continue;
    16061643
    1607     list[n] = mosaic[i].Mcal;
     1644    list[n] = mosaic[i].McalPSF;
    16081645    dlist[n] = 1;
    16091646    n++;
     
    16771714      c = MosaicToCatalog[i][j];
    16781715
    1679       Mcal = getMcal  (m, c, flatcorr, catalog);
     1716      Mcal = getMcal  (m, c, MAG_CLASS_PSF);
    16801717      if (isnan(Mcal)) continue;
    16811718      Mgrid = getMgrid (m, c);
    16821719      if (isnan(Mgrid)) continue;
    1683       Mrel = getMrel  (catalog, m, c);
     1720      Mrel = getMrel  (catalog, m, c, MAG_CLASS_PSF, MAG_SRC_CHP);
    16841721      if (isnan(Mrel)) continue;
    16851722      N++;
     
    17191756    if (KEEP_UBERCAL && (mosaic[i].flags & ID_IMAGE_PHOTOM_UBERCAL)) continue;
    17201757
    1721     list[n]  = pow (10.0, 0.01*mosaic[i].Xm);
     1758    list[n]  = mosaic[i].McalChiSq;
    17221759    dlist[n] = 1;
    17231760    n++;
     
    17511788    if (mosaic[i].flags & IMAGE_BAD) continue;
    17521789    if (mosaic[i].skipCal) continue;
    1753     mlist[N] = mosaic[i].Mcal;
     1790    mlist[N] = mosaic[i].McalPSF;
    17541791    slist[N] = mosaic[i].dMcal;
    17551792    dlist[N] = 1;
     
    17811818      Nscatter ++;
    17821819    }
    1783     if (fabs(mosaic[i].Mcal - MedOffset) > MaxOffset) {
     1820    if (fabs(mosaic[i].McalPSF - MedOffset) > MaxOffset) {
    17841821      mark = TRUE;
    17851822      Noffset ++;
     
    18581895
    18591896  for (i = 0; i < Nmosaic; i++) {
    1860     Mlist[i] = mosaic[i].Mcal;
     1897    Mlist[i] = mosaic[i].McalPSF;
    18611898    dlist[i] = mosaic[i].dMcal;
    18621899    xlist[i] = mosaic[i].secz;
  • trunk/Ohana/src/relphot/src/StarOps.c

    r40019 r40291  
    3434
    3535
    36 float getMrel (Catalog *catalog, off_t meas, int cat) {
    37 
    38   int Nsec, Nsecfilt, photcode;
    39   int ave;
    40   float value;
    41 
    42   ave = catalog[cat].measureT[meas].averef;
    43   photcode = catalog[cat].measureT[meas].photcode;
     36//
     37float getMrel (Catalog *catalog, off_t meas, int cat, dvoMagClassType class, dvoMagSourceType source) {
     38
     39  int ave = catalog[cat].measureT[meas].averef;
     40  int photcode = catalog[cat].measureT[meas].photcode;
    4441
    4542  int ecode = GetPhotcodeEquivCodebyCode (photcode);
    46   Nsec = GetPhotcodeNsec(ecode);
    47   Nsecfilt = GetPhotcodeNsecfilt ();
     43  int Nsec = GetPhotcodeNsec(ecode);
     44  int Nsecfilt = GetPhotcodeNsecfilt ();
     45
     46  int entry = Nsecfilt*ave+Nsec;
     47
     48  SecFilt *secfilt = &catalog[cat].secfilt[entry];
    4849
    4950  // is this star OK?
    50   if (catalog[cat].secfilt[Nsecfilt*ave+Nsec].flags & STAR_BAD) return (NAN); 
    51  
    52   value = catalog[cat].secfilt[Nsecfilt*ave+Nsec].M;
    53   return (value);
     51  if (secfilt->flags & STAR_BAD) return (NAN); 
     52
     53  switch (class) {
     54    case MAG_CLASS_PSF:
     55      switch (source) {
     56        case MAG_SRC_CHP:
     57          return secfilt->MpsfChp;
     58        case MAG_SRC_WRP:
     59          return secfilt->MpsfWrp;
     60        case MAG_SRC_STK:
     61          return secfilt->MpsfStk;
     62        default:
     63          return NAN;
     64      }
     65      break;
     66    case MAG_CLASS_KRON:
     67      switch (source) {
     68        case MAG_SRC_CHP:
     69          return secfilt->MkronChp;
     70        case MAG_SRC_WRP:
     71          return secfilt->MkronWrp;
     72        case MAG_SRC_STK:
     73          return secfilt->MkronStk;
     74        default:
     75          return NAN;
     76      }
     77      break;
     78    case MAG_CLASS_APER:
     79      switch (source) {
     80        case MAG_SRC_CHP:
     81          return secfilt->MapChp;
     82        case MAG_SRC_WRP:
     83          return secfilt->MapWrp;
     84        case MAG_SRC_STK:
     85          return secfilt->MapStk;
     86        default:
     87          return NAN;
     88      }
     89      break;
     90    case MAG_CLASS_NONE:
     91    case MAG_CLASS_DEV: // DeVaucouleur Model (only for galphot)
     92    case MAG_CLASS_EXP: // Exponential Model (only for galphot)
     93    case MAG_CLASS_SER: // Sersic Model (only for galphot)
     94      return NAN;
     95      break;
     96  }
     97  return NAN; // should not be able to reach here
    5498}
    5599
     
    429473  int i;
    430474  off_t j, k, m;
    431   float Mcal, Mmos, Mgrid;
    432475
    433476  MEAS_BAD = ID_MEAS_NOCAL;
     
    440483      for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
    441484        if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
    442         Mcal  = getMcal  (m, i, flatcorr, catalog);
    443         if (isnan(Mcal)) continue;
    444         Mmos  = getMmos  (m, i);
     485        float McalPSF  = getMcal  (m, i, MAG_CLASS_PSF);
     486        float McalAPER = getMcal  (m, i, MAG_CLASS_APER);
     487        if (isnan(McalPSF)) continue;
     488        float Mmos  = getMmos  (m, i);
    445489        if (isnan(Mmos)) continue;
    446         Mgrid = getMgrid (m, i);
     490        float Mgrid = getMgrid (m, i);
    447491        if (isnan(Mgrid)) continue;
     492
     493        // Note that this operation is setting measure->McalAPER to image->McalAPER only
     494        // for the stacks.  Elsewhere (setMrelCatalog) we are using image->McalPSF for all
     495        // types of measurements EXCEPT stacks.
     496
     497        // we need to use McalAPER for stacks (and only stacks)
     498        int useStackAper = !USE_MCAL_PSF_FOR_STACK_APER && isGPC1stack(catalog[i].measureT[m].photcode);
    448499
    449500        // note that measurements for which the image is not selected will not be modified
     
    451502
    452503        // set the output calibration
    453         catalog[i].measure[m].Mcal = Mcal + Mmos + Mgrid;
     504        catalog[i].measure[m].McalPSF  = McalPSF  + Mmos + Mgrid;
     505        catalog[i].measure[m].McalAPER = useStackAper ? McalAPER + Mmos + Mgrid : McalPSF  + Mmos + Mgrid;
    454506
    455507        if (catalog[i].measureT[m].dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
    456           myAssert (isfinite(catalog[i].measure[m].Mcal), "oops, broke an ubercal mag");
     508          myAssert (isfinite(catalog[i].measure[m].McalPSF), "oops, broke an ubercal mag");
    457509        }
    458510      }
     
    501553}
    502554
     555int dumpMags (FILE *fout, Catalog *catalog, int Ncatalog) {
     556
     557  int i, n;
     558  off_t j;
     559
     560  int Nsecfilt = GetPhotcodeNsecfilt ();
     561
     562  for (i = 0; i < Ncatalog; i++) {
     563    for (j = 0; (j < catalog[i].Naverage) && (j < 2); j++) {
     564      fprintf (fout, "%08x %08x %10.6f %10.6f : \n", catalog[i].averageT[j].catID, catalog[i].averageT[j].objID, catalog[i].averageT[j].R, catalog[i].averageT[j].D);
     565      for (n = 0; n < 5; n++) {
     566        fprintf (fout, "secf  %5d | %6.3f %6.3f | %6.3f %6.3f | %6.3f %6.3f\n", n, catalog[i].secfilt[j*Nsecfilt + n].MpsfChp, catalog[i].secfilt[j*Nsecfilt + n].MkronChp, catalog[i].secfilt[j*Nsecfilt + n].MpsfStk, catalog[i].secfilt[j*Nsecfilt + n].MkronStk, catalog[i].secfilt[j*Nsecfilt + n].MpsfWrp, catalog[i].secfilt[j*Nsecfilt + n].MkronWrp);
     567      }
     568      int m = catalog[i].averageT[j].measureOffset;
     569      for (n = 0; n < catalog[i].averageT[j].Nmeasure; n++) {
     570        fprintf (fout, "meas %5d %5d | %6.3f %6.3f | %6.3f %6.3f\n", m+n, catalog[i].measureT[m+n].photcode, catalog[i].measureT[m+n].M, catalog[i].measureT[m+n].Mkron, catalog[i].measureT[m+n].McalPSF, catalog[i].measureT[m+n].McalAPER);
     571      }
     572    }
     573  }
     574  return TRUE;
     575}
     576
    503577void clean_stars (Catalog *catalog, int Ncatalog) {
    504578
     
    513587  if (VERBOSE) fprintf (stderr, "marking poor stars\n");
    514588
    515   /* find Xm median -> ChiSq lim must be > median */
     589  /* find Mchisq median -> ChiSq lim must be > median */
    516590  for (i = Ntot = 0; i < Ncatalog; i++) {
    517591    Ntot += catalog[i].Naverage;
     
    535609        if (isnan(Mchisq)) continue;
    536610        xlist[Ntot] = Mchisq;
    537         slist[Ntot] = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
     611        slist[Ntot] = catalog[i].secfilt[Nsecfilt*j+Nsec].dMpsfChp;
    538612        dlist[Ntot] = 1;
    539613        Ntot ++;
     
    551625    for (i = 0; i < Ncatalog; i++) {
    552626      for (j = 0; j < catalog[i].Naverage; j++) {
    553         dM = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
     627        dM = catalog[i].secfilt[Nsecfilt*j+Nsec].dMpsfChp;
    554628        float Mchisq = catalog[i].secfilt[Nsecfilt*j+Nsec].Mchisq;
    555629        mark = (dM > MaxScatter) || (isnan(Mchisq)) || (Mchisq > MaxChisq);
     
    578652// NSIGMA_REJECT (5) sigma of the mean
    579653
     654// this function only operations on the PSF magnitudes
     655
    580656# define NSIGMA_CLIP 3.0
    581657# define NSIGMA_REJECT 5.0
     
    644720         
    645721          // NOTE: we do not skip MEAS_BAD because this measurement is just an internal assessment of the outliers
    646           Mcal  = getMcal  (m, i, flatcorr, catalog);
     722          Mcal  = getMcal  (m, i, MAG_CLASS_PSF);
    647723          if (isnan(Mcal)) { Ncal ++; continue; }
    648724          Mmos  = getMmos  (m, i);
     
    695771         
    696772          // NOTE: we do not skip MEAS_BAD because this measurement is just an internal assessment of the outliers
    697           Mcal  = getMcal  (m, i, flatcorr, catalog);
     773          Mcal  = getMcal  (m, i, MAG_CLASS_PSF);
    698774          if (isnan(Mcal)) continue;
    699775          Mmos  = getMmos  (m, i);
     
    776852        int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
    777853        if (ecode != seccode) { continue;}
    778         Mcal = getMcal  (m, i, flatcorr, catalog);
     854        Mcal = getMcal  (m, i, MAG_CLASS_PSF);
    779855        if (isnan(Mcal)) { continue;}
    780856        Mmos = getMmos  (m, i);
     
    865941      if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue; 
    866942
    867       dM = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
     943      dM = catalog[i].secfilt[Nsecfilt*j+Nsec].dMpsfChp;
    868944      if (isnan(dM)) continue;
    869945      list[n] = dM;
     
    906982      for (j = 0; j < catalog[i].Naverage; j++) {
    907983        if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue; 
    908         dMrel = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
     984        dMrel = catalog[i].secfilt[Nsecfilt*j+Nsec].dMpsfChp;
    909985        bin = dMrel / 0.00025;
    910986        bin = MAX (0, MIN (NBIN-1, bin));
     
    9461022      for (j = 0; j < catalog[i].Naverage; j++) {
    9471023        if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;
    948         xlist[N] = catalog[i].secfilt[Nsecfilt*j+Nsec].M;
     1024        xlist[N] = catalog[i].secfilt[Nsecfilt*j+Nsec].MpsfChp;
    9491025        value    = catalog[i].secfilt[Nsecfilt*j+Nsec].Mchisq;
    9501026        if (isnan((double)(value))) continue;
  • trunk/Ohana/src/relphot/src/args.c

    r39926 r40291  
    325325    remove_argument (N, &argc, argv);
    326326    CALIBRATE_STACKS_AND_WARPS = TRUE;
     327  }
     328  USE_MCAL_PSF_FOR_STACK_APER = FALSE;
     329  if ((N = get_argument (argc, argv, "-use-mcal-psf-for-stack-aper"))) {
     330    remove_argument (N, &argc, argv);
     331    USE_MCAL_PSF_FOR_STACK_APER = TRUE;
    327332  }
    328333
     
    696701  }
    697702
     703  USE_MCAL_PSF_FOR_STACK_APER = FALSE;
     704  if ((N = get_argument (argc, argv, "-use-mcal-psf-for-stack-aper"))) {
     705    remove_argument (N, &argc, argv);
     706    USE_MCAL_PSF_FOR_STACK_APER = TRUE;
     707  }
     708
    698709  /* define time */
    699710  TimeSelect = FALSE;
  • trunk/Ohana/src/relphot/src/bcatalog.c

    r39641 r40291  
    153153        if (subcatalog[0].measureT[Nmeasure].dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
    154154          if (!KEEP_UBERCAL) {
    155             subcatalog[0].measureT[Nmeasure].Mcal = 0.0;
     155            subcatalog[0].measureT[Nmeasure].McalPSF  = 0.0;
     156            subcatalog[0].measureT[Nmeasure].McalAPER = 0.0;
    156157            subcatalog[0].measureT[Nmeasure].dbFlags &= ~ID_MEAS_PHOTOM_UBERCAL;
    157158          }
    158159        } else {
    159160          if (getImageEntry (Nmeasure, Ncat) >= 0) {
    160             subcatalog[0].measureT[Nmeasure].Mcal = 0.0;
     161            subcatalog[0].measureT[Nmeasure].McalPSF  = 0.0;
     162            subcatalog[0].measureT[Nmeasure].McalAPER = 0.0;
    161163          }
    162164        }
  • trunk/Ohana/src/relphot/src/launch_region_hosts.c

    r39511 r40291  
    5858    strextend (&command, "-cloud-limit %f", CLOUD_TOLERANCE);
    5959
    60     if (VERBOSE)             strextend (&command, "-v");
    61     if (VERBOSE2)            strextend (&command, "-vv");
    62     if (RESET)               strextend (&command, "-reset");
    63     if (RESET_ZEROPTS)       strextend (&command, "-reset-zpts");
    64     if (!KEEP_UBERCAL)       strextend (&command, "-reset-ubercal");
    65     if (DophotSelect)        strextend (&command, "-dophot %d", DophotValue);
    66     if (ImagSelect)          strextend (&command, "-instmag %f %f", ImagMin, ImagMax);
    67     if (MaxDensityUse)       strextend (&command, "-max-density %f", MaxDensityValue);
    68     if (SyntheticPhotometry) strextend (&command, "-synthphot");
    69     if (USE_BASIC_CHECK)     strextend (&command, "-basic-image-search");
     60    if (VERBOSE)                    strextend (&command, "-v");
     61    if (VERBOSE2)                   strextend (&command, "-vv");
     62    if (RESET)                      strextend (&command, "-reset");
     63    if (RESET_ZEROPTS)              strextend (&command, "-reset-zpts");
     64    if (!KEEP_UBERCAL)              strextend (&command, "-reset-ubercal");
     65    if (DophotSelect)               strextend (&command, "-dophot %d", DophotValue);
     66    if (ImagSelect)                 strextend (&command, "-instmag %f %f", ImagMin, ImagMax);
     67    if (MaxDensityUse)              strextend (&command, "-max-density %f", MaxDensityValue);
     68    if (SyntheticPhotometry)        strextend (&command, "-synthphot");
     69    if (USE_BASIC_CHECK)            strextend (&command, "-basic-image-search");
    7070
    71     if (UPDATE)              strextend (&command, "-update");
    72     if (MOSAIC_ZEROPT)       strextend (&command, "-mosaic");
    73     if (FREEZE_IMAGES)       strextend (&command, "-imfreeze");
    74     if (FREEZE_MOSAICS)      strextend (&command, "-mosfreeze");
    75     if (PARALLEL)            strextend (&command, "-parallel");
    76     if (PARALLEL_MANUAL)     strextend (&command, "-parallel-manual");
    77     if (PARALLEL_SERIAL)     strextend (&command, "-parallel-serial");
     71    if (UPDATE)                     strextend (&command, "-update");
     72    if (MOSAIC_ZEROPT)              strextend (&command, "-mosaic");
     73    if (FREEZE_IMAGES)              strextend (&command, "-imfreeze");
     74    if (FREEZE_MOSAICS)             strextend (&command, "-mosfreeze");
     75    if (CALIBRATE_STACKS_AND_WARPS) strextend (&command, "-only-stacks-and-warps");
     76    if (USE_MCAL_PSF_FOR_STACK_APER) { strextend (&command, "-use-mcal-psf-for-stack-aper"); }
     77
     78    if (PARALLEL)                   strextend (&command, "-parallel");
     79    if (PARALLEL_MANUAL)            strextend (&command, "-parallel-manual");
     80    if (PARALLEL_SERIAL)            strextend (&command, "-parallel-serial");
    7881
    7982    // XXX deprecate this if we are happy with the new version
  • trunk/Ohana/src/relphot/src/liststats.c

    r39648 r40291  
    194194# define WEIGHT_THRESHOLD 0.3
    195195
    196 int fit_least_squares (double *fit, double *y, double *dy, double *wgt, double *wt, int Npts);
     196int fit_least_squares (double *fit, double *err, double *y, double *dy, double *wgt, double *wt, int Npts);
    197197double VectorFractionInterpolate (double *values, float fraction, int Npts);
    198198double weight_cauchy (double x);
     
    253253   
    254254    double oldValue = value;
    255     if (!fit_least_squares (&value, dataset->flxlist, dataset->errlist, dataset->wgtlist, dataset->wtvals, Npoints)) {
     255    if (!fit_least_squares (&value, NULL, dataset->flxlist, dataset->errlist, dataset->wgtlist, dataset->wtvals, Npoints)) {
    256256      value = oldValue;
    257257      break;
     
    330330    }
    331331
    332     if (!fit_least_squares (&value, dataset->ysample, dataset->dysample, dataset->wtsample, NULL, Nkeep)) continue;
     332    if (!fit_least_squares (&value, NULL, dataset->ysample, dataset->dysample, dataset->wtsample, NULL, Nkeep)) continue;
    333333
    334334    dataset->bvalue[Nboot] = value;
     
    342342  stats->error = (Shi - Slo) / 2.0;
    343343
     344  // bootstrap can sometimes yield an excessively-optimistic result for the error.  Do not let
     345  // the reported error be smaller than the formal error
     346  double errvalue;
     347  if (fit_least_squares (&value, &errvalue, dataset->ykeep, dataset->dykeep, dataset->wtkeep, NULL, Nkeep)) {
     348    stats->error = MAX (stats->error, errvalue);
     349  }
     350
    344351  return TRUE;
    345352}
    346353
    347354// wgt is externally-supplied weight, wt is optional
    348 int fit_least_squares (double *fit, double *y, double *dy, double *wgt, double *wt, int Npts) {
     355int fit_least_squares (double *fit, double *err, double *y, double *dy, double *wgt, double *wt, int Npts) {
    349356
    350357  int i;
     
    364371  }
    365372  if (S0 == 0.0) return FALSE;
    366   *fit = S1 / S0;
     373  *fit = S1  / S0;
     374  if (err) { *err = 1.0 / S0; }
    367375  return TRUE;
    368376}
  • trunk/Ohana/src/relphot/src/load_images.c

    r39642 r40291  
    5252      free (subset);
    5353      subset = image;
     54    }
     55  }
     56
     57  // reset image values as needed.  always allow 'few' images to succeed, if possible (new
     58  // images / detections may have been added
     59  for (off_t i = 0; i < Nsubset; i++) {
     60    if (subset[i].flags & ID_IMAGE_PHOTOM_FEW) {
     61      subset[i].flags &= ~(ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_POOR);
     62    }
     63    if (RESET) {
     64      if (RESET_ZEROPTS) {
     65        if (!KEEP_UBERCAL || !(subset[i].flags & ID_IMAGE_PHOTOM_UBERCAL)) {
     66          subset[i].McalPSF  = 0.0;
     67          subset[i].McalAPER = 0.0;
     68          subset[i].dMcal    = NAN;
     69          subset[i].flags   &= ~ID_IMAGE_PHOTOM_UBERCAL;
     70        }
     71      }
     72      subset[i].flags &= ~ID_IMAGE_PHOTOM_POOR;
     73      subset[i].ubercalDist = 1000;
    5474    }
    5575  }
  • trunk/Ohana/src/relphot/src/plot_scatter.c

    r37037 r40291  
    4040
    4141                if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
    42                 Mcal = getMcal  (m, i, flatcorr, catalog);
     42                Mcal = getMcal  (m, i, MAG_CLASS_PSF);
    4343                if (isnan(Mcal)) continue;
    4444                Mmos = getMmos  (m, i);
     
    4747                if (isnan(Mgrid)) continue;
    4848
    49                 Mrel = catalog[i].secfilt[Nsecfilt*j+Nsec].M;
     49                Mrel = catalog[i].secfilt[Nsecfilt*j+Nsec].MpsfChp;
    5050                if (isnan(Mrel)) continue;
    5151
  • trunk/Ohana/src/relphot/src/plotstuff.c

    r39457 r40291  
    179179
    180180  KapaInitGraph (graphdata);
    181   graphdata[0].style = 2;
     181  graphdata[0].style = KAPA_PLOT_POINTS; /* points */
    182182  graphdata[0].ptype = 2;
    183183  graphdata[0].ltype = 0;
  • trunk/Ohana/src/relphot/src/reload_catalogs.c

    r39926 r40291  
    247247    if (SYNTH_ZERO_POINTS) { strextend (&command, "-synthphot-zpts %s", SYNTH_ZERO_POINTS); }
    248248    if (USE_BASIC_CHECK)   {  strextend (&command, "-basic-image-search"); }
     249    if (USE_MCAL_PSF_FOR_STACK_APER) { strextend (&command, "-use-mcal-psf-for-stack-aper"); }
    249250
    250251    if (!(STAGES & STAGE_CHIP))  { strextend (&command, "-skip-chip"); }
  • trunk/Ohana/src/relphot/src/relphot_images.c

    r39643 r40291  
    4242  MARKTIME("-- load images: %f sec\n", dtime);
    4343
     44  // checkImages("load images");
     45
    4446  /* unlock, if we can (else, unlocked below) */
    4547  if (!UPDATE) dvo_image_unlock (&db);
    4648
    4749  // load the flat correction table (if defined)
     50  // we no longer need to do this: the flatcorrection is now saved in measure.Mflat
    4851# if (0)
    4952  char flatcorrfile[256];
     
    6063    /* add in a loop over the catalogs calling dvo_catalog_chipcoords */
    6164
     65    // checkImages("load catalog");
     66
    6267    /* match measurements with images, mosaics */
    6368    initImageBins  (catalog, Ncatalog, TRUE);
     
    7782
    7883    setExclusions (catalog, Ncatalog, TRUE);
     84    // checkImages("set Exclu");
    7985
    8086    global_stats (catalog, Ncatalog, flatcorr, 0);
     
    108114        plot_scatter (catalog, Ncatalog, flatcorr);
    109115      }
     116      if (VERBOSE2) dumpMags (stderr, catalog, Ncatalog);
     117      // checkImages("start loop");
     118
    110119      setMrel  (catalog, Ncatalog, flatcorr); // threaded
     120      if (VERBOSE2) dumpMags (stderr, catalog, Ncatalog);
     121      // checkImages("set Mrel");
     122
    111123      if (PLOTSTUFF) {
    112124        plot_scatter (catalog, Ncatalog, flatcorr);
    113125      }
    114126      setMcal  (catalog, FALSE, flatcorr);
     127      // checkImages("set Mcal");
     128      if (VERBOSE2) dumpMags (stderr, catalog, Ncatalog);
     129
    115130      setMmos  (catalog, FALSE, flatcorr);
    116131      setMgrid (catalog, flatcorr);
     
    213228        // ensure the db format is updated
    214229        dbX.format = dvo_catalog_catformat (UPDATE_CATFORMAT);
     230        gfits_modify (&dbX.header, "FORMAT", "%s", 1, UPDATE_CATFORMAT);
     231
    215232        char photcodeFile[1024];
    216233        sprintf (photcodeFile, "%s/Photcodes.dat", CATDIR);
     
    257274    // ensure the db format is updated
    258275    db.format = dvo_catalog_catformat (UPDATE_CATFORMAT);
     276    gfits_modify (&db.header, "FORMAT", "%s", 1, UPDATE_CATFORMAT);
    259277  }
    260278  if (CALIBRATE_STACKS_AND_WARPS || (NLOOP > 0)) {
  • trunk/Ohana/src/relphot/src/relphot_objects.c

    r39926 r40291  
    251251    if (USE_BASIC_CHECK)   { strextend (&command, "-basic-image-search"); }
    252252    if (USE_ALL_IMAGES)    { strextend (&command, "-use-all-images"); }
     253    if (USE_MCAL_PSF_FOR_STACK_APER) { strextend (&command, "-use-mcal-psf-for-stack-aper"); }
    253254
    254255    if (!(STAGES & STAGE_CHIP))  { strextend (&command, "-skip-chip"); }
  • trunk/Ohana/src/relphot/src/relphot_parallel_images.c

    r39643 r40291  
    3636  client_logger_message ("loaded images\n");
    3737
    38   // once we have read this table, we should remove it for repeat runs
    39   // unlink (IMAGE_TABLE); // XXX a bit risky, add some protection?
    40 
    4138  makeMosaics (image, Nimage, TRUE);
    4239
     
    4441
    4542  // load the flat correction table (if defined)
     43  // we no longer need to do this: the flatcorrection is now saved in measure.Mflat
    4644# if (0)
    4745  char flatcorrfile[256];
     
    150148  client_logger_message ("all hosts are done the loops\n");
    151149
    152   /* set Mcal & Mmos for bad images */
    153   setMcal  (catalog, TRUE, flatcorr);
    154   setMmos  (catalog, TRUE, flatcorr);
     150  /* set Mcal & Mmos for bad images (for stack_and_warps, force images to be measured) */
     151  int onlyPoorImages = !CALIBRATE_STACKS_AND_WARPS;
     152  setMcal  (catalog, onlyPoorImages, flatcorr);
     153  setMmos  (catalog, onlyPoorImages, flatcorr);
    155154  MARKTIME("-- finalize Mcal values: %f sec\n", dtime);
    156155
     
    171170  freeGridBins (Ncatalog);
    172171  freeImages((char *)image);
     172  free (image);
    173173
    174174  SkyListFree(skylist);
  • trunk/Ohana/src/relphot/src/relphot_parallel_regions.c

    r39643 r40291  
    5151    // ensure the db format is updated
    5252    db.format = dvo_catalog_catformat (UPDATE_CATFORMAT);
     53    gfits_modify (&db.header, "FORMAT", "%s", 1, UPDATE_CATFORMAT);
    5354  }
    5455
     
    8485
    8586  // save the changes to the image parameters
    86   dvo_image_save (&db, VERBOSE); // this function modifies the db.ftable.buffer: do not free stored Image table
     87  dvo_image_save (&db, VERBOSE);
     88
     89  // dvo_image_save frees db.ftable.buffer (== image) and replaces it: do not free stored Image table
     90  clearImages ();
     91
    8792  dvo_image_unlock (&db);
    8893  MARKTIME ("finished relphot -parallel-regions: %f sec total\n", dtime);
  • trunk/Ohana/src/relphot/src/relphot_synthphot_catalog.c

    r38062 r40291  
    9292      float *value = (float *) zpts->matrix[Nsec].buffer;
    9393      float ZP = !isnan(value[Npix]) ? value[Npix] : 0.0;
    94       secfilt[Nsec].M    = measure[measSYN[i]].M + ZP;
    95       secfilt[Nsec].dM  = 0.6;
    96       secfilt[Nsec].Mchisq = NAN;
    97       secfilt[Nsec].flags |= ID_SECF_USE_SYNTH;
     94      secfilt[Nsec].MpsfChp = measure[measSYN[i]].M + ZP;
     95      secfilt[Nsec].dMpsfChp = 0.6;
     96      secfilt[Nsec].Mchisq   = NAN;
     97      secfilt[Nsec].flags   |= ID_SECF_USE_SYNTH;
    9898    }
    9999  }
  • trunk/Ohana/src/relphot/src/select_images.c

    r39648 r40291  
    251251    image[nimage] = timage[i];
    252252    inSubset[i] = TRUE;
    253     /* always allow 'few' images to succeed, if possible (new images / detections may have
    254      * been added) */
    255     if (image[nimage].flags & ID_IMAGE_PHOTOM_FEW) {
    256       image[nimage].flags &= ~(ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_POOR);
    257     }
    258     if (RESET) {
    259       if (RESET_ZEROPTS) {
    260         if (!KEEP_UBERCAL || !(image[nimage].flags & ID_IMAGE_PHOTOM_UBERCAL)) {
    261           image[nimage].Mcal = 0.0;
    262           image[nimage].dMcal = NAN;
    263           image[nimage].flags &= ~ID_IMAGE_PHOTOM_UBERCAL;
    264         }
    265       }
    266       image[nimage].flags &= ~ID_IMAGE_PHOTOM_POOR;
    267       image[nimage].ubercalDist = 1000;
    268     }
    269253    line_number[nimage] = i;
    270254    nimage ++;
  • trunk/Ohana/src/relphot/src/setMrelCatalog.c

    r39926 r40291  
    22
    33# if (0)
    4 # define TEST_OBJ_ID 0x0000000e
    5 # define TEST_CAT_ID 0x000076ee
     4# define TEST_OBJ_ID 0x000098f9
     5# define TEST_CAT_ID 0x00005ad3
    66# else
    77# define TEST_OBJ_ID 0
     
    102102// 3) UBERCAL measurements can have their weight increased by a big factor to help tie down the averages
    103103// 4) some reference photcode of some kind can be specified as fixed and have a high weight
     104
     105// Although I calculate McalAPER for exposures, I am only using McalPSF for chips.  Note
     106// in StarOps.c:setMcalOutput I am setting measure->McalAPER to image->McalPSF for chips
     107// and warps, but not stacks
    104108
    105109// set mean of chip measurements (selected by photcode range for now):
     
    224228      // overlaps).  Msys + measure.Mcal is our best guess of the true magnitude
    225229      Mmos = Mgrid = 0;
    226       Mcal = measureT[k].Mcal; // check that this is zero for loaded REF value
     230      Mcal = measureT[k].McalPSF; // check that this is zero for loaded REF value
    227231    } else {
    228       // getMcal_alt returns image[].Mcal modified by flatcorr(image.photom_map_id,x,y)
    229       // NOTE: getMcal_alt does not include measure.Mflat
    230       Mcal  = getMcal_alt (meas, cat, flatcorr, measureT[k].Xccd, measureT[k].Yccd);
     232      // getMcal returns image[].Mcal; note the flat-field correction is stored in measure.Mflat
     233      Mcal  = getMcal (meas, cat, MAG_CLASS_PSF);
    231234      if (isnan(Mcal))  SKIP_THIS_MEAS(Ncal);
    232235      Mmos  = getMmos  (meas, cat);
     
    268271    }
    269272
    270     float Mkron, dMkron;
     273    // NOTE: we need to calculate the averge chip Kron on each pass to be able to calibrate the stacks
     274    float Mkron = PhotCatTiny (&measureT[k], MAG_CLASS_KRON);
     275    float dMkron;
    271276    if (isSetMrelFinal) {
    272       Mkron = PhotCat (&measure[k], MAG_CLASS_KRON);
    273277      dMkron = MAX (hypot(measure[k].dMkron, code->photomErrSys), MIN_ERROR);
    274       if (CHECK_VALID_MAG(Mkron, dMkron)) {
    275         int Nkron = results->kronData[Nsec].Nlist;
    276         results->kronData[Nsec].flxlist[Nkron] = Mkron - Mflat - Mcal - Mmos - Mgrid; // this is consistent with PhotRel
    277         results->kronData[Nsec].errlist[Nkron] = dMkron;
    278         results->kronData[Nsec].wgtlist[Nkron] = isUbercal ? UBERCAL_WEIGHT : 1.0;
    279         results->kronData[Nsec].ranking[Nkron] = measureRank[k];
    280         results->kronData[Nsec].measSeq[Nkron] = k;
    281         results->kronData[Nsec].msklist[Nkron] = 0;
    282         results->kronData[Nsec].Nlist ++;
    283       }
     278    } else {
     279      dMkron = MAX (hypot(measureT[k].dM, code->photomErrSys), MIN_ERROR);
     280    }
     281    if (CHECK_VALID_MAG(Mkron, dMkron)) {
     282      int Nkron = results->kronData[Nsec].Nlist;
     283      results->kronData[Nsec].flxlist[Nkron] = Mkron - Mflat - Mcal - Mmos - Mgrid; // this is consistent with PhotRel
     284      results->kronData[Nsec].errlist[Nkron] = dMkron;
     285      results->kronData[Nsec].wgtlist[Nkron] = isUbercal ? UBERCAL_WEIGHT : 1.0;
     286      results->kronData[Nsec].ranking[Nkron] = measureRank ? measureRank[k] : 0;
     287      results->kronData[Nsec].measSeq[Nkron] = k;
     288      results->kronData[Nsec].msklist[Nkron] = 0;
     289      results->kronData[Nsec].Nlist ++;
    284290    }
    285291
     
    377383        }
    378384      }
    379       secfilt[Nsec].M      = Mpsf + ZP;
    380       secfilt[Nsec].dM    = 0.6;
    381       secfilt[Nsec].Mchisq = 0.0;
    382       secfilt[Nsec].flags |= ID_SECF_USE_SYNTH;
     385      secfilt[Nsec].MpsfChp  = Mpsf + ZP;
     386      secfilt[Nsec].dMpsfChp = 0.6;
     387      secfilt[Nsec].Mchisq   = 0.0;
     388      secfilt[Nsec].flags   |= ID_SECF_USE_SYNTH;
    383389      continue;
    384390    }
     
    386392    // if too few valid measurements meet the minimum criteria, go to the next entry
    387393    StatType *psfstats = &results->psfstats;
    388     int Nranking = MAG_STATS_BY_RANKING (&results->psfData[Nsec], psfstats);
    389     if (Nranking < Nminmeas) {
     394    int NrankingPSF = MAG_STATS_BY_RANKING (&results->psfData[Nsec], psfstats);
     395    if (NrankingPSF < Nminmeas) {
    390396      secfilt[Nsec].flags |= ID_OBJ_FEW;
    391397    } else {
    392       secfilt[Nsec].M    = psfstats->mean;
    393       secfilt[Nsec].dM   = psfstats->error;
    394       secfilt[Nsec].Mchisq = (psfstats->Nmeas > 1) ? psfstats->chisq : NAN;
    395     }
    396     int minRankPSF = (Nranking > 0) ? results->psfData[Nsec].ranking[0] : 10;
     398      secfilt[Nsec].MpsfChp  = psfstats->mean;
     399      secfilt[Nsec].dMpsfChp = psfstats->error;
     400      secfilt[Nsec].Mchisq   = (psfstats->Nmeas > 1) ? psfstats->chisq : NAN;
     401    }
    397402
    398403    // when running -averages, we have no information about the images, so we cannot set this
     
    401406    }
    402407
     408    StatType *kronstats = &results->kronstats;
     409    int NrankingKRON = MAG_STATS_BY_RANKING (&results->kronData[Nsec], kronstats);
     410    if (NrankingKRON) {
     411      secfilt[Nsec].MkronChp  = kronstats->mean;
     412      secfilt[Nsec].dMkronChp = kronstats->error;
     413      secfilt[Nsec].sMkronChp = kronstats->sigma;
     414      secfilt[Nsec].NusedKron = NrankingKRON;
     415    }
     416
    403417    if (isSetMrelFinal) {
    404418      if ((average[0].objID == TEST_OBJ_ID) && (average[0].catID == TEST_CAT_ID)) {
     
    406420      }
    407421
    408       // mark the measurements matching this ranking
    409       markMeasureByRanking (&results->psfData[Nsec], measure, minRankPSF, ID_MEAS_PHOTOM_PSF, ID_MEAS_MASKED_PSF);
    410 
    411       if (Nranking) {
    412         secfilt[Nsec].Mstdev = psfstats->sigma;
    413         secfilt[Nsec].Nused  = psfstats->Nmeas;
    414         secfilt[Nsec].Mmax   = psfstats->max;
    415         secfilt[Nsec].Mmin   = psfstats->min;
     422      if (NrankingPSF) {
     423        secfilt[Nsec].sMpsfChp = psfstats->sigma;
     424        secfilt[Nsec].Nused    = psfstats->Nmeas;
     425        secfilt[Nsec].Mmax     = psfstats->max;
     426        secfilt[Nsec].Mmin     = psfstats->min;
    416427      }
    417428      secfilt[Nsec].psfQfMax     = results->psfQfMax[Nsec];
     
    421432      MAX_NOT_NAN (psfQfPerfMax, secfilt[Nsec].psfQfPerfMax);
    422433
     434      // mark the measurements matching this ranking
     435      int minRankPSF = (NrankingPSF > 0) ? results->psfData[Nsec].ranking[0] : 10;
     436      markMeasureByRanking (&results->psfData[Nsec], measure, minRankPSF, ID_MEAS_PHOTOM_PSF, ID_MEAS_MASKED_PSF);
     437
     438      int minRankKron = (NrankingKRON > 0) ? results->kronData[Nsec].ranking[0] : 10;
     439      markMeasureByRanking (&results->kronData[Nsec], measure, minRankKron, ID_MEAS_PHOTOM_KRON, ID_MEAS_MASKED_KRON);
     440
    423441      StatType *apstats = &results->apstats;
    424       Nranking = MAG_STATS_BY_RANKING (&results->aperData[Nsec], apstats);
    425       if (Nranking) {
    426         secfilt[Nsec].Map     = apstats->mean;
    427         secfilt[Nsec].dMap    = apstats->error;
    428         secfilt[Nsec].sMap    = apstats->sigma;
    429         secfilt[Nsec].NusedAp = Nranking;
    430       }
    431       int minRankAper = (Nranking > 0) ? results->aperData[Nsec].ranking[0] : 10;
     442      int NrankingAPER = MAG_STATS_BY_RANKING (&results->aperData[Nsec], apstats);
     443      if (NrankingAPER) {
     444        secfilt[Nsec].MapChp  = apstats->mean;
     445        secfilt[Nsec].dMapChp = apstats->error;
     446        secfilt[Nsec].sMapChp = apstats->sigma;
     447        secfilt[Nsec].NusedAp = NrankingAPER;
     448      }
     449      int minRankAper = (NrankingAPER > 0) ? results->aperData[Nsec].ranking[0] : 10;
    432450      markMeasureByRanking (&results->aperData[Nsec], measure, minRankAper, ID_MEAS_PHOTOM_APER, ID_MEAS_MASKED_APER);
    433 
    434       StatType *kronstats = &results->kronstats;
    435       Nranking = MAG_STATS_BY_RANKING (&results->kronData[Nsec], kronstats);
    436       if (Nranking) {
    437         secfilt[Nsec].Mkron     = kronstats->mean;
    438         secfilt[Nsec].dMkron    = kronstats->error;
    439         secfilt[Nsec].sMkron    = kronstats->sigma;
    440         secfilt[Nsec].NusedKron = Nranking;
    441       }
    442       int minRankKron = (Nranking > 0) ? results->kronData[Nsec].ranking[0] : 10;
    443       markMeasureByRanking (&results->kronData[Nsec], measure, minRankKron, ID_MEAS_PHOTOM_KRON, ID_MEAS_MASKED_KRON);
    444451
    445452      // does this object appear extended in > 50% of measurements?
     
    518525// 2) select the BEST detections per filter (regardless of PRIMARY)
    519526// 3) apply the zero point and AB->Jy transformations
     527
     528// I calculate McalAPER and McalPSF independently for stacks.  I use McalAPER for Mkron
     529// and Map, and McalPSF for Mpsf.  Note in StarOps.c:setMcalOutput I am setting
     530// measure->McalAPER to image->McalPSF for chips and warps, but not stacks
     531
    520532int setMrelAverageStack (Catalog *catalog, int cat, off_t ave, int Nsecfilt, FlatCorrectionTable *flatcorr) {
    521533
     
    528540  SecFilt *secfilt = &catalog[0].secfilt[ave*Nsecfilt];
    529541
    530   off_t k;
    531 
    532   float Mcal = 0, Mmos = 0, Mgrid = 0, Finst = 0;
     542  float McalPSF = 0, McalAPER = 0, Mmos = 0, Mgrid = 0;
    533543
    534544  // set the primary projection cell and skycell for this coordinate
     
    546556  int haveStackObject = FALSE;
    547557
    548   int Ns;
    549   for (Ns = 0; Ns < Nphotcodes; Ns++) {
     558  for (int Ns = 0; Ns < Nphotcodes; Ns++) {
    550559
    551560    int thisCode = photcodes[Ns][0].code;
     
    556565    int haveStack = FALSE;
    557566
    558     float psfQFbest = 0.0;
    559 
    560     off_t stackBestMeasure = -1;
    561     off_t stackPrimaryMeasure = -1;
    562 
    563567    int isBad = FALSE;
    564568    int isSuspect = FALSE;
    565569
    566     int Nstack = 0; // number for this photcode
    567     int NstackDet = 0; // number for this photcode
    568 
     570    int NstackMeas = 0; // number of stack measurements for this photcode
     571    int NstackDet  = 0; // number of stack detections for this photcode (not forced)
     572
     573    int   primaryLevelMax = 0;
     574    off_t primaryEntryMax = -1;
     575    float primaryValueMax = 0.0;
     576
     577    int   bestLevelMax = 0;
     578    off_t bestEntryMax = -1;
     579    float bestValueMax = 0.0;
     580
     581    int Nprimary = 0;
     582
     583    // reset all stack-related values for this secfilt:
    569584    secfilt[Nsec].stackBestOff = -1;
    570585    secfilt[Nsec].stackPrmryOff = -1;
    571 
    572     off_t meas = measureOffset;
    573     for (k = 0; k < Nmeasure; k++, meas++) {
     586    secfilt[Nsec].FpsfStk   = NAN;
     587    secfilt[Nsec].dFpsfStk  = NAN;
     588    secfilt[Nsec].FkronStk  = NAN;
     589    secfilt[Nsec].dFkronStk = NAN;
     590    secfilt[Nsec].FapStk    = NAN;
     591    secfilt[Nsec].dFapStk   = NAN;
     592    secfilt[Nsec].MpsfStk   = NAN;
     593    secfilt[Nsec].MkronStk  = NAN;
     594    secfilt[Nsec].MapStk    = NAN;
     595    secfilt[Nsec].Nstack    = 0;
     596    secfilt[Nsec].NstackDet = 0;
     597    secfilt[Nsec].flags    &= ~ID_SECF_STACK_FLAGS; // reset the stack flags
     598
     599    for (off_t k = 0; k < Nmeasure; k++) {
    574600
    575601      // only examine gpc1 stack data
     
    581607      if (code->equiv != thisCode) { continue; }
    582608
    583       Nstack ++;
    584       if ((measure[k].photFlags2 & 0x00000004) == 0) NstackDet ++;
     609      NstackMeas ++;
     610      if ((measure[k].photFlags2 & 0x00000004) == 0) NstackDet ++; // 0x04 = PM_SOURCE_MODE2_MATCHED
    585611
    586612      // clear this bit for all measurements
     
    590616      haveStack = TRUE;
    591617      haveStackObject = TRUE;
     618
     619      // if (measure[k].dbFlags & MEAS_BAD) SKIP_THIS_MEAS_STACK(Nbad);
     620
     621      int isPrimary = FALSE;
    592622     
    593       // ** find the PRIMARY measurement
    594 
    595       // if we request the primary (USE_TREE_FOR_PRIMARY), this is true if the measurement is from the
    596       // primary skycell for this position
    597       if (MatchImageSkycellID (meas, cat, tessID, projectID, skycellID)) {
    598         stackPrimaryMeasure = k;
     623      // ** is this a PRIMARY measurement? (there may be more than one)
     624
     625      // if we request the primary (USE_TREE_FOR_PRIMARY), this is true if the measurement
     626      // is from the primary skycell for this position. (note that MatchImageSkycellID
     627      // requires the entry in the full Measure table, not just this object)
     628      if (MatchImageSkycellID (measureOffset + k, cat, tessID, projectID, skycellID)) {
    599629        measure[k].dbFlags |= ID_MEAS_STACK_PRIMARY;
    600         secfilt[Nsec].stackPrmryOff = meas;
    601         myAssert (secfilt[Nsec].stackPrmryOff <= catalog[0].Nmeasure, "stackPrmryOff out of range");
    602       }
    603 
    604       // ** now choose the BEST measurements (may also be PRIMARY)
    605 
    606       // ensure that we at least have a single best measure
    607       if (stackBestMeasure == -1) stackBestMeasure = k;
    608 
    609       // choose the best psfQFperf value for the BEST measurement
    610       if (isfinite(measure[k].psfQFperf) && (measure[k].psfQFperf > psfQFbest)) {
    611         psfQFbest = measure[k].psfQFperf;
    612         stackBestMeasure = k;
    613       }
    614       // ... UNLESS psfQFperf > 0.98 for the primary, in which case just use the primary.
    615       if ((measure[k].dbFlags & ID_MEAS_STACK_PRIMARY) && isfinite(measure[k].psfQFperf) && (measure[k].psfQFperf > 0.98)) {
    616         psfQFbest = 1000; // force this to be the best entry
    617         stackBestMeasure = k;
    618       }
    619 
    620       if (measure[k].dbFlags & MEAS_BAD) SKIP_THIS_MEAS_STACK(Nbad);
    621 
     630        isPrimary = TRUE;
     631        Nprimary ++;
     632      }
     633
     634      // soften the error floor (can dM be 0.0?)
     635      int SNvalue = isfinite(measure[k].dM) ? 1.0 / hypot (measure[k].dM, MIN_ERROR) : NAN;
     636      int psfQFperfAboveLimit = isfinite(SNvalue) && isfinite(measure[k].psfQFperf) && (measure[k].psfQFperf > 0.95);
     637
     638      // ** determine the BEST level
     639      int bestLevel = 0;
     640      if (!isPrimary && !psfQFperfAboveLimit) bestLevel = 1;
     641      if ( isPrimary && !psfQFperfAboveLimit) bestLevel = 2;
     642      if (!isPrimary &&  psfQFperfAboveLimit) bestLevel = 3;
     643      if ( isPrimary &&  psfQFperfAboveLimit) bestLevel = 4;
     644
     645      // here is the rule for choosing the best value:
     646      float bestValue = (bestLevel > 2) ? SNvalue : measure[k].psfQFperf;
     647
     648      // if we have not reached this level before, set the new level
     649      if (bestLevelMax < bestLevel) {
     650        bestValueMax = bestValue;
     651        bestLevelMax = bestLevel;
     652        bestEntryMax = k;
     653      }
     654      // if we have reached this level before, set the new value if we beat the old one
     655      if ((bestLevelMax == bestLevel) && (bestValueMax < bestValue)) {
     656        bestValueMax = bestValue;
     657        bestEntryMax = k;
     658      }
     659      myAssert (bestEntryMax > -1, "this should not happen");
     660
     661      // ** determine the PRIMARY level
     662      int primaryLevel = 0;
     663      if ( isPrimary && !psfQFperfAboveLimit) primaryLevel = 1;
     664      if ( isPrimary &&  psfQFperfAboveLimit) primaryLevel = 2;
     665
     666      // here is the rule for choosing the PRIMARY value:
     667      float primaryValue = 0.0;
     668      if (primaryLevel == 1) { primaryValue = measure[k].psfQFperf; }
     669      if (primaryLevel == 2) { primaryValue = SNvalue; }
     670
     671      // if we have not reached this level before, set the new level
     672      if (isPrimary && (primaryLevelMax < primaryLevel)) {
     673        primaryValueMax = primaryValue;
     674        primaryLevelMax = primaryLevel;
     675        primaryEntryMax = k;
     676      }
     677      // if we have reached this level before, set the new value if we beat the old one
     678      if (isPrimary && (primaryLevelMax == primaryLevel) && (primaryValueMax < primaryValue)) {
     679        primaryValueMax = primaryValue;
     680        primaryEntryMax = k;
     681      }
     682    }
     683
     684    // if we do not have a stack measurement for this photcode, skip everything below
     685    if (!haveStack) continue;
     686 
     687    // now we have a BEST and a PRIMARY entry (may be the same entry)
     688
     689    off_t meas = bestEntryMax;
     690    off_t measSeq = meas + measureOffset;
     691
     692    myAssert (meas >= 0, "this should not happen");
     693
     694    // measurements without an image are either external reference photometry or
     695    // data for which the associated image has not been loaded (probably because of
     696    // overlaps).  we only want measurements associated with stack images in this loop
     697   
     698    // match measurement to its image (this is just a check, right?)
     699    if (getImageEntry (measSeq, cat) < 0) {
    622700      // measurements without an image are either external reference photometry or
    623701      // data for which the associated image has not been loaded (probably because of
    624       // overlaps).  we only want measurements associated with stack images in this loop
    625 
    626       // match measurement to its image (this is just a check, right?)
    627       if (getImageEntry (meas, cat) < 0) {
    628         // measurements without an image are either external reference photometry or
    629         // data for which the associated image has not been loaded (probably because of
    630         // overlaps).  Msys + measure.Mcal is our best guess of the true magnitude
    631         Mmos = Mgrid = 0;
    632         Mcal = measure[k].Mcal; // check that this is zero for loaded REF value
    633       } else {
    634         Mcal  = getMcal_alt  (meas, cat, flatcorr, measure[k].Xccd, measure[k].Yccd);
    635         if (isnan(Mcal))  SKIP_THIS_MEAS_STACK(Ncal);
    636         Mmos  = getMmos  (meas, cat);
    637         if (isnan(Mmos))  SKIP_THIS_MEAS_STACK(Nmos);
    638         Mgrid = getMgrid (meas, cat);
    639         if (isnan(Mgrid)) SKIP_THIS_MEAS_STACK(Ngrid);
    640       }
    641 
    642       // NOTE: negative and insignificant flux values are allowed, but not NAN flux values
    643       Finst = PhotFluxInst (&measure[k], MAG_CLASS_PSF);
    644       if (isnan(Finst)) SKIP_THIS_MEAS_STACK(Ninst);
    645 
    646       // data quality assessment
    647       isBad |= (measure[k].photFlags & code->photomBadMask);
    648       isBad |= (measure[k].psfQF < 0.85);
    649       isBad |= isnan(measure[k].psfQF);
    650       isBad |= measure[k].dM > 0.2; // S/N < 5.0
    651 
    652       isSuspect |= (measure[k].photFlags & code->photomPoorMask);
    653       isSuspect |= (measure[k].psfQFperf < 0.85);
    654     }
    655 
    656     if (!haveStack) continue;
    657 
    658     if (!isSuspect && !isBad) {
    659       NstackGood ++;
    660     }
    661     if (isSuspect && !isBad) {
    662       NstackSuspect ++;
    663     }
    664 
    665     // measurements which are bad will not have a valid stack entry and are skipped
    666     k = (stackBestMeasure >= 0) ? stackBestMeasure : stackPrimaryMeasure;
    667     if (k < 0) continue;
    668 
    669     // we are now populating stackDetectID not stack Image ID in secfilt
    670     // ID = (stackPrimaryMeasureMin >= 0) ? stackPrimaryIDmin      : stackCenterIDmin;
    671     // ID = measure[k].extID; // for the stack, this is the stackDetectID
    672 
    673     // get the zero point for the selected image
    674     float zp = PhotZeroPoint (&measure[k], &average[0], &secfilt[0]) - (Mcal + Mmos + Mgrid);
     702      // overlaps).  Msys + measure.Mcal is our best guess of the true magnitude
     703      McalPSF  = measure[meas].McalPSF; // check that this is zero for loaded REF value
     704      McalAPER = McalPSF; // check that this is zero for loaded REF value
     705      Mmos     = 0.0;
     706      Mgrid    = 0.0;
     707    } else {
     708      McalPSF   = getMcal  (measSeq, cat, MAG_CLASS_PSF);
     709      McalAPER  = USE_MCAL_PSF_FOR_STACK_APER ? getMcal  (measSeq, cat, MAG_CLASS_PSF) : getMcal  (measSeq, cat, MAG_CLASS_KRON);
     710      Mmos      = getMmos  (measSeq, cat);
     711      Mgrid     = getMgrid (measSeq, cat);
     712    }
     713   
     714    // ** Here is the math to relate mag,zp to flux
    675715
    676716    // flux_cgs : erg sec^1 cm^-2 Hz^-1
     
    703743    // flux_Jy = flux_inst * ten(-0.4*ZP + 3.56)
    704744
     745    // get the zero point for the selected image
     746    // Use a different zero point for the PSF-like and APERTURE-like magnitudes
     747    float zpPSF  = PhotZeroPoint (&measure[meas], &average[0], &secfilt[0]) - (McalPSF  + Mmos + Mgrid);
     748    float zpAPER = PhotZeroPoint (&measure[meas], &average[0], &secfilt[0]) - (McalAPER + Mmos + Mgrid);
     749
    705750    // zpFactor to go from instrumental flux to Janskies
    706     float zpFactor = pow(10.0, -0.4*zp + 3.56);
     751    float zpFactorPSF  = pow(10.0, -0.4*zpPSF  + 3.56);
     752    float zpFactorAPER = pow(10.0, -0.4*zpAPER + 3.56);
    707753
    708754    // need to put in AB mag factor to get to Janskies (or uJy?)
    709     secfilt[Nsec].FpsfStk   = zpFactor * measure[k].FluxPSF; 
    710     secfilt[Nsec].dFpsfStk  = zpFactor * measure[k].dFluxPSF;
    711     secfilt[Nsec].FkronStk  = zpFactor * measure[k].FluxKron;
    712     secfilt[Nsec].dFkronStk = zpFactor * measure[k].dFluxKron;
    713     secfilt[Nsec].FapStk    = zpFactor * measure[k].FluxAp;
     755    secfilt[Nsec].FpsfStk   = zpFactorPSF  * measure[meas].FluxPSF; 
     756    secfilt[Nsec].dFpsfStk  = zpFactorPSF  * measure[meas].dFluxPSF;
     757    secfilt[Nsec].FkronStk  = zpFactorAPER * measure[meas].FluxKron;
     758    secfilt[Nsec].dFkronStk = zpFactorAPER * measure[meas].dFluxKron;
     759    secfilt[Nsec].FapStk    = zpFactorAPER * measure[meas].FluxAp;
    714760
    715761    // NOTE: for PV3, apFluxErr is broken (see pmSourcePhotometry.c:245).  we are going to
    716762    // use PSF flux error instead here:
    717     // secfilt[Nsec].dFapStk   = zpFactor * measure[k].dFluxAp;
    718     secfilt[Nsec].dFapStk   = zpFactor * measure[k].dFluxPSF;
     763    // secfilt[Nsec].dFapStk   = zpFactorAPER * measure[meas].dFluxAp;
     764    secfilt[Nsec].dFapStk   = zpFactorAPER * measure[meas].dFluxPSF;
    719765
    720766    // Jy to AB mags
    721     secfilt[Nsec].MpsfStk   = (measure[k].FluxPSF  > 0.0) ? 8.9 - 2.5*log10(secfilt[Nsec].FpsfStk) : NAN;
    722     secfilt[Nsec].MkronStk  = (measure[k].FluxKron > 0.0) ? 8.9 - 2.5*log10(secfilt[Nsec].FkronStk) : NAN;
    723     secfilt[Nsec].MapStk    = (measure[k].FluxAp   > 0.0) ? 8.9 - 2.5*log10(secfilt[Nsec].FapStk) : NAN;
    724 
    725     secfilt[Nsec].stackBestOff = k + measureOffset;
     767    secfilt[Nsec].MpsfStk   = (measure[meas].FluxPSF  > 0.0) ? 8.9 - 2.5*log10(secfilt[Nsec].FpsfStk)  : NAN;
     768    secfilt[Nsec].MkronStk  = (measure[meas].FluxKron > 0.0) ? 8.9 - 2.5*log10(secfilt[Nsec].FkronStk) : NAN;
     769    secfilt[Nsec].MapStk    = (measure[meas].FluxAp   > 0.0) ? 8.9 - 2.5*log10(secfilt[Nsec].FapStk)   : NAN;
     770
     771    // record the measurement which gave the best value
     772    secfilt[Nsec].stackBestOff = (bestEntryMax == -1) ? -1 : bestEntryMax + measureOffset;
    726773    myAssert (secfilt[Nsec].stackBestOff <= catalog[0].Nmeasure, "stackBestOff out of range");
    727774
    728     secfilt[Nsec].Nstack    = Nstack;
     775    // record the selected primary measurement
     776    secfilt[Nsec].stackPrmryOff = (primaryEntryMax == -1) ? -1 : primaryEntryMax + measureOffset;
     777    myAssert (secfilt[Nsec].stackPrmryOff <= catalog[0].Nmeasure, "stackPrmryOff out of range");
     778
     779    // this is the selected measurement used by secfilt[]
     780    measure[meas].dbFlags |= ID_MEAS_STACK_PHOT_SRC;
     781    if (Nprimary) {
     782      secfilt[Nsec].flags |= ID_SECF_STACK_PRIMARY;
     783      if (Nprimary > 1) {
     784        secfilt[Nsec].flags |= ID_SECF_STACK_PRIMARY_MULTIPLE;
     785      }
     786    }
     787
     788    // stack measurement used for 'best' was a detection (not forced from the other bands)
     789    if ((measure[bestEntryMax].photFlags2 & 0x00000004) == 0) {
     790      secfilt[Nsec].flags |= ID_SECF_STACK_BESTDET;
     791    }
     792
     793    // stack measurement used for 'primary' was a detection (not forced from the other bands)
     794    if ((primaryEntryMax >= 0) && ((measure[primaryEntryMax].photFlags2 & 0x00000004) == 0)) {
     795      secfilt[Nsec].flags |= ID_SECF_STACK_PRIMDET;
     796    }
     797
     798    secfilt[Nsec].flags |= ID_SECF_HAS_PS1_STACK;
     799
     800    // NOTE: negative and insignificant flux values are allowed, but not NAN flux values
     801    // float Finst = PhotFluxInst (&measure[meas], MAG_CLASS_PSF);
     802   
     803    // data quality assessment
     804    isBad |= (measure[meas].photFlags & photcodes[Ns][0].photomBadMask);
     805    isBad |= (measure[meas].psfQF < 0.85);
     806    isBad |= isnan(measure[meas].psfQF);
     807    isBad |= measure[meas].dM > 0.2; // S/N < 5.0
     808
     809    isSuspect |= (measure[meas].photFlags & photcodes[Ns][0].photomPoorMask);
     810    isSuspect |= (measure[meas].psfQFperf < 0.85);
     811
     812    if (!isSuspect && !isBad) {
     813      NstackGood ++;
     814    }
     815    if (isSuspect && !isBad) {
     816      NstackSuspect ++;
     817    }
     818
     819    secfilt[Nsec].Nstack    = NstackMeas;
    729820    secfilt[Nsec].NstackDet = NstackDet;
    730821
    731     // this is the measurement used by secfilt[]
    732     measure[k].dbFlags |= ID_MEAS_STACK_PHOT_SRC;
    733     if (k == stackPrimaryMeasure) {
    734       secfilt[Nsec].flags |= ID_SECF_STACK_PRIMARY;
    735     }
    736 
    737     // stack measurement used for 'best' was a detection (not forced from the other bands)
    738     if ((measure[k].photFlags2 & 0x00000004) == 0) {
    739       secfilt[Nsec].flags |= ID_SECF_STACK_BESTDET;
    740     }
    741     // stack measurement used for 'primary' was a detection (not forced from the other bands)
    742     if ((stackPrimaryMeasure >= 0) && ((measure[stackPrimaryMeasure].photFlags2 & 0x00000004) == 0)) {
    743       secfilt[Nsec].flags |= ID_SECF_STACK_PRIMDET;
    744     }
    745 
    746     secfilt[Nsec].flags |= ID_SECF_HAS_PS1_STACK;
    747822  } // Nsecfilt loop
    748823
     
    755830
    756831  int PrimaryIsBest = TRUE;
    757   for (Ns = 0; Ns < Nphotcodes; Ns++) {
     832  for (int Ns = 0; Ns < Nphotcodes; Ns++) {
    758833    if (!(secfilt[Ns].flags & ID_SECF_HAS_PS1_STACK)) continue; // no stack detection in PS1, nothing is best
    759834    if (secfilt[Ns].flags & ID_SECF_STACK_PRIMARY) continue;    // primary stack detection is best
     
    780855// * no grid, no mosaic, no 2MASS, no SYNTH, no Ubercal, no flatcorr
    781856// analysis is done on flux, not mags (as the faintest objects will be nearly insignificant)
     857
     858// Although I calculate McalAPER for exposures, I am only using McalPSF for warps..
     859// Note in StarOps.c:setMcalOutput I am setting measure->McalAPER to image->McalPSF for
     860// chips and warps, but not stacks
     861
    782862int setMrelAverageForcedWarp (Catalog *catalog, int cat, off_t ave, int Nsecfilt, FlatCorrectionTable *flatcorr, SetMrelInfo *results) {
    783863  OHANA_UNUSED_PARAM(flatcorr);
     
    857937      // data for which the associated image has not been loaded (probably because of
    858938      // overlaps).  Msys + measure.Mcal is our best guess of the true magnitude
    859       Mcal = measure[k].Mcal; // check that this is zero for loaded REF value
     939      Mcal = measure[k].McalPSF; // check that this is zero for loaded REF value
    860940    } else {
    861       // use getMcal not getMcal_alt?
    862       Mcal  = getMcal_alt (meas, cat, NULL, measure[k].Xccd, measure[k].Yccd);
    863       // Mcal  = getMcal (meas, cat);
     941      Mcal  = getMcal (meas, cat, MAG_CLASS_PSF);
    864942      if (isnan(Mcal))  SKIP_THIS_MEAS(Ncal);
    865943    }
  • trunk/Ohana/src/relphot/src/setMrelFinal.c

    r39632 r40291  
    6565          if (catalog[0].measure[m].dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
    6666            if (!KEEP_UBERCAL) {
    67               catalog[0].measure[m].Mcal = 0.0;
     67              catalog[0].measure[m].McalPSF  = 0.0;
     68              catalog[0].measure[m].McalAPER = 0.0;
    6869              catalog[0].measure[m].dbFlags &= ~ID_MEAS_PHOTOM_UBERCAL;
    6970            }
    7071          } else {
    7172            if (RESET_ZEROPTS && (getImageEntry (m, 0) >= 0)) {
    72               catalog[0].measure[m].Mcal = 0.0;
     73              catalog[0].measure[m].McalPSF  = 0.0;
     74              catalog[0].measure[m].McalAPER = 0.0;
    7375            }
    7476          }
     
    105107  ALLOCATE (catalog[0].measureRank, char, catalog[0].Nmeasure);
    106108  setMeasureRank (catalog);
    107   setMrelOutput (catalog, 1, flatcorr); // sets the values secfilt.M = <measure.M + measure.Mflat - image.Mcal>
     109  setMrelOutput (catalog, 1, flatcorr); // sets the values secfilt.MpsfChp = <measure.M + measure.Mflat - image.Mcal>
    108110  setMcalOutput (catalog, 1, flatcorr); // sets measure.Mcal = image.Mcal
    109111
     
    226228        off_t Nim = getImageEntry (m, 0);
    227229        if (Nim > -1) {
    228           if (isnan(getMcal (m, 0, flatcorr, catalog))) goto skip;
     230          if (isnan(getMcal (m, 0, MAG_CLASS_PSF))) goto skip;
    229231          if (isnan(getMmos (m, 0))) goto skip;
    230232        }
  • trunk/Ohana/src/relphot/src/share_image_mags.c

    r36630 r40291  
    7474      continue;
    7575    }
    76     images[seq].Mcal        = image_mags[i].Mcal;
     76    images[seq].McalPSF     = image_mags[i].McalPSF;
     77    images[seq].McalAPER    = image_mags[i].McalAPER;
    7778    images[seq].dMcal       = image_mags[i].dMcal;
    7879    images[seq].dMagSys     = image_mags[i].dMagSys;
    79     images[seq].Xm          = image_mags[i].Xm;
     80    images[seq].McalChiSq   = image_mags[i].McalChiSq;
    8081    images[seq].nFitPhotom  = image_mags[i].nFitPhotom;
    8182    images[seq].flags       = image_mags[i].flags;
     
    9192int set_image_mags (ImageMag *image_mags, Image *image) {
    9293
    93   image_mags->Mcal        = image->Mcal;
     94  image_mags->McalPSF     = image->McalPSF;
     95  image_mags->McalAPER    = image->McalAPER;
    9496  image_mags->dMcal       = image->dMcal;
    9597  image_mags->dMagSys     = image->dMagSys;
    96   image_mags->Xm          = image->Xm;
     98  image_mags->McalChiSq   = image->McalChiSq;
    9799  image_mags->nFitPhotom  = image->nFitPhotom;
    98100  image_mags->flags       = image->flags;
  • trunk/Ohana/src/relphot/src/share_mean_mags.c

    r39457 r40291  
    111111    if (Nsec < 0) continue;
    112112
    113     catalog[catSeq].secfilt[objSeq*Nsecfilt + Nsec].M = meanmags[i].M;
     113    catalog[catSeq].secfilt[objSeq*Nsecfilt + Nsec].MpsfChp = meanmags[i].M;
    114114  }
    115115  free (meanmags);
     
    122122int set_mean_mags (MeanMag *meanmags, AverageTiny *average, SecFilt *secfilt, int Nsec) {
    123123
    124   meanmags->M  = secfilt->M;
    125   meanmags->dM = secfilt->dM;
     124  meanmags->M  = secfilt->MpsfChp;
     125  meanmags->dM = secfilt->dMpsfChp;
    126126  meanmags->Mchisq = secfilt->Mchisq;
    127127  meanmags->Nsec = Nsec; // key to secfilt entry
  • trunk/Ohana/src/relphot/src/synthetic_mags.c

    r39457 r40291  
    3737  // (r - i > 0.5) : w = r + 0.268 - 0.435 (r-i) - 0.078(r-i)^2
    3838
    39   float Mr = secfilt[NSr].M;
    40   float Mi = secfilt[NSi].M;
     39  float Mr = secfilt[NSr].MpsfChp;
     40  float Mi = secfilt[NSi].MpsfChp;
    4141
    4242  if (!isfinite(Mr)) return FALSE;
Note: See TracChangeset for help on using the changeset viewer.