IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 13, 2011, 11:30:02 AM (15 years ago)
Author:
eugene
Message:

various fixes to photdbc, relastro and relphot based on analysis of the first reference database

Location:
trunk/Ohana/src/relastro
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/relastro/include/relastro.h

    r29001 r30616  
    2626  double dPos;
    2727  int mask;
     28  int Nmeas;
    2829} StarData;
    2930
     
    9495
    9596double SIGMA_LIM;
    96 int SRC_MEAS_TOOFEW; //catalog objects wich fewer detections then this are ignored
     97int    SRC_MEAS_TOOFEW; //catalog objects wich fewer detections then this are ignored
    9798double MIN_ERROR;
     99
     100int    IMFIT_CLIP_NITER; // number of clipping iterations to perform in FitChip
     101double IMFIT_CLIP_NSIGMA; // number of sigma to clip in FitChip
     102double IMFIT_SYS_SIGMA_LIM; // max dMag for objects used to measure systematic scatter
    98103
    99104double RADIUS; // match radius for high-speed objects
     
    141146int FlagOutlier;
    142147int    CLIP_THRESH;
     148int USE_BASIC_CHECK;
    143149
    144150FitMode FIT_MODE;
     
    296302void fixImageRaw (Catalog *catalog, int Ncatalog, off_t im);
    297303void FlagOutliers(Catalog *catalog);
    298 int MeasFilterTest(Measure *measure);
     304int MeasFilterTest(Measure *measure, int applySigmaLim);
    299305
    300306int sun_ecliptic (double jd, double *lambda, double *beta, double *epsilon);
     
    325331int UpdateObjectOffsets (SkyList *skylist);
    326332
    327 int relastroVisualPlotRawRef(StarData *raw, StarData *ref, double dRmax, int numObj);
    328 int relastroVisualPlotScatter(double values[], double thresh, int npts);
    329 int relastroVisualPlotOutliers(Catalog *catalog, int offset, int Nmeasure,
    330                               StatType statsR, StatType statsD, double thresh);
    331 
    332 
     333int relastroVisualPlotChipFit(StarData *raw, StarData *ref, double dRmax, int numObj);
     334// int relastroVisualPlotRawRef(StarData *raw, StarData *ref, double dRmax, int numObj);
     335// int relastroVisualPlotScatter(double values[], double thresh, int npts);
     336// int relastroVisualPlotOutliers(Catalog *catalog, int offset, int Nmeasure, StatType statsR, StatType statsD, double thresh);
     337void relastroSetVisual(int state);
     338int relastroGetVisual(void);
    333339
    334340int FixProblemImages (SkyList *skylist);
     
    352358int createStarMapPoints();
    353359int checkStarMap(int N);
     360
     361int GetScatterRawRef(float *dLsig, float *dMsig, float *dRsig, int *nKeep, StarData *raw, StarData *ref, int Nstars, float SigmaLimit);
  • trunk/Ohana/src/relastro/src/ConfigInit.c

    r29001 r30616  
    1919  if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
    2020
    21   GetConfig (config, "RELASTRO_SIGMA_LIM",     "%lf", 0, &SIGMA_LIM);
     21  GetConfig (config, "RELASTRO_SIGMA_LIM",         "%lf", 0, &SIGMA_LIM); // exclude measurements on this basis
    2222  GetConfig (config, "RELASTRO_SRC_MEAS_TOOFEW",   "%d",  0, &SRC_MEAS_TOOFEW);
     23
     24  if (!ScanConfig (config, "RELASTRO_IMFIT_CLIP_NITER",    "%d",  0, &IMFIT_CLIP_NITER))    IMFIT_CLIP_NITER  = 3;
     25  if (!ScanConfig (config, "RELASTRO_IMFIT_CLIP_NSIGMA",   "%lf", 0, &IMFIT_CLIP_NSIGMA))   IMFIT_CLIP_NSIGMA = 3.0;
     26  if (!ScanConfig (config, "RELASTRO_IMFIT_SYS_SIGMA_LIM", "%lf", 0, &IMFIT_SYS_SIGMA_LIM)) IMFIT_SYS_SIGMA_LIM = 0.01;
    2327
    2428  // XXX these are used in relphot to identify poor stars / images -- define
  • trunk/Ohana/src/relastro/src/FitChip.c

    r29001 r30616  
    22# include "relastroVisual.h"
    33
    4 // XXX make these user parameters
    5 # define FIT_CHIP_NITER     3
    6 # define FIT_CHIP_NSIGMA    3.0
    7 
    8 // XXX save the fit[0].Npts value in the image table?
    9 
    10 // XXX save measurements of the fit quality (scatter, chisq) in the image table
    11 
    124int FitChip (StarData *raw, StarData *ref, int Nmatch, Image *image) {
    135
    14   int i, Nscatter, Niter, skip;
    15   CoordFit *fit;
    16   double dL, dM, dR, dRmax, *values;
    17 
    18   ALLOCATE (values, double, Nmatch);
    19   for (Niter = 0; Niter < FIT_CHIP_NITER; Niter ++) {
     6  int i, NstatFull, Nstat, Niter, skip;
     7  float dLsig, dMsig, dRsig;
     8  float dLsigFull, dMsigFull, dRsigFull;
     9  float dL, dM, dR, dRmax;
     10
     11  CoordFit *fit = NULL;
     12
     13  dRmax = 0.0;
     14
     15  for (Niter = 0; Niter < IMFIT_CLIP_NITER; Niter ++) {
     16   
     17    // measure the scatter for the unmarked (good) measurements with dM < limit
     18    // SIGMA_LIM here is redundant with ImageOps.c:915
     19    GetScatterRawRef(&dLsig, &dMsig, &dRsig, &Nstat, raw, ref, Nmatch, SIGMA_LIM);
     20    dRmax = IMFIT_CLIP_NSIGMA*dRsig;
     21
     22    // (a) skip unfittable points
     23    // (b) mark good and bad points for fit (in and outliers)
    2024
    2125    // measure the scatter distribution (use only the bright end detections)
    22     for (i = Nscatter = 0; i < Nmatch; i++) {
     26    for (i = 0; i < Nmatch; i++) {
    2327      if (raw[i].mask) continue;
    24       if (isnan(raw[i].dMag) || raw[i].dMag > SIGMA_LIM) continue;
     28      if (isnan(raw[i].dMag)) {
     29        raw[i].mask |= 0x0004;
     30        continue;
     31      }
     32      if (raw[i].dMag > SIGMA_LIM) {
     33        raw[i].mask |= 0x0008;
     34        continue;
     35      } // is this redundant with ImageOps.c:915?
    2536
    2637      dL = raw[i].L - ref[i].L;
    2738      dM = raw[i].M - ref[i].M;
    2839      dR = hypot (dL, dM);
    29 
    30       values[Nscatter] = dR;
    31       Nscatter++;
    32     }
    33 
    34     if (Nscatter > 5) {
    35       // for a 2D Gaussian, 40% of the points are within 1 sigma; dRmax is ~ 3 sigma
    36       // XXX this test is not sensible for Nscatter < XXX (5?)
    37       dsort (values, Nscatter);
    38       dRmax = FIT_CHIP_NSIGMA*values[(int)(0.40*Nscatter)];
    39       relastroVisualPlotScatter(values, dRmax, Nscatter);
    40       relastroVisualPlotRawRef(raw, ref, dRmax, Nmatch);
    41     } else {
    42       dRmax = 0;
    43     }
     40      if (dR > dRmax) {
     41        raw[i].mask |= 0x0010;
     42        continue;
     43      }
     44    }
     45
     46    // figures to assess the fitting process:
     47    // x vs dx, x vs dy, y vs dx, y vs dy :
     48    // residual vector field
     49    // dx vs mag, dy vs mag
     50    relastroVisualPlotChipFit(raw, ref, dRmax, Nmatch);
    4451
    4552    // fit the requested order polynomial
     
    4754      image[0].coords.Npolyterms = CHIPORDER;
    4855    }
     56    if (fit) fit_free (fit);
    4957    fit = fit_init (image[0].coords.Npolyterms);
    5058
    5159    // generate the fit matches
    5260    for (i = 0; i < Nmatch; i++) {
    53       if (raw[i].mask) {
    54         continue;
    55       }
    56       if (isnan(raw[i].dMag) || raw[i].dMag > SIGMA_LIM) {
    57         continue;
    58       }
    59 
    60       // only keep objects within dRmax
    61       dL = raw[i].L - ref[i].L;
    62       dM = raw[i].M - ref[i].M;
    63       dR = hypot (dL, dM);
    64 
    65       // fprintf (stderr, "fit %f %f -> %f %f : %f %f (%f vs %f) wt: %f\n", raw[i].X, raw[i].Y, ref[i].L, ref[i].M, raw[i].L, raw[i].M, dR, dRmax, raw[i].dPos);
    66 
    67       if ((dRmax > 0.0) && (dR > dRmax)) continue;
    68 
     61      if (raw[i].mask) continue;
    6962      fit_add (fit, raw[i].X, raw[i].Y, ref[i].L, ref[i].M, raw[i].dPos);
    7063    }
     
    8881    }
    8982    if (skip) {
    90       if (VERBOSE) fprintf (stderr, "insufficient measurements (%d) for requested order (%d)\n", fit[0].Npts, image[0].coords.Npolyterms);
     83      if (VERBOSE2) fprintf (stderr, "insufficient measurements (%d) for requested order (%d)\n", fit[0].Npts, image[0].coords.Npolyterms);
    9184      fit_free (fit);
    92       free (values);
    9385      image[0].flags |= ID_IMAGE_ASTROM_FEW;
    9486      return FALSE;
    9587    }
    96 
    97     // fprintf (stderr, "scatter limit: %f based on %d detections; using %d of %d for fit\n", dRmax, Nscatter, fit[0].Npts, Nmatch);
    9888
    9989    // measure the fit, update the coords & object coordinates
     
    110100    }
    111101
    112     fit_free (fit);
    113 
    114102    for (i = 0; i < Nmatch; i++) {
    115103      XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, &image[0].coords);
     
    118106  }
    119107
    120   free (values);
     108  // count mask classes
     109  int nMask1 = 0;
     110  int nMask2 = 0;
     111  int nMask3 = 0;
     112  int nMask4 = 0;
     113  int nMask5 = 0;
     114  for (i = 0; i < Nmatch; i++) {
     115    if (!raw[i].mask) continue;
     116    if (raw[i].mask & 0x01) nMask1 ++;
     117    if (raw[i].mask & 0x02) nMask2 ++;
     118    if (raw[i].mask & 0x04) nMask3 ++;
     119    if (raw[i].mask & 0x08) nMask4 ++;
     120    if (raw[i].mask & 0x10) nMask5 ++;
     121  }
     122
     123  GetScatterRawRef(&dLsigFull, &dMsigFull, &dRsigFull, &NstatFull, raw, ref, Nmatch, SIGMA_LIM);
     124  GetScatterRawRef(&dLsig, &dMsig, &dRsig, &Nstat, raw, ref, Nmatch, IMFIT_SYS_SIGMA_LIM);
     125
     126  int Nm = 0;
     127  int Ns = 0;
     128  for (i = 0; i < Nmatch; i++) {
     129    if (raw[i].mask) continue;
     130    Nm += raw[i].Nmeas;
     131    Ns++;
     132  }
     133  image[0].nLinkAstrom = (Nm / Ns);
     134
     135  if (VERBOSE) fprintf (stderr, "fit sigma: %f (%f, %f) : full: %f (%f, %f), scatter limit: %f (%d full, %d bright, %d fit, %d all) (%d %d %d %d %d)\n", dRsig, dLsig, dMsig, dRsigFull, dLsigFull, dMsigFull, dRmax, NstatFull, Nstat, fit[0].Npts, Nmatch, nMask1, nMask2, nMask3, nMask4, nMask5);
     136
     137  image[0].dXpixSys = dLsig;
     138  image[0].dYpixSys = dMsig;
     139  image[0].nFitAstrom = fit[0].Npts;
     140
     141  if (fit) fit_free (fit);
     142
    121143  return TRUE;
     144}
     145
     146// measure the scatter distribution (limit selected objects by dMag)
     147int GetScatterRawRef(float *dLsig, float *dMsig, float *dRsig, int *nKeep, StarData *raw, StarData *ref, int Nstars, float SigmaLimit) {
     148
     149  int i, Ns;
     150  float dL, dM, dLsum, dLsum2, dMsum, dMsum2, *dR;
     151
     152  ALLOCATE (dR, float, Nstars);
     153
     154  Ns = 0;
     155  dLsum = dLsum2 = dMsum = dMsum2 = 0.0;
     156  for (i = 0; i < Nstars; i++) {
     157    if (raw[i].mask) continue;
     158    if (isnan(raw[i].dMag)) continue;
     159    if ((SigmaLimit > 0.0) && (raw[i].dMag > SigmaLimit)) continue;
     160   
     161    dL = raw[i].L - ref[i].L;
     162    dM = raw[i].M - ref[i].M;
     163
     164    dLsum  += dL;
     165    dLsum2 += dL*dL;
     166    dMsum  += dM;
     167    dMsum2 += dM*dM;
     168
     169    dR[Ns] = hypot (dL, dM);
     170    Ns++;
     171  }
     172
     173  *dLsig = sqrt((dLsum2 / Ns) - SQ(dLsum/Ns));
     174  *dMsig = sqrt((dMsum2 / Ns) - SQ(dMsum/Ns));
     175  *nKeep = Ns;
     176
     177  if (Ns < 5) {
     178    *dRsig = NAN;
     179    free  (dR);
     180    return (FALSE);
     181  }
     182
     183  // for a 2D Gaussian, 40% of the points are within R = 1 sigma
     184  fsort (dR, Ns);
     185  *dRsig = dR[(int)(0.40*Ns)];
     186 
     187  free  (dR);
     188  return (TRUE);
    122189}
    123190
     
    162229  fclose (f);
    163230*/
     231
  • trunk/Ohana/src/relastro/src/GetAstromError.c

    r29938 r30616  
    11# include "relastro.h"
     2# define WEIGHTED_ERRORS 1
    23
    34float GetAstromError (Measure *measure, int mode) {
    4   //BIG HACKXXXXXXXX
    5   return 0.1;
     5
    66  PhotCode *code;
    7   float dPobs, dPsys, dPtotal, dM, AS, MS;
     7  float dPobs, dPsys, dPtotal, dM, AS, MS, dX, dY;
     8
     9  if (!WEIGHTED_ERRORS) {
     10    // if we don't understand the errors at all, this at least lets us get things roughly
     11    // right:
     12    return 0.1;
     13  }
     14
    815  switch (mode) {
    916    case ERROR_MODE_RA:
    10       dPobs = measure[0].dXccd / 100.0;  // need to redefine this as RAerr
     17      dPobs = FromShortPixels(measure[0].dXccd);  // dXccd is a value in pixels
    1118      break;
    1219    case ERROR_MODE_DEC:
    13       dPobs = measure[0].dYccd / 100.0;  // need to redefine this as RAerr
     20      dPobs = FromShortPixels(measure[0].dYccd);  // dYccd is a value in pixels
    1421      break;
    1522    case ERROR_MODE_POS:
    16       dPobs = hypot (measure[0].dXccd, measure[0].dYccd) / 100.0;  // need to redefine this as RAerr
     23      dX = FromShortPixels(measure[0].dXccd);  // dXccd is a value in pixels
     24      dY = FromShortPixels(measure[0].dYccd);  // dYccd is a value in pixels
     25      dPobs = hypot (dX, dY);
    1726      break;
    1827    default:
    1928      abort();
    2029  }
    21   /* the astrometric errors are not being carried yet (but should be!) */
    22   /* we use the photometric mag error as a weighting term */
    2330
    2431  code  = GetPhotcodebyCode (measure[0].photcode);
     
    2734  dPsys = code[0].astromErrSys;
    2835  dM    = measure[0].dM;
    29   dPtotal = sqrt(SQ(dPsys) + AS*SQ(dPobs) + MS*SQ(dM));
    30 
    31   //XXX dXccd, dYccd are now working correctly
    32   //dPtotal = AS * dPobs;
     36  dPtotal = sqrt(SQ(dPsys) + SQ(AS*dPobs) + SQ(MS*dM));
    3337
    3438  dPtotal = MAX (dPtotal, MIN_ERROR);
     
    3640}
    3741
     42/* for a long time, psphot was either not reported position errors, or was reporting
     43 * completely wrong astrometry errors.  This function lets us handle, in the
     44 * configuration, different strategies to generating a position error
     45 *
     46 * astrometry systematic error : this is the minimum expected per-position error.  You
     47 * should probably measure this from you data.  watch out for the chicken and egg problem!
     48 *
     49 * astrometry error scale : this field lets you accept position errors in (say) pixels and
     50 * convert them with this term to arcsec.  AS : pixel scale in arcsec
     51 *
     52 * astrometry mag scale : this field lets you define position errors based on the
     53 * photometry error.  the scale factor should be something like a typical seeing number
     54 * (in arcsec) for the given instrument
     55 *
     56 */
  • trunk/Ohana/src/relastro/src/ImageOps.c

    r29001 r30616  
    146146  free (clist);
    147147  free (mlist);
     148  free (Nlist);
     149  free (NLIST);
    148150}
    149151
     
    325327  for (j = 0; j < average[0].Nmeasure; j++) {
    326328    off = average[0].measureOffset + j;
    327     fprintf (stderr, "%f, %f\n", measure[off].dR, measure[off].dD);
     329    fprintf (stderr, "dR, dD, mag, dMag: %f, %f, %f, %f\n", measure[off].dR, measure[off].dD, measure[off].M, measure[off].dM);
    328330  }
    329331  return;
     
    336338  off_t i, m, c, n, nPos;
    337339  double X, Y, L, M, P, Q, R, D, dR, dD;
    338   double dPos, DPOS_MAX_ASEC;
     340  double dPos, dPosSys, DPOS_MAX_ASEC;
    339341
    340342  Mosaic *mosaic;
     
    359361  }
    360362
    361   // accumulate the rms position offsets.  if this value, or any specific entry, is too
    362   // large, we will reset the image to the original coords at the end of the analysis
    363 
     363  // these are used to accumulate the rms position offsets.  if this value, or any
     364  // specific entry, is too large, we will reset the image to the original coords at the
     365  // end of the analysis
    364366  dPos = 0.0;
    365367  nPos = 0;
     368
     369  // convert the image systematic error in pixels to a value in arcsec
     370  {
     371    double dLsig, dMsig;
     372    double Ro, Do, Rx, Dx, dP0, dP1;
     373    Coords *coords;
     374
     375    // these values are in pixels, but we to convert to arcsec
     376    dLsig = image[0].dXpixSys;
     377    dMsig = image[0].dYpixSys;
     378
     379    if (moscoords == NULL) {
     380      coords = imcoords;
     381    } else {
     382      coords = moscoords;
     383    }
     384    XY_to_LM (&Ro, &Do, 0.0, 0.0, coords);
     385    XY_to_LM (&Rx, &Dx, dLsig, 0.0, coords);
     386    dP0 = 3600.0 * hypot(Rx - Ro, Dx - Do); // convert to arcsec
     387    XY_to_LM (&Rx, &Dx, 0.0, dLsig, coords);
     388    dP1 = 3600.0 * hypot(Rx - Ro, Dx - Do); // convert to arcsec
     389    dPosSys = 0.5 * (dP0 + dP1);
     390  }     
    366391
    367392  for (i = 0; i < Nlist[im]; i++) {
     
    390415    // complain if the new location is far from the average location
    391416    // NOTE: This should never happen, or our StarMap tests are not working
    392     if (fabs(dR) > 1.5*ADDSTAR_RADIUS) {
     417    if (fabs(dR) > 3.0*ADDSTAR_RADIUS) {
    393418      fprintf (stderr, "measurement is far from average location (R): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
    394419      dump_measures (&catalog[c].average[n], catalog[c].measure);
    395420      // abort ();
    396421    }
    397     if (fabs(dD) > 1.5*ADDSTAR_RADIUS) {
     422    if (fabs(dD) > 3.0*ADDSTAR_RADIUS) {
    398423      fprintf (stderr, "measurement is far from average location (D): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
    399424      dump_measures (&catalog[c].average[n], catalog[c].measure);
     
    418443    catalog[c].measure[m].dR = dR;
    419444    catalog[c].measure[m].dD = dD;
    420 
     445   
    421446    if (catalog[c].measure[m].dR > +180.0*3600.0) {
    422447      // average on high end of boundary, move star up
     
    429454      catalog[c].measure[m].dR = 3600.0*(catalog[c].average[n].R - R);
    430455    }
     456
     457    // set the systematic error for this image:
     458    catalog[c].measure[m].dRsys = ToShortPixels(dPosSys);
    431459  }
    432460
     
    544572
    545573    // an object with only one detection provides no information about the image calibration
    546     //XXX this is already taken care of in bcatalog
    547     raw[i].mask = FALSE;
    548     int mask = FALSE;
     574    // XXX this is already taken care of in bcatalog
     575    raw[i].mask = 0x0000;
    549576    if (catalog[c].average[n].Nmeasure <= SRC_MEAS_TOOFEW) {
    550       mask = TRUE;
     577      raw[i].mask |= 0x0001;
    551578    }
    552579    if (!finite(catalog[c].measure[m].dR) || !finite(catalog[c].measure[m].dD)) {
    553       mask = TRUE;
    554     }
    555 
    556     raw[i].mask = mask;
    557 
     580      raw[i].mask |= 0x0002;
     581    }
     582    raw[i].Nmeas = catalog[c].average[n].Nmeasure; // record so we can check how well connected an image is
    558583
    559584    switch (mode) {
     
    685710     
    686711      // skip measurements based on user selected criteria
    687       if (!MeasFilterTest(&catalog[0].measure[m])) continue;
     712      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue;
    688713      R[N] = catalog[0].measure[m].dR;
    689714      D[N] = catalog[0].measure[m].dD;
     
    711736
    712737      // skip measurements based on user selected criteria
    713       if (!MeasFilterTest(&catalog[0].measure[m])) continue;
     738      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue;
    714739     
    715740      x = catalog[0].measure[m].dR - statsR.median;
     
    725750    }
    726751
    727     //examine results
    728     relastroVisualPlotOutliers(catalog, catalog[0].average[j].measureOffset,
    729                                catalog[0].average[j].Nmeasure,
    730                                statsR, statsD, Ns);
     752    // examine results
     753    // relastroVisualPlotOutliers(catalog, catalog[0].average[j].measureOffset, catalog[0].average[j].Nmeasure, statsR, statsD, Ns);
    731754  }
    732755 
     
    784807     
    785808      // skip measurements based on user selected criteria
    786       if (!MeasFilterTest(&catalog[0].measure[m])) continue;
     809      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue;
    787810      R[N] = catalog[0].measure[m].dR;
    788811      D[N] = catalog[0].measure[m].dD;
     
    806829    for (k = 0; k < catalog[0].average[j].Nmeasure; k++, m++) {
    807830      //skip bad measurements
    808       if (!MeasFilterTest(&catalog[0].measure[m])) continue; 
     831      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue; 
    809832      x = catalog[0].measure[m].dR - statsR.median;
    810833      y = catalog[0].measure[m].dD - statsD.median;
     
    838861    for (k = 0; k < catalog[0].average[j].Nmeasure; k++, m++) {
    839862      //skip bad measurements
    840       if (!MeasFilterTest(&catalog[0].measure[m])) continue; 
     863      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue; 
    841864      x = catalog[0].measure[m].dR - statsR.median;
    842865      y = catalog[0].measure[m].dD - statsD.median;
     
    852875    }  //done rejecting outliers
    853876
    854     //examine results
    855     relastroVisualPlotOutliers(catalog, catalog[0].average[j].measureOffset,
    856                                catalog[0].average[j].Nmeasure,
    857                                statsR, statsD, Ns);
     877    // examine results
     878    // relastroVisualPlotOutliers(catalog, catalog[0].average[j].measureOffset, catalog[0].average[j].Nmeasure, statsR, statsD, Ns);
    858879   
    859880  } //done looping over objects
     
    871892
    872893/** Determine whether a measurement should be included in the analysis, based on supplied filter criteria */
    873 int MeasFilterTest(Measure *measure) {
     894// we only optionally apply the sigma limit: for object averages, this should not be used (should it?)
     895int MeasFilterTest(Measure *measure, int applySigmaLim) {
    874896  int found, k;
    875897  long mask;
     
    917939
    918940  /* select measurements by measurement error */
    919   if ((SIGMA_LIM > 0) && (measure[0].dM > SIGMA_LIM)) return FALSE;
     941  if (applySigmaLim && (SIGMA_LIM > 0) && (measure[0].dM > SIGMA_LIM)) {
     942    return FALSE;
     943  }
    920944 
    921945  /* select measurements by mag limit */
  • trunk/Ohana/src/relastro/src/StarMaps.c

    r29001 r30616  
    7979    starmap[N].stars[ybin*NX_MAP + xbin] ++;
    8080  }
    81   MARKTIME("assign stars to starmap bins: %f sec\n", dtime);
     81  if (VERBOSE2) { MARKTIME("assign stars to starmap bins: %f sec\n", dtime); }
    8282
    8383  return (TRUE);
     
    117117    }
    118118
    119     if (VERBOSE) fprintf (stderr, "starmap: %d points for image %s\n", starmap[i].Npoints, images[i].name);
     119    if (VERBOSE2) fprintf (stderr, "starmap: %d points for image %s\n", starmap[i].Npoints, images[i].name);
    120120  }
    121121
     
    145145  }
    146146
    147   if (VERBOSE) fprintf (stderr, "max deviations for %s using %d pts : %f, %f\n", images[N].name, starmap[N].Npoints, dLmax, dMmax);
     147  if (VERBOSE2) fprintf (stderr, "max deviations for %s using %d pts : %f, %f\n", images[N].name, starmap[N].Npoints, dLmax, dMmax);
    148148
    149   if (dLmax > DPOS_MAX) return (FALSE);
    150   if (dMmax > DPOS_MAX) return (FALSE);
     149  if (dLmax > DPOS_MAX) {
     150      if (VERBOSE) fprintf (stderr, "max deviations for %s using %d pts : %f, %f\n", images[N].name, starmap[N].Npoints, dLmax, dMmax);
     151      return (FALSE);
     152  }
     153  if (dMmax > DPOS_MAX) {
     154      if (VERBOSE) fprintf (stderr, "max deviations for %s using %d pts : %f, %f\n", images[N].name, starmap[N].Npoints, dLmax, dMmax);
     155      return (FALSE);
     156  }
    151157  return (TRUE);
    152158}
  • trunk/Ohana/src/relastro/src/UpdateChips.c

    r29001 r30616  
    11# include "relastro.h"
     2int plotChipFits (double *Ro, double *Do, char *mode, int Nimage);
     3int saveCenter (Image *image, double *Ro, double *Do, int im);
    24
    35int UpdateChips (Catalog *catalog, int Ncatalog) {
    46
     7  int Nskip, Nmosaic, NnewFit, NoldFit;
     8
    59  /* we can measure new image parameters for each non-mosaic chip independently */
    6   off_t i, Nimage, Nraw, Nref;
     10  off_t i, Nimage, Nraw, Nref, nFitAstr;
    711  Image *image;
    812  StarData *raw, *ref;
    913  Coords *oldCoords;
     14  float dXpixSys, dYpixSys;
     15  double *Ro, *Do;
     16  char *mode;
     17
     18  Nskip = Nmosaic = NnewFit = NoldFit = 0;
    1019
    1120  image = getimages (&Nimage);
    1221
     22  // save fit results for summary plot
     23  ALLOCATE (Ro, double, Nimage);
     24  ALLOCATE (Do, double, Nimage);
     25  ALLOCATE (mode, char, Nimage);
     26
    1327  for (i = 0; i < Nimage; i++) {
    1428
    1529    /* skip all except WRP images */
    16     if (strcmp(&image[i].coords.ctype[4], "-WRP")) continue;
     30    if (strcmp(&image[i].coords.ctype[4], "-WRP")) {
     31      Nmosaic ++;
     32      mode[i] = 0;
     33      continue;
     34    }
    1735
    1836    /* convert measure coordinates to raw entries */
    1937    raw = getImageRaw (catalog, Ncatalog, i, &Nraw, MODE_MOSAIC);
    20     if (!raw) continue;
     38    if (!raw) {
     39      Nskip ++;
     40      mode[i] = 0;
     41      continue;
     42    }
    2143
    2244    /* convert average coordinates to ref entries */
    2345    ref = getImageRef (catalog, Ncatalog, i, &Nref, MODE_MOSAIC);
    24     if (!ref) continue;
     46    if (!ref) {
     47      Nskip ++;
     48      mode[i] = 0;
     49      continue;
     50    }
    2551
    2652    // note that Nraw & Nref must be equal: if not, we made a programming error in one of these two functions.
    2753    assert (Nraw == Nref);
    2854
     55    // save these in case of failure
    2956    saveCoords (&image[i].coords, i);
     57    dXpixSys = image[i].dXpixSys;
     58    dYpixSys = image[i].dYpixSys;
     59    nFitAstr = image[i].nFitAstrom;
    3060
    3161    // FitChip does iterative, clipped fitting
     
    3363    if (!FitChip (raw, ref, Nraw, &image[i])) {
    3464      if (VERBOSE) fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
     65
     66      // restore status quo ante
    3567      oldCoords = getCoords (i);
    3668      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
     69      image[i].dXpixSys = dXpixSys;
     70      image[i].dYpixSys = dYpixSys;
     71      image[i].nFitAstrom = nFitAstr;
     72
     73      saveCenter (image, &Ro[i], &Do[i], i);
     74      mode[i] = 1;
     75      NoldFit ++;
    3776      free (raw);
    3877      free (ref);
     
    4281    if (!checkStarMap (i)) {
    4382      if (VERBOSE) fprintf (stderr, "fit diverges too much for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
     83      // restore status quo ante
    4484      oldCoords = getCoords (i);
    4585      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
     86      image[i].dXpixSys = dXpixSys;
     87      image[i].dYpixSys = dYpixSys;
     88      image[i].nFitAstrom = nFitAstr;
     89
     90      saveCenter (image, &Ro[i], &Do[i], i);
     91      mode[i] = 2;
    4692      image[i].flags |= ID_IMAGE_ASTROM_POOR;
    47     }
    48 
     93      NoldFit ++;
     94      free (raw);
     95      free (ref);
     96      continue;
     97    }
     98
     99    saveCenter (image, &Ro[i], &Do[i], i);
     100    mode[i] = 3;
     101    NnewFit ++;
    49102    free (raw);
    50103    free (ref);
    51104  }
    52105
     106  plotChipFits (Ro, Do, mode, Nimage);
     107
     108  fprintf (stderr, "UpdateChips: %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", NnewFit, NoldFit, Nskip, Nmosaic);
    53109  return (TRUE);
    54110}
    55111
     112int saveCenter (Image *image, double *Ro, double *Do, int im) {
     113
     114  Mosaic *mosaic;
     115  Coords *moscoords, *imcoords;
     116  double X, Y, L, M, P, Q, R, D;
     117
     118  moscoords = NULL;
     119  if (!strcmp(&image[im].coords.ctype[4], "-WRP")) {
     120    mosaic = getMosaicForImage (im);
     121    if (mosaic == NULL) return FALSE;  // if we cannot find the associated image, skip it
     122    moscoords = &mosaic[0].coords;
     123  }
     124  imcoords = &image[im].coords;
     125 
     126  if (!strcmp(&image[im].coords.ctype[4], "-WRP")) {
     127    X = 0.5*image[im].NX;
     128    Y = 0.5*image[im].NY;
     129  } else {
     130    X = 0.0;
     131    Y = 0.0;
     132  }
     133
     134  if (moscoords == NULL) {
     135    // this is a Simple image (not a mosaic)
     136    // note that for a Simple image, L,M = P,Q
     137    XY_to_LM (&L, &M, X, Y, imcoords);
     138    LM_to_RD (&R, &D, L, M, imcoords);
     139  } else {
     140    XY_to_LM (&L, &M, X, Y, imcoords);
     141    XY_to_LM (&P, &Q, L, M, moscoords);
     142    LM_to_RD (&R, &D, P, Q, moscoords);
     143  }
     144
     145  double Rmid;
     146  if (UserCatalog) {
     147      Rmid = UserCatalogRA;
     148  } else {
     149      Rmid = 0.5*(UserPatch.Rmin + UserPatch.Rmax);
     150  }
     151
     152  R = ohana_normalize_angle_to_midpoint (R, Rmid);
     153
     154  *Ro = R;
     155  *Do = D;
     156
     157  return (TRUE);
     158}
     159 
     160int plotChipFits (double *Ro, double *Do, char *mode, int Nimage) {
     161
     162  static int kapa = -1;
     163
     164  int i, N;
     165  double Rmin, Rmax, Dmin, Dmax;
     166  float *xvec, *yvec;
     167  Graphdata graphdata;
     168
     169  if (!relastroGetVisual()) return (TRUE);
     170
     171  if (kapa == -1) {
     172    kapa = KapaOpenNamedSocket("kapa", "relastro");
     173    if (kapa == -1) {
     174      fprintf (stderr, "can't open kapa window\n");
     175      return FALSE;
     176    }
     177  }
     178
     179  Rmin = +720;
     180  Rmax = -720;
     181  Dmin = +90;
     182  Dmax = -90;
     183
     184  // find the R, D range
     185  for (i = 0; i < Nimage; i++) {
     186    if (!mode[i]) continue;
     187
     188    Rmin = MIN(Rmin, Ro[i]);
     189    Rmax = MAX(Rmax, Ro[i]);
     190    Dmin = MIN(Dmin, Do[i]);
     191    Dmax = MAX(Dmax, Do[i]);
     192  }
     193
     194  ALLOCATE (xvec, float, Nimage);
     195  ALLOCATE (yvec, float, Nimage);
     196
     197  bzero (&graphdata, sizeof(Graphdata));
     198  plot_defaults (&graphdata);
     199  graphdata.xmin = Rmin;
     200  graphdata.xmax = Rmax;
     201  graphdata.ymin = Dmin;
     202  graphdata.ymax = Dmax;
     203  graphdata.style = 2;
     204  graphdata.size = 1;
     205
     206  KapaSetFont (kapa, "helvetica", 14);
     207  KapaSetLimits (kapa, &graphdata);
     208  KapaBox (kapa, &graphdata);
     209
     210  // *** good images ***
     211  N = 0;
     212  for (i = 0; i < Nimage; i++) {
     213    if (mode[i] != 3) continue;
     214    xvec[N] = Ro[i];
     215    yvec[N] = Do[i];
     216    N++;
     217  }
     218  graphdata.ptype = 7;
     219  graphdata.color = KapaColorByName("black");
     220  KapaPrepPlot (kapa, N, &graphdata);
     221  KapaPlotVector (kapa, N, xvec, "x");
     222  KapaPlotVector (kapa, N, yvec, "y");
     223 
     224  // *** reject fit ***
     225  N = 0;
     226  for (i = 0; i < Nimage; i++) {
     227    if (mode[i] != 1) continue;
     228    xvec[N] = Ro[i];
     229    yvec[N] = Do[i];
     230    N++;
     231  }
     232  graphdata.ptype = 3;
     233  graphdata.color = KapaColorByName("red");
     234  KapaPrepPlot (kapa, N, &graphdata);
     235  KapaPlotVector (kapa, N, xvec, "x");
     236  KapaPlotVector (kapa, N, yvec, "y");
     237 
     238  // *** divergent fit ***
     239  N = 0;
     240  for (i = 0; i < Nimage; i++) {
     241    if (mode[i] != 2) continue;
     242    xvec[N] = Ro[i];
     243    yvec[N] = Do[i];
     244    N++;
     245  }
     246  graphdata.ptype = 2;
     247  graphdata.color = KapaColorByName("blue");
     248  KapaPrepPlot (kapa, N, &graphdata);
     249  KapaPlotVector (kapa, N, xvec, "x");
     250  KapaPlotVector (kapa, N, yvec, "y");
     251 
     252  free (xvec);
     253  free (yvec);
     254
     255  return (TRUE);
     256}
     257
     258// XXX if (!FindMosaicForImage (image, Nimage, i)) { }
  • trunk/Ohana/src/relastro/src/UpdateObjectOffsets.c

    r27581 r30616  
    4040    UpdateMeasures (&catalog, 1);
    4141
     42    UpdateObjects (&catalog, 1);
     43
    4244    freeImageBins (1);
    4345
  • trunk/Ohana/src/relastro/src/UpdateObjects.c

    r29938 r30616  
    103103
    104104        //does the measurement pass the supplied filtering constraints?
    105         if (!MeasFilterTest(&catalog[i].measure[m])) {
     105        if (!MeasFilterTest(&catalog[i].measure[m], FALSE)) {
    106106          catalog[i].measure[m].dbFlags &= ~ID_MEAS_USED_OBJ;
    107107          continue;
     
    134134
    135135        // dX, dY : error in arcsec --
    136         // dX[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_RA);
    137         // dY[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_DEC);
    138 
    139         dX[N] = 0.1;
    140         dY[N] = 0.1;
     136        dX[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_RA);
     137        dY[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_DEC);
     138
     139        // add systematic error in quadrature, if desired
     140        // only do this after the fit has converged (or you will never improve the poor images)
     141        // if (INCLUDE_SYS_ERR) {
     142        // float dRsys = FromShortPixels(catalog[i].measure[m].dRsys);
     143        // dX[N] = hypot(dX[N], dRsys);
     144        // dY[N] = hypot(dY[N], dRsys);
     145        // }
     146
     147        // dX[N] = 0.1;
     148        // dY[N] = 0.1;
     149
    141150        dT[N] = catalog[i].measure[m].dt;
    142151
  • trunk/Ohana/src/relastro/src/args.c

    r28184 r30616  
    1313    remove_argument (N, &argc, argv);
    1414    FIT_TARGET = TARGET_OBJECTS;
    15 
    16     // check for object fitting modes (not valid for images)
    17     if ((N = get_argument (argc, argv, "-pm"))) {
    18         remove_argument (N, &argc, argv);
    19         FIT_MODE = FIT_PM_ONLY;
    20     }
    21     if ((N = get_argument (argc, argv, "-par"))) {
    22         remove_argument (N, &argc, argv);
    23         FIT_MODE = FIT_PAR_ONLY;
    24     }
    25     if ((N = get_argument (argc, argv, "-pmpar"))) {
    26         remove_argument (N, &argc, argv);
    27         FIT_MODE = FIT_PM_AND_PAR;
    28     }
    29   }
     15  }
     16
     17  // check for object fitting modes
     18  if ((N = get_argument (argc, argv, "-pm"))) {
     19    remove_argument (N, &argc, argv);
     20    FIT_MODE = FIT_PM_ONLY;
     21  }
     22  if ((N = get_argument (argc, argv, "-par"))) {
     23    remove_argument (N, &argc, argv);
     24    FIT_MODE = FIT_PAR_ONLY;
     25  }
     26  if ((N = get_argument (argc, argv, "-pmpar"))) {
     27    remove_argument (N, &argc, argv);
     28    FIT_MODE = FIT_PM_AND_PAR;
     29  }
     30
    3031  if ((N = get_argument (argc, argv, "-high-speed"))) {
    3132    // XXX include a parallax / no-parallax option
     
    9394      usage ();
    9495    }
     96  }
     97
     98  USE_BASIC_CHECK = FALSE;
     99  if ((N = get_argument (argc, argv, "-basic-image-search"))) {
     100    remove_argument (N, &argc, argv);
     101    USE_BASIC_CHECK = TRUE;
    95102  }
    96103
     
    144151    VERBOSE = VERBOSE2 = TRUE;
    145152    remove_argument (N, &argc, argv);
     153  }
     154
     155  if ((N = get_argument (argc, argv, "-visual"))) {
     156    remove_argument (N, &argc, argv);
     157    relastroSetVisual(TRUE);
    146158  }
    147159
  • trunk/Ohana/src/relastro/src/bcatalog.c

    r29001 r30616  
    4545      offset = catalog[0].average[i].measureOffset + j;
    4646     
    47       // filter objects based on user supplied criteria
    48       if (!MeasFilterTest(&catalog[0].measure[offset])) {
     47      // filter objects based on user supplied criteria, including SIGMA_LIM
     48      if (!MeasFilterTest(&catalog[0].measure[offset], TRUE)) {
    4949        catalog[0].measure[offset].dbFlags &= ~ID_MEAS_USED_CHIP;
    5050        continue;
     
    7878        subcatalog[0].measure[Nmeasure].dbFlags &= ~ID_MEAS_AREA;
    7979      }
    80      
     80
    8181      Nmeasure ++;
    8282      Nm ++;
  • trunk/Ohana/src/relastro/src/load_catalogs.c

    r29001 r30616  
    2626    pcatalog[0].Nsecfilt  = GetPhotcodeNsecfilt ();
    2727
    28     if (!dvo_catalog_open (pcatalog, skylist[0].regions[i], VERBOSE, "w")) {
     28    if (!dvo_catalog_open (pcatalog, skylist[0].regions[i], VERBOSE2, "w")) {
    2929      fprintf (stderr, "ERROR: failure reading catalog %s\n", pcatalog[0].filename);
    3030      exit (1);
    3131    }
    32     if (VERBOSE && !pcatalog[0].Naves_disk) fprintf (stderr, "no data in %s, skipping\n", pcatalog[0].filename);
     32    if (VERBOSE2 && !pcatalog[0].Naves_disk) fprintf (stderr, "no data in %s, skipping\n", pcatalog[0].filename);
    3333
    3434    //outlier rejection
  • trunk/Ohana/src/relastro/src/plotstuff.c

    r27588 r30616  
    146146  graphdata[0].ymax = dUNDEF;
    147147   
     148  graphdata[0].ticktextPad = NAN;
     149  graphdata[0].labelPadXm = NAN;
     150  graphdata[0].labelPadXp = NAN;
     151  graphdata[0].labelPadYm = NAN;
     152  graphdata[0].labelPadYp = NAN;
     153  graphdata[0].padXm = NAN;
     154  graphdata[0].padXp = NAN;
     155  graphdata[0].padYm = NAN;
     156  graphdata[0].padYp = NAN;
    148157}
  • trunk/Ohana/src/relastro/src/relastro.c

    r29001 r30616  
    7878        MARKTIME("update chips: %f sec\n", dtime);
    7979      }
     80      // create summary plots of the process
     81      // relastroVisualSummaryChips();
    8082      break;
    8183
  • trunk/Ohana/src/relastro/src/relastroVisual.c

    r24308 r30616  
    1111#define KAPAY 700
    1212
    13 static int kapa = -1;
     13static int kapa1 = -1;
    1414static int kapa2 = -1;
    1515static int kapa3 = -1;
     16static int kapa4 = -1;
    1617
    1718static int isVisual = FALSE;
    18 static int plotRawRef = FALSE;
    19 static int plotScatter = FALSE;
    20 static int plotResid = FALSE;
    21 static int plotVector = FALSE;
     19static int plotRawRef = TRUE;
     20// static int plotScatter = TRUE;
     21// static int plotResid = TRUE;
     22// static int plotVector = TRUE;
    2223static int plotOutliers = TRUE;
    2324
     
    2930      fprintf(stderr, "Failure to open kapa.\n");
    3031      isVisual = 0;
    31       return 0;
     32      return FALSE;
    3233    }
    33     //    KapaResize (*kapid, KAPAX, KAPAY);
    34   }
    35   return 1;
     34  }
     35  return TRUE;
    3636}
    3737
     
    4949    isVisual = 0;
    5050  }
    51   return 1;
     51  return TRUE;
     52}
     53
     54void relastroSetVisual(int state) {
     55  isVisual = state;
     56}
     57
     58int relastroGetVisual(void) {
     59  return isVisual;
    5260}
    5361
     
    7078    graphdata->xmax = xhi;
    7179    graphdata->ymax = yhi;
    72     return 1;
    73 }
    74 
    75 static int residPlot(float x[], float y[],
    76                      float xVec[], float yVec[],
    77                      int npts, int *kapaID) {
    78     if (!isVisual || !plotResid) return TRUE;
     80    return TRUE;
     81}
     82
     83/** 4-panel plot of x vs dx, y vs dx, x vs dy, y vs dy **/
     84int relastroVisualRawRef(int *kapaID, float *x, float *y, float *dx, float *dy, float *dPos, int npts) {
     85
    7986    Graphdata graphdata;
    8087    KapaSection section;
     
    8390
    8491    KapaInitGraph(&graphdata);
    85     KapaClearPlots(*kapaID);
     92    KapaClearSections(*kapaID);
    8693    KapaSetFont(*kapaID, "helvetica", 14);
    8794
     
    8996    section.x = 0.0; section.y = 0.0;
    9097    section.dx = .45, section.dy = .45;
     98    section.bg = KapaColorByName("white");
    9199    graphdata.ptype = 7;
    92100    graphdata.style = 2;
     101    graphdata.etype |= 0x01;
    93102
    94103    KapaSetSection(*kapaID, &section);
    95     if(!scaleGraphdata(x, xVec, &graphdata, npts)) return 0;
     104    if(!scaleGraphdata(x, dx, &graphdata, npts)) return 0;
    96105    KapaSetLimits(*kapaID, &graphdata);
    97106    KapaBox(*kapaID, &graphdata);
     
    100109    KapaPrepPlot(*kapaID, npts, &graphdata);
    101110    KapaPlotVector(*kapaID, npts, x, "x");
    102     KapaPlotVector(*kapaID, npts, xVec, "y");
     111    KapaPlotVector(*kapaID, npts, dx, "y");
     112    KapaPlotVector(*kapaID, npts, dPos, "dym");
     113    KapaPlotVector(*kapaID, npts, dPos, "dyp");
    103114
    104115    section.x = .5; section.y = 0; section.name="1";
    105116    KapaSetSection(*kapaID, &section);
    106     if(!scaleGraphdata(x, yVec, &graphdata, npts)) return 0;
     117    if(!scaleGraphdata(x, dy, &graphdata, npts)) return 0;
    107118    KapaSetLimits(*kapaID, &graphdata);
    108119    KapaBox(*kapaID, &graphdata);
     
    111122    KapaPrepPlot(*kapaID, npts, &graphdata);
    112123    KapaPlotVector(*kapaID, npts, x, "x");
    113     KapaPlotVector(*kapaID, npts, yVec, "y");
     124    KapaPlotVector(*kapaID, npts, dy, "y");
     125    KapaPlotVector(*kapaID, npts, dPos, "dym");
     126    KapaPlotVector(*kapaID, npts, dPos, "dyp");
    114127
    115128    section.x = .0; section.y = .5; section.name="2";
    116129    KapaSetSection(*kapaID, &section);
    117     if(!scaleGraphdata(y, xVec, &graphdata, npts)) return 0;;
     130    if(!scaleGraphdata(y, dx, &graphdata, npts)) return 0;;
    118131    KapaSetLimits(*kapaID, &graphdata);
    119132    KapaBox(*kapaID, &graphdata);
     
    122135    KapaPrepPlot(*kapaID, npts, &graphdata);
    123136    KapaPlotVector(*kapaID, npts, y, "x");
    124     KapaPlotVector(*kapaID, npts, xVec, "y");
     137    KapaPlotVector(*kapaID, npts, dx, "y");
     138    KapaPlotVector(*kapaID, npts, dPos, "dym");
     139    KapaPlotVector(*kapaID, npts, dPos, "dyp");
    125140
    126141    section.x = .5; section.y = .5; section.name="3";
    127142    KapaSetSection(*kapaID, &section);
    128     if(!scaleGraphdata(y, yVec, &graphdata, npts)) return 0;
     143    if(!scaleGraphdata(y, dy, &graphdata, npts)) return 0;
    129144    KapaSetLimits(*kapaID, &graphdata);
    130145    KapaBox(*kapaID, &graphdata);
     
    133148    KapaPrepPlot(*kapaID, npts, &graphdata);
    134149    KapaPlotVector(*kapaID, npts, y, "x");
    135     KapaPlotVector(*kapaID, npts, yVec, "y");
    136 
    137     return 1;
    138 }
    139 
    140 
    141 /**Plot a vector field*/
    142 static int plotVectorField(float x[], float y[],
    143                            float xVec[], float yVec[],
    144                            int npts, int *kapaID, double maxVecLength) {
    145 
    146     if(!plotVector) return 1;
     150    KapaPlotVector(*kapaID, npts, dy, "y");
     151    KapaPlotVector(*kapaID, npts, dPos, "dym");
     152    KapaPlotVector(*kapaID, npts, dPos, "dyp");
     153
     154    return TRUE;
     155}
     156
     157
     158/** Plot a vector field (circles at vector origin, scaled lines giving vector directions */
     159int relastroVisualVectorField(int *kapaID, float *x, float *y, float *dx, float *dy, int npts, double maxVecLength) {
    147160
    148161    Graphdata graphdata;
    149     float singleX[2], singleY[2];
     162    float *xVec, *yVec;
    150163    float vecScaleFactor;
    151164    float graphSize;
    152165    int i;
    153166    char plotTitle[50];
    154     sprintf(plotTitle, "Maximum Vector Size = %5.1e", maxVecLength);
    155 
     167
     168    if (!npts) return FALSE;
    156169    if (!initWindow(kapaID)) return 0;
    157170
     171    snprintf(plotTitle, 50, "Maximum Vector Size = %5.1e", maxVecLength);
     172
    158173    KapaInitGraph(&graphdata);
    159     KapaClearPlots(*kapaID);
    160     if(!scaleGraphdata(x, y, &graphdata, npts)) return 0;
     174    KapaClearSections(*kapaID);
     175    KapaSetFont(*kapaID, "helvetica", 14);
     176
     177    if (!scaleGraphdata(x, y, &graphdata, npts)) return 0;
    161178
    162179    graphSize = graphdata.xmax - graphdata.xmin;
     
    164181        graphSize = graphdata.ymax - graphdata.ymin;
    165182    }
    166 
    167183    vecScaleFactor = graphSize * 0.02 / (float)maxVecLength;
    168 #ifdef TESTING
    169     fprintf(stderr, "GraphSize: %e\n maxVecLength: %e\n vecScaleFactor: %e\n",
    170             graphSize, maxVecLength, vecScaleFactor);
    171 #endif
    172 
     184
     185    // fprintf(stderr, "GraphSize: %e\n maxVecLength: %e\n vecScaleFactor: %e\n", graphSize, maxVecLength, vecScaleFactor);
    173186
    174187    KapaSetFont (*kapaID, "helvetica", 14);
     
    179192    graphdata.ptype = 7;
    180193    graphdata.style = 2;
     194    graphdata.color = KapaColorByName("black");
    181195    KapaPrepPlot(*kapaID, npts, &graphdata);
    182196    KapaPlotVector(*kapaID, npts, x, "x");
    183197    KapaPlotVector(*kapaID, npts, y, "y");
    184198
    185     //plot each vector individually
    186     graphdata.ptype = 0;
    187     graphdata.style = 0;
     199    ALLOCATE (xVec, float, 2*npts);
     200    ALLOCATE (yVec, float, 2*npts);
     201    for (i = 0; i < npts; i++) {
     202      xVec[2*i + 0] = x[i];
     203      yVec[2*i + 0] = y[i];
     204      xVec[2*i + 1] = x[i] + dx[i] * vecScaleFactor;
     205      yVec[2*i + 1] = y[i] + dy[i] * vecScaleFactor;
     206    }
     207
     208    graphdata.ptype = 100; // line segements by point pair
     209    graphdata.style = 2;
    188210    graphdata.color = KapaColorByName("blue");
    189     for(i = 0; i < npts; i++) {
    190         singleX[0] = x[i];
    191         singleY[0] = y[i];
    192         singleX[1] = x[i] + xVec[i] * vecScaleFactor;
    193         singleY[1] = y[i] + yVec[i] * vecScaleFactor;
    194         KapaPrepPlot(*kapaID, 2, &graphdata);
    195         KapaPlotVector(*kapaID, 2, singleX, "x");
    196         KapaPlotVector(*kapaID, 2, singleY, "y");
    197     }
    198     return 1;
    199 }
    200 
    201 int relastroVisualPlotScatter(double values[], double thresh, int npts) {
    202     float *x, *data;
    203     int i;
    204     float xline[2], yline[2];
    205     if (!isVisual || !plotScatter) return 1;
    206     if (!initWindow(&kapa2)) return 0;
    207 
    208     ALLOCATE(x, float, npts);
    209     ALLOCATE(data, float, npts);
    210 
    211     for(i = 0; i < npts; i++) {
    212         x[i] = i;
    213         data[i] = (float) values[i];
    214     }
     211    KapaPrepPlot  (*kapaID, 2*npts, &graphdata);
     212    KapaPlotVector(*kapaID, 2*npts, xVec, "x");
     213    KapaPlotVector(*kapaID, 2*npts, yVec, "y");
     214
     215    free (xVec);
     216    free (yVec);
     217    return TRUE;
     218}
     219
     220int relastroVisualPlotScatter(int *kapaID, float *dXfit, float *dYfit, float *mag, int npts) {
    215221
    216222    Graphdata graphdata;
    217223    KapaSection section;
    218     section.x = 0; section.y = 0; section.dx = 1; section.dy = 1;
    219     section.name = "junk";
     224
     225    if (!initWindow(kapaID)) return 0;
    220226
    221227    KapaInitGraph(&graphdata);
    222     KapaClearPlots(kapa2);
    223     KapaSetSection(kapa2, &section);
    224 
    225     graphdata.ptype = 0;
    226     graphdata.style = 0;
    227     graphdata.xmin = 0;
    228     graphdata.xmax = npts;
    229     graphdata.ymin = 0;
    230     graphdata.ymax = data[npts-1];
    231 
    232     KapaSetFont(kapa2, "helvetica", 14);
    233     KapaSetLimits(kapa2, &graphdata);
    234     KapaBox(kapa2, &graphdata);
    235     KapaSendLabel( kapa2, "Object", KAPA_LABEL_XM);
    236     KapaSendLabel( kapa2, "Offset(pixels)", KAPA_LABEL_YM);
    237     KapaSendLabel( kapa2, "Astrometric Offset with fit cutoff",
    238                    KAPA_LABEL_XP);
    239     KapaPrepPlot(kapa2, npts, &graphdata);
    240     KapaPlotVector(kapa2, npts, x, "x");
    241     KapaPlotVector(kapa2, npts, data, "y");
    242 
    243     graphdata.color = KapaColorByName("red");
    244     KapaPrepPlot(kapa2, 2, &graphdata);
    245     yline[0] = (float) thresh;
    246     yline[1] = (float) thresh;
    247     xline[0] = graphdata.xmin;
    248     xline[1] = graphdata.xmax;
    249     KapaPlotVector(kapa2, 2, xline, "x");
    250     KapaPlotVector(kapa2, 2, yline, "y");
    251 
    252     askUser(&plotScatter);
    253     return 1;
    254 }
    255 
    256 
     228    KapaClearSections(*kapaID);
     229    KapaSetFont(*kapaID, "helvetica", 14);
     230
     231    section.name = "a";
     232    section.x = 0.0; section.y = 0.0; section.dx = 1.0; section.dy = 0.5;
     233    section.bg = KapaColorByName("white");
     234    KapaSetSection(*kapaID, &section);
     235
     236    graphdata.ptype = 2;
     237    graphdata.style = 2;
     238
     239    if(!scaleGraphdata(mag, dXfit, &graphdata, npts)) return 0;
     240    KapaSetLimits(*kapaID, &graphdata);
     241    KapaBox(*kapaID, &graphdata);
     242    KapaSendLabel(*kapaID, "mag", KAPA_LABEL_XM);
     243    KapaSendLabel(*kapaID, "dXfit", KAPA_LABEL_YM);
     244    KapaPrepPlot(*kapaID, npts, &graphdata);
     245    KapaPlotVector(*kapaID, npts, mag, "x");
     246    KapaPlotVector(*kapaID, npts, dXfit, "y");
     247
     248    section.name = "b";
     249    section.x = 0.0; section.y = 0.5; section.dx = 1.0; section.dy = 0.5;
     250    section.bg = KapaColorByName("white");
     251    KapaSetSection(*kapaID, &section);
     252
     253    graphdata.ptype = 2;
     254    graphdata.style = 2;
     255
     256    if(!scaleGraphdata(mag, dYfit, &graphdata, npts)) return 0;
     257    KapaSetLimits(*kapaID, &graphdata);
     258    KapaBox(*kapaID, &graphdata);
     259    KapaSendLabel(*kapaID, "mag", KAPA_LABEL_XM);
     260    KapaSendLabel(*kapaID, "dYfit", KAPA_LABEL_YM);
     261    KapaPrepPlot(*kapaID, npts, &graphdata);
     262    KapaPlotVector(*kapaID, npts, mag, "x");
     263    KapaPlotVector(*kapaID, npts, dYfit, "y");
     264
     265    return TRUE;
     266}
    257267
    258268/** plot raw vs ref (L, M). Only those whose distance is < drMax are used in fit*/
    259 int relastroVisualPlotRawRef(StarData *raw, StarData *ref, double dRmax, int numObj) {
    260 
    261   if( !isVisual || !plotRawRef) return 1;
    262   if( !initWindow(&kapa)) return 0;
     269int relastroVisualPlotFittedStars(int *kapaID, float *rawX, float *rawY, float *refX, float *refY, int numNoFit, float *rawXfit, float *rawYfit, float *refXfit, float *refYfit, int numFit) {
     270
     271  Graphdata graphdata;
     272
     273  if (!initWindow(kapaID)) return 0;
     274
     275  KapaInitGraph(&graphdata);
     276  KapaClearPlots(*kapaID);
     277
     278  graphdata.ptype = 7;
     279  graphdata.style = 2;
     280
     281  if(!scaleGraphdata(rawXfit, rawYfit, &graphdata, numFit)) {
     282      fprintf(stderr, "Not enough finite points for plotting");
     283      return 0;
     284  }
     285
     286  KapaSetFont(*kapaID, "helvetica", 14);
     287  KapaSetLimits(*kapaID, &graphdata);
     288  KapaBox(*kapaID, &graphdata);
     289  KapaSendLabel( *kapaID, "X", KAPA_LABEL_XM);
     290  KapaSendLabel( *kapaID, "Y", KAPA_LABEL_YM);
     291  KapaSendLabel( *kapaID, "orange, red, green, blue: (raw, ref), (nofit, fit)",
     292                 KAPA_LABEL_XP);
     293
     294  graphdata.color = KapaColorByName("orange");
     295  graphdata.size = 1;
     296  KapaPrepPlot(*kapaID, numNoFit, &graphdata);
     297  KapaPlotVector(*kapaID, numNoFit, rawX, "x");
     298  KapaPlotVector(*kapaID, numNoFit, rawY, "y");
     299
     300  graphdata.color = KapaColorByName("red");
     301  graphdata.size = 2;
     302  KapaPrepPlot(*kapaID, numNoFit, &graphdata);
     303  KapaPlotVector(*kapaID, numNoFit, refX, "x");
     304  KapaPlotVector(*kapaID, numNoFit, refY, "y");
     305
     306  graphdata.color = KapaColorByName("green");
     307  graphdata.size = 1;
     308  KapaPrepPlot(*kapaID, numFit,  &graphdata);
     309  KapaPlotVector(*kapaID, numFit, rawXfit, "x");
     310  KapaPlotVector(*kapaID, numFit, rawYfit, "y");
     311
     312  graphdata.color = KapaColorByName("blue");
     313  graphdata.size = 2;
     314  KapaPrepPlot(*kapaID, numFit, &graphdata);
     315  KapaPlotVector(*kapaID, numFit, refXfit, "x");
     316  KapaPlotVector(*kapaID, numFit, refYfit, "y");
     317
     318  return TRUE;
     319}
     320
     321/** create various plots using the data in raw and ref **/
     322int relastroVisualPlotChipFit(StarData *raw, StarData *ref, double dRmax, int numObj) {
    263323
    264324  float *rawX, *rawY,  *refX, *refY;
    265325  float *rawXfit, *rawYfit, *refXfit, *refYfit;
    266326  float *magRaw, *magRef, *magRawfit, *magReffit;
    267   float *xVec, *yVec;
     327  float *dXfit, *dYfit, *dPos;
    268328  int numFit = 0, numNoFit = 0;
    269329  double dL, dM, dR;
     330
     331  if (!isVisual) return TRUE;
    270332
    271333  ALLOCATE(rawX,      float, numObj);
     
    273335  ALLOCATE(refX,      float, numObj);
    274336  ALLOCATE(refY,      float, numObj);
     337  ALLOCATE(magRaw,    float, numObj);
     338  ALLOCATE(magRef,    float, numObj);
     339
    275340  ALLOCATE(rawXfit,   float, numObj);
    276341  ALLOCATE(rawYfit,   float, numObj);
    277342  ALLOCATE(refXfit,   float, numObj);
    278343  ALLOCATE(refYfit,   float, numObj);
    279   ALLOCATE(magRaw,    float, numObj);
    280   ALLOCATE(magRef,    float, numObj);
    281344  ALLOCATE(magRawfit, float, numObj);
    282345  ALLOCATE(magReffit, float, numObj);
     346  ALLOCATE(dXfit,     float, numObj);
     347  ALLOCATE(dYfit,     float, numObj);
     348  ALLOCATE(dPos,      float, numObj);
    283349
    284350  int i;
    285351  for(i = 0; i < numObj; i++) {
    286     if  (raw[i].mask) continue;
     352    if (raw[i].mask) continue; // XXX
    287353
    288354    dL = raw[i].L - ref[i].L;
    289355    dM = raw[i].M - ref[i].M;
    290356    dR = hypot (dL, dM);
     357
     358    // XXX change the selection to a mask-based thing
    291359    if (dR > dRmax) {
     360      // UNFITTED values
    292361      rawX[numNoFit] = raw[i].X;
    293362      rawY[numNoFit] = raw[i].Y;
     
    298367      numNoFit++;
    299368    } else {
    300       rawXfit[numFit] = raw[i].X;
    301       rawYfit[numFit] = raw[i].Y;
    302       refXfit[numFit] = ref[i].X;
    303       refYfit[numFit] = ref[i].Y;
    304       magRaw[numFit] = raw[i].Mag;
    305       magRef[numFit] = ref[i].Mag;
     369      // FITTED values
     370      rawXfit[numFit]   = raw[i].X;
     371      rawYfit[numFit]   = raw[i].Y;
     372      refXfit[numFit]   = ref[i].X;
     373      refYfit[numFit]   = ref[i].Y;
     374      magRawfit[numFit] = raw[i].Mag;
     375      magReffit[numFit] = ref[i].Mag;
     376      dPos[numFit]      = ref[i].dPos;
     377      dXfit[numFit]     = raw[i].X - ref[i].X;
     378      dYfit[numFit]     = raw[i].Y - ref[i].Y;
    306379      numFit++;
    307380    }
     
    310383  if (numFit == 0) return 0;
    311384
    312   Graphdata graphdata;
    313 
    314   KapaInitGraph(&graphdata);
    315   KapaClearPlots(kapa);
    316 
    317   graphdata.ptype = 7;
    318   graphdata.style = 2;
    319 
    320   if(!scaleGraphdata(rawXfit, rawYfit, &graphdata, numFit)) {
    321       fprintf(stderr, "Not enough finite points for plotting");
    322       return 0;
    323   }
    324 
    325   KapaSetFont(kapa, "helvetica", 14);
    326   KapaSetLimits(kapa, &graphdata);
    327   KapaBox(kapa, &graphdata);
    328   KapaSendLabel( kapa, "X", KAPA_LABEL_XM);
    329   KapaSendLabel( kapa, "Y", KAPA_LABEL_YM);
    330   KapaSendLabel( kapa, "orange, red, green, blue: (raw, ref), (nofit, fit)",
    331                  KAPA_LABEL_XP);
    332 
    333   graphdata.color = KapaColorByName("orange");
    334   graphdata.size = 1;
    335   KapaPrepPlot(kapa, numNoFit, &graphdata);
    336   KapaPlotVector(kapa, numNoFit, rawX, "x");
    337   KapaPlotVector(kapa, numNoFit, rawY, "y");
    338 
    339   graphdata.color = KapaColorByName("red");
    340   graphdata.size = 2;
    341   KapaPrepPlot(kapa, numNoFit, &graphdata);
    342   KapaPlotVector(kapa, numNoFit, refX, "x");
    343   KapaPlotVector(kapa, numNoFit, refY, "y");
    344 
    345   graphdata.color = KapaColorByName("green");
    346   graphdata.size = 1;
    347   KapaPrepPlot(kapa, numFit,  &graphdata);
    348   KapaPlotVector(kapa, numFit, rawXfit, "x");
    349   KapaPlotVector(kapa, numFit, rawYfit, "y");
    350 
    351   graphdata.color = KapaColorByName("blue");
    352   graphdata.size = 2;
    353   KapaPrepPlot(kapa, numFit, &graphdata);
    354   KapaPlotVector(kapa, numFit, refXfit, "x");
    355   KapaPlotVector(kapa, numFit, refYfit, "y");
    356 
    357   ALLOCATE(xVec, float, numFit);
    358   ALLOCATE(yVec, float, numFit);
    359 
    360   //plot the fitted objects as vectors
    361   for(i = 0; i < numFit; i++) {
    362       xVec[i] = rawXfit[i] - refXfit[i];
    363       yVec[i] = rawYfit[i] - refYfit[i];
    364   }
    365 
    366   plotVectorField(rawXfit, rawYfit, xVec, yVec, numFit, &kapa3, dRmax);
    367   if(!residPlot(rawXfit, rawYfit, xVec, yVec, numFit, &kapa2)) {
    368       fprintf(stderr, "Unable to plot residuals");
    369       return 0;
    370   }
    371 
    372   FREE(xVec);
    373   FREE(yVec);
     385  // 4-panel plot of x vs dx, y vs dx, x vs dy, y vs dy
     386  relastroVisualRawRef(&kapa1, rawXfit, rawYfit, dXfit, dYfit, dPos, numFit);
     387
     388  // vector line plot for the fit
     389  relastroVisualVectorField(&kapa2, rawXfit, rawYfit, dXfit, dYfit, numFit, dRmax);
     390
     391  // dXfit, dYfit vs mag
     392  relastroVisualPlotScatter(&kapa3, dXfit, dYfit, magRawfit, numFit);
     393
     394  // plot the positions of the fitted stars on the chip
     395  relastroVisualPlotFittedStars(&kapa4, rawX, rawY, refX, refY, numNoFit, rawXfit, rawYfit, refXfit, refYfit, numFit);
    374396
    375397  askUser(&plotRawRef);
    376398
     399  FREE(dXfit);
     400  FREE(dYfit);
     401  FREE(dPos);
    377402  FREE(rawX);
    378403  FREE(rawY);
     
    388413  FREE(magReffit);
    389414
    390   return 1;
     415  return TRUE;
    391416}
    392417
     
    403428  KapaSection section;
    404429 
    405   if (!isVisual || !plotOutliers) return 1;
    406   if (!initWindow(&kapa)) return 0;
     430  if (!isVisual || !plotOutliers) return TRUE;
     431  if (!initWindow(&kapa1)) return 0;
    407432 
    408433  // populate vectors
     
    426451  for(i = 0; i < Nmeasure; i++, m++) {
    427452    meas = catalog[0].measure[m];
    428     if (!MeasFilterTest(&meas)) continue;
     453    if (!MeasFilterTest(&meas, FALSE)) continue;
    429454    xmin = MIN(xmin, meas.dR);
    430455    xmax = MAX(xmax, meas.dR);
     
    459484  section.x = 0; section.y = 0; section.dx = 1; section.dy = 1;
    460485  section.name = "junk";
     486  section.bg = KapaColorByName("white");
    461487 
    462488  KapaInitGraph(&graphdata);
    463   KapaClearPlots(kapa);
    464   KapaSetFont(kapa, "helvetica", 14);
     489  KapaClearPlots(kapa1);
     490  KapaSetFont(kapa1, "helvetica", 14);
    465491 
    466492  graphdata.ptype = 7;
     
    472498  graphdata.ymax = ymax;
    473499
    474   KapaSetSection(kapa, &section);
    475   KapaSetLimits(kapa, &graphdata);
    476   KapaBox(kapa, &graphdata);
    477 
    478   KapaSendLabel( kapa, "RA (arcsec)", KAPA_LABEL_XM);
    479   KapaSendLabel( kapa, "Dec (arcsec)", KAPA_LABEL_YM);
    480   KapaSendLabel( kapa, "Points flagged as outliers (red)",
     500  KapaSetSection(kapa1, &section);
     501  KapaSetLimits(kapa1, &graphdata);
     502  KapaBox(kapa1, &graphdata);
     503
     504  KapaSendLabel( kapa1, "RA (arcsec)", KAPA_LABEL_XM);
     505  KapaSendLabel( kapa1, "Dec (arcsec)", KAPA_LABEL_YM);
     506  KapaSendLabel( kapa1, "Points flagged as outliers (red)",
    481507                 KAPA_LABEL_XP);
    482508
    483509  graphdata.color = KapaColorByName("green");
    484   KapaPrepPlot(kapa, Nin, &graphdata);
    485   KapaPlotVector(kapa, Nin, Rin, "x");
    486   KapaPlotVector(kapa, Nin, Din, "y");
     510  KapaPrepPlot(kapa1, Nin, &graphdata);
     511  KapaPlotVector(kapa1, Nin, Rin, "x");
     512  KapaPlotVector(kapa1, Nin, Din, "y");
    487513
    488514  graphdata.color = KapaColorByName("red");
    489   KapaPrepPlot(kapa, Nout, &graphdata);
    490   KapaPlotVector(kapa, Nout, Rout, "x");
    491   KapaPlotVector(kapa, Nout, Dout, "y");
     515  KapaPrepPlot(kapa1, Nout, &graphdata);
     516  KapaPlotVector(kapa1, Nout, Rout, "x");
     517  KapaPlotVector(kapa1, Nout, Dout, "y");
    492518
    493519  graphdata.color = KapaColorByName("black");
    494520  graphdata.ptype = 0;
    495521  graphdata.style = 0;
    496   KapaPrepPlot(kapa, 100, &graphdata);
    497   KapaPlotVector(kapa, 100, xCirc, "x");
    498   KapaPlotVector(kapa, 100, yCirc, "y");
    499 
    500 
    501  
     522  KapaPrepPlot(kapa1, 100, &graphdata);
     523  KapaPlotVector(kapa1, 100, xCirc, "x");
     524  KapaPlotVector(kapa1, 100, yCirc, "y");
     525
    502526  askUser(&plotOutliers);
    503527
     
    510534}
    511535 
    512  
     536# if (0)
     537int relastroVisualSummaryChips() {
     538
     539  // plot the dXsys, dYsys histograms
     540
     541  // plot x vs dx, y vs dy, etc for all mosaics
     542
     543  // plot a map of median star scatter
     544
     545}
     546# endif
     547
  • trunk/Ohana/src/relastro/src/select_images.c

    r29001 r30616  
    1515void dsortindex (double *X, off_t *Y, int N);
    1616off_t getRegionStartByRA (double R, double *Rref, off_t Nregions);
     17
     18# define MARKTIME(MSG,...) { \
     19  float dtime; \
     20  gettimeofday (&stop, (void *) NULL); \
     21  dtime = DTIME (stop, start); \
     22  fprintf (stderr, MSG, __VA_ARGS__); }
    1723
    1824Image *select_images (SkyList *skylist, Image *timage, off_t Ntimage, off_t **LineNumber, off_t *Nimage) {
     
    2531  Coords tcoords;
    2632  SkyRegionCoords *skycoords;
    27  
     33  struct timeval start, stop;
     34 
     35  double RmaxSkyRegion, RminSkyRegion, RmidSkyRegion, DminSkyRegion, DmaxSkyRegion;
     36
    2837  double *RmaxSky;
    2938  off_t *index;
     
    4049    return NULL;
    4150  }
     51
     52  gettimeofday (&start, (void *) NULL);
    4253
    4354  // the comparison is made in the catalog local projection. below we set crval1,2
     
    5263  ALLOCATE (RmaxSky, double, skylist[0].Nregions);
    5364  ALLOCATE (index, off_t, skylist[0].Nregions);
     65
     66  RminSkyRegion = +360.0;
     67  RmaxSkyRegion = -360.0;
     68  DminSkyRegion = +90.0;
     69  DmaxSkyRegion = -90.0;
    5470
    5571  /* compare with each region file */
     
    8096    skycoords[i].Xc[3] -= dx; skycoords[i].Yc[3] += dy;
    8197    skycoords[i].Xc[4] -= dx; skycoords[i].Yc[4] -= dy;
    82   }
     98
     99    RminSkyRegion = MIN(RminSkyRegion, skylist[0].regions[i][0].Rmin);
     100    RmaxSkyRegion = MAX(RmaxSkyRegion, skylist[0].regions[i][0].Rmax);
     101    DminSkyRegion = MIN(DminSkyRegion, skylist[0].regions[i][0].Dmin);
     102    DmaxSkyRegion = MAX(DmaxSkyRegion, skylist[0].regions[i][0].Dmax);
     103  }
     104  RmidSkyRegion = 0.5*(RminSkyRegion + RmaxSkyRegion);
     105  MARKTIME("create sky region coords: %f sec\n", dtime);
    83106
    84107  dsortindex (RmaxSky, index, skylist[0].Nregions);
     108  MARKTIME("sort sky coords: %f sec\n", dtime);
    85109
    86110  if (VERBOSE) fprintf (stderr, "finding images\n");
    87111  BuildChipMatch (timage, Ntimage);
     112  MARKTIME("build chip match: %f sec\n", dtime);
    88113
    89114  nimage = 0;
     
    124149    }
    125150
    126     /* define image corners */
    127     Xi[0] = 0;            Yi[0] = 0;
    128     Xi[1] = timage[i].NX; Yi[1] = 0;
    129     Xi[2] = timage[i].NX; Yi[2] = timage[i].NY;
    130     Xi[3] = 0;            Yi[3] = timage[i].NY;
    131     Xi[4] = 0;            Yi[4] = 0;
     151    /* define image corners - note the DIS images (mosaic phu) are special */
     152    if (!strcmp(&timage[i].coords.ctype[4], "-DIS")) {
     153      Xi[0] = -0.5*timage[i].NX; Yi[0] = -0.5*timage[i].NY;
     154      Xi[1] = +0.5*timage[i].NX; Yi[1] = -0.5*timage[i].NY;
     155      Xi[2] = +0.5*timage[i].NX; Yi[2] = +0.5*timage[i].NY;
     156      Xi[3] = -0.5*timage[i].NX; Yi[3] = +0.5*timage[i].NY;
     157      Xi[4] = -0.5*timage[i].NX; Yi[4] = -0.5*timage[i].NY;
     158    } else {
     159      Xi[0] = 0;            Yi[0] = 0;
     160      Xi[1] = timage[i].NX; Yi[1] = 0;
     161      Xi[2] = timage[i].NX; Yi[2] = timage[i].NY;
     162      Xi[3] = 0;            Yi[3] = timage[i].NY;
     163      Xi[4] = 0;            Yi[4] = 0;
     164    }
    132165    found = FALSE;
    133166
    134167    /* transform corners to ra,dec */
    135168    double RminImage = 360.0;
     169    double RmaxImage =   0.0;
     170    double DminImage = +90.0;
     171    double DmaxImage = -90.0;
     172    // int leftside = FALSE;
    136173    for (j = 0; j < 5; j++) {
    137174      XY_to_RD (&Ri[j], &Di[j], Xi[j], Yi[j], &timage[i].coords);
     175      Ri[j] = ohana_normalize_angle_to_midpoint (Ri[j], RmidSkyRegion);
     176
    138177      RminImage = MIN(RminImage, Ri[j]);
    139     }
     178      RmaxImage = MAX(RmaxImage, Ri[j]);
     179      DminImage = MIN(DminImage, Di[j]);
     180      DmaxImage = MAX(DmaxImage, Di[j]);
     181    }
     182    if (RmaxImage - RminImage > 180.0) {
     183        double tmp = RminImage;
     184        RmaxImage = RminImage;
     185        RminImage = tmp - 360.0;
     186    }
     187
     188    // check that this image is even in range of the searched region
     189    if (DminImage > DmaxSkyRegion) continue;
     190    if (DmaxImage < DminSkyRegion) continue;
     191   
     192    // the sky region RA is defined to be 0 - 360.0
     193    if (RminImage > RmaxSkyRegion) continue;
     194    if (RmaxImage < RminSkyRegion) continue;
     195
     196    // image overlaps region, keep it
     197    if (USE_BASIC_CHECK) goto found_it;
    140198
    141199    // RA(nStart) is guaranteed to be < RminImage:
     
    161219      for (j = 0; (j < 4) && !found; j++) {
    162220        found = corner_check (&Xi[j], &Yi[j], &skycoords[m].Xc[0], &skycoords[m].Yc[0]);
     221        if (found) goto found_it;
    163222      }
    164223      /* check if catalog corner inside image */
    165224      for (j = 0; (j < 4) && !found; j++) {
    166225        found = corner_check (&skycoords[m].Xc[j], &skycoords[m].Yc[j], &Xi[0], &Yi[0]);
     226        if (found) goto found_it;
    167227      }
    168228      /* check if edges cross */
     
    170230        for (k = 0; (k < 4) && !found; k++) {
    171231          found = edge_check (&Xi[j], &Yi[j], &skycoords[m].Xc[k], &skycoords[m].Yc[k]);
     232          if (found) goto found_it;
    172233        }
    173234      }
    174       if (!found) continue;
    175 
    176       image[nimage] = timage[i];
    177       /* always allow 'few' images to succeed, if possible */
    178       if (image[nimage].flags & ID_IMAGE_ASTROM_FEW) {
    179         image[nimage].flags &= ~ID_IMAGE_ASTROM_FEW;
    180       }
    181       if (RESET) {
    182         // XXX do we need / want to do this in relastro?
    183         assignMcal (&image[nimage], (double *) NULL, -1);
    184         image[nimage].dMcal = NAN;
    185         image[nimage].flags &= ~badImage;
    186       }
    187       line_number[nimage] = i;
    188       nimage ++;
    189       if (nimage == NIMAGE) {
    190         NIMAGE += 100;
    191         REALLOCATE (image, Image, NIMAGE);
    192         REALLOCATE (line_number, off_t, NIMAGE);
    193       }
    194     }
    195   }
    196      
     235    }
     236    if (!found) continue;
     237
     238  found_it:
     239    image[nimage] = timage[i];
     240    /* always allow 'few' images to succeed, if possible */
     241    if (image[nimage].flags & ID_IMAGE_ASTROM_FEW) {
     242      image[nimage].flags &= ~ID_IMAGE_ASTROM_FEW;
     243    }
     244    if (RESET) {
     245      // XXX do we need / want to do this in relastro?
     246      // assignMcal (&image[nimage], (double *) NULL, -1);
     247      // image[nimage].Mcal = NAN;
     248      // image[nimage].dMcal = NAN;
     249      image[nimage].flags &= ~badImage;
     250    }
     251    line_number[nimage] = i;
     252    nimage ++;
     253    if (nimage == NIMAGE) {
     254      NIMAGE += 100;
     255      REALLOCATE (image, Image, NIMAGE);
     256      REALLOCATE (line_number, off_t, NIMAGE);
     257    }
     258  }
     259  MARKTIME("finish image selection: %f sec\n", dtime);
     260   
    197261  if (VERBOSE) fprintf (stderr, "found "OFF_T_FMT" images\n",  nimage);
    198262
Note: See TracChangeset for help on using the changeset viewer.