IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 1, 2012, 2:22:08 PM (14 years ago)
Author:
eugene
Message:

fix byteswap error for 64bit tables (simplify byteswap code generally to clean up); add function to get type of specified column by seq number; defaults for Bzero and Bscale in tables without temp (0.0,1.0); function to set a bintable column from a vector of different type; better error handling for F_print,F_modify

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libfits/table/F_set_column.c

    r31371 r33648  
    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.