IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 29734


Ignore:
Timestamp:
Nov 10, 2010, 9:35:40 AM (15 years ago)
Author:
eugene
Message:

dvorepair: now able to remove identified detections by image ID

Location:
branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/dvorepair.c

    r29715 r29734  
    22
    33# define myAbort(MSG) { fprintf (stderr, "%s\n", MSG); abort(); }
    4 # define myAssert(LOGIC,MSG) { if (!LOGIC) { fprintf (stderr, "%s\n", MSG); abort(); } }
    5 
    6 // find images which are missing detections:
     4# define myAssert(LOGIC,MSG) { if (!(LOGIC)) { fprintf (stderr, "%s\n", MSG); abort(); } }
     5
     6// delete images based on a text table of the target IDs
     7// * load the delete table file (validate format)
    78// * load the Images.dat file
    89// * create an index for imageID (seq = imageIDindex[i])
    9 // * scan over all catalog files
     10// * create an index of the imageIDs to be deleted (delete (T/F) = imageDELindex[i])
     11// * determine the full RA & DEC range of the images to be deleted
     12// * scan over all catalog files in the specified RA & DEC range
    1013// * load the cpm file (pad if short, identify the padded section)
    11 // * loop over detections: increment detection count for each imageID
     14// * create a new cpm file
     15// * loop over detections : only keep those not from images to be deleted
     16// * load the cpt file
     17// * rebuild the cpt file or delete the matching entries?
     18
     19int *ReadDeleteList(char *filename, int *nindex);
     20int RepairTableCPT(char *cptFilenameSrc, char *cptFilenameTgt, char *cpsFilenameSrc, char *cpsFilenameTgt, Measure *measure, off_t Nmeasure, Image *image, off_t Nimage, char catformat);
    1221
    1322int main (int argc, char **argv) {
    1423
    15   off_t i, j, Nmeasure, Nvalid, Nimage, Nindex, *imageIdx, index, imageSeq;
    16   int Nbytes, NbytesPerRow, Nbad, Ncheck, Ntol;
    17   int *detCounts;
     24  off_t i, j, Nmeasure, NmeasureNew, Ndelete, Nvalid, Nimage, Nindex, *imageIdx, index;
     25  int seq, Nbytes, NbytesPerRow, Ncheck, nPass, raPass;
     26  double Rthis, Dthis, Rmin, Rmax, Dmin, Dmax, Qthis, Qmin, Qmax, dR, dQ;
    1827
    1928  Image *image;
    2029  Measure *measure;
     30  Measure *measureNew;
     31
     32  Matrix matrix;
    2133
    2234  SkyTable *insky;
    2335  SkyList *inlist;
    2436
    25   char *catdir = NULL;
    26   char *cpmFilename = NULL;
     37  char *cpmFilenameSrc = NULL;
     38  char *cptFilenameSrc = NULL;
     39  char *cpsFilenameSrc = NULL;
     40  char *cpmFilenameTgt = NULL;
     41  char *cptFilenameTgt = NULL;
     42  char *cpsFilenameTgt = NULL;
     43
    2744  char *imageFilename = NULL;
    2845
     
    3653
    3754  if (argc != 3) {
    38     fprintf (stderr, "USAGE: dvorepair (catdir) (Ntol)\n");
     55    fprintf (stderr, "USAGE: dvorepair (catdir) (deleteList)\n");
    3956    fprintf (stderr, "  catdir : database of interest\n");
    40     fprintf (stderr, "  Ntol : allow Ntol missing detections\n");
     57    fprintf (stderr, "  deleteList : table from dvorepair giving images to be deleted\n");
    4158    exit (2);
    4259  }
    4360
    44   catdir = argv[1];
    45   Ntol = atoi(argv[2]);
     61  char *catdir = argv[1];
     62  char *delList = argv[2];
    4663
    4764  // load the image data
     
    5168  BuildChipMatch (image, Nimage);
    5269
    53   // generate an index for imageIDs:
     70  // generate an index for imageIDs
     71
     72  // find the full range of the imageID values
    5473  Nindex = 0;
    5574  for (i = 0; i < Nimage; i++) {
    5675    Nindex = MAX(image[i].imageID, Nindex);
    5776  }
     77
     78  // create the index vector
    5879  ALLOCATE (imageIdx, off_t, (Nindex + 1));
    5980  memset (imageIdx, 0, (Nindex + 1)*sizeof(off_t));
    6081
     82  // assign the imageIDs to the index vector
    6183  for (i = 0; i < Nimage; i++) {
    6284    index = image[i].imageID;
    63     if (index == 0) {
    64       fprintf (stderr, "?");
     85    myAssert(index, "image index cannot be 0");
     86    myAssert(!imageIdx[index], "stomping on an earlier index");
     87    imageIdx[index] = i;
     88  }
     89
     90  // generate an index of the deletion image
     91  int *deleteIndex;
     92  ALLOCATE (deleteIndex, int, Nindex + 1);
     93  memset (deleteIndex, 0, (Nindex + 1)*sizeof(int));
     94
     95  int NdeleteIDs;
     96  int *deleteIDs = ReadDeleteList(delList, &NdeleteIDs);
     97
     98  for (i = 0; i < NdeleteIDs; i++) {
     99    index = deleteIDs[i];
     100    myAssert(index > 0, "image index cannot be <= 0");
     101    myAssert(index <= Nindex, "delete index out of range of real data");
     102    myAssert(!deleteIndex[index], "stomping on an earlier index");
     103    deleteIndex[index] = TRUE;
     104  }
     105
     106  // find the RA & DEC range of the images we want to delete
     107  Rmin = 360.0;
     108  Rmax = 0.0;
     109  Dmin = +90.0;
     110  Dmax = -90.0;
     111  for (i = 0; i < NdeleteIDs; i++) {
     112    index = deleteIDs[i];
     113    seq = imageIdx[index];
     114    if (!FindMosaicForImage (image, Nimage, seq)) {
     115      fprintf (stderr, "cannot find mosaic for %s, skipping\n", image[seq].name);
    65116      continue;
    66117    }
    67     if (imageIdx[index]) {
    68       fprintf (stderr, "!");
    69       continue;
    70     }
    71     imageIdx[index] = i;
    72   }
    73 
    74   // generate a list of the detection counts:
    75   ALLOCATE (detCounts, int, Nimage);
    76   memset (detCounts, 0, Nimage*sizeof(int));
     118
     119    XY_to_RD(&Rthis, &Dthis, 0, 0, &image[seq].coords);
     120    Rmin = MIN(Rthis, Rmin);
     121    Rmax = MAX(Rthis, Rmax);
     122    Dmin = MIN(Dthis, Dmin);
     123    Dmax = MAX(Dthis, Dmax);
     124    Qthis = (Rthis < 180.0) ? Rthis : Rthis - 360.0;
     125    Qmin = MIN(Qthis, Qmin);
     126    Qmax = MAX(Qthis, Qmax);
     127
     128    fprintf (stderr, "image @ %f,%f\n", Rthis, Dthis);
     129
     130    XY_to_RD(&Rthis, &Dthis, image[seq].NX, 0, &image[seq].coords);
     131    Rmin = MIN(Rthis, Rmin);
     132    Rmax = MAX(Rthis, Rmax);
     133    Dmin = MIN(Dthis, Dmin);
     134    Dmax = MAX(Dthis, Dmax);
     135    Qthis = (Rthis < 180.0) ? Rthis : Rthis - 360.0;
     136    Qmin = MIN(Qthis, Qmin);
     137    Qmax = MAX(Qthis, Qmax);
     138
     139    XY_to_RD(&Rthis, &Dthis, 0, image[seq].NY, &image[seq].coords);
     140    Rmin = MIN(Rthis, Rmin);
     141    Rmax = MAX(Rthis, Rmax);
     142    Dmin = MIN(Dthis, Dmin);
     143    Dmax = MAX(Dthis, Dmax);
     144    Qthis = (Rthis < 180.0) ? Rthis : Rthis - 360.0;
     145    Qmin = MIN(Qthis, Qmin);
     146    Qmax = MAX(Qthis, Qmax);
     147
     148    XY_to_RD(&Rthis, &Dthis, image[seq].NX, image[seq].NY, &image[seq].coords);
     149    Rmin = MIN(Rthis, Rmin);
     150    Rmax = MAX(Rthis, Rmax);
     151    Dmin = MIN(Dthis, Dmin);
     152    Dmax = MAX(Dthis, Dmax);
     153    Qthis = (Rthis < 180.0) ? Rthis : Rthis - 360.0;
     154    Qmin = MIN(Qthis, Qmin);
     155    Qmax = MAX(Qthis, Qmax);
     156  }
     157
     158  dQ = Qmax - Qmin;
     159  dR = Rmax - Rmin;
    77160
    78161  // load the sky table for the existing database
     
    80163  myAssert(insky, "can't read SkyTable");
    81164  SkyTableSetFilenames (insky, catdir, "cpt");
    82 
     165 
    83166  // XXX apply this...generate the subset matching the user-selected region
    84167  SkyRegion UserPatch;
    85   UserPatch.Rmin = 0.0;
    86   UserPatch.Rmax = 360.0;
    87   UserPatch.Dmin = -90.0;
    88   UserPatch.Dmax = +90.0;
    89   inlist = SkyListByPatch (insky, -1, &UserPatch);
    90 
    91   // SkyListPopulatedRange (&Ns, &Ne, inlist, 0);
    92   // depth = inlist[0].regions[Ns][0].depth;
     168
     169  if (dR < dQ) {
     170    fprintf (stderr, "R,D range: %f - %f, %f - %f\n", Rmin, Rmax, Dmin, Dmax);
     171    nPass = 1;
     172  } else {
     173    fprintf (stderr, "R,D range: %f - %f, %f - %f\n", Qmin, Qmax, Dmin, Dmax);
     174    nPass = 2;
     175  }
    93176 
    94   ALLOCATE(cpmFilename, char, strlen(catdir) + 64);
    95 
    96   // loop over the populated input regions
    97   Ncheck = 0;
    98   for (i = 0; i < inlist[0].Nregions; i++) {
    99     if (!inlist[0].regions[i][0].table) continue;
    100 
    101     if (1) {
    102       sprintf (cpmFilename, "%s/%s.cpm", catdir, inlist[0].regions[i][0].name);
     177  ALLOCATE(cpmFilenameSrc, char, strlen(catdir) + 64);
     178  ALLOCATE(cptFilenameSrc, char, strlen(catdir) + 64);
     179  ALLOCATE(cpsFilenameSrc, char, strlen(catdir) + 64);
     180  ALLOCATE(cpmFilenameTgt, char, strlen(catdir) + 64);
     181  ALLOCATE(cptFilenameTgt, char, strlen(catdir) + 64);
     182  ALLOCATE(cpsFilenameTgt, char, strlen(catdir) + 64);
     183
     184  for (raPass = 0; raPass < nPass; raPass++) {
     185   
     186    UserPatch.Dmin = Dmin + 0.1;
     187    UserPatch.Dmax = Dmax + 0.1;
     188
     189    if (dR < dQ) {
     190      UserPatch.Rmin = Rmin;
     191      UserPatch.Rmax = Rmax;
     192    } else {
     193      if (raPass == 0) {
     194        UserPatch.Rmin = 0.0;
     195        UserPatch.Rmax = Qmax;
     196      } else {
     197        UserPatch.Rmin = Qmin + 360.0;
     198        UserPatch.Rmax = 360.0;
     199      }
     200    }
     201
     202    inlist = SkyListByPatch (insky, -1, &UserPatch);
     203 
     204    // SkyListPopulatedRange (&Ns, &Ne, inlist, 0);
     205    // depth = inlist[0].regions[Ns][0].depth;
     206 
     207    // loop over the populated input regions
     208    Ncheck = 0;
     209    for (i = 0; i < inlist[0].Nregions; i++) {
     210      if (!inlist[0].regions[i][0].table) continue;
     211
     212      sprintf (cpmFilenameSrc, "%s/%s.cpm", catdir, inlist[0].regions[i][0].name);
     213      sprintf (cptFilenameSrc, "%s/%s.cpt", catdir, inlist[0].regions[i][0].name);
     214      sprintf (cpsFilenameSrc, "%s/%s.cps", catdir, inlist[0].regions[i][0].name);
     215      sprintf (cpmFilenameTgt, "%s/%s.cpm.fixed", catdir, inlist[0].regions[i][0].name);
     216      sprintf (cptFilenameTgt, "%s/%s.cpt.fixed", catdir, inlist[0].regions[i][0].name);
     217      sprintf (cpsFilenameTgt, "%s/%s.cps.fixed", catdir, inlist[0].regions[i][0].name);
    103218      cpmFtable.header = &cpmHeaderTBL;
    104219
    105       // open cpm file
    106       cpmFile = fopen(cpmFilename, "r");
    107       if (!cpmFile) continue;
    108       // myAssert(cpmFile, "failed to open cpm file");
    109    
    110       // fprintf (stderr, "input: %s\n", inlist[0].regions[i][0].name);
    111 
    112       // load the cpm header
    113       if (!gfits_fread_header (cpmFile, &cpmHeaderPHU)) {
    114         myAbort("failure to cpm header");
     220      /*** read and examine the CPM file ***/
     221      {
     222        fprintf (stderr, "check %s\n", cpmFilenameSrc);
     223
     224        // open cpm file
     225        cpmFile = fopen(cpmFilenameSrc, "r");
     226        if (!cpmFile) continue;
     227        // myAssert(cpmFile, "failed to open cpm file");
     228   
     229        // load the cpm header
     230        if (!gfits_fread_header (cpmFile, &cpmHeaderPHU)) {
     231          myAbort("failure to cpm header");
     232        }
     233
     234        // move to TBL header
     235        Nbytes = cpmHeaderPHU.datasize + gfits_data_size (&cpmHeaderPHU);
     236        fseeko (cpmFile, Nbytes, SEEK_SET);
     237
     238        // read cpm TBL header
     239        if (!gfits_fread_header (cpmFile, &cpmHeaderTBL)) {
     240          myAbort("can't read header for cpm table");
     241        }
     242
     243        // read Measure table data : format is irrelevant here */
     244        if (!gfits_fread_ftable_data (cpmFile, &cpmFtable, TRUE)) {
     245          myAbort("can't read data for cpm table");
     246        }
     247
     248        gfits_scan(&cpmHeaderTBL, "NAXIS1", "%d", 1, &NbytesPerRow);
     249
     250        measure = FtableToMeasure (&cpmFtable, &Nmeasure, &catformat);
     251        myAssert(measure, "failed to convert ftable to measure data");
     252   
     253        Nvalid = (int)(cpmFtable.validsize / NbytesPerRow);
     254        Nvalid = MIN(Nmeasure, Nvalid);
     255
     256        // close the input cpm file
     257        fclose(cpmFile);
     258
     259        // allocate an output array of measures (to replace, if needed)
     260        ALLOCATE (measureNew, Measure, Nvalid);
     261
     262        NmeasureNew = 0;
     263        Ndelete = 0;
     264
     265        // examine all measurements: find ones that need to be deleted
     266        for (j = 0; j < Nvalid; j++) {
     267          index = measure[j].imageID;
     268          myAssert(index, "measure is missing an image ID");
     269
     270          if (deleteIndex[index]) {
     271            Ndelete ++;
     272            continue;
     273          }
     274          measureNew[NmeasureNew] = measure[j];
     275          NmeasureNew ++;
     276        }
    115277      }
    116278
    117       // move to TBL header
    118       Nbytes = cpmHeaderPHU.datasize + gfits_data_size (&cpmHeaderPHU);
    119       fseeko (cpmFile, Nbytes, SEEK_SET);
    120 
    121       // read cpm TBL header
    122       if (!gfits_fread_header (cpmFile, &cpmHeaderTBL)) {
    123         myAbort("can't read header for cpm table");
     279      fprintf (stderr, "deleting %d of %d (keep %d) detections from %s -> %s\n", (int) Ndelete, (int) Nvalid, (int) NmeasureNew, cpmFilenameSrc, cpmFilenameTgt);
     280
     281      // if we actually want to delete any measurements, write out a new cpm file
     282      if (Ndelete > 0) {
     283
     284        // the CPT and CPS tables need to be regenerated.  This must happen first because, in the process, we also update measure->averef
     285        RepairTableCPT(cptFilenameSrc, cptFilenameTgt, cpsFilenameSrc, cpsFilenameTgt, measureNew, NmeasureNew, image, Nimage, catformat);
     286
     287        // convert internal to external format
     288        if (!MeasureToFtable (&cpmFtable, measureNew, NmeasureNew, catformat)) {
     289          myAbort("trouble converting format");
     290        }
     291
     292        // create and write the output file
     293        cpmFile = fopen(cpmFilenameTgt, "w");
     294        myAssert(cpmFile, "failed to open cpt file");
     295       
     296        // write PHU header
     297        if (!gfits_fwrite_header (cpmFile, &cpmHeaderPHU)) {
     298          myAbort("can't write primary header");
     299        }
     300
     301        // write the PHU matrix; this is probably a NOP, do I have to keep it in?
     302        gfits_create_matrix (&cpmHeaderPHU, &matrix);
     303        if (!gfits_fwrite_matrix  (cpmFile, &matrix)) {
     304          myAbort("can't write primary matrix");
     305        }
     306        gfits_free_matrix (&matrix);
     307       
     308        // write the table data
     309        if (!gfits_fwrite_ftable_range (cpmFile, &cpmFtable, 0, NmeasureNew, 0, NmeasureNew)) {
     310          myAbort("can't write table data");
     311        }
     312        fclose (cpmFile);
    124313      }
    125 
    126       // read Measure table data : format is irrelevant here */
    127       if (!gfits_fread_ftable_data (cpmFile, &cpmFtable, TRUE)) {
    128         myAbort("can't read data for cpm table");
    129       }
    130 
    131       gfits_scan(&cpmHeaderTBL, "NAXIS1", "%d", 1, &NbytesPerRow);
    132 
    133       measure = FtableToMeasure (&cpmFtable, &Nmeasure, &catformat);
    134       myAssert(measure, "failed to convert ftable to measure data");
    135    
    136       Nvalid = (int)(cpmFtable.validsize / NbytesPerRow);
    137       Nvalid = MIN(Nmeasure, Nvalid);
    138 
    139       // examine all measurements and new objects as needed
    140       for (j = 0; j < Nvalid; j++) {
    141         index = measure[j].imageID;
    142         if (!index) {
    143           fprintf (stderr, "?");
    144           continue;
    145         }
    146 
    147         imageSeq = imageIdx[index];
    148         // XXX check the range?
    149 
    150         detCounts[imageSeq] ++;
    151       }
    152       fclose(cpmFile);
    153314      gfits_free_header (&cpmHeaderPHU);
    154315      gfits_free_header (&cpmHeaderTBL);
    155316      free (measure);
     317      free (measureNew);
    156318
    157319      Ncheck ++;
     
    160322      }
    161323    }
    162   }
    163   fprintf (stderr, "\n");
    164 
    165   Nbad = 0;
    166   for (i = 0; i < Nimage; i++) {
    167     // careful: off_t math does not do well with subtractions...
    168     if (detCounts[i] + Ntol < image[i].nstar) {
    169       fprintf (stdout, "image %s (%d) : %d vs %d\n", image[i].name, image[i].imageID, detCounts[i], image[i].nstar);
    170       Nbad ++;
    171     }
    172   }
    173   if (!Nbad) {
    174     fprintf (stderr, "no bad images found\n");
    175   }
     324    fprintf (stderr, "\n");
     325    SkyListFree(inlist);
     326  }
     327
     328  free (imageFilename);
     329  free (imageIdx);
     330  free (deleteIndex);
     331  free (deleteIDs);
     332
     333  free(cpmFilenameSrc);
     334  free(cptFilenameSrc);
     335  free(cpsFilenameSrc);
     336  free(cpmFilenameTgt);
     337  free(cptFilenameTgt);
     338  free(cpsFilenameTgt);
     339
     340  SkyTableFree(insky);
    176341
    177342  exit (0);
    178343}
     344
     345int *ReadDeleteList(char *filename, int *nindex) {
     346
     347  int j, index, Nindex, NINDEX, *indexList, Nline;
     348  int Nstart, Nbytes, Nread, status;
     349  char *c0, *c1, *space, *indexPoint, *buffer;
     350
     351  FILE *f = fopen (filename, "r");
     352  myAssert(f, "failed to open delete list");
     353 
     354  Nindex = 0;
     355  NINDEX = 1000;
     356  ALLOCATE(indexList, int, NINDEX);
     357
     358  ALLOCATE (buffer, char, 0x10001);
     359  bzero (buffer, 0x10001);
     360
     361  Nstart = 0;
     362  Nline = 0;
     363  while (1) {
     364    Nbytes = 0x10000 - Nstart;
     365    bzero (&buffer[Nstart], Nbytes);
     366    Nread = fread (&buffer[Nstart], 1, Nbytes, f);
     367    if (ferror (f)) {
     368      perror ("error reading data file");
     369      exit (1);
     370    }
     371    if (Nread == 0) break; // end of data
     372    // nbytes = Nread + Nstart;
     373   
     374    status = TRUE;
     375    c0 = buffer;
     376    while (status) {
     377      // identify the range of the line
     378      c1 = strchr (c0, '\n');
     379      if (c1 == (char *) NULL) {
     380        Nstart = strlen (c0);
     381        memmove (buffer, c0, Nstart);
     382        break;
     383      } else {
     384        *c1 = 0;
     385      }     
     386      if (*c0 == '#') goto next_line;
     387
     388      // confirm 'image' as first token:
     389      if (strncmp(c0, "image", 5)) {
     390        fprintf (stderr, "error on line %d: missing 'image' token\n", Nline);
     391        goto next_line;
     392      }
     393
     394      // confirm we have 7 fields broken by 6 spaces:
     395      space = c0;
     396      for (j = 0; j < 6; j++) {
     397        space = strchr(space, ' ');
     398        if (!space) {
     399          fprintf (stderr, "line only has %d word(s)\n", j + 1);
     400          goto next_line;
     401        }
     402        space ++;
     403        if (j == 1) {
     404          indexPoint = space + 1;
     405        }
     406      }
     407
     408      // save the value we have found:
     409      index = atoi(indexPoint);
     410      indexList[Nindex] = index;
     411
     412      fprintf (stderr, "index: %d, line: %s\n", index, c0);
     413
     414      Nindex ++;
     415      if (Nindex == NINDEX) {
     416        NINDEX += 1000;
     417        REALLOCATE (indexList, int, NINDEX);
     418      }
     419     
     420    next_line:
     421      Nline ++;
     422      c0 = c1 + 1;
     423    }
     424  }
     425
     426  *nindex = Nindex;
     427  return (indexList);
     428}
     429
     430/* Delete List Format:
     431   image o5252g0035o.140669.cm.51017.smf[XY11.hdr] (219974) : 368 vs 375
     432   image o5252g0051o.140685.cm.51123.smf[XY11.hdr] (226417) : 350 vs 356
     433   image o5399g0207o.194950.cm.98351.smf[XY46.hdr] (1683277) : 10341 vs 10347
     434   image o5464g0284o.230438.cm.123774.smf[XY21.hdr] (2634913) : 214 vs 242
     435   image o5464g0305o.230459.cm.123795.smf[XY21.hdr] (2635758) : 232 vs 257
     436   image o5465g0306o.231171.cm.124201.smf[XY01.hdr] (2654941) : 0 vs 494
     437   image o5465g0306o.231171.cm.124201.smf[XY02.hdr] (2654942) : 10 vs 813
     438   image o5465g0306o.231171.cm.124201.smf[XY10.hdr] (2654947) : 0 vs 534
     439   image o5465g0306o.231171.cm.124201.smf[XY11.hdr] (2654948) : 0 vs 447
     440*/
     441
     442int RepairTableCPT(char *cptFilenameSrc, char *cptFilenameTgt, char *cpsFilenameSrc, char *cpsFilenameTgt, Measure *measure, off_t Nmeasure, Image *image, off_t Nimage, char catformat) {
     443
     444  off_t *averefMatch;
     445  off_t i, NaveMax, Naverage, NAVERAGE, NaverageOut, Nave, Nout, Nold;
     446  int *found, Nsecfilt;
     447
     448  Image *thisImage;
     449  Average *average, *averageOut;
     450
     451  Matrix matrix;
     452
     453  Header cptHeaderPHU;
     454  Header cptHeaderTBL;
     455  FTable cptFtable;
     456
     457  FILE *cptFileSrc = NULL;
     458  FILE *cptFileTgt = NULL;
     459
     460  cptFtable.header = &cptHeaderTBL;
     461
     462  NaveMax = 0;
     463  NAVERAGE = 1000;
     464  ALLOCATE (average, Average, NAVERAGE);
     465  memset (average, 0, NAVERAGE*sizeof(Average));
     466
     467  ALLOCATE (found, int, NAVERAGE);
     468  memset (found, 0, NAVERAGE*sizeof(int));
     469
     470  // examine all measurements and new objects as needed
     471  for (i = 0; i < Nmeasure; i++) {
     472    Nave = measure[i].averef;
     473
     474    if (Nave >= NAVERAGE) {
     475      Nold = NAVERAGE;
     476      NAVERAGE = MAX(Nave + 1000, NAVERAGE + 1000);
     477      REALLOCATE (average, Average, NAVERAGE);
     478      memset (&average[Nold], 0, (NAVERAGE - Nold)*sizeof(Average));
     479
     480      REALLOCATE (found, int, NAVERAGE);
     481      memset (&found[Nold], 0, (NAVERAGE - Nold)*sizeof(int));
     482    }
     483
     484    if (found[Nave]) {
     485      average[Nave].Nmeasure ++;
     486      myAssert(average[Nave].objID == measure[i].objID, "objIDs do not match!");
     487      myAssert(average[Nave].catID == measure[i].catID, "catIDs do not match!");
     488      continue;
     489    }
     490
     491    NaveMax = MAX(Nave, NaveMax);
     492
     493    found[Nave] = TRUE;
     494
     495    // we are going to leave most of the elements of average unset: they are the result of
     496    // the relastro analysis for this object and can be recreated with a call to relastro
     497
     498    // need to find image so we can use ccd coordinates to determine RA & DEC
     499    thisImage = MatchImage (image, Nimage, measure[i].t, measure[i].photcode, measure[i].imageID);
     500    XY_to_RD (&average[Nave].R, &average[Nave].D, measure[i].Xccd, measure[i].Yccd, &thisImage[0].coords);
     501
     502    average[Nave].Nmeasure = 1;
     503    average[Nave].Nmissing = 0;
     504
     505    // assume the resulting table set is unsorted
     506    average[Nave].measureOffset = -1;
     507    average[Nave].missingOffset = -1;
     508    average[Nave].extendOffset = -1;
     509
     510    average[Nave].objID = measure[i].objID;
     511    average[Nave].catID = measure[i].catID;
     512    average[Nave].extID = CreatePSPSObjectID(average[Nave].R, average[Nave].D);
     513  }
     514  Naverage = NaveMax + 1;
     515
     516  // we now have an average table, but there will be holes due to deleted measurements
     517  // create a new average table with only existing entries
     518
     519  ALLOCATE (averageOut, Average, Naverage);
     520  memset (averageOut, 0, Naverage*sizeof(Average));
     521
     522  ALLOCATE (averefMatch, off_t, Naverage);
     523  memset (averefMatch, 0, Naverage*sizeof(int));
     524
     525  Nave = 0;
     526  for (i = 0; i < Naverage; i++) {
     527    if (!found[i]) continue;
     528    averageOut[Nave] = average[i]; // use a memcpy?
     529    averefMatch[i] = Nave;
     530    Nave ++;
     531  }
     532  NaverageOut = Nave;
     533
     534  for (i = 0; i < Nmeasure; i++) {
     535    Nave = measure[i].averef;
     536    Nout = averefMatch[Nave];
     537    myAssert(Nout < NaverageOut, "output averef is wrong");
     538   
     539    myAssert(average[Nave].objID == measure[i].objID, "objIDs do not match");
     540    myAssert(average[Nave].catID == measure[i].catID, "objIDs do not match");
     541    myAssert(averageOut[Nout].objID == measure[i].objID, "objIDs do not match");
     542    myAssert(averageOut[Nout].catID == measure[i].catID, "objIDs do not match");
     543
     544    measure[i].averef = Nout;
     545  }
     546
     547  fprintf (stderr, "cpt file : %d obj -> %d obj (%s -> %s)\n", (int) Naverage, (int) NaverageOut, cptFilenameSrc, cptFilenameTgt);
     548
     549  // open source cpt file
     550  cptFileSrc = fopen(cptFilenameSrc, "r");
     551  myAssert(cptFileSrc, "failed to open cpt file");
     552
     553  // load the cpt header (use for CATID, RA,DEC range, filenames)
     554  if (!gfits_fread_header (cptFileSrc, &cptHeaderPHU)) {
     555    myAbort("failure to cpt header");
     556  }
     557
     558  // update the output header
     559  gfits_modify (&cptHeaderPHU, "NSTARS",     OFF_T_FMT, 1,  NaverageOut);
     560  gfits_modify (&cptHeaderPHU, "NMEAS",      OFF_T_FMT, 1,  Nmeasure);
     561  gfits_modify (&cptHeaderPHU, "NMISS",      "%d",      1,  0);
     562  gfits_modify_alt (&cptHeaderPHU, "SORTED", "%t",      1,  FALSE);
     563
     564  gfits_scan (&cptHeaderPHU, "NSECFILT",     "%d",      1,  &Nsecfilt);
     565
     566  /* convert internal to external format */
     567  if (!AverageToFtable (&cptFtable, averageOut, NaverageOut, catformat, NULL)) {
     568    myAbort("trouble converting format");
     569  }
     570
     571  // create and write the output file
     572  cptFileTgt = fopen(cptFilenameTgt, "w");
     573  myAssert(cptFileTgt, "failed to open cpt file");
     574   
     575  // write PHU header
     576  if (!gfits_fwrite_header (cptFileTgt, &cptHeaderPHU)) {
     577    myAbort("can't write primary header");
     578  }
     579
     580  // write the PHU matrix; this is probably a NOP, do I have to keep it in?
     581  gfits_create_matrix (&cptHeaderPHU, &matrix);
     582  if (!gfits_fwrite_matrix  (cptFileTgt, &matrix)) {
     583    myAbort("can't write primary matrix");
     584  }
     585  gfits_free_matrix (&matrix);
     586
     587  // write the table data
     588  if (!gfits_fwrite_ftable_range (cptFileTgt, &cptFtable, 0, NaverageOut, 0, NaverageOut)) {
     589    myAbort("can't write table data");
     590  }
     591 
     592  fclose(cptFileTgt);
     593  fclose(cptFileSrc);
     594
     595  gfits_free_header (&cptHeaderPHU);
     596  gfits_free_header (&cptHeaderTBL);
     597  free (average);
     598  free (averageOut);
     599
     600  free (found);
     601  free (averefMatch);
     602
     603  {
     604    Header cpsHeaderPHU;
     605    Header cpsHeaderTBL;
     606    FTable cpsFtable;
     607
     608    FILE *cpsFileSrc = NULL;
     609    FILE *cpsFileTgt = NULL;
     610
     611    SecFilt *secfilt = NULL;
     612
     613    cpsFtable.header = &cpsHeaderTBL;
     614
     615    // open source cpt file
     616    cpsFileSrc = fopen(cpsFilenameSrc, "r");
     617    myAssert(cpsFileSrc, "failed to open cps file");
     618   
     619    // load the cps header (use for CATID, RA,DEC range, filenames)
     620    if (!gfits_fread_header (cpsFileSrc, &cpsHeaderPHU)) {
     621      myAbort("failure to cps header");
     622    }
     623
     624    int Nrows = Nsecfilt*NaverageOut;
     625    ALLOCATE (secfilt, SecFilt, Nrows);
     626
     627    /* convert internal to external format */
     628    if (!SecFiltToFtable (&cpsFtable, secfilt, Nrows, catformat)) {
     629      myAbort("trouble converting format");
     630    }
     631
     632    // create and write the output file
     633    cpsFileTgt = fopen(cpsFilenameTgt, "w");
     634    myAssert(cpsFileTgt, "failed to open cps file");
     635   
     636    // write PHU header
     637    if (!gfits_fwrite_header (cpsFileTgt, &cpsHeaderPHU)) {
     638      myAbort("can't write primary header");
     639    }
     640
     641    // write the PHU matrix; this is probably a NOP, do I have to keep it in?
     642    gfits_create_matrix (&cpsHeaderPHU, &matrix);
     643    if (!gfits_fwrite_matrix  (cpsFileTgt, &matrix)) {
     644      myAbort("can't write primary matrix");
     645    }
     646    gfits_free_matrix (&matrix);
     647
     648    // write the table data
     649    if (!gfits_fwrite_ftable_range (cpsFileTgt, &cpsFtable, 0, Nrows, 0, Nrows)) {
     650      myAbort("can't write table data");
     651    }
     652 
     653    fclose(cpsFileTgt);
     654    fclose(cpsFileSrc);
     655
     656    gfits_free_header (&cpsHeaderPHU);
     657    gfits_free_header (&cpsHeaderTBL);
     658    free (secfilt);
     659  }
     660
     661  return (TRUE);
     662}
     663
  • branches/eam_branches/ipp-20101103/Ohana/src/dvomerge/src/dvorepairCPT.c

    r29714 r29734  
    22
    33# define myAbort(MSG) { fprintf (stderr, "%s\n", MSG); abort(); }
    4 # define myAssert(LOGIC,MSG) { if (!LOGIC) { fprintf (stderr, "%s\n", MSG); abort(); } }
     4# define myAssert(LOGIC,MSG) { if (!(LOGIC)) { fprintf (stderr, "%s\n", MSG); abort(); } }
    55
    66// broken cpt file, valid cpm file: we can recover everything in the cpt file from the cpm file:
Note: See TracChangeset for help on using the changeset viewer.