IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
29 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/Ohana

  • branches/simtest_nebulous_branches/Ohana/src/relastro

    • Property svn:mergeinfo set to (toggle deleted branches)
      /trunk/Ohana/src/relastromergedeligible
      /branches/eam_branches/20091201/Ohana/src/relastro26295-26885
      /branches/eam_branches/Ohana.20100407/src/relastro27635-27772
      /branches/eam_branches/largefiles.20100314/Ohana/src/relastro27281-27430
      /branches/eam_branches/relastro.2010032627487-27559
      /branches/pap_delete/Ohana/src/relastro27530-27595
  • branches/simtest_nebulous_branches/Ohana/src/relastro/Makefile

    r24308 r27840  
    5656$(SRC)/save_catalogs.$(ARCH).o       \
    5757$(SRC)/write_coords.$(ARCH).o        \
     58$(SRC)/CoordOps.$(ARCH).o        \
     59$(SRC)/FixProblemImages.$(ARCH).o        \
    5860$(SRC)/relastroVisual.$(ARCH).o
    5961
  • branches/simtest_nebulous_branches/Ohana/src/relastro/doc/notes.txt

    r17043 r27840  
     1
     22010.03.24
     3
     4  I have finished only part of the optimizations listed below: I have
     5  made the detection->image and image->mosaic lookups use bracket
     6  searches.  I have NOT converted the image->mosaic lookups to use the
     7  parentID, so these matches are still based on time ranges.  I have
     8  also added a bit of positional constraint to the image match
     9  function in 'select_images' (comparing Rmax of the sky regions to
     10  Rmin of the images).  These have had a huge improvement.  Here is
     11  the execution time for the SAS data (~450 images) with the old vs
     12  the new code on pikake:
     13
     14  old : 39min
     15  new :  7min
     16
     17  The bulk of the time for the new code is now disk I/O.
     18
     192010.03.23
     20
     21  I've been working on optimization.  I've updated ImageOps to make
     22  the measure->image relationship generation use the image IDs along
     23  with a bracket search -- this goes much faster as a result.  More
     24  work is needed:
     25
     26  * select_images needs to apply astrometry to all image corners:
     27    -> cache the image center and radius and use this to narrow down
     28       the searches.
     29
     30  * MosaicOps uses the time to match mosaics.  I've added sorting to
     31    speed this up, but this should be done based on the image and
     32    parent index.
     33
     34    -> make a tool to create the parent ID for existing DBs.
     35    -> make sure addstar is populating the parent IDs
     36    -> use the parent IDs to make the link.
     37
     38load image data: 0.006347 sec
     39  setup sky: 0.091475 sec
     40  convert image table: 0.091498 sec
     41  select images: 2.925978 sec
     42  init images: 2.926957 sec
     43  init mosaics: 2.927319 sec
     44load images: 2.933762 sec
     45load catalog data: 9.772259 sec
     46make image bins: 9.780618 sec
     47set up image indexes: 10.196519 sec
     48
     49load image data: 0.006746 sec
     50  setup sky: 0.091440 sec
     51  convert image table: 0.091467 sec
     52  select images: 0.374193 sec
     53  init images: 0.375355 sec
     54  init mosaics: 0.375753 sec
     55load images: 0.382606 sec
     56load catalog data: 7.260094 sec
     57make image bins: 7.268564 sec
     58set up image indexes: 7.687485 sec
     59
    160
    2612008.03.01
  • branches/simtest_nebulous_branches/Ohana/src/relastro/include/relastro.h

    r24308 r27840  
    9191
    9292int    VERBOSE;
     93int    VERBOSE2;
    9394
    9495int    RESET;
     96int    NLOOP;
    9597int    UPDATE;
    9698int    PLOTSTUFF;
     
    104106int    CHIPORDER;
    105107
     108int UserCatalog;
     109double UserCatalogRA, UserCatalogDEC;
     110
    106111char          *PHOTCODE_KEEP_LIST, *PHOTCODE_SKIP_LIST;
    107112int           NphotcodesKeep,      NphotcodesSkip;
     
    140145char         *GetPhotnamebyCode   PROTO((PhotCodeData *photcodes, int code));
    141146void          InterpolateGrid     PROTO((float *buffer, int Nx, int Ny, Coords *ccd, Coords *gcoords));
    142 int          *SelectRefMosaic     PROTO((Mosaic **refmosaic, int *Nimage));
     147off_t          *SelectRefMosaic     PROTO((Mosaic **refmosaic, off_t *Nimage));
    143148int           args                PROTO((int argc, char **argv));
    144149int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog));
     
    151156void          dump_grid           PROTO((void));
    152157int           edge_check          PROTO((double *x1, double *y1, double *x2, double *y2));
    153 void          findImages          PROTO((Catalog *catalog, int Ncatalog));
     158void          findImages          PROTO((Catalog *catalog, int Ncatalog, int MATCHCAT));
    154159int           findMosaics         PROTO((Catalog *catalog, int Ncatalog));
    155 Image        *find_images         PROTO((FITS_DB *db, GSCRegion *region, int Nregion, int *Nimage, int **LineNum));
     160Image        *find_images         PROTO((FITS_DB *db, GSCRegion *region, off_t Nregion, off_t *Nimage, off_t **LineNum));
    156161void set_db (FITS_DB *in);
    157 int Shutdown (char *format, ...);
     162int Shutdown (char *format, ...) OHANA_FORMAT(printf, 1, 2);
    158163void TrapSignal (int sig);
    159164void SetProtect (int mode);
    160165int SetSignals (void);
    161166
    162 GSCRegion    *find_regions        PROTO((Image *image, int Nimage, int *Nregions, GSCRegion *fullregion));
     167GSCRegion    *find_regions        PROTO((Image *image, off_t Nimage, int *Nregions, GSCRegion *fullregion));
    163168void          freeGridBins        PROTO((int Ncatalog));
    164169void          freeImageBins       PROTO((int Ncatalog));
     
    166171void          free_catalogs       PROTO((Catalog *catalog, int Ncatalog));
    167172int           gcatalog            PROTO((Catalog *catalog, int FINAL));
    168 Coords       *getCoords           PROTO((int meas, int cat));
    169 float         getMcal             PROTO((int meas, int cat));
    170 float         getMgrid            PROTO((int meas, int cat));
    171 float         getMmos             PROTO((int meas, int cat));
    172 float         getMrel             PROTO((Catalog *catalog, int meas, int cat));
    173 GSCRegion    *get_regions         PROTO((double minRa, double maxRa, double minDec, double maxDec, int *Nregions));
    174 void          getfullregion       PROTO((Image *image, int Nimage, GSCRegion *fullregion));
    175 Image        *getimage            PROTO((int N));
    176 Image        *getimages           PROTO((int *N));
     173// Coords       *getCoords           PROTO((off_t meas, int cat));
     174float         getMcal             PROTO((off_t meas, int cat));
     175float         getMgrid            PROTO((off_t meas, int cat));
     176float         getMmos             PROTO((off_t meas, int cat));
     177float         getMrel             PROTO((Catalog *catalog, off_t meas, int cat));
     178GSCRegion    *get_regions         PROTO((double minRa, double maxRa, double minDec, double maxDec, off_t *Nregions));
     179void          getfullregion       PROTO((Image *image, off_t Nimage, GSCRegion *fullregion));
     180Image        *getimage            PROTO((off_t N));
     181Image        *getimages           PROTO((off_t *N));
    177182void          global_stats        PROTO((Catalog *catalog, int Ncatalog));
    178183void          initGrid            PROTO((int dX, int dY));
    179184void          initGridBins        PROTO((Catalog *catalog, int Ncatalog));
    180 void          initImageBins       PROTO((Catalog *catalog, int Ncatalog));
    181 void          initImages          PROTO((Image *input, int N));
     185void          initImageBins       PROTO((Catalog *catalog, int Ncatalog, int FULLINIT));
     186void          initImages          PROTO((Image *input, off_t N));
    182187void          initMosaicBins      PROTO((Catalog *catalog, int Ncatalog));
    183 void          initMosaicGrid      PROTO((Image *image, int Nimage));
    184 void          initMosaics         PROTO((Image *image, int Nimage));
     188void          initMosaicGrid      PROTO((Image *image, off_t Nimage));
     189void          initMosaics         PROTO((Image *image, off_t Nimage));
    185190void          initMrel            PROTO((Catalog *catalog, int Ncatalog));
    186191void          initialize          PROTO((int argc, char **argv));
     
    189194Catalog      *load_catalogs       PROTO((SkyList *skylist, int *Ncatalog, int subselect));
    190195SkyList      *load_images         PROTO((FITS_DB *db, SkyRegion *region));
    191 Image        *select_images       PROTO((SkyList *skylist, Image *timage, int Ntimage, int **LineNumber, int *Nimage));
     196Image        *select_images       PROTO((SkyList *skylist, Image *timage, off_t Ntimage, off_t **LineNumber, off_t *Nimage));
    192197
    193198void check_permissions (char *basefile);
     
    198203
    199204int           main                PROTO((int argc, char **argv));
    200 void          mark_images         PROTO((Image *image, int Nimage, Image *timage, int Ntimage));
    201 void          matchImage          PROTO((Catalog *catalog, int meas, int cat));
    202 void          matchMosaics        PROTO((Catalog *catalog, int meas, int cat));
    203 GSCRegion    *name_region         PROTO((char *name, int *Nregions));
     205void          mark_images         PROTO((Image *image, off_t Nimage, Image *timage, off_t Ntimage));
     206void          matchImage          PROTO((Catalog *catalog, off_t meas, int cat, int MATCHCAT));
     207void          matchMosaics        PROTO((Catalog *catalog, off_t meas, int cat));
     208GSCRegion    *name_region         PROTO((char *name, off_t *Nregions));
    204209double        opening_angle       PROTO((double x1, double y1, double x2, double y2, double x3, double y3));
    205210void          plot_chisq          PROTO((Catalog *catalog, int Ncatalog));
     
    224229int           setMrelOutput       PROTO((Catalog *catalog, int Ncatalog, int mark));
    225230void          set_ZP              PROTO((double ZERO));
    226 int           setrefcode          PROTO((Image *image, int Nimage));
     231int           setrefcode          PROTO((Image *image, off_t Nimage));
    227232void          skip_measurements   PROTO((Catalog *catalog, int pass));
    228233void          sortA               PROTO((double *X, int N));
     
    250255void fit_free (CoordFit *fit);
    251256void fit_add (CoordFit *fit, double x1, double y1, double x2, double y2, double wt);
    252 void fit_eval (CoordFit *fit);
     257int fit_eval (CoordFit *fit);
    253258void fit_apply (CoordFit *fit, double *x2, double *y2, double x1, double y1);
    254259double **poly2d_dx (double **poly, int Nx, int Ny);
     
    256261double **poly2d_copy (double **poly, int Nx, int Ny);
    257262double poly2d_eval (double **poly, int Nx, int Ny, double x, double y);
    258 CoordFit *fit_apply_coords (CoordFit *fit, Coords *coords);
     263int fit_apply_coords (CoordFit *fit, Coords *coords);
    259264int CoordsGetCenter (CoordFit *fit, double tol, double *xo, double *yo);
    260265CoordFit *CoordsSetCenter (CoordFit *input, double Xo, double Yo);
    261 void FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
     266int FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
    262267void FitMosaic (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
    263268void FitSimple (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
     
    268273int UpdateMosaic (Catalog *catalog, int Ncatalog);
    269274int UpdateMeasures (Catalog *catalog, int Ncatalog);
    270 void fixImageRaw (Catalog *catalog, int Ncatalog, int im);
     275void fixImageRaw (Catalog *catalog, int Ncatalog, off_t im);
    271276void FlagOutliers(Catalog *catalog);
    272277int MeasFilterTest(Measure *measure);
    273278
    274279int sun_ecliptic (double jd, double *lambda, double *beta, double *epsilon);
    275 int ParFactor (double *pR, double *pD, double R, double D, time_t T);
     280int ParFactor (double *pR, double *pD, double R, double D, double T, double Tmean);
    276281int FitPM (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, int Npts);
    277282int FitPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *pR, double *pD, int Npts);
    278283int FitPMandPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, int Npts);
    279284
    280 Mosaic *getMosaicForImage (int N);
    281 
    282 StarData *getImageRef (Catalog *catalog, int Ncatalog, int im, int *Nstars, CoordMode mode);
    283 StarData *getImageRaw (Catalog *catalog, int Ncatalog, int im, int *Nstars, CoordMode mode);
    284 
    285 Mosaic *getmosaics (int *N);
    286 void initMosaics (Image *image, int Nimage);
    287 StarData *getMosaicRaw (Catalog *catalog, int Ncatalog, int mos, int *Nstars);
    288 StarData *getMosaicRef (Catalog *catalog, int Ncatalog, int mos, int *Nstars);
    289 Mosaic *getMosaicForImage (int im);
     285Mosaic *getMosaicForImage (off_t N);
     286
     287StarData *getImageRef (Catalog *catalog, int Ncatalog, off_t im, off_t *Nstars, CoordMode mode);
     288StarData *getImageRaw (Catalog *catalog, int Ncatalog, off_t im, off_t *Nstars, CoordMode mode);
     289
     290Mosaic *getmosaics (off_t *N);
     291void initMosaics (Image *image, off_t Nimage);
     292StarData *getMosaicRaw (Catalog *catalog, int Ncatalog, off_t mos, off_t *Nstars);
     293StarData *getMosaicRef (Catalog *catalog, int Ncatalog, off_t mos, off_t *Nstars);
     294Mosaic *getMosaicForImage (off_t im);
    290295
    291296double getMeanR (Measure *measure, Average *average, SecFilt *secfilt);
     
    297302int relastro_objects (void);
    298303int UpdateObjectOffsets (SkyList *skylist);
     304
     305int relastroVisualPlotRawRef(StarData *raw, StarData *ref, double dRmax, int numObj);
     306int relastroVisualPlotScatter(double values[], double thresh, int npts);
     307int relastroVisualPlotOutliers(Catalog *catalog, int offset, int Nmeasure,
     308                               StatType statsR, StatType statsD, double thresh);
     309
     310
     311
     312int FixProblemImages (SkyList *skylist);
     313int *getCatlist (int *N, off_t im);
     314
     315void initCoords (void);
     316void getOffsets (double *dPos, off_t *nPos, off_t N);
     317void saveOffsets (double dPos, off_t nPos, off_t N);
     318void setBadCoords (off_t N);
     319int badCoords (off_t N);
     320Coords *getCoords (off_t N);
     321int saveCoords (Coords *coords, off_t N);
     322void resetImageRaw (Catalog *catalog, int Ncatalog, off_t im);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/ConfigInit.c

    r24308 r27840  
    6060  /* update master photcode table if not defined */
    6161  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
    62   if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
     62  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile, TRUE)) {
    6363    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
    6464    exit (1);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/FitChip.c

    r24308 r27840  
    1717// XXX save measurements of the fit quality (scatter, chisq) in the image table
    1818
    19 void FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {
     19int FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {
    2020
    2121  int i, Nscatter, Niter, skip;
     
    8484      default:
    8585        fprintf (stderr, "invalid chip order %d\n", coords[0].Npolyterms);
    86         abort ();
     86        skip = TRUE;
    8787    }
    8888    if (skip) {
     
    9090      fit_free (fit);
    9191      free (values);
    92       return;
     92      return FALSE;
    9393    }
    9494
    95     fprintf (stderr, "scatter limit: %f based on %d detections; using %d of %d for fit\n", dRmax, Nscatter, fit[0].Npts, Nmatch);
     95    // fprintf (stderr, "scatter limit: %f based on %d detections; using %d of %d for fit\n", dRmax, Nscatter, fit[0].Npts, Nmatch);
    9696
    9797    // measure the fit, update the coords & object coordinates
    98     fit_eval (fit);
    99     fit_apply_coords (fit, coords);
     98    if (!fit_eval (fit)) {
     99      fprintf (stderr, "failed to fit new model\n");
     100      return FALSE;
     101    }
     102
     103    if (!fit_apply_coords (fit, coords)) {
     104      fprintf (stderr, "failed to fit new model\n");
     105      return FALSE;
     106    }
     107
    100108    fit_free (fit);
    101109
     
    107115
    108116  free (values);
    109   return;
     117  return TRUE;
    110118}
    111119
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/FitSimple.c

    r16810 r27840  
    1717
    1818    // XXX the value needs to be set in a more intelligent way
    19     if (dR > 0.15) continue;
     19    if (dR > 0.50) continue;
    2020   
    2121    fit_add (fit, raw[i].X, raw[i].Y, ref[i].P, ref[i].Q, 1.0);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/GetAstromError.c

    r24308 r27840  
    88  switch (mode) {
    99    case ERROR_MODE_RA:
    10       dPobs = measure[0].dXccd;  // need to redefine this as RAerr
     10      dPobs = measure[0].dXccd / 100.0;  // need to redefine this as RAerr
    1111      break;
    1212    case ERROR_MODE_DEC:
    13       dPobs = measure[0].dYccd;  // need to redefine this as RAerr
     13      dPobs = measure[0].dYccd / 100.0;  // need to redefine this as RAerr
    1414      break;
    1515    case ERROR_MODE_POS:
    16       dPobs = hypot (measure[0].dXccd, measure[0].dYccd);  // need to redefine this as RAerr
     16      dPobs = hypot (measure[0].dXccd, measure[0].dYccd) / 100.0;  // need to redefine this as RAerr
    1717      break;
    1818    default:
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/ImageOps.c

    r24308 r27840  
    11# include "relastro.h"
    2 #define TESTING
    3 
     2
     3# define USE_IMAGE_ID 1
     4
     5static off_t       **bin;     // link from catalog,measure to image
     6static int         **clist;   // catalog which supplied measurement on image
     7static off_t       **mlist;   // measure reference for measurement on image
     8static off_t        *Nlist;   // number of measurements on image
     9static off_t        *NLIST;   // allocated number of measurements on image   
     10
     11static Image        *image;   // list of available images
     12static off_t        Nimage;   // number of available images
     13
     14static int         *Ncatlist;  // catalogs associated with each image
     15static int         *NCATLIST;  // catalogs associated with each image
     16static int         **catlist;  // catalogs associated with each image
     17
     18// if we search by image ID, we sort (imageIDs, imageIdx) by imageIDs to get a sorted
     19// index
     20
     21# if USE_IMAGE_ID
     22static off_t        *imageIDs; // list of all image IDs
     23static off_t        *imageIdx; // list of index for image IDs
     24# else
    425static unsigned int *start;
    526static unsigned int *stop;
    6 static int         **bin;
    7 
    8 static int         **clist;
    9 static int         **mlist;
    10 static int          *Nlist;
    11 static int          *NLIST;
    12 
    13 static Image        *image;
    14 static int          Nimage;
    15 
    16 Image *getimages (int *N) {
     27# endif
     28
     29Image *getimages (off_t *N) {
    1730  *N = Nimage;
    1831  return (image);
    1932}
    2033
    21 Image *getimage (int N) {
     34Image *getimage (off_t N) {
    2235  return (&image[N]);
    2336}
    2437
    25 void initImages (Image *input, int N) {
    26 
    27   int i;
     38int *getCatlist (int *N, off_t im) {
     39
     40  *N = Ncatlist[im];
     41  return (catlist[im]);
     42}
     43
     44void initImages (Image *input, off_t N) {
     45
     46  off_t i;
    2847
    2948  image = input;
    3049  Nimage = N;
    3150
    32   ALLOCATE (start,   unsigned, Nimage);
    33   ALLOCATE (stop,    unsigned, Nimage);
     51# if USE_IMAGE_ID
     52  ALLOCATE (imageIDs, off_t, Nimage);
     53  ALLOCATE (imageIdx, off_t, Nimage);
     54
     55  for (i = 0; i < Nimage; i++) {
     56    imageIdx[i] = i;
     57    imageIDs[i] = image[i].imageID;
     58  }
     59  llsortpair (imageIDs, imageIdx, Nimage);
     60# else
     61  ALLOCATE (start, unsigned, Nimage);
     62  ALLOCATE (stop, unsigned, Nimage);
    3463
    3564  for (i = 0; i < Nimage; i++) {
     
    3766    stop[i]  = image[i].tzero + MAX(1.01*image[i].trate*image[i].NY, 1);
    3867  }
    39 }
    40 
    41 void initImageBins (Catalog *catalog, int Ncatalog) {
    42 
    43   int i, j;
    44 
    45   ALLOCATE (bin, int *, Ncatalog);
     68# endif
     69}
     70
     71off_t getImageByID (off_t ID) {
     72
     73  // we have a pair of vectors (imageIDs, imageIdx) sorted by imageIDs
     74  // use bisection to find the specified image ID
     75
     76# if USE_IMAGE_ID
     77  off_t Nlo, Nhi, N;
     78
     79  Nlo = 0; Nhi = Nimage;
     80  while (Nhi - Nlo > 10) {
     81    N = 0.5*(Nlo + Nhi);
     82    if (imageIDs[N] < ID) {
     83      Nlo = MAX(N, 0);
     84    } else {
     85      Nhi = MIN(N + 1, Nimage);
     86    }
     87  }
     88
     89  for (N = Nlo; N < Nhi; N++) {
     90    if (imageIDs[N] == ID)
     91      return (imageIdx[N]);
     92  }
     93# endif
     94
     95  return (-1);
     96}
     97
     98// these are really image & catalog indexes
     99void initImageBins (Catalog *catalog, int Ncatalog, int FULLINIT) {
     100
     101  off_t i, j;
     102
     103  ALLOCATE (bin, off_t *, Ncatalog);
    46104  for (i = 0; i < Ncatalog; i++) {
    47     ALLOCATE (bin[i], int, MAX (catalog[i].Nmeasure, 1));
     105    ALLOCATE (bin[i], off_t, MAX (catalog[i].Nmeasure, 1));
    48106    for (j = 0; j < catalog[i].Nmeasure; j++) bin[i][j] = -1;
    49107  }
    50108
    51   ALLOCATE (Nlist, int, Nimage);
    52   ALLOCATE (NLIST, int, Nimage);
    53   ALLOCATE (clist, int *, Nimage);
    54   ALLOCATE (mlist, int *, Nimage);
     109  ALLOCATE (Nlist, off_t,  Nimage);
     110  ALLOCATE (NLIST, off_t,  Nimage);
     111  ALLOCATE (clist, int *,   Nimage);
     112  ALLOCATE (mlist, off_t *, Nimage);
    55113
    56114  for (i = 0; i < Nimage; i++) {
    57     Nlist[i] = 0;
     115    Nlist[i] =   0;
    58116    NLIST[i] = 100;
    59117    ALLOCATE (clist[i], int, NLIST[i]);
    60     ALLOCATE (mlist[i], int, NLIST[i]);
     118    ALLOCATE (mlist[i], off_t, NLIST[i]);
     119  }
     120
     121  if (FULLINIT) {
     122    ALLOCATE (Ncatlist, int,  Nimage);
     123    ALLOCATE (NCATLIST, int,  Nimage);
     124    ALLOCATE (catlist, int *, Nimage);
     125
     126    for (i = 0; i < Nimage; i++) {
     127      Ncatlist[i] =  0;
     128      NCATLIST[i] = 32;
     129      ALLOCATE (catlist[i], int, NCATLIST[i]);
     130    }
    61131  }
    62132}
     
    64134void freeImageBins (int Ncatalog) {
    65135
    66   int i;
     136  off_t i;
    67137
    68138  for (i = 0; i < Ncatalog; i++) {
     
    79149
    80150/* match measurements to images */
    81 void findImages (Catalog *catalog, int Ncatalog) {
    82 
    83   int i, j;
     151void findImages (Catalog *catalog, int Ncatalog, int MATCHCAT) {
     152
     153  off_t i, j;
    84154  char *name;
    85155
    86156  for (i = 0; i < Ncatalog; i++) {
    87157    for (j = 0; j < catalog[i].Nmeasure; j++) {
    88       matchImage (catalog, j, i);
    89     }
    90   }
    91 
    92   for (i = 0; VERBOSE && (i < Nimage); i++) {
     158      // XXX : update to optionally restrict by photcode equiv a la relphot
     159      // ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].photcode);
     160      // if (photcode[0].code != ecode) continue;
     161      matchImage (catalog, j, i, MATCHCAT);
     162    }
     163  }
     164
     165  for (i = 0; VERBOSE2 && (i < Nimage); i++) {
    93166    name = GetPhotcodeNamebyCode (image[i].photcode);
    94     fprintf (stderr, "image %d has %d measures (%s, %s)\n", i, Nlist[i],
     167    fprintf (stderr, "image %lld has %lld measures (%s, %s)\n", (long long) i, (long long) Nlist[i],
    95168             ohana_sec_to_date(image[i].tzero), name);
    96169  }
    97170}
    98171
    99 /* modify this function to use the measure->imageID field */
    100 void matchImage (Catalog *catalog, int meas, int cat) {
    101 
    102   int i;
     172# if USE_IMAGE_ID
     173// this is the imageID-based match
     174void matchImage (Catalog *catalog, off_t meas, int cat, int MATCHCAT) {
     175
     176  off_t idx, ID;
     177  Measure *measure;
     178  int i, found;
     179
     180  measure = &catalog[cat].measure[meas];
     181
     182  ID = measure[0].imageID;
     183  idx = getImageByID (ID);
     184  if (idx == -1) {
     185    if (VERBOSE2) fprintf (stderr, "can't match detection to image?\n");
     186    return;
     187  }
     188
     189  // index for (catalog, measure) -> image
     190  bin[cat][meas] = idx;
     191
     192  // index for image, Nentry -> catalog
     193  clist[idx][Nlist[idx]] = cat;
     194
     195  // index for image, Nentry -> measure
     196  mlist[idx][Nlist[idx]] = meas;
     197  Nlist[idx] ++;
     198
     199  if (Nlist[idx] == NLIST[idx]) {
     200    NLIST[idx] += 100;
     201    REALLOCATE (clist[idx], int,   NLIST[idx]);
     202    REALLOCATE (mlist[idx], off_t, NLIST[idx]);
     203  }
     204
     205  if (MATCHCAT) {
     206    // index for image -> catalog list
     207    found = FALSE;
     208    for (i = 0; !found && (i < Ncatlist[idx]); i++) {
     209      if (catlist[idx][i] == cat) found = TRUE;
     210    }
     211    if (!found) {
     212      catlist[idx][Ncatlist[idx]] = cat;
     213      Ncatlist[idx] ++;
     214      if (Ncatlist[idx] == NCATLIST[idx]) {
     215        NCATLIST[idx] += 32;
     216        REALLOCATE (catlist[idx], int, NCATLIST[idx]);
     217      }
     218    }
     219  }
     220
     221  return;
     222}
     223
     224# else
     225// this is the time-based match
     226void matchImage (Catalog *catalog, off_t meas, int cat, int MATCHCAT) {
     227
     228  off_t i;
    103229  Measure *measure;
    104230
     
    107233  /* find the image that supplied this measurement */
    108234  for (i = 0; i < Nimage; i++) {
     235    // let's try the very slow method first before adding a bisection search
     236    // if (image[i].imageID != measure[0].imageID) continue;
    109237    if (image[0].photcode == -1) continue;
    110238    if (measure[0].photcode != image[i].photcode) continue;
     
    124252    if (Nlist[i] == NLIST[i]) {
    125253      NLIST[i] += 100;
    126       REALLOCATE (clist[i], int, NLIST[i]);
    127       REALLOCATE (mlist[i], int, NLIST[i]);
     254      REALLOCATE (clist[i], int,   NLIST[i]);
     255      REALLOCATE (mlist[i], off_t, NLIST[i]);
    128256    }
    129257    return;
    130258  }
    131 }
    132 
    133 Coords *getCoords (int meas, int cat) {
    134 
    135   int i;
     259  if (VERBOSE2) fprintf (stderr, "can't match detection to image?\n");
     260  return;
     261}
     262# endif
     263
     264/*
     265Coords *getCoords (off_t meas, int cat) {
     266
     267  off_t i;
    136268
    137269  i = bin[cat][meas];
     
    139271  return (&image[i].coords);
    140272}
     273*/
    141274
    142275void plot_images () {
    143276
    144   int i, bin;
     277  off_t i, bin;
    145278  double *xlist, *Mlist, *dlist;
    146279  Graphdata graphdata;
     
    188321// return StarData values for detections in the specified image, converting coordinates from the
    189322// chip positions: X,Y -> L,M -> P,Q -> R,D
    190 void fixImageRaw (Catalog *catalog, int Ncatalog, int im) {
    191 
    192   int i, m, c, n;
     323void fixImageRaw (Catalog *catalog, int Ncatalog, off_t im) {
     324
     325  off_t i, m, c, n, nPos;
    193326  double X, Y, L, M, P, Q, R, D, dR, dD;
     327  double dPos;
    194328
    195329  Mosaic *mosaic;
    196330  Coords *moscoords, *imcoords;
    197331
     332  // check if this image is bad and should be skipped
     333  if (badCoords(im)) return;
     334
     335  // WRP images need to have an associated mosaic
    198336  moscoords = NULL;
    199   mosaic = getMosaicForImage (im);
    200   if (mosaic != NULL) {
    201       moscoords = &mosaic[0].coords;
     337  if (!strcmp(&image[im].coords.ctype[4], "-WRP")) {
     338    mosaic = getMosaicForImage (im);
     339    if (mosaic == NULL) return;  // if we cannot find the associated image, skip it
     340    moscoords = &mosaic[0].coords;
    202341  }
    203342  imcoords = &image[im].coords;
     343
     344  // accumulate the rms position offsets.  if this value, or any specific entry, is too
     345  // large, we will reset the image to the original coords at the end of the analysis
     346
     347  dPos = 0.0;
     348  nPos = 0;
    204349
    205350  for (i = 0; i < Nlist[im]; i++) {
     
    226371    dD = 3600.0*(catalog[c].average[n].D - D);
    227372
    228     if (fabs(catalog[c].measure[m].dR - dR) > 10.0) {
    229       // XXXXX running into this still for last megacam exposure: wrong mosaic?
    230       // ???? inconsistently hitting this????
     373    // complain if the new location is far from the average location
     374    if (fabs(dR) > 2.0) {
    231375      fprintf (stderr, "!");
    232       // abort ();
    233     }
    234     if (fabs(catalog[c].measure[m].dD - dD) > 10.0) {
     376      setBadCoords (im); // report a failure for this image
     377      return;
     378    }
     379    if (fabs(dD) > 2.0) {
    235380      fprintf (stderr, "*");
    236       // abort ();
    237     }
     381      setBadCoords (im); // report a failure for this image
     382      return;
     383    }
     384
     385    // complain if the new location is far from the old location
     386    if (fabs(catalog[c].measure[m].dR - dR) > 2.0) {
     387      fprintf (stderr, "@");
     388      setBadCoords (im); // report a failure for this image
     389      return;
     390    }
     391    if (fabs(catalog[c].measure[m].dD - dD) > 2.0) {
     392      fprintf (stderr, "#");
     393      setBadCoords (im); // report a failure for this image
     394      return;
     395    }
     396
     397    dPos += SQ(catalog[c].measure[m].dR - dR) + SQ(catalog[c].measure[m].dD - dD);
     398    nPos ++;
    238399
    239400    catalog[c].measure[m].dR = dR;
     
    251412    }
    252413  }
     414
     415  saveOffsets (dPos, nPos, im);
     416
     417  return;
     418}
     419
     420// return StarData values for detections in the specified image, converting coordinates from the
     421// chip positions: X,Y -> L,M -> P,Q -> R,D
     422void resetImageRaw (Catalog *catalog, int Ncatalog, off_t im) {
     423
     424  off_t i, m, c, n;
     425  double X, Y, L, M, P, Q, R, D, dR, dD;
     426
     427  Mosaic *mosaic;
     428  Coords *moscoords, *imcoords, *oldcoords;
     429
     430  // check if this image is bad and should be skipped
     431  if (!badCoords(im)) {
     432    fprintf (stderr, "ERROR: inconsistent result?");
     433    abort();
     434  }
     435
     436  // replace the current coords with the old coords:
     437  oldcoords = getCoords (im);
     438  memcpy (&image[im].coords, oldcoords, sizeof(Coords));
     439
     440  // WRP images need to have an associated mosaic
     441  moscoords = NULL;
     442  if (!strcmp(&image[im].coords.ctype[4], "-WRP")) {
     443    mosaic = getMosaicForImage (im);
     444    if (mosaic == NULL) return;  // if we cannot find the associated image, skip it
     445    moscoords = &mosaic[0].coords;
     446  }
     447  imcoords = &image[im].coords;
     448
     449  for (i = 0; i < Nlist[im]; i++) {
     450    m = mlist[im][i];
     451    c = clist[im][i];
     452
     453    X = catalog[c].measure[m].Xccd;
     454    Y = catalog[c].measure[m].Yccd;
     455    n = catalog[c].measure[m].averef;
     456
     457    dR = dD = 0.0;
     458    if (moscoords == NULL) {
     459      // this is a Simple image (not a mosaic)
     460      // note that for a Simple image, L,M = P,Q
     461      XY_to_LM (&L, &M, X, Y, imcoords);
     462      LM_to_RD (&R, &D, L, M, imcoords);
     463    } else {
     464      XY_to_LM (&L, &M, X, Y, imcoords);
     465      XY_to_LM (&P, &Q, L, M, moscoords);
     466      LM_to_RD (&R, &D, P, Q, moscoords);
     467    }
     468
     469    catalog[c].measure[m].dR = dR;
     470    catalog[c].measure[m].dD = dD;
     471
     472    if (catalog[c].measure[m].dR > +180.0*3600.0) {
     473      // average on high end of boundary, move star up
     474      R += 360.0;
     475      catalog[c].measure[m].dR = 3600.0*(catalog[c].average[n].R - R);
     476    }
     477    if (catalog[c].measure[m].dR < -180.0*3600.0) {
     478      // average on low end of boundary, move star down
     479      R -= 360.0;
     480      catalog[c].measure[m].dR = 3600.0*(catalog[c].average[n].R - R);
     481    }
     482  }
     483
    253484  return;
    254485}
     
    257488// chip positions: X,Y -> L,M -> P,Q -> R,D.  This function is used by the image fitting steps, for
    258489// which the detections have already been filtered when they were loaded (bcatalog)
    259 StarData *getImageRaw (Catalog *catalog, int Ncatalog, int im, int *Nstars, CoordMode mode) {
    260 
    261   int i, m, c, n;
     490StarData *getImageRaw (Catalog *catalog, int Ncatalog, off_t im, off_t *Nstars, CoordMode mode) {
     491
     492  off_t i, m, c, n;
    262493
    263494  Mosaic *mosaic;
     
    265496  StarData *raw;
    266497
    267   ALLOCATE (raw, StarData, Nlist[im]);
    268 
    269498  mosaic = NULL;
    270499  moscoords = NULL;
    271500  if (mode == MODE_MOSAIC) {
    272       mosaic = getMosaicForImage (im);
    273       if (mosaic == NULL) {
    274         fprintf (stderr, "mosaic not found for image %s\n", image[im].name);
    275         exit (1);
    276       }
    277       moscoords = &mosaic[0].coords;
    278   }
     501    mosaic = getMosaicForImage (im);
     502    if (mosaic == NULL) {
     503      fprintf (stderr, "mosaic not found for image %s\n", image[im].name);
     504      return NULL;
     505    }
     506    moscoords = &mosaic[0].coords;
     507  }
     508
     509  ALLOCATE (raw, StarData, Nlist[im]);
    279510
    280511  for (i = 0; i < Nlist[im]; i++) {
     
    319550        LM_to_RD (&raw[i].R, &raw[i].D, raw[i].P, raw[i].Q, moscoords);
    320551        break;
    321     default:
    322       fprintf (stderr, "error: invalid mode in getImageRaw");
    323       abort ();
     552      default:
     553        fprintf (stderr, "error: invalid mode in getImageRaw");
     554        abort ();
    324555    }
    325556  }
     
    332563// the sky positions: R,D -> P,Q -> L,M -> X,Y
    333564
    334 StarData *getImageRef (Catalog *catalog, int Ncatalog, int im, int *Nstars, CoordMode mode) {
    335 
    336   int i, m, c, n;
     565StarData *getImageRef (Catalog *catalog, int Ncatalog, off_t im, off_t *Nstars, CoordMode mode) {
     566
     567  off_t i, m, c, n;
    337568
    338569  Mosaic *mosaic;
    339570  Coords *moscoords;
    340571  StarData *ref;
    341 
    342   ALLOCATE (ref, StarData, Nlist[im]);
    343572
    344573  mosaic = NULL;
     
    348577    if (mosaic == NULL) {
    349578      fprintf (stderr, "mosaic not found for image %s\n", image[im].name);
    350       exit (1);
     579      return NULL;
    351580    }
    352581    moscoords = &mosaic[0].coords;
    353582  }
     583
     584  ALLOCATE (ref, StarData, Nlist[im]);
    354585
    355586  for (i = 0; i < Nlist[im]; i++) {
     
    371602    switch (mode) {
    372603      case MODE_SIMPLE:
    373       RD_to_LM (&ref[i].P, &ref[i].Q, ref[i].R, ref[i].D, &image[im].coords);
    374       ref[i].L = ref[i].P;
    375       ref[i].M = ref[i].Q;
    376       LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, &image[im].coords);
    377       break;
     604        RD_to_LM (&ref[i].P, &ref[i].Q, ref[i].R, ref[i].D, &image[im].coords);
     605        ref[i].L = ref[i].P;
     606        ref[i].M = ref[i].Q;
     607        LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, &image[im].coords);
     608        break;
    378609      case MODE_MOSAIC:
    379       RD_to_LM (&ref[i].P, &ref[i].Q, ref[i].R, ref[i].D, moscoords);
    380       LM_to_XY (&ref[i].L, &ref[i].M, ref[i].P, ref[i].Q, moscoords);
    381       LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, &image[im].coords);
    382       break;
     610        RD_to_LM (&ref[i].P, &ref[i].Q, ref[i].R, ref[i].D, moscoords);
     611        LM_to_XY (&ref[i].L, &ref[i].M, ref[i].P, ref[i].Q, moscoords);
     612        LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, &image[im].coords);
     613        break;
    383614      default:
    384615        fprintf (stderr, "invalid case");
     
    399630  return;
    400631
    401   int i, j, k, m, N, Ndel, Nave, Nmax, TOOFEW, Nsecfilt;
     632  int Ndel, Nave;
     633  off_t i, j, k, m, N, Nmax, TOOFEW, Nsecfilt;
    402634  double Ns, theta, x, y;
    403635  double *R, *D, *dR, *dD;
     
    490722void FlagOutliers2D (Catalog *catalog) {
    491723
    492   int i, j, k, m, N, Ndel, Nave, Nmax, TOOFEW, Nsecfilt;
     724  int Ndel, Nave;
     725  off_t i, j, k, m, N, Nmax, TOOFEW, Nsecfilt;
    493726  double *index;
    494727  double Ns, theta, x, y;
     
    569802    // recalculate image center, sigma based on closest 50% of points
    570803    for(k = 0;  k < N; k++) {
    571       int ind = (int) index[k];
     804      off_t ind = (off_t) index[k];
    572805      R[k] = catalog[0].measure[ind].dR;
    573806      D[k] = catalog[0].measure[ind].dD;
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/MosaicOps.c

    r21508 r27840  
    22
    33// array of mosaic definition structures
    4 static int    Nmosaic;
     4static off_t   Nmosaic;
    55static Mosaic *mosaic;
    66
    77// list of all images associated with a mosaic
    8 static int   *Nmosaic_own_images; // number of images for this mosaic
    9 static int   *Amosaic_own_images; // size of allocated array
    10 static int   **mosaic_own_images; // array of arrays: mosaic -> images
     8static off_t   *Nmosaic_own_images; // number of images for this mosaic
     9static off_t   *Amosaic_own_images; // size of allocated array
     10static off_t   **mosaic_own_images; // array of arrays: mosaic -> images
    1111
    1212// list of mosaic associated with each image 
    13 static int    Nmosaic_for_images; // number of images (for internal checks)
    14 static int    *mosaic_for_images; // array of: image -> mosaic
    15 
    16 Mosaic *getmosaics (int *N) {
     13static off_t    Nmosaic_for_images; // number of images (for off_ternal checks)
     14static off_t    *mosaic_for_images; // array of: image -> mosaic
     15
     16Mosaic *getmosaics (off_t *N) {
    1717  *N = Nmosaic;
    1818  return (mosaic);
    1919}
    2020
     21off_t getMosaicByTimes (unsigned int start, unsigned int stop, unsigned int *startMos, unsigned int *stopMos, off_t *indexMos) {
     22
     23  // use bisection to find the overlapping mosaic
     24
     25  off_t Nlo, Nhi, N;
     26
     27  // find the last mosaic before start
     28  Nlo = 0; Nhi = Nmosaic;
     29  while (Nhi - Nlo > 10) {
     30    N = 0.5*(Nlo + Nhi);
     31    if (startMos[N] < start) {
     32      Nlo = MAX(N, 0);
     33    } else {
     34      Nhi = MIN(N + 1, Nmosaic);
     35    }
     36  }
     37
     38  // check for the matched mosaic starting from Nlo
     39  // we may have to go much beyond Nlo since stop is not sorted
     40  // can we use a sorted version of stop to check when we are beyond the valid range??
     41  for (N = Nlo; N < Nmosaic; N++) {
     42    if (stop  < stopMos[N]) continue;
     43    if (start > startMos[N])  continue;
     44    if (stop  < startMos[N]) return (-1);
     45    return (indexMos[N]);
     46  }
     47
     48  return (-1);
     49}
     50
     51// sort two times vectors and an index by first time vector
     52void sort_mosaic_times (unsigned int *S, unsigned int *E, off_t *I, off_t N) {
     53
     54# define SWAPFUNC(A,B){ unsigned int tmp_t; off_t tmp_i; \
     55  tmp_t = S[A]; S[A] = S[B]; S[B] = tmp_t; \
     56  tmp_t = E[A]; E[A] = E[B]; E[B] = tmp_t; \
     57  tmp_i = I[A]; I[A] = I[B]; I[B] = tmp_i; \
     58}
     59# define COMPARE(A,B)(S[A] < S[B])
     60
     61  OHANA_SORT (N, COMPARE, SWAPFUNC);
     62
     63# undef SWAPFUNC
     64# undef COMPARE
     65
     66}
     67
     68// first, let's continue to use the time to make the match, but use bracketing to make it faster:
     69
    2170// find mosaic frames (unique time periods & photcode name matches mosaic)
    22 void initMosaics (Image *image, int Nimage) {
    23 
    24   int i, j, found, NMOSAIC;
     71void initMosaics (Image *image, off_t Nimage) {
     72
     73  off_t i, Nmos, NMOSAIC;
    2574  unsigned int start, stop;
     75
     76  unsigned int *startMos, *stopMos;
     77  off_t *indexMos;
    2678
    2779  Nmosaic = 0;
     
    2981  ALLOCATE (mosaic, Mosaic, NMOSAIC);
    3082
    31   ALLOCATE (Nmosaic_own_images, int, NMOSAIC);
    32   ALLOCATE (Amosaic_own_images, int, NMOSAIC);
    33   ALLOCATE (mosaic_own_images, int *, NMOSAIC);
     83  ALLOCATE (Nmosaic_own_images, off_t, NMOSAIC);
     84  ALLOCATE (Amosaic_own_images, off_t, NMOSAIC);
     85  ALLOCATE (mosaic_own_images, off_t *, NMOSAIC);
     86  ALLOCATE (startMos, unsigned int, NMOSAIC);
     87  ALLOCATE (stopMos, unsigned int, NMOSAIC);
     88  ALLOCATE (indexMos, off_t, NMOSAIC);
    3489
    3590  /* find the mosaic images (coords.ctype = DIS); generate list of unique mosaics */
     
    54109    Nmosaic_own_images[Nmosaic] = 0;
    55110    Amosaic_own_images[Nmosaic] = 10;
    56     ALLOCATE (mosaic_own_images[Nmosaic], int, Amosaic_own_images[Nmosaic]);
     111    ALLOCATE (mosaic_own_images[Nmosaic], off_t, Amosaic_own_images[Nmosaic]);
     112
     113    startMos[Nmosaic] = start;
     114    stopMos[Nmosaic] = stop;
     115    indexMos[Nmosaic] = Nmosaic;
    57116
    58117    Nmosaic ++;
     
    60119      NMOSAIC += 10;
    61120      REALLOCATE (mosaic, Mosaic, NMOSAIC);
    62       REALLOCATE (mosaic_own_images, int *, NMOSAIC);
    63       REALLOCATE (Nmosaic_own_images, int, NMOSAIC);
    64       REALLOCATE (Amosaic_own_images, int, NMOSAIC);
    65     }
    66   }
    67 
     121      REALLOCATE (mosaic_own_images, off_t *, NMOSAIC);
     122      REALLOCATE (Nmosaic_own_images, off_t, NMOSAIC);
     123      REALLOCATE (Amosaic_own_images, off_t, NMOSAIC);
     124      REALLOCATE (startMos, unsigned int, NMOSAIC);
     125      REALLOCATE (stopMos, unsigned int, NMOSAIC);
     126      REALLOCATE (indexMos, off_t, NMOSAIC);
     127    }
     128  }
     129
     130  // sort the index, start, and stop by the start times:
     131  sort_mosaic_times (startMos, stopMos, indexMos, Nmosaic);
     132 
    68133  // array to store image->mosaic index
    69134  Nmosaic_for_images = Nimage;
    70   ALLOCATE (mosaic_for_images, int, Nmosaic_for_images);
     135  ALLOCATE (mosaic_for_images, off_t, Nmosaic_for_images);
    71136
    72137  /* now assign the WRP images to these mosaics */
     
    80145    stop  = image[i].tzero + MAX(1.01*image[i].trate*image[i].NY, 1);
    81146
    82     /* find existing mosaic with this time range */
    83     found = FALSE;
    84     for (j = 0; !found && (j < Nmosaic); j++) {
    85       if (stop  < mosaic[j].start) continue;
    86       if (start > mosaic[j].stop)  continue;
    87       found = TRUE;
    88       break;
    89     }
    90     /* if no matching mosaic exists, skip this image */
    91     if (!found) continue;
     147    Nmos = getMosaicByTimes (start, stop, startMos, stopMos, indexMos);
     148    if (Nmos == -1) {
     149      fprintf (stderr, "cannot match mosaic for %s\n", image[i].name);
     150      continue;
     151    }
    92152
    93153    // mosaic corresponding to this image
    94     mosaic_for_images[i] = j;
     154    mosaic_for_images[i] = Nmos;
    95155
    96156    // add image to mosaic_own_image list
    97     mosaic_own_images[j][Nmosaic_own_images[j]] = i;
    98     Nmosaic_own_images[j] ++;
    99     if (Nmosaic_own_images[j] == Amosaic_own_images[j]) {
    100       Amosaic_own_images[j] += 10;
    101       REALLOCATE (mosaic_own_images[j], int, Amosaic_own_images[j]);
    102     }
    103     assert (Nmosaic_own_images[j] < Amosaic_own_images[j]);
    104   }
    105 
     157    mosaic_own_images[Nmos][Nmosaic_own_images[Nmos]] = i;
     158    Nmosaic_own_images[Nmos] ++;
     159    if (Nmosaic_own_images[Nmos] == Amosaic_own_images[Nmos]) {
     160      Amosaic_own_images[Nmos] += 10;
     161      REALLOCATE (mosaic_own_images[Nmos], off_t, Amosaic_own_images[Nmos]);
     162    }
     163    assert (Nmosaic_own_images[Nmos] < Amosaic_own_images[Nmos]);
     164  }
     165
     166  free (startMos);
     167  free (stopMos);
     168  free (indexMos);
    106169  return;
    107170}
     
    109172// return StarData values for detections in the specified image, converting coordinates from the
    110173// chip positions: X,Y -> L,M -> P,Q -> R,D
    111 StarData *getMosaicRaw (Catalog *catalog, int Ncatalog, int mos, int *Nstars) {
    112 
    113   int i, j, im, Nraw, Nnew;
     174StarData *getMosaicRaw (Catalog *catalog, int Ncatalog, off_t mos, off_t *Nstars) {
     175
     176  off_t i, j, im, Nraw, Nnew;
    114177  StarData *raw, *new;
    115178
     
    125188    // this function does the reverse-lookup for the mosaic corresponding to this image
    126189    new = getImageRaw (catalog, Ncatalog, im, &Nnew, MODE_MOSAIC);
     190    if (!new) {
     191      fprintf (stderr, "inconsistent: missing mosaic for image already associated with a mosaic? (1)\n");
     192      abort();
     193    }
    127194   
    128195    // merge new and raw
     
    142209// return StarData values for averages positions in the specified image, converting coordinates from
    143210// the sky positions: R,D -> P,Q -> L,M -> X,Y
    144 StarData *getMosaicRef (Catalog *catalog, int Ncatalog, int mos, int *Nstars) {
    145 
    146   int i, j, im, Nref, Nnew;
     211StarData *getMosaicRef (Catalog *catalog, int Ncatalog, off_t mos, off_t *Nstars) {
     212
     213  off_t i, j, im, Nref, Nnew;
    147214  StarData *ref, *new;
    148215 
     
    157224    // this function does the reverse-lookup for the mosaic corresponding to this image
    158225    new = getImageRef (catalog, Ncatalog, im, &Nnew, MODE_MOSAIC);
     226    if (!new) {
     227      fprintf (stderr, "inconsistent: missing mosaic for image already associated with a mosaic? (2)\n");
     228      abort();
     229    }
    159230   
    160231    // merge new and ref
     
    172243}
    173244
    174 Mosaic *getMosaicForImage (int im) {
    175 
    176   int mos;
     245Mosaic *getMosaicForImage (off_t im) {
     246
     247  off_t mos;
    177248
    178249  if (im < 0) abort();
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/ParFactor.c

    r14590 r27840  
    4242
    4343/* given RA, DEC, Time, calculate the parallax factor */
    44 int ParFactor (double *pR, double *pD, double R, double D, time_t T) {
     44int ParFactor (double *pR, double *pD, double R, double D, double T, double Tmean) {
    4545
    4646  double jd;
     
    4949  /* given a time T in UNIX seconds, determine the solar longitude S */
    5050
    51   jd = ohana_sec_to_jd (T);
     51  jd = ohana_sec_to_jd (365.25*86400.0*(T + Tmean));
    5252  sun_ecliptic (jd, &L, &B, &E);
    5353
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/UpdateChips.c

    r17151 r27840  
    44
    55  /* we can measure new image parameters for each non-mosaic chip independently */
    6   int i, Nimage, Nraw, Nref;
     6  off_t i, Nimage, Nraw, Nref;
    77  Image *image;
    88  StarData *raw, *ref;
     9  Coords *oldCoords;
    910
    1011  image = getimages (&Nimage);
     
    1718    /* convert measure coordinates to raw entries */
    1819    raw = getImageRaw (catalog, Ncatalog, i, &Nraw, MODE_MOSAIC);
     20    if (!raw) continue;
    1921
    2022    /* convert average coordinates to ref entries */
    2123    ref = getImageRef (catalog, Ncatalog, i, &Nref, MODE_MOSAIC);
     24    if (!ref) continue;
    2225
    2326    // note that Nraw & Nref must be equal: if not, we made a programming error in one of these two functions.
    2427    assert (Nraw == Nref);
    2528
     29    saveCoords (&image[i].coords, i);
     30
    2631    // FitChip does iterative, clipped fitting
    27     fprintf (stderr, "image %d : Nstars: %d\n", i, Nraw);
    28     FitChip (raw, ref, Nraw, &image[i].coords);
     32    // fprintf (stderr, "image %lld : Nstars: %lld\n", (long long) i, (long long) Nraw);
     33    if (!FitChip (raw, ref, Nraw, &image[i].coords)) {
     34      fprintf (stderr, "reject fit for image %s (%lld) : Nstars: %lld\n", image[i].name, (long long) i, (long long) Nraw);
     35      oldCoords = getCoords (i);
     36      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
     37    }
    2938
    3039    free (raw);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/UpdateMeasures.c

    r16810 r27840  
    33int UpdateMeasures (Catalog *catalog, int Ncatalog) {
    44
    5   int i, Nimage;
     5  off_t i, Nimage;
    66  Image *image;
    77
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/UpdateMosaic.c

    r15600 r27840  
    44
    55  /* we can measure new image parameters for each mosaic independently */
    6   int i, Nmosaic, Nstars;
     6  off_t i, Nmosaic, Nstars;
    77  Mosaic *mosaic;
    88  StarData *raw, *ref;
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/UpdateObjectOffsets.c

    r17243 r27840  
    11# include "relastro.h"
     2
     3// We run through each DVO catalog, updating the measures that come from the modified images
     4// We need to watch for failures:
     5// * in UpdateMeasures, in fixImageRaw, we track the cumulative offset for each image
     6// * after all updates are done, we can check for any bad images and reset them to the
     7//   original coordinates
    28
    39int UpdateObjectOffsets (SkyList *skylist) {
     
    2834
    2935    // match measurements with images
    30     initImageBins (&catalog, 1);
    31     findImages (&catalog, 1);
     36    initImageBins (&catalog, 1, FALSE);
     37    findImages (&catalog, 1, FALSE);
    3238
    3339    // update the detection coordinates using the new image parameters
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/UpdateObjects.c

    r24308 r27840  
    11# include "relastro.h"
    22
    3 static int Nmax;
     3static off_t  Nmax;
    44static double *X, *dX;
    55static double *Y, *dY;
     
    1313void initObjectData (Catalog *catalog, int Ncatalog) {
    1414
    15   int i, j;
     15  off_t i, j;
    1616 
    1717  Nmax = 0;
     
    4040int UpdateObjects (Catalog *catalog, int Ncatalog) {
    4141
    42   int i, j, k, m, N, Nsecfilt, found, kp;
     42  off_t j, k, m;
     43  int i, N, Nsecfilt, mode, result, status, XVERB;
    4344  StatType statsR, statsD;
    4445  Coords coords;
    45   PMFit fit;
    46   time_t To;
    47   int mode, Nave, Npm, Npar, Nskip;
    48   double Tmin, Tmax;
    49   float mag;
    50   int mask;
    51   PhotCode *code;
    52 
     46  PMFit fitAve, fitPM, fitPAR, fit;
     47  time_t T2000;
     48  off_t Nave, Npm, Npar, Nskip;
     49  off_t NaveSum, NpmSum, NparSum, NskipSum;
     50  double Tmin, Tmax, Tmean, Trange;
     51
     52  memset (&fit,    0, sizeof(fit));
     53  memset (&fitAve, 0, sizeof(fitAve));
     54  memset (&fitPM,  0, sizeof(fitPM));
     55  memset (&fitPAR, 0, sizeof(fitPAR));
     56 
    5357  initObjectData (catalog, Ncatalog);
    5458
     
    6468  strcpy (coords.ctype, "RA---SIN");
    6569
     70  XVERB = FALSE;
     71
    6672  // use J2000 as a reference time
    67   To = ohana_date_to_sec ("2000/01/01");
    68   Nave = Npar = Npm = 0;
     73  T2000 = ohana_date_to_sec ("2000/01/01");
    6974
    7075  // XXX in the future, use catalog[0].Nsecfilt only?  allow catalogs to have variable Nsecfilt?
     
    7277  assert (catalog[0].Nsecfilt == Nsecfilt);
    7378
     79  NaveSum = NparSum = NpmSum = NskipSum = 0;
    7480  for (i = 0; i < Ncatalog; i++) {
    7581
    76     if (VERBOSE) fprintf (stderr, "astrometrize catalog %d : %d ave, %d meas\n", i, catalog[i].Naverage, catalog[i].Nmeasure);
    77 
    78     Nskip = 0;
     82    if (VERBOSE) fprintf (stderr, "astrometrize catalog %d : %lld ave, %lld meas\n", i, (long long) catalog[i].Naverage, (long long) catalog[i].Nmeasure);
     83
     84    Nave = Npar = Npm = Nskip = 0;
    7985    for (j = 0; j < catalog[i].Naverage; j++) {
    8086      /* calculate the average value of R,D for a single star */
     
    8288      // skip objects which are known to be problematic
    8389      // XXX include this code or not?
    84       # if (0)
     90# if (0)
    8591      if (catalog[i].average[j].code & STAR_BAD) {
    8692        Nskip ++;
    8793        continue; 
    8894      }
    89       # endif
     95# endif
    9096
    9197      N = 0;
    9298      m = catalog[i].average[j].measureOffset;
    9399
    94       Tmin = Tmax = (catalog[i].measure[m].t - To) / (86400*365.25);
     100      Tmin = Tmax = (catalog[i].measure[m].t - T2000) / (86400*365.25);
    95101      mode = FIT_MODE;
    96102
     103      // find the basic properties of the detections for this object (Tmin, Tmax, Tmean)
     104      Tmean = 0;
    97105      for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
    98106
     
    111119        // exclude measurements by previous outlier detection
    112120        // XXX include this code or not?
    113         # if (0)
     121# if (0)
    114122        if (catalog[i].measure[m].dbFlags & MEAS_BAD) {
    115123          catalog[i].measure[m].dbFlags |= ID_MEAS_SKIP_ASTROM;
    116124          continue;
    117125        }
    118         # endif
     126# endif
    119127
    120128        catalog[i].measure[m].dbFlags |= ID_MEAS_USED_OBJ;
     
    122130        R[N] = getMeanR (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*Nsecfilt]);
    123131        D[N] = getMeanD (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*Nsecfilt]);
    124         T[N] = (catalog[i].measure[m].t - To) / (86400*365.25) ; // time relative to J2000 in years
     132        T[N] = (catalog[i].measure[m].t - T2000) / (86400*365.25) ; // time relative to J2000 in years
    125133
    126134        Tmin = MIN(Tmin, T[N]);
    127135        Tmax = MAX(Tmax, T[N]);
    128 
    129         dR[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_RA);
    130         dD[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_DEC);
     136        Tmean += T[N];
     137
     138        // dX, dY : error in arcsec --
     139        dX[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_RA);
     140        dY[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_DEC);
    131141        dT[N] = catalog[i].measure[m].dt;
     142
     143        // XXX this is (slightly) inconsistent: dX,dY are the X and Y direction errors in
     144        // arcseconds.  dR, dD are the errors in those directions in degrees.  IF we have
     145        // non-circular errors (different values for X and Y), then dR and dD will be
     146        // incorrect: they would need to be rotated to take out the position angle
     147        dR[k] = dX[k] / 3600.0;
     148        dD[k] = dY[k] / 3600.0;
    132149
    133150        N++;
     
    141158
    142159      // XXX add the parallax factor range as a criterion as well
    143       if ((Tmax - Tmin) < PM_DT_MIN) mode = FIT_AVERAGE;
     160      Trange = Tmax - Tmin;
     161      if (Trange < PM_DT_MIN) mode = FIT_AVERAGE;
    144162      if ((mode == FIT_PM_ONLY) && (N < PM_TOOFEW)) mode = FIT_AVERAGE;
    145163
     
    147165      if (N < SRC_MEAS_TOOFEW) {
    148166        // XXX need to define PHOTOM and ASTROM object flags
     167        // XXX reset the average value fields?
    149168        catalog[i].average[j].flags |= ID_STAR_FEW;
     169        catalog[i].average[j].ChiSqAve  = NAN;
     170        catalog[i].average[j].ChiSqPM   = NAN;
     171        catalog[i].average[j].ChiSqPar  = NAN;
    150172        if (N < 2) continue;
    151173      }
     
    154176      coords.crval1 = R[0];
    155177      coords.crval2 = D[0];
     178      Tmean /= (float) N;
    156179     
    157       /* project all of the R,D coordinates to a plane centered on this coordinate */
    158       for (k = 0; k < N; k++) {
    159         RD_to_XY (&X[k], &Y[k], R[k], D[k], &coords);
    160         dX[k] =  dR[k];
    161         dY[k] =  dD[k];
    162         // fprintf (stderr, "%d %f %f %f  %f %f\n", k, T[k], R[k], D[k], X[k], Y[k]);
     180      XVERB = FALSE && (catalog[i].measure[m].dM < 0.01) && (N == 6) && (mode == FIT_PM_ONLY);
     181
     182      // to judge the quality of the PM and PAR fits, we need to fit all three models and compare Chisq
     183
     184      if ((mode == FIT_PM_ONLY) || (mode == FIT_PM_AND_PAR)) {
     185        // project all of the R,D coordinates to a plane centered on this coordinate set
     186        // the times to be relative to Tmean (this is required for parallax as well)
     187        for (k = 0; k < N; k++) {
     188          RD_to_XY (&X[k], &Y[k], R[k], D[k], &coords);
     189          T[k] -= Tmean;
     190          if (XVERB) {
     191            fprintf (stderr, "%lld %f %f %f  %f %f +/- %f %f\n", (long long) k, T[k], R[k], D[k], X[k], Y[k], dX[k], dY[k]);
     192          }
     193        }         
     194
     195        FitPM (&fitPM, X, dX, Y, dY, T, N);
     196        if (XVERB) fprintf (stderr, "fitted:  %f - %f : %f %f : %f %f : %f vs %f\n", Tmin, Tmax, fitPM.Ro, fitPM.Do, fitPM.uR, fitPM.uD, fitPM.chisq, fitAve.chisq);
     197
     198        // project Ro, Do back to RA,DEC
     199        XY_to_RD (&fitPM.Ro, &fitPM.Do, fitPM.Ro, fitPM.Do, &coords);
     200        if (XVERB) fprintf (stderr, "project: %f %f : %f %f : %f\n", fitPM.Ro, fitPM.Do, fitPM.uR, fitPM.uD, fitPM.p);
     201
     202        fitPM.p  = fitPM.dp  = 0.0;
     203        catalog[i].average[j].flags |= ID_STAR_FIT_PM;
     204        Npm ++;
     205      }
     206
     207      if (mode == FIT_PM_AND_PAR) {
     208        fprintf (stderr, "parallax fitting is still untested (%s, %d)", __FILE__, __LINE__);
     209        exit (2);
     210
     211        for (k = 0; k < N; k++) {
     212          ParFactor (&pX[k], &pY[k], R[k], D[k], T[k], Tmean);
     213        }
     214        FitPMandPar (&fitPAR, X, dX, Y, dY, T, pX, pY, N);
     215        XY_to_RD (&fitPAR.Ro, &fitPAR.Do, fitPAR.Ro, fitPAR.Do, &coords);
     216        catalog[i].average[j].flags |= ID_STAR_FIT_PAR;
     217        Npar ++;
    163218      }   
    164219
    165       /* fit the model components as needed */
    166       switch (mode) {
     220      // fit the average model
     221      if ((mode == FIT_AVERAGE) || (mode == FIT_PM_ONLY) || (mode == FIT_PM_AND_PAR)) {
     222        liststats (R, dR, N, &statsR); // WARNING: this function modifies R (do not use after here)
     223        liststats (D, dD, N, &statsD); // WARNING: this function modifies D (do not use after here)
     224
     225        fitAve.Ro = statsR.mean;
     226        fitAve.dRo = 3600.0*statsR.sigma;
     227
     228        fitAve.Do = statsD.mean;
     229        fitAve.dDo = 3600.0*statsD.sigma;
     230
     231        fitAve.chisq = 0.5*(statsR.chisq + statsD.chisq);
     232        fitAve.Nfit = N;
     233
     234        fitAve.uR = fitAve.duR = 0.0;
     235        fitAve.uD = fitAve.duD = 0.0;
     236        fitAve.p  = fitAve.dp  = 0.0;
     237        catalog[i].average[j].flags |= ID_STAR_FIT_AVE;
     238        Nave ++;
     239      }
     240
     241      /* choose the result based on the chisq values */
     242      // XXXX for now, just use the mode as the result:
     243      result = mode;
     244
     245      switch (result) {
    167246        case FIT_AVERAGE:
    168           liststats (R, dR, N, &statsR);
    169           liststats (D, dD, N, &statsD);
    170 
    171           fit.Ro = statsR.mean;
    172           fit.dRo = 3600.0*statsR.sigma;
    173 
    174           fit.Do = statsD.mean;
    175           fit.dDo = 3600.0*statsD.sigma;
    176 
    177           fit.chisq = 0.5*(statsR.chisq + statsD.chisq);
    178           fit.Nfit = N;
    179 
    180           fit.uR = fit.duR = 0.0;
    181           fit.uD = fit.duD = 0.0;
    182           fit.p  = fit.dp  = 0.0;
    183 
    184           Nave ++;
     247          catalog[i].average[j].flags |= ID_STAR_USE_AVE;
     248          fit = fitAve;
    185249          break;
    186 
    187250        case FIT_PM_ONLY:
    188           FitPM (&fit, X, dX, Y, dY, T, N);
    189           // fprintf (stderr, "fitted:  %f - %f : %f %f : %f %f : %f\n", Tmin, Tmax, fit.Ro, fit.Do, fit.uR, fit.uD, fit.p);
    190           // project Ro, Do back to RA,DEC
    191           XY_to_RD (&fit.Ro, &fit.Do, fit.Ro, fit.Do, &coords);
    192           // fprintf (stderr, "project: %f %f : %f %f : %f\n", fit.Ro, fit.Do, fit.uR, fit.uD, fit.p);
    193           // continue;
    194 
    195           fit.p  = fit.dp  = 0.0;
    196 
    197           Npm ++;
     251          catalog[i].average[j].flags |= ID_STAR_USE_PM;
     252          fit = fitPM;
    198253          break;
    199 
    200         case FIT_PAR_ONLY:
    201           fprintf (stderr, "programming error at %s, %d", __FILE__, __LINE__);
    202           exit (2);
    203 
    204           for (k = 0; k < N; k++) {
    205             ParFactor (&pX[k], &pY[k], R[k], D[k], T[k]);
    206           }
    207           FitPar (&fit, X, dX, Y, dY, pX, pY, N);
    208 
    209           // project Ro, Do back to RA,DEC
    210           XY_to_RD (&fit.Ro, &fit.Do, fit.Ro, fit.Do, &coords);
    211 
    212           fit.uR = fit.duR = 0.0;
    213           fit.uD = fit.duD = 0.0;
    214 
    215           Npar ++;
     254        case FIT_PM_AND_PAR:
     255          catalog[i].average[j].flags |= ID_STAR_USE_PAR;
     256          fit = fitPAR;
    216257          break;
    217 
    218         case FIT_PM_AND_PAR:
    219           fprintf (stderr, "programming error at %s, %d", __FILE__, __LINE__);
    220           exit (2);
    221 
    222           for (k = 0; k < N; k++) {
    223             ParFactor (&pX[k], &pY[k], R[k], D[k], T[k]);
    224           }
    225           FitPMandPar (&fit, X, dX, Y, dY, T, pX, pY, N);
    226           XY_to_RD (&fit.Ro, &fit.Do, fit.Ro, fit.Do, &coords);
    227           Npar ++;
    228           break;
    229 
    230         default:
    231           fprintf (stderr, "programming error at %s, %d", __FILE__, __LINE__);
    232           exit (2);
    233       }   
    234 
    235       if (0 && (j < 100)) {
    236         fprintf (stderr, "%f %f -> %f %f (%f,%f)\n",
    237                  catalog[i].average[j].R,
    238                  catalog[i].average[j].D,
    239                  fit.Ro, fit.Do,
    240                  3600*(catalog[i].average[j].R - fit.Ro),
    241                  3600*(catalog[i].average[j].D - fit.Do));
    242       }
     258      }
     259
     260      if (XVERB) fprintf (stderr, "%f %f -> %f %f (%f,%f)\n",
     261                          catalog[i].average[j].R,
     262                          catalog[i].average[j].D,
     263                          fit.Ro, fit.Do,
     264                          3600*(catalog[i].average[j].R - fit.Ro),
     265                          3600*(catalog[i].average[j].D - fit.Do));
    243266
    244267      //make sure that the fit succeeded
    245       assert(finite(fit.Ro) && finite(fit.Do) &&
    246              finite(fit.dRo) && finite(fit.dDo) &&
    247              finite(fit.uR) && finite(fit.uD) &&
    248              finite(fit.duR) && finite(fit.duD) &&
    249              finite(fit.p) && finite(fit.dp));
     268      status  = finite(fit.Ro);
     269      status &= finite(fit.Do);
     270      status &= finite(fit.dRo);
     271      status &= finite(fit.dDo);
     272      status &= finite(fit.uR);
     273      status &= finite(fit.uD);
     274      status &= finite(fit.duR);
     275      status &= finite(fit.duD);
     276      status &= finite(fit.p);
     277      status &= finite(fit.dp);
     278      if (!status) {
     279        Nskip ++;
     280        continue;
     281      }
    250282
    251283      // the measure fields must be updated before the average fields
    252284      m = catalog[i].average[j].measureOffset;
    253285      for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
    254         // XXX why was this here?? if (catalog[i].measure[m].dbFlags & MEAS_BAD) continue;
    255286        setMeanR (fit.Ro, &catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*Nsecfilt]);
    256287        setMeanD (fit.Do, &catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*Nsecfilt]);
     
    270301      catalog[i].average[j].dP  = fit.dp; // parallax error in arcsec
    271302
    272       catalog[i].average[j].Xp  = (fit.Nfit > 1) ? 100.0*log10(fit.chisq) : NAN_S_SHORT;
     303      // Xp is supposed to be the position scatter, not the chisq : fix this:
     304      // catalog[i].average[j].Xp  = (fit.Nfit > 1) ? 100.0*log10(fit.chisq) : NAN_S_SHORT;
     305      catalog[i].average[j].ChiSqAve  = fitAve.chisq;
     306      catalog[i].average[j].ChiSqPM   = fitPM.chisq;
     307      catalog[i].average[j].ChiSqPar  = fitPAR.chisq;
     308      catalog[i].average[j].Xp        = 0.0;
     309      catalog[i].average[j].Tmean = (Tmean * 86400 * 365.26) + T2000;
     310      catalog[i].average[j].Trange = (Trange * 86400 * 365.26);
     311      catalog[i].average[j].Npos = fit.Nfit;
    273312    }
    274313
    275     if (VERBOSE) fprintf (stderr, "catalog %d : %d ave, %d pm, %d par : Nskip % d\n", i, Nave, Npm, Npar, Nskip);
     314    NaveSum += Nave;
     315    NpmSum += Npm;
     316    NparSum += Npar;
     317    NskipSum += Nskip;
     318    if (VERBOSE) fprintf (stderr, "catalog %lld : %lld ave, %lld pm, %lld par : Nskip %lld\n", (long long) i, (long long) Nave, (long long) Npm, (long long) Npar, (long long) Nskip);
    276319  }
    277320
    278   if (VERBOSE) fprintf (stderr, "fitted %d objects (%d ave, %d pm, %d par)\n", Nave + Npm + Npar, Nave, Npm, Npar);
     321  fprintf (stderr, "fitted %lld objects (%lld ave, %lld pm, %lld par), skipped %lld\n", (long long) (NaveSum + NpmSum + NparSum), (long long) NaveSum, (long long) NpmSum, (long long) NparSum, (long long) NskipSum);
    279322  return (TRUE);
    280323}
     
    282325/* fitting proper-motion and parallax:
    283326
    284 given a source at position r,d, at a time t, we need to calculate a vector (pr,pd)
    285 
    286 let x,y be the coordinate in the linearized frame with y parallel to DEC lines
    287 
    288 L,B are the ecliptic longitude and latitude of the object,
    289 dL and dB are the offsets in the L and B directions
    290 
    291 dL = sin(t - topp)
    292 dB = cos(t - topp)*sin(B)
    293 
    294 these need to be rotated to the R,D frame to yield pR,pD.  Then, the equation of motion
    295 for the source in the x,y frame is:
    296 
    297 x = Ro + uR * (t - to) + p * pR
    298 y = Do + uD * (t - to) + p * pD
    299 
    300 the unknowns in these equations are Ro, uR, Do, uD, and p
    301 
    302 XXX think through the concepts for the pole a bit better.  all objects near the pole
    303 move the same way with the same phase.  choose a projection center and define dL,dB relative
    304 to that center point coordinate system?
     327   given a source at position r,d, at a time t, we need to calculate a vector (pr,pd)
     328
     329   let x,y be the coordinate in the linearized frame with y parallel to DEC lines
     330
     331   L,B are the ecliptic longitude and latitude of the object,
     332   dL and dB are the offsets in the L and B directions
     333
     334   dL = sin(t - topp)
     335   dB = cos(t - topp)*sin(B)
     336
     337   these need to be rotated to the R,D frame to yield pR,pD.  Then, the equation of motion
     338   for the source in the x,y frame is:
     339
     340   x = Ro + uR * (t - to) + p * pR
     341   y = Do + uD * (t - to) + p * pD
     342
     343   the unknowns in these equations are Ro, uR, Do, uD, and p
     344
     345   XXX think through the concepts for the pole a bit better.  all objects near the pole
     346   move the same way with the same phase.  choose a projection center and define dL,dB relative
     347   to that center point coordinate system?
    305348
    306349*/
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/UpdateSimple.c

    r15590 r27840  
    44
    55  /* we can measure new image parameters for each non-mosaic chip independently */
    6   int i, Nimage, Nstars;
     6  off_t i, Nimage, Nstars;
    77  Image *image;
    88  StarData *raw, *ref;
     
    1818    /* convert measure coordinates to raw entries */
    1919    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, MODE_SIMPLE);
     20    if (!raw) continue;
    2021
    2122    /* convert average coordinates to ref entries */
    2223    ref = getImageRef (catalog, Ncatalog, i, &Nstars, MODE_SIMPLE);
     24    if (!ref) continue;
    2325
    2426    FitSimple (raw, ref, Nstars, &image[i].coords);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/args.c

    r24308 r27840  
    5050
    5151  if (FIT_TARGET == TARGET_NONE) usage();
     52
     53  UserCatalog = FALSE;
     54  if ((N = get_argument (argc, argv, "-catalog"))) {
     55    remove_argument (N, &argc, argv);
     56    UserCatalogRA = atof(argv[N]);
     57    remove_argument (N, &argc, argv);
     58    UserCatalogDEC = atof(argv[N]);
     59    remove_argument (N, &argc, argv);
     60    UserCatalog = TRUE;
     61  }
    5262
    5363  /* specify portion of the sky : allow default of all sky? */
     
    6777    remove_argument (N, &argc, argv);
    6878  } else {
    69     usage ();
     79    if (!UserCatalog) {
     80      usage ();
     81    }
    7082  }
    7183
     
    111123  }
    112124
    113   VERBOSE = FALSE;
     125  VERBOSE = VERBOSE2 = FALSE;
    114126  if ((N = get_argument (argc, argv, "-v"))) {
    115127    VERBOSE = TRUE;
     128    remove_argument (N, &argc, argv);
     129  }
     130  if ((N = get_argument (argc, argv, "-vv"))) {
     131    VERBOSE = VERBOSE2 = TRUE;
    116132    remove_argument (N, &argc, argv);
    117133  }
     
    242258  }
    243259
     260  NLOOP = 4;
     261  if ((N = get_argument (argc, argv, "-nloop"))) {
     262    remove_argument (N, &argc, argv);
     263    NLOOP = atof (argv[N]);
     264    remove_argument (N, &argc, argv);
     265  }
     266
    244267  if (argc != 1) usage ();
    245268  return TRUE;
     
    248271void usage () {
    249272  fprintf (stderr, "ERROR: USAGE: relastro -region RA RA DEC DEC\n");
     273  fprintf (stderr, "       OR:    relastro -catalog (ra) (dec)\n");
    250274  fprintf (stderr, "  working options: \n");
    251275  fprintf (stderr, "  -update-objects\n");
     
    263287  fprintf (stderr, "  -statmode (mode)\n");
    264288  fprintf (stderr, "  -reset");
     289  fprintf (stderr, "  -nloop (N) : number of image-fit iterations");
    265290  fprintf (stderr, "  -update : apply new fit to database\n");
    266291  fprintf (stderr, "  -params\n");
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/bcatalog.c

    r24308 r27840  
    33int bcatalog (Catalog *subcatalog, Catalog *catalog) {
    44
    5   int i, j, k, offset, found;
    6   int NAVERAGE, NMEASURE, Naverage, Nmeasure, Nm, Nsecfilt;
    7   float mag;
    8   int mask;
    9   PhotCode *code;
     5  off_t i, j, offset;
     6  off_t NAVERAGE, NMEASURE, Naverage, Nmeasure, Nm;
     7  int Nsecfilt;
    108
    119  // XXX in the future, use catalog[0].Nsecfilt only?  allow catalogs to have variable Nsecfilt?
     
    106104
    107105  if (VERBOSE) {
    108     fprintf (stderr, "%d: using %d stars (%d measures) for catalog\n", i,
    109              subcatalog[0].Naverage, subcatalog[0].Nmeasure);
     106    fprintf (stderr, "%lld: using %lld stars (%lld measures) for catalog\n", (long long) i, (long long) subcatalog[0].Naverage, (long long) subcatalog[0].Nmeasure);
    110107   }
    111108  return (TRUE);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/fitpoly.c

    r17419 r27840  
    103103
    104104/* convert the xsum,ysum,sum terms into vector,matrix and solve */
    105 void fit_eval (CoordFit *fit) {
     105int fit_eval (CoordFit *fit) {
    106106
    107107  int i, j, ix, iy, jx, jy;
     
    146146    // ix, iy, vector[i][0], ix, iy, vector[i][1]);
    147147  }     
    148 
    149   dgaussjordan (matrix, vector, fit[0].Nelems, 2);
     148 
     149  if (!dgaussjordan (matrix, vector, fit[0].Nelems, 2)) {
     150    return (FALSE);
     151  }
    150152
    151153  for (i = 0; i < fit[0].Nelems; i++) {
     
    166168  array_free (matrix, fit[0].Nelems);
    167169  array_free (vector, fit[0].Nelems);
     170  return (TRUE);
    168171}
    169172
     
    271274/* this should only apply to the polynomial, not the projection terms */
    272275/* compare with psastro supporting code */
    273 CoordFit *fit_apply_coords (CoordFit *fit, Coords *coords) {
     276int fit_apply_coords (CoordFit *fit, Coords *coords) {
    274277
    275278  double Xo, Yo, R1, R2;
     
    281284  // L = pc1_1*cd1*(x - cp1) + pc1_2*cd2*(y - cp2) + ...
    282285
    283   CoordsGetCenter (fit, 0.001, &Xo, &Yo);
     286  if (!CoordsGetCenter (fit, 0.001, &Xo, &Yo)) {
     287    fprintf (stderr, "failed to modify model\n");
     288    return (FALSE);
     289  }
    284290  coords[0].crpix1 = Xo;
    285291  coords[0].crpix2 = Yo;
     
    329335  /* keep the order and type from initial values */
    330336 
    331   // XXX if desired in the future, return modfit (and free above)
    332   return (NULL);
    333 }
    334 
     337  return (TRUE);
     338}
     339
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/initialize.c

    r24308 r27840  
    55  int NPHOTCODES;
    66  char *codename, *ptr, *list;
     7
     8  ptr = NULL;
    79
    810  ConfigInit (&argc, argv);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/load_images.c

    r12332 r27840  
    11# include "relastro.h"
     2
     3# define MARKTIME(MSG,...) { \
     4  float dtime; \
     5  gettimeofday (&stop, (void *) NULL); \
     6  dtime = DTIME (stop, start); \
     7  fprintf (stderr, MSG, __VA_ARGS__); }
    28
    39SkyList *load_images (FITS_DB *db, SkyRegion *region) {
    410
    511  Image     *image, *subset;
    6   int        Nimage, Nsubset;
    7   int       *LineNumber;
     12  off_t      Nimage, Nsubset;
     13  off_t     *LineNumber;
     14  struct timeval start, stop;
    815
    916  SkyTable *sky = NULL;
    1017  SkyList *skylist = NULL;
    1118
     19  gettimeofday (&start, (void *) NULL);
     20
    1221  // load the current sky table (layout of all SkyRegions)
    13   sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, SKY_DEPTH, VERBOSE);
     22  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
    1423  SkyTableSetFilenames (sky, CATDIR, "cpt");
    1524 
    1625  // determine the populated SkyRegions overlapping the requested area
    17   skylist = SkyListByPatch (sky, -1, region);
     26  if (UserCatalog) {
     27    skylist = SkyRegionByPoint (sky, -1, UserCatalogRA, UserCatalogDEC);
     28  } else {
     29    skylist = SkyListByPatch (sky, -1, region);
     30  }
     31  MARKTIME("  setup sky: %f sec\n", dtime);
    1832
    1933  // convert database table to internal structure
    2034  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     35  MARKTIME("  convert image table: %f sec\n", dtime);
    2136
    2237  // select the images which overlap the selected sky regions
    2338  subset = select_images (skylist, image, Nimage, &LineNumber, &Nsubset);
     39  MARKTIME("  select images: %f sec\n", dtime);
    2440
    2541  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, LineNumber, Nsubset);
    2642
    2743  initImages (subset, Nsubset);
     44  MARKTIME("  init images: %f sec\n", dtime);
     45
    2846  initMosaics (subset, Nsubset);
     47  MARKTIME("  init mosaics: %f sec\n", dtime);
    2948 
    3049  /* unlock, if we can (else, unlocked below) */
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/mkpolyterm.c

    r16810 r27840  
    7373    if (dPos > dPosRef) {
    7474      fprintf (stderr, "*** warning : non-convergence in model conversion (mkpolyterm.c;73) *** \n");
     75      return FALSE;
    7576    }
    7677    array_free (alpha, 2);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/plotstuff.c

    r17203 r27840  
    125125  if (PLOTDELAY > 500000) {
    126126    fprintf (stdout, "press return\n");
    127     fscanf (stdin, "%*c");
     127    if (fscanf (stdin, "%*c") != 0) fprintf (stderr, "\n");
    128128  } else {
    129129    usleep (PLOTDELAY);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/relastro.c

    r17243 r27840  
    11# include "relastro.h"
     2
     3# define MARKTIME(MSG,...) { \
     4  float dtime; \
     5  gettimeofday (&stop, (void *) NULL); \
     6  dtime = DTIME (stop, start); \
     7  fprintf (stderr, MSG, __VA_ARGS__); }
    28
    39int main (int argc, char **argv) {
    410
    5   int status, Ncatalog;
     11  int i, status, Ncatalog;
    612  Catalog *catalog;
    713  FITS_DB db;
     14  struct timeval start, stop;
     15  SkyList *skylist = NULL;
    816
    9   SkyList *skylist = NULL;
     17  gettimeofday (&start, (void *) NULL);
    1018
    1119  /* get configuration info, args */
     
    2634  if (db.dbstate == LCK_EMPTY) Shutdown ("ERROR: No images in catalog %s (1)", db.filename);
    2735  if (!dvo_image_load (&db, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", db.filename);
     36  MARKTIME("load image data: %f sec\n", dtime);
    2837
    2938  /* load regions and images based on specified sky patch (default depth) */
    3039  skylist = load_images (&db, &UserPatch);
     40  MARKTIME("load images: %f sec\n", dtime);
     41
     42  initCoords();
    3143
    3244  /* load catalog data from region files : subselect high-quality measurements */
    3345  catalog = load_catalogs (skylist, &Ncatalog, TRUE);
     46  MARKTIME("load catalog data: %f sec\n", dtime);
    3447
    3548  /* match measurements with images */
    36   initImageBins (catalog, Ncatalog);
    37   findImages (catalog, Ncatalog);
     49  initImageBins (catalog, Ncatalog, TRUE);
     50  MARKTIME("make image bins: %f sec\n", dtime);
     51
     52  findImages (catalog, Ncatalog, TRUE);
     53  MARKTIME("set up image indexes: %f sec\n", dtime);
    3854
    3955  if (PLOTSTUFF) {
     
    4561  switch (FIT_TARGET) {
    4662    case TARGET_SIMPLE:
    47       UpdateSimple (catalog, Ncatalog);
     63      for (i = 0; i < NLOOP; i++) {
     64        UpdateObjects (catalog, Ncatalog);
     65        UpdateSimple (catalog, Ncatalog);
     66      }
    4867      break;
    4968
    5069    case TARGET_CHIPS:
    51       UpdateChips (catalog, Ncatalog);
     70      for (i = 0; i < NLOOP; i++) {
     71        UpdateObjects (catalog, Ncatalog);
     72        UpdateChips (catalog, Ncatalog);
     73        MARKTIME("update chips: %f sec\n", dtime);
     74      }
    5275      break;
    5376
    5477    case TARGET_MOSAICS:
    55       UpdateMosaic (catalog, Ncatalog);
     78      for (i = 0; i < NLOOP; i++) {
     79        UpdateObjects (catalog, Ncatalog);
     80        UpdateMosaic (catalog, Ncatalog);
     81      }
    5682      break;
    5783
     
    6995  UpdateObjectOffsets (skylist);
    7096
     97  // iterate over catalogs to make detection coordinates consistant
     98  FixProblemImages (skylist);
     99
    71100  // save the updated image parameters
    72101  dvo_image_update (&db, VERBOSE);
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/relastro_objects.c

    r24308 r27840  
    1111
    1212  // load the current sky table (layout of all SkyRegions)
    13   sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, SKY_DEPTH, VERBOSE);
     13  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
    1414  SkyTableSetFilenames (sky, CATDIR, "cpt");
    1515 
    1616  // determine the populated SkyRegions overlapping the requested area (default depth)
    17   skylist = SkyListByPatch (sky, -1, &UserPatch);
     17  if (UserCatalog) {
     18    skylist = SkyRegionByPoint (sky, -1, UserCatalogRA, UserCatalogDEC);
     19  } else {
     20    skylist = SkyListByPatch (sky, -1, &UserPatch);
     21  }
    1822
    1923  // load data from each region file, only use bright stars
  • branches/simtest_nebulous_branches/Ohana/src/relastro/src/select_images.c

    r21508 r27840  
    1313} SkyRegionCoords;
    1414
    15 Image *select_images (SkyList *skylist, Image *timage, int Ntimage, int **LineNumber, int *Nimage) {
     15void dsortindex (double *X, off_t *Y, int N);
     16off_t getRegionStartByRA (double R, double *Rref, off_t Nregions);
     17
     18Image *select_images (SkyList *skylist, Image *timage, off_t Ntimage, off_t **LineNumber, off_t *Nimage) {
    1619 
    1720  Image *image;
    18   int i, j, k, m, found, nimage, NIMAGE;
    19   int InRange;
     21  off_t i, j, k, m, nStart, iSky, nimage, NIMAGE;
     22  off_t *line_number;
     23  int InRange, found;
    2024  double Ri[5], Di[5], Xi[5], Yi[5], dx, dy;
    21   int *line_number;
    2225  Coords tcoords;
    2326  SkyRegionCoords *skycoords;
    2427 
     28  double *RmaxSky;
     29  off_t *index;
     30
    2531  if (skylist[0].Nregions < 1) {
    2632    *Nimage = 0;
     
    3844
    3945  ALLOCATE (skycoords, SkyRegionCoords, skylist[0].Nregions);
     46
     47  ALLOCATE (RmaxSky, double, skylist[0].Nregions);
     48  ALLOCATE (index, off_t, skylist[0].Nregions);
    4049
    4150  /* compare with each region file */
     
    5665    skycoords[i].Yc[4] = skycoords[i].Yc[0];   
    5766
     67    RmaxSky[i] = skylist[0].regions[i][0].Rmax;
     68    index[i] = i;
     69
    5870    dx = 0.02*(skycoords[i].Xc[2] - skycoords[i].Xc[0]);
    5971    dy = 0.02*(skycoords[i].Yc[2] - skycoords[i].Yc[0]);
     
    6577  }
    6678
     79  dsortindex (RmaxSky, index, skylist[0].Nregions);
     80
    6781  if (VERBOSE) fprintf (stderr, "finding images\n");
    6882  BuildChipMatch (timage, Ntimage);
     
    7185  NIMAGE = 100;
    7286  ALLOCATE (image, Image, NIMAGE);
    73   ALLOCATE (line_number, int, NIMAGE);
     87  ALLOCATE (line_number, off_t, NIMAGE);
    7488 
    7589  // go through the complete list of images, selecting ones which overlap any region
     
    100114    }
    101115   
    102     if (!FindMosaicForImage (timage, Ntimage, i)) continue;
     116    if (!FindMosaicForImage (timage, Ntimage, i)) {
     117      fprintf (stderr, "cannot find mosaic for %lld\n", (long long) i);
     118      continue;
     119    }
    103120
    104121    /* define image corners */
     
    110127    found = FALSE;
    111128
    112     /* transform to ra,dec */
     129    /* transform corners to ra,dec */
     130    double RminImage = 360.0;
    113131    for (j = 0; j < 5; j++) {
    114132      XY_to_RD (&Ri[j], &Di[j], Xi[j], Yi[j], &timage[i].coords);
    115     }
     133      RminImage = MIN(RminImage, Ri[j]);
     134    }
     135
     136    // RA(nStart) is guaranteed to be < RminImage:
     137    nStart = getRegionStartByRA (RminImage, RmaxSky, skylist[0].Nregions);
    116138
    117139    /* compare with each region file */
    118     for (m = 0; (m < skylist[0].Nregions) && !found; m++) {
     140    for (iSky = nStart; (iSky < skylist[0].Nregions) && !found; iSky++) {
     141
     142      m = index[iSky];
    119143
    120144      /* we make positional comparisons in the projection of catalog */
     
    122146      tcoords.crval2 = skycoords[m].Dc;
    123147
    124       /* transform to ra,dec */
     148      /* transform corner coords to X,Y in this catalog system */
    125149      InRange = TRUE;
    126150      for (j = 0; (j < 5) && InRange; j++) {
     
    151175      }
    152176      if (RESET) {
    153         // XXX do we need this in relastro?
     177        // XXX do we need / want to do this in relastro?
    154178        assignMcal (&image[nimage], (double *) NULL, -1);
    155179        image[nimage].dMcal = NAN;
     
    161185        NIMAGE += 100;
    162186        REALLOCATE (image, Image, NIMAGE);
    163         REALLOCATE (line_number, int, NIMAGE);
     187        REALLOCATE (line_number, off_t, NIMAGE);
    164188      }
    165189    }
    166190  }
    167191     
    168   if (VERBOSE) fprintf (stderr, "found %d images\n", nimage);
     192  if (VERBOSE) fprintf (stderr, "found %lld images\n", (long long) nimage);
    169193
    170194  REALLOCATE (image, Image, MAX (nimage, 1));
    171   REALLOCATE (line_number, int, MAX (nimage, 1));
     195  REALLOCATE (line_number, off_t, MAX (nimage, 1));
    172196  free (skycoords);
    173197
     
    253277}
    254278
     279void dsortindex (double *X, off_t *Y, int N) {
     280
     281# define SWAPFUNC(A,B){ double tmpf; off_t tmpi; \
     282  tmpf = X[A]; X[A] = X[B]; X[B] = tmpf; \
     283  tmpi = Y[A]; Y[A] = Y[B]; Y[B] = tmpi; \
     284}
     285# define COMPARE(A,B)(X[A] < X[B])
     286
     287  OHANA_SORT (N, COMPARE, SWAPFUNC);
     288
     289# undef SWAPFUNC
     290# undef COMPARE
     291
     292}
     293
     294off_t getRegionStartByRA (double R, double *Rref, off_t Nregions) {
     295
     296  // use bisection to find the overlapping mosaic
     297
     298  off_t Nlo, Nhi, N;
     299
     300  // find the last mosaic before start
     301  Nlo = 0; Nhi = Nregions;
     302  while (Nhi - Nlo > 10) {
     303    N = 0.5*(Nlo + Nhi);
     304    if (Rref[N] < R) {
     305      Nlo = MAX(N, 0);
     306    } else {
     307      Nhi = MIN(N, Nregions);
     308    }
     309  }
     310  return (Nlo);
     311}
Note: See TracChangeset for help on using the changeset viewer.