IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33128


Ignore:
Timestamp:
Jan 22, 2012, 6:26:57 AM (14 years ago)
Author:
eugene
Message:

add function gfits_set_bintable_column_reformat (sets column type to match output table description); give normal TZERO and TSCALE defaults (0,1); handle NULL argument to gfits_print/modify string functions with an error not a crash

Location:
branches/eam_branches/ipp-20111122/Ohana/src/libfits
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20111122/Ohana/src/libfits/header/F_modify.c

    r28246 r33128  
    7070  /* string value.  Quotes must be at least 18 chars apart */
    7171  if (!strcmp (mode, "%s")) {
    72     strncpy (data, va_arg (argp, char *), 68);
     72    char *ptr = va_arg (argp, char *);
     73    if (!ptr) goto invalid;
     74    strncpy (data, ptr, 68);
    7375    snprintf (string, 81, "%-8s= '%-18s' / %-s ", field, data, comment);
    7476    goto found_it;
     
    7678
    7779  /* failed to find mode */
     80invalid:
     81  va_end (argp);
    7882  return (FALSE);
    7983
     
    8286  va_end (argp);
    8387  return (TRUE);
    84 
    8588}
    8689
     
    143146  /* comment type of value.  */
    144147  if (!strcmp (mode, "%S")) {
    145     strncpy (data, va_arg (argp, char *), 71);
     148    char *ptr = va_arg (argp, char *);
     149    if (!ptr) goto invalid;
     150    strncpy (data, ptr, 71);
    146151    snprintf (string, 81, "%-8s %-71s", field, data);
    147152  } 
     
    157162
    158163    strncpy (data, qs, MAX (MIN (qe - qs, 71), 0));
    159     strncpy (comment, va_arg (argp, char *), 80);
     164
     165    char *ptr = va_arg (argp, char *);
     166    if (!ptr) goto invalid;
     167    strncpy (comment, ptr, 80);
    160168    gfits_pad_ending (comment, ' ', 82);  /* comment must contain spaces to the end */
    161169    snprintf (string, 81, "%-8s= %s / %-s", field, data, comment);
     
    167175  return (TRUE);
    168176
     177invalid:
     178  va_end (argp);
     179  return (FALSE);
    169180}
    170181
  • branches/eam_branches/ipp-20111122/Ohana/src/libfits/header/F_print.c

    r28246 r33128  
    6565  /* string value.  Quotes must be at least 18 chars apart.  Longer lines will this should be fixed to allow arbitrary string lengths, up to 69 chars */
    6666  if (!strcmp (mode, "%s")) {
    67     strcpy (line, va_arg (argp, char *));
     67    char *ptr = va_arg (argp, char *);
     68    if (!ptr) goto invalid;
     69    strcpy (line, ptr);
    6870    line[68] = 0;
    6971    snprintf (string, 81, "%-8s= '%-18s' / %46s ", field, line, blank);
    7072    goto found_it;
    7173  }
     74
     75invalid:
     76  va_end (argp);
    7277  return (FALSE);
    7378
     
    7681  va_end (argp);
    7782  return (TRUE);
    78 
    7983}
    8084
     
    130134  if (!strcmp (mode, "%S")) {
    131135    /* we are forcing the entry to be <= 69 char long */
    132     strcpy (line, va_arg (argp, char *));
    133     line[71] = 0;
     136    char *ptr = va_arg (argp, char *);
     137    if (!ptr) goto invalid;
     138    bzero (line, 80);
     139    strncpy (line, ptr, 71);
    134140    snprintf (string, 81, "%-8s %-71s", field, line);
    135141  } 
     
    146152
    147153  /* can't write the comment in gfits_print - use gfits_modify */
    148   if (!strcmp (mode, "%C")) return (FALSE);
     154  if (!strcmp (mode, "%C")) goto invalid;
    149155
    150156  strncpy (p, string, 80);
     
    153159  return (TRUE);
    154160
     161invalid:
     162  va_end (argp);
     163  return (FALSE);
    155164}
  • branches/eam_branches/ipp-20111122/Ohana/src/libfits/include/gfitsio.h

    r31663 r33128  
    190190int     gfits_read_table               PROTO((char *filename, FTable *ftable));
    191191int     gfits_set_bintable_column      PROTO((Header *header, FTable *table, char *label, void *data, off_t Nrow));
     192int     gfits_set_bintable_column_reformat PROTO((Header *header, FTable *table, char *label, char *intype, void *data, off_t Nrow));
    192193int     gfits_set_table_column         PROTO((Header *header, FTable *table, char *label, void *data, off_t Nrow));
    193194int     gfits_table_column             PROTO((FTable *ftable, char *field, char *mode,...)) OHANA_FORMAT(printf, 3, 4);
  • branches/eam_branches/ipp-20111122/Ohana/src/libfits/table/F_define_column.c

    r28241 r33128  
    44/***********************/
    55int gfits_define_bintable_column (Header *header, char *format, char *label, char *comment, char *unit, double bscale, double bzero) {
     6
     7  assert (label);
     8  assert (format);
    69
    710  off_t Naxis1;
  • branches/eam_branches/ipp-20111122/Ohana/src/libfits/table/F_set_column.c

    r31371 r33128  
    4040  /* interpret format */
    4141  sprintf (field, "TSCAL%d", N);
    42   gfits_scan (header, field, "%lf", 1, &Bscale);
     42  if (!gfits_scan (header, field, "%lf", 1, &Bscale)) {
     43    Bscale = 1.0;
     44  }
    4345  sprintf (field, "TZERO%d", N);
    44   gfits_scan (header, field, "%lf", 1, &Bzero);
     46  if (!gfits_scan (header, field, "%lf", 1, &Bzero)) {
     47    Bzero = 0.0;
     48  }
    4549  sprintf (field, "TFORM%d", N);
    4650  gfits_scan (header, field, "%s", 1, format);
     
    126130  for (i = 0; i < Nrow; i++, Pout += Nx, Pin += Nval*Nbytes) {
    127131    memcpy (Pout, Pin, Nval*Nbytes);
     132  }
     133
     134  free (array);
     135  return (TRUE);
     136}
     137
     138/***********************/
     139int gfits_set_bintable_column_reformat (Header *header, FTable *table, char *label, char *intype, void *data, off_t Nrow) {
     140
     141  off_t Nx, Ny, nbytes;
     142  int i, N, Nfields;
     143  int Nval, NbytesOut, Nstart, Nv, Nb;
     144  char tlabel[80], field[80], format[80], outtype[16], tmpline[16];
     145  char *Pin, *Pout, *array;
     146  double Bscale, Bzero;
     147
     148  if (label == (char *) NULL) return (FALSE);
     149  if (label[0] == 0) return (FALSE);
     150
     151  /* find label in header */
     152  tlabel[0] = 0;
     153  gfits_scan (header, "TFIELDS", "%d", 1, &Nfields);
     154  for (i = 1; strcasecmp (label, tlabel) && (i < Nfields + 1); i++) {
     155    sprintf (field, "TTYPE%d", i);
     156    gfits_scan (header, field, "%s", 1, tlabel);
     157  }
     158  if (strcasecmp (label, tlabel)) return (FALSE);
     159  N = i - 1;
     160
     161  /* interpret format */
     162  sprintf (field, "TSCAL%d", N);
     163  if (!gfits_scan (header, field, "%lf", 1, &Bscale)) {
     164    Bscale = 1.0;
     165  }
     166  sprintf (field, "TZERO%d", N);
     167  if (!gfits_scan (header, field, "%lf", 1, &Bzero)) {
     168    Bzero = 0.0;
     169  }
     170  sprintf (field, "TFORM%d", N);
     171  gfits_scan (header, field, "%s", 1, format);
     172
     173  if (!gfits_bintable_format (format, outtype, &Nval, &NbytesOut)) return (FALSE);
     174 
     175  /* check existing table dimensions */
     176  gfits_scan (header, "NAXIS1", OFF_T_FMT, 1,  &Nx);
     177  gfits_scan (header, "NAXIS2", OFF_T_FMT, 1,  &Ny);
     178  if (Ny == 0) {
     179    Ny = Nrow;
     180    header[0].Naxis[1] = Ny;
     181    gfits_modify (header, "NAXIS2", OFF_T_FMT, 1,  Ny);
     182
     183    nbytes = gfits_data_size (header);
     184    REALLOCATE (table[0].buffer, char, MAX (nbytes, 1));
     185    bzero (table[0].buffer, nbytes);
     186    table[0].datasize = nbytes;
     187  }
     188  if (Ny != Nrow) return (FALSE);
     189
     190  /* scan columns to find insert point */
     191  Nstart = 0;
     192  for (i = 1; i < N; i++) {
     193    sprintf (field, "TFORM%d", i);
     194    gfits_scan (header, field, "%s", 1, format);
     195    gfits_bintable_format (format, tmpline, &Nv, &Nb);
     196    Nstart += Nv*Nb;
     197  }
     198
     199  /* make duplicate of data with correct type
     200     byte swap and Bzero/Bscale */
     201  ALLOCATE (array, char, NbytesOut*Nval*Nrow);
     202  Pin = data;
     203  Pout = array;
     204
     205  /** input == char **/
     206  if (!strcmp (outtype, "char") && !strcmp (intype, "char")) {
     207    int NbytesIn = 1;
     208    for (i = 0; i < Nval*Nrow; i++, Pin+=NbytesIn, Pout+=NbytesOut) {
     209      *(char *)Pout = (*(char *)Pin - Bzero) / Bscale;
     210    }
     211  }
     212  if (!strcmp (outtype, "short") && !strcmp (intype, "char")) {
     213    int NbytesIn = 1;
     214    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     215      *(short *)Pout = (*(char *)Pin - Bzero) / Bscale;
     216# ifdef BYTE_SWAP
     217      SWAP_BYTE;
     218# endif
     219    } 
     220  }
     221  if (!strcmp (outtype, "int") && !strcmp (intype, "char")) {
     222    int NbytesIn = 1;
     223    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     224      *(int *)Pout = (*(char *)Pin - Bzero) / Bscale;
     225# ifdef BYTE_SWAP
     226      SWAP_WORD;
     227# endif
     228    }
     229  }
     230  if (!strcmp (outtype, "float") && !strcmp (intype, "char")) {
     231    int NbytesIn = 1;
     232    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     233      *(float *)Pout = (*(char *)Pin - Bzero) / Bscale;
     234# ifdef BYTE_SWAP
     235      SWAP_WORD;
     236# endif
     237    }
     238  }
     239  if (!strcmp (outtype, "double") && !strcmp (intype, "char")) {
     240    int NbytesIn = 1;
     241    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     242      *(double *)Pout = (*(char *)Pin - Bzero) / Bscale;
     243# ifdef BYTE_SWAP
     244      SWAP_DBLE;
     245# endif
     246    }
     247  }
     248
     249  /** input == short **/
     250  if (!strcmp (outtype, "char") && !strcmp (intype, "short")) {
     251    int NbytesIn = 2;
     252    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     253      *(char *)Pout = (*(short *)Pin - Bzero) / Bscale;
     254    }
     255  }
     256  if (!strcmp (outtype, "short") && !strcmp (intype, "short")) {
     257    int NbytesIn = 2;
     258    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     259      *(short *)Pout = (*(short *)Pin - Bzero) / Bscale;
     260# ifdef BYTE_SWAP
     261      SWAP_BYTE;
     262# endif
     263    } 
     264  }
     265  if (!strcmp (outtype, "int") && !strcmp (intype, "short")) {
     266    int NbytesIn = 2;
     267    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     268      *(int *)Pout = (*(short *)Pin - Bzero) / Bscale;
     269# ifdef BYTE_SWAP
     270      SWAP_WORD;
     271# endif
     272    }
     273  }
     274  if (!strcmp (outtype, "float") && !strcmp (intype, "short")) {
     275    int NbytesIn = 2;
     276    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     277      *(float *)Pout = (*(short *)Pin - Bzero) / Bscale;
     278# ifdef BYTE_SWAP
     279      SWAP_WORD;
     280# endif
     281    }
     282  }
     283  if (!strcmp (outtype, "double") && !strcmp (intype, "short")) {
     284    int NbytesIn = 2;
     285    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     286      *(double *)Pout = (*(short *)Pin - Bzero) / Bscale;
     287# ifdef BYTE_SWAP
     288      SWAP_DBLE;
     289# endif
     290    }
     291  }
     292
     293  /** input == int **/
     294  if (!strcmp (outtype, "char") && !strcmp (intype, "int")) {
     295    int NbytesIn = 4;
     296    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     297      *(char *)Pout = (*(int *)Pin - Bzero) / Bscale;
     298    }
     299  }
     300  if (!strcmp (outtype, "short") && !strcmp (intype, "int")) {
     301    int NbytesIn = 4;
     302    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     303      *(short *)Pout = (*(int *)Pin - Bzero) / Bscale;
     304# ifdef BYTE_SWAP
     305      SWAP_BYTE;
     306# endif
     307    } 
     308  }
     309  if (!strcmp (outtype, "int") && !strcmp (intype, "int")) {
     310    int NbytesIn = 4;
     311    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     312      *(int *)Pout = (*(int *)Pin - Bzero) / Bscale;
     313# ifdef BYTE_SWAP
     314      SWAP_WORD;
     315# endif
     316    }
     317  }
     318  if (!strcmp (outtype, "float") && !strcmp (intype, "int")) {
     319    int NbytesIn = 4;
     320    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     321      *(float *)Pout = (*(int *)Pin - Bzero) / Bscale;
     322# ifdef BYTE_SWAP
     323      SWAP_WORD;
     324# endif
     325    }
     326  }
     327  if (!strcmp (outtype, "double") && !strcmp (intype, "int")) {
     328    int NbytesIn = 4;
     329    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     330      *(double *)Pout = (*(int *)Pin - Bzero) / Bscale;
     331# ifdef BYTE_SWAP
     332      SWAP_DBLE;
     333# endif
     334    }
     335  }
     336
     337  /** input == float **/
     338  if (!strcmp (outtype, "char") && !strcmp (intype, "float")) {
     339    int NbytesIn = 4;
     340    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     341      *(char *)Pout = (*(float *)Pin - Bzero) / Bscale;
     342    }
     343  }
     344  if (!strcmp (outtype, "short") && !strcmp (intype, "float")) {
     345    int NbytesIn = 4;
     346    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     347      *(short *)Pout = (*(float *)Pin - Bzero) / Bscale;
     348# ifdef BYTE_SWAP
     349      SWAP_BYTE;
     350# endif
     351    } 
     352  }
     353  if (!strcmp (outtype, "int") && !strcmp (intype, "float")) {
     354    int NbytesIn = 4;
     355    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     356      *(int *)Pout = (*(float *)Pin - Bzero) / Bscale;
     357# ifdef BYTE_SWAP
     358      SWAP_WORD;
     359# endif
     360    }
     361  }
     362  if (!strcmp (outtype, "float") && !strcmp (intype, "float")) {
     363    int NbytesIn = 4;
     364    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     365      *(float *)Pout = (*(float *)Pin - Bzero) / Bscale;
     366# ifdef BYTE_SWAP
     367      SWAP_WORD;
     368# endif
     369    }
     370  }
     371  if (!strcmp (outtype, "double") && !strcmp (intype, "float")) {
     372    int NbytesIn = 4;
     373    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     374      *(double *)Pout = (*(float *)Pin - Bzero) / Bscale;
     375# ifdef BYTE_SWAP
     376      SWAP_DBLE;
     377# endif
     378    }
     379  }
     380
     381  /** input == double **/
     382  if (!strcmp (outtype, "char") && !strcmp (intype, "double")) {
     383    int NbytesIn = 8;
     384    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     385      *(char *)Pout = (*(double *)Pin - Bzero) / Bscale;
     386    }
     387  }
     388  if (!strcmp (outtype, "short") && !strcmp (intype, "double")) {
     389    int NbytesIn = 8;
     390    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     391      *(short *)Pout = (*(double *)Pin - Bzero) / Bscale;
     392# ifdef BYTE_SWAP
     393      SWAP_BYTE;
     394# endif
     395    } 
     396  }
     397  if (!strcmp (outtype, "int") && !strcmp (intype, "double")) {
     398    int NbytesIn = 8;
     399    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     400      *(int *)Pout = (*(double *)Pin - Bzero) / Bscale;
     401# ifdef BYTE_SWAP
     402      SWAP_WORD;
     403# endif
     404    }
     405  }
     406  if (!strcmp (outtype, "float") && !strcmp (intype, "double")) {
     407    int NbytesIn = 8;
     408    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     409      *(float *)Pout = (*(double *)Pin - Bzero) / Bscale;
     410# ifdef BYTE_SWAP
     411      SWAP_WORD;
     412# endif
     413    }
     414  }
     415  if (!strcmp (outtype, "double") && !strcmp (intype, "double")) {
     416    int NbytesIn = 8;
     417    for (i = 0; i < Nval*Nrow; i++, Pin += NbytesIn, Pout += NbytesOut) {
     418      *(double *)Pout = (*(double *)Pin - Bzero) / Bscale;
     419# ifdef BYTE_SWAP
     420      SWAP_DBLE;
     421# endif
     422    }
     423  }
     424
     425  /* check array space */
     426  if (Nx*Ny < Nx*(Nrow - 1) + Nstart + Nval*NbytesOut) {
     427    fprintf (stderr, "mismatch in array sizes\n");
     428    return (FALSE);
     429  }
     430
     431  /* insert bytes from array into appropriate section of buffer */
     432  Pout = table[0].buffer + Nstart;
     433  Pin  = array;
     434  for (i = 0; i < Nrow; i++, Pout += Nx, Pin += Nval*NbytesOut) {
     435    memcpy (Pout, Pin, Nval*NbytesOut);
    128436  }
    129437
Note: See TracChangeset for help on using the changeset viewer.