IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 27, 2019, 11:07:45 AM (7 years ago)
Author:
eugene
Message:

add options to include extra header keywords in output FITS binary tables; plug memory leak; accept --no-rc, -norc, -no-rc arguments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/VectorIO.c

    r40291 r41161  
    110110}
    111111
     112// insert a new header line, return end pointer
     113char *gfits_insert_header_line (Header *header, char *endptr, char *newline) {
     114
     115  if (0) {
     116    char temp1[32], temp2[32];
     117    memset (temp1, 0, 32);
     118    memset (temp2, 0, 32);
     119
     120    if (endptr) {
     121      memcpy (temp1, endptr, 8);
     122    }
     123    memcpy (temp2, newline, 8);
     124    fprintf (stderr, "insert: %s : %s\n", temp1, temp2);
     125  }
     126
     127  /* find the END of the header, if not supplied */
     128  if (!endptr) {
     129    endptr = gfits_header_field (header, "END", 1);
     130    if (endptr == NULL) return NULL;
     131  }
     132
     133  /* is there enough space for 1 more line? */
     134  if (header[0].datasize - (endptr - (header[0].buffer)) < 2*FT_LINE_LENGTH) {
     135    // no, so expand by a full header block (FT_RECORD_SIZE = 32*80 = 2880)
     136    header[0].datasize += FT_RECORD_SIZE;
     137    REALLOCATE (header[0].buffer, char, header[0].datasize);
     138    // re-find the "END" marker, in case new memory block is used
     139    endptr = gfits_header_field (header, "END", 1);
     140    if (endptr == NULL) return NULL;
     141    memset (endptr + FT_LINE_LENGTH, ' ', FT_RECORD_SIZE);
     142  }
     143
     144  /* push END line back 1 */
     145  memmove ((endptr + FT_LINE_LENGTH), endptr, FT_LINE_LENGTH);
     146  memset (endptr, ' ', FT_LINE_LENGTH);
     147
     148  strncpy (endptr, newline, 80);
     149  endptr += FT_LINE_LENGTH;
     150
     151  return endptr;
     152}
     153 
     154static char *rawkeywords[] = {"SIMPLE", "BITPIX", "NAXIS", "PCOUNT", "GCOUNT", "EXTEND", "EXTNAME", "BSCALE", "BZERO", "TFIELDS", "TFORM", "TZERO", "TSCAL", "        ", NULL};
     155
    112156// write a set of vectors to a FITS file (vectors names become fits column names)
    113 int WriteVectorTableFITS (char *filename, char *extname, Vector **vec, int Nvec, int append, char *compress, char *format, int Ntile) {
     157int WriteVectorTableFITS (char *filename, char *extname, Header *extraheader, Vector **vec, int Nvec, int append, char *compress, char *format, int Ntile) {
    114158 
    115159  Header rawheader;
     
    139183  rawtable.header = &rawheader;
    140184  if (!WriteVectorTable (&rawtable, extname, vec, Nvec, format, (compress != NULL))) goto escape;
    141   // NOTE : for compression, the table is constructed in native by order
     185  // NOTE : for compression, the table is constructed in native byte order
    142186
    143187  FTable *outtable = &rawtable;
     
    163207
    164208  if (!append) {
     209    // generate a blank PHU header
    165210    Header header;
    166211    Matrix matrix;
     
    176221  }
    177222
     223  if (extraheader) {
     224    // copy keywords which are not the standard or table keywords
     225    char *buf = extraheader->buffer;
     226    char *endptr = NULL;
     227    for (int i = 0; i < extraheader->datasize; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
     228
     229      if (0) {
     230        char temp1[32];
     231        memset (temp1, 0, 32);
     232        memcpy (temp1, buf, 8);
     233        fprintf (stderr, "buffer: %s\n", temp1);
     234      }
     235
     236      for (int j = 0; rawkeywords[j] != NULL; j++) {
     237        if (!strncmp (buf, rawkeywords[j], strlen(rawkeywords[j]))) goto skip_insert;
     238      }
     239      endptr = gfits_insert_header_line (outheader, endptr, buf);
     240      if (!endptr) {
     241        gprint (GP_ERR, "failed to update FITS header with extra keywords\n");
     242        return (FALSE);
     243      }
     244    skip_insert:
     245      continue;
     246    }
     247  }
     248
    178249  // write the actual table data
    179250  gfits_fwrite_Theader (f, outheader);
Note: See TracChangeset for help on using the changeset viewer.