IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 15210


Ignore:
Timestamp:
Oct 3, 2007, 4:56:44 PM (19 years ago)
Author:
eugene
Message:

cleanup code, allow multi-file, multi-extension addstar

Location:
trunk/Ohana/src/addstar
Files:
2 edited

Legend:

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

    r15036 r15210  
    3939  char *refcat;
    4040} DVO_DATA;
     41
     42typedef struct {
     43  char *exthead;
     44  char *extdata;
     45  char *exttype;
     46  int extnum_head;
     47  int extnum_data;
     48} HeaderSet;
    4149
    4250typedef struct sockaddr_in SockAddress;
     
    154162Stars     *grefcat                PROTO((char *Refcat, SkyRegion *catstats, int photcode, int *nstars));
    155163Stars     *grefstars              PROTO((char *file, int photcode, int *Nstars));
     164
    156165Stars     *LoadStars              PROTO((char *file, int *Nstars, Image **images, int *Nimages, int photcode));
     166Header   **LoadHeaders            PROTO((FILE *f, int *mode, int *Nheader));
     167HeaderSet *MatchHeaders           PROTO((int **extsize, int *nimage, int mode, Header **headers, int Nheaders));
     168int        LoadData               PROTO((FILE *f, char *file, Image **images, int *nvalid, Stars **stars, int *Nstars, Header **headers, int *extsize, HeaderSet *headerSets, int NheaderSets));
     169
    157170int        in_image               PROTO((double r, double d, Image *image));
    158171int        load_pt_catalog        PROTO((Catalog *catalog, SkyRegion *region));  /*** choose new name ***/
  • trunk/Ohana/src/addstar/src/LoadStars.c

    r15036 r15210  
    33Stars *LoadStars (char *filename, int *Nstars, Image **images, int *Nimages, int photcode) {
    44
    5   int i, j, N, Nfile, Nheader, NHEADER, Nimage, NIMAGE;
    6   int Nskip, Nhead, Ndata, done, status, mode, NinStars;
    7   char **file, *name;
     5  int i, Nfile, Nheaders, NheaderSets, mode, *extsize;
     6  char **file;
    87  FILE *f;
    98  glob_t globList;
    10   char **exthead, **extdata, **exttype, tmpword[80];
    11   int *extnum_head, *extnum_data, *extsize;
    12   Header *header, **headers;
    13   Image *image;
    14   Stars *inStars, *stars;
     9  Header **headers;
     10  Stars *stars;
     11  HeaderSet *headerSets;
    1512
    1613  // parse the filename as a glob
     
    3229  }
    3330
    34   // open the first file, read the PHU header
    35   f = fopen (file[0], "r");
    36   if (f == NULL) {
    37     fprintf (stderr, "ERROR: can't read header for %s\n", file[0]);
    38     exit (1);
    39   }
    40   ALLOCATE (header, Header, 1);
    41   gfits_fread_header (f, header);
    42 
    43   mode = GetFileMode (header);
    44 
     31  *Nimages = 0;
     32  *Nstars = 0;
     33  *images = NULL;
    4534  stars = NULL;
    46   *Nstars = 0;
    47 
    48   /*** load data from a single PHU or a collection of PHU files ***/
    49   if ((mode != SIMPLE_MEF) && (mode != MOSAIC_MEF)) {
    50     Nimage = Nfile;
    51     ALLOCATE (image, Image, Nimage);
    52     for (i = N = 0; i < Nfile; i++) {
    53       // XXX we should not drop all input images if one fails
    54       if (i > 0) {
    55         f = fopen (file[i], "r");
    56         if (f == NULL) {
    57           fprintf (stderr, "can't read header for %s, skipping\n", file[i]);
    58           continue;
    59         }
    60         gfits_fread_header (f, header);
     35
     36  for (i = 0; i < Nfile; i++) {
     37    f = fopen (file[i], "r");
     38    if (f == NULL) {
     39      fprintf (stderr, "can't read file %s, skipping\n", file[i]);
     40      continue;
     41    }
     42
     43    headers = LoadHeaders (f, &mode, &Nheaders);
     44    headerSets = MatchHeaders (&extsize, &NheaderSets, mode, headers, Nheaders);
     45    if (headerSets == NULL) {
     46      fprintf (stderr, "ERROR: can't read headers for %s\n", file[i]);
     47      continue;
     48    }
     49    if (NheaderSets == 0) {
     50      fprintf (stderr, "no object data in file %s, skipping\n", file[i]);
     51      continue;
     52    }
     53    if (VERBOSE) fprintf (stderr, "file %s has %d headers, including %d images\n", file[i], Nheaders, NheaderSets);
     54
     55    /* supplied photcode is incompatible with multi-chip images */
     56    if ((NheaderSets > 1) && photcode) {
     57      fprintf (stderr, "ERROR: photcode cannot be supplied to multi-chip images -- manually adjust the headers\n");
     58      exit (1);
     59    }
     60
     61    LoadData (f, file[i], images, Nimages, &stars, Nstars, headers, extsize, headerSets, NheaderSets);
     62  }
     63
     64  if (*Nimages == 0) {
     65    if (Nfile == 1)
     66      fprintf (stderr, "no valid image data in any of these files, giving up\n");
     67    else
     68      fprintf (stderr, "no valid image data in this file, giving up\n");
     69    exit (0);
     70  }
     71
     72  return stars;
     73}
     74
     75// load all of the headers, jump in file to skip data segments
     76Header **LoadHeaders (FILE *f, int *mode, int *Nheaders) {
     77
     78  int i, status, Nskip, NHEADERS;
     79  Header **headers;
     80
     81  /* we need to examine the extensions to determine the headers and the data */
     82  NHEADERS = 10;
     83  ALLOCATE (headers, Header *, NHEADERS);
     84
     85  // load all headers into memory
     86  for (i = 0;; i++) {
     87    ALLOCATE (headers[i], Header, 1);
     88    status = gfits_fread_header (f, headers[i]);
     89    if (!status) {
     90      *Nheaders = i;
     91      return (headers);
     92    }
     93
     94    // check the mode for this file
     95    if (i == 0) {
     96      *mode = GetFileMode (headers[0]);
     97      if ((*mode == SIMPLE_CMP) || (*mode == MOSAIC_CMP)) {
     98        *Nheaders = i;
     99        return (headers);
    61100      }
    62 
    63       if (!ReadImageHeader (header, &image[N], photcode)) {
    64         fprintf (stderr, "skipping %s\n", file[i]);
    65         continue;
    66       }
    67      
    68       /* find image rootname */
    69       name = filebasename (file[i]);
    70       snprintf (image[N].name, 64, name);
    71       free (name);
    72    
    73       switch (mode) {
    74         case SIMPLE_CMP:
    75         case MOSAIC_CMP:
    76           inStars = ReadStarsTEXT (f, &image[N].nstar);
    77           inStars = FilterStars (inStars, &image[N]);
    78           stars = MergeStars (stars, Nstars, inStars, image[N].nstar);
    79           break;
    80 
    81         case SIMPLE_CMF:
    82         case MOSAIC_CMF:
    83           inStars = ReadStarsFITS (f, header, NULL, &image[N].nstar);
    84           inStars = FilterStars (inStars, &image[N]);
    85           stars = MergeStars (stars, Nstars, inStars, image[N].nstar);
    86           break;
    87 
    88         case MOSAIC_PHU:
    89           inStars = NULL;
    90           NinStars = 0;
    91           break;
    92       }
    93       fclose (f);
    94       gfits_free_header (header);
    95       N++;
    96     }
    97     *Nimages = N;
    98     *images = image;
    99    
    100     if (N == 0) {
    101       // XXX how do we track this error?
    102       fprintf (stderr, "no valid image data in %s, giving up\n", filename);
    103       exit (0);
    104     }
    105 
    106     return stars;
    107   }
    108    
    109   /* we have a multi-chip image */
    110 
    111   /* supplied photcode is incompatible with multi-chip images */
    112   if (photcode) {
    113     fprintf (stderr, "ERROR: photcode cannot be supplied to multi-chip images -- manually adjust the headers\n");
    114     exit (1);
    115   }
    116 
    117   /* we need to examine the extensions to determine the headers and the data */
    118   NHEADER = 10;
    119   ALLOCATE (headers, Header *, NHEADER);
    120 
    121   // the first header is already loaded
    122   headers[0] = header;
    123   Nskip = gfits_matrix_size (header);
    124   fseek (f, Nskip, SEEK_CUR);
    125 
    126   // load all headers into memory
    127   done = FALSE;
    128   for (i = 1; !done; i++) {
    129       ALLOCATE (headers[i], Header, 1);
    130       status = gfits_fread_header (f, headers[i]);
    131       if (!status) {
    132           done = TRUE;
    133       } else {
    134           Nskip = gfits_matrix_size (headers[i]);
    135           fseek (f, Nskip, SEEK_CUR);
    136       }
    137       if (i == NHEADER - 1) {
    138           NHEADER += 10;
    139           REALLOCATE (headers, Header *, NHEADER);
    140       }
    141   }
    142   Nheader = i - 1; /* we failed on the last loop */
    143    
    144   // space to store the images, indexes to the matching headers
     101    }
     102
     103    // advance to the next header
     104    Nskip = gfits_matrix_size (headers[i]);
     105    fseek (f, Nskip, SEEK_CUR);
     106    if (i == NHEADERS - 1) {
     107      NHEADERS += 10;
     108      REALLOCATE (headers, Header *, NHEADERS);
     109    }
     110  }
     111}
     112
     113HeaderSet *MatchHeaders (int **extsize, int *nimage, int mode, Header **headers, int Nheaders) {
     114
     115  int i, j, Nimage, NIMAGE;
     116  char extname[80], exttype[80], exthead[80];
     117  HeaderSet *headerSets;
     118
     119  ALLOCATE (extsize[0], int, Nheaders);
     120
    145121  Nimage = 0;
    146   NIMAGE = Nheader;
    147   ALLOCATE (image, Image, NIMAGE);
    148   ALLOCATE (exthead, char *, NIMAGE);
    149   ALLOCATE (extdata, char *, NIMAGE);
    150   ALLOCATE (exttype, char *, NIMAGE);
    151   ALLOCATE (extnum_head, int, NIMAGE);
    152   ALLOCATE (extnum_data, int, NIMAGE);
    153   ALLOCATE (extsize, int, Nheader);
     122  NIMAGE = 10;
     123  ALLOCATE (headerSets, HeaderSet, NIMAGE);
     124
     125  // what is the mode of the first header (ie, do we have a PHU DIS image?)
     126  mode = GetFileMode (headers[0]);
    154127
    155128  if (mode == MOSAIC_MEF) {
    156       exthead[Nimage] = strcreate ("PHU");
    157       extdata[Nimage] = strcreate ("NONE");
    158       extnum_data[Nimage] = -1;
    159       extnum_head[Nimage] = 0;
    160       Nimage ++;
     129    headerSets[Nimage].exthead    = strcreate ("PHU");
     130    headerSets[Nimage].extdata    = strcreate ("NONE");
     131    headerSets[Nimage].extnum_data = -1;
     132    headerSets[Nimage].extnum_head = 0;
     133    Nimage ++;
    161134  }
    162135
    163136  // now examine the headers, count the table entries, find corresponding headers
    164   for (i = 0; i < Nheader; i++) {
    165       extsize[i] = headers[i][0].size + gfits_matrix_size (headers[i]);
    166       gfits_scan (headers[i], "EXTTYPE", "%s", 1, tmpword);
    167 
    168       if (!strcmp (tmpword, "SMPDATA")   || 
    169           !strcmp (tmpword, "PS1_DEV_0") || 
    170           !strcmp (tmpword, "PS1_DEV_1")) {
    171 
    172           exttype[Nimage] = strcreate (tmpword);
    173           gfits_scan (headers[i], ExtnameKeyword, "%s", 1, tmpword);
    174           extdata[Nimage] = strcreate (tmpword);
    175           gfits_scan (headers[i], "EXTHEAD", "%s", 1, tmpword);
    176           exthead[Nimage] = strcreate (tmpword);
    177           extnum_data[Nimage] = i;
    178           extnum_head[Nimage] = -1;
    179           // find the matching exthead entry
    180           for (j = 0; j < Nheader; j++) {
    181             if (gfits_scan (headers[j], ExtnameKeyword, "%s", 1, tmpword)) {
    182               if (!strcmp (tmpword, exthead[Nimage])) {
    183                 extnum_head[Nimage] = j;
    184               }
    185             }
    186           }
    187           // skip or crash on table with missing matching header?
    188           if (extnum_head[Nimage] == -1) {
    189               fprintf (stderr, "ERROR: can't read header for %s\n", file[0]);
    190               exit (1);
    191           }
    192           Nimage ++;
    193       }
    194   }
     137  for (i = 0; i < Nheaders; i++) {
     138    if (mode == SIMPLE_CMP) {
     139      extsize[0][i] = headers[i][0].size;
     140    } else {
     141      extsize[0][i] = headers[i][0].size + gfits_matrix_size (headers[i]);
     142    }
     143    gfits_scan (headers[i], "EXTTYPE", "%s", 1, exttype);
     144
     145    if (!strcmp (exttype, "SMPDATA")) goto keep;
     146    if (!strcmp (exttype, "PS1_DEV_0")) goto keep;
     147    if (!strcmp (exttype, "PS1_DEV_1")) goto keep;
     148    continue;
     149
     150  keep:
     151    headerSets[Nimage].exttype = strcreate (exttype);
     152
     153    gfits_scan (headers[i], ExtnameKeyword, "%s", 1, extname);
     154    gfits_scan (headers[i], "EXTHEAD", "%s", 1, exthead);
     155
     156    headerSets[Nimage].extdata     = strcreate (extname);
     157    headerSets[Nimage].exthead     = strcreate (exthead);
     158    headerSets[Nimage].extnum_data = i;
     159    headerSets[Nimage].extnum_head = -1;
     160
     161    // find the matching exthead entry
     162    for (j = 0; j < Nheaders; j++) {
     163      if (!gfits_scan (headers[j], ExtnameKeyword, "%s", 1, extname)) continue;
     164      if (strcmp (extname, headerSets[Nimage].exthead)) continue;
     165      headerSets[Nimage].extnum_head = j;
     166      break;
     167    }
     168
     169    // skip or crash on table with missing matching header?
     170    if (headerSets[Nimage].extnum_head == -1) {
     171      return NULL;
     172    }
     173    Nimage ++;
     174    if (Nimage == NIMAGE) {
     175      NIMAGE += 10;
     176      REALLOCATE (headerSets, HeaderSet, NIMAGE);
     177    }
     178  }
     179
    195180  // some old format files did not write EXTTYPE.  they have a single table in the first
    196181  // extension matched to the header in the PHU
    197182  if (Nimage == 0) {
    198       extsize[0] = headers[0][0].size + gfits_matrix_size (headers[0]);
    199       extsize[1] = headers[1][0].size + gfits_matrix_size (headers[1]);
    200       gfits_scan (headers[1], ExtnameKeyword, "%s", 1, tmpword);
    201       if (!strcmp (tmpword, "SMPFILE")) {
    202           extdata[Nimage] = strcreate (tmpword);
    203           exttype[Nimage] = strcreate ("SMPDATA");
    204           exthead[Nimage] = strcreate ("PHU");
    205           extnum_head[Nimage] = 0;
    206           extnum_data[Nimage] = 1;
    207           Nimage = 1;
     183    extsize[0][0] = headers[0][0].size + gfits_matrix_size (headers[0]);
     184    extsize[0][1] = headers[1][0].size + gfits_matrix_size (headers[1]);
     185    gfits_scan (headers[1], ExtnameKeyword, "%s", 1, extname);
     186    if (!strcmp (extname, "SMPFILE")) {
     187      headerSets[Nimage].extdata     = strcreate (extname);
     188      headerSets[Nimage].exttype     = strcreate ("SMPDATA");
     189      headerSets[Nimage].exthead     = strcreate ("PHU");
     190      headerSets[Nimage].extnum_head = 0;
     191      headerSets[Nimage].extnum_data = 1;
     192      Nimage = 1;
     193    }
     194  }
     195 
     196  *nimage = Nimage;
     197  return (headerSets);
     198}
     199
     200// examine the header sets and set the Image entries for the the valid images
     201int LoadData (FILE *f, char *file, Image **images, int *nvalid, Stars **stars, int *Nstars, Header **headers, int *extsize, HeaderSet *headerSets, int Nimages) {
     202
     203  char *name;
     204  int i, j, Nvalid, Nhead, Ndata, Nskip;
     205  Stars *inStars;
     206
     207  if (images[0] == NULL) {
     208    Nvalid = 0;
     209    NVALID = 10;
     210    ALLOCATE (images[0], Image, NVALID);
     211  } else {
     212    Nvalid = *nvalid;
     213    NVALID = Nvalid + 10;
     214    REALLOCATE (images[0], Image, NVALID);
     215  }   
     216
     217  // find image rootname
     218  name = filebasename (file);
     219
     220  // now run through the images, interpret the headers and read the stars
     221  for (i = 0; i < Nimages; i++) {
     222    Nhead = headerSets[i].extnum_head;
     223
     224    if (VERBOSE) fprintf (stderr, "reading header for %s (%s)\n", headerSets[i].exthead, headerSets[i].extdata);
     225    if (!ReadImageHeader (headers[Nhead], &images[0][Nvalid], 0)) {
     226      fprintf (stderr, "skipping %s\n", headerSets[i].exthead);
     227      continue;
     228    }
     229
     230    // XXX use something to set the chip name? EXTNAME?
     231    if (!strcmp(headerSets[i].exthead, "PHU") && (Nimages == 1)) {
     232      snprintf (images[0][Nvalid].name, 64, "%s", name);
     233    } else {
     234      snprintf (images[0][Nvalid].name, 64, "%s[%s]", name, headerSets[i].exthead);
     235    }
     236
     237    // skip the table if there is no data segment (eg, mosaic WRP image)
     238    if (!strcmp(headerSets[i].extdata, "NONE")) {
     239      Nvalid++;
     240      if (Nvalid == NVALID) {
     241        NVALID += 10;
     242        REALLOCATE (images[0], Image, NVALID);
    208243      }
    209   }
    210   if (Nimage == 0) Shutdown ("no object data in file");
    211    
    212   if (VERBOSE) fprintf (stderr, "file %s has %d headers, including %d images\n", file[0], Nheader, Nimage);
    213 
    214   /* find image rootname */
    215   name = filebasename (file[0]);
    216 
    217   // now run through the images, interpret the headers and read the stars
    218   for (i = N = 0; i < Nimage; i++) {
    219       Nhead = extnum_head[i];
    220 
    221       if (VERBOSE) fprintf (stderr, "reading header for %s (%s)\n", exthead[i], extdata[i]);
    222       if (!ReadImageHeader (headers[Nhead], &image[N], 0)) {
    223           fprintf (stderr, "skipping %s\n", exthead[i]);
    224           continue;
    225       }
    226 
    227       // XXX use something to set the chip name? EXTNAME?
    228       if (!strcmp(exthead[i], "PHU") && (Nimage == 1)) {
    229         snprintf (image[N].name, 64, "%s", name);
    230       } else {
    231         snprintf (image[N].name, 64, "%s[%s]", name, exthead[i]);
    232       }
    233 
    234       // skip the table if there is not data segment (eg, mosaic WRP image)
    235       if (!strcmp(extdata[i], "NONE")) {
    236           N++;
    237           continue;
    238       }
    239 
    240       // advance the pointer to the start of the corresponding table block
    241       Ndata = extnum_data[i];
    242       Nskip = 0;
    243       for (j = 0; j < Ndata; j++) {
    244           Nskip += extsize[j];
    245       }
    246       fseek (f, Nskip, SEEK_SET);
     244      continue;
     245    }
     246
     247    // advance the pointer to the start of the corresponding table block
     248    Ndata = headerSets[i].extnum_data;
     249    Nskip = 0;
     250    for (j = 0; j < Ndata; j++) {
     251      Nskip += extsize[j];
     252    }
     253    fseek (f, Nskip, SEEK_SET);
    247254         
    248       inStars = ReadStarsFITS (f, headers[Nhead], headers[Ndata], &image[N].nstar);
    249       inStars = FilterStars (inStars, &image[N]);
    250       stars = MergeStars (stars, Nstars, inStars, image[N].nstar);
    251       N++;
     255    inStars = ReadStarsFITS (f, headers[Nhead], headers[Ndata], &images[0][Nvalid].nstar);
     256    inStars = FilterStars (inStars, &images[0][Nvalid]);
     257    *stars = MergeStars (*stars, Nstars, inStars, images[0][Nvalid].nstar);
     258    Nvalid++;
    252259  }
    253260  free (name);
    254   *Nimages = N;
    255   *images = image;
    256 
    257   if (N == 0) {
    258     fprintf (stderr, "no valid image data in %s, giving up\n", filename);
    259     exit (0);
    260   }
    261 
    262   return stars;
    263 }
     261  *nvalid = Nvalid;
     262  return (TRUE);
     263}
     264
Note: See TracChangeset for help on using the changeset viewer.