IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 38946


Ignore:
Timestamp:
Oct 22, 2015, 10:34:05 PM (11 years ago)
Author:
eugene
Message:

update pspsconvert

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20150625/Ohana/src/tools/src/pspsconvert.c

    r38942 r38946  
    33# include "inttypes.h"
    44
     5# define NMAX    0x100
     6# define NBUFFER 0x100000
     7# define NROWS 10000
     8
     9# define TO_BUFFER 1
     10
     11# if (TO_BUFFER)
     12# define FPRINT(FILE,...) {                                             \
     13  Nout = snprintf (&buffer[Nbuffer], NMAX, __VA_ARGS__);                \
     14  /* fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); */          \
     15  /* fprintf (stderr, "Nbuf: %d, Nout: %d\n", Nbuffer, Nout);*/         \
     16  Nbuffer += Nout; }                                           
     17# else
     18// # define FPRINT(FILE,...) { fprintf (FILE, __VA_ARGS__); }
     19# define FPRINT(FILE,...) if (0) fprintf (FILE, __VA_ARGS__)
     20# endif
     21
     22
    523void usage();
    624
     25void print_data (FTable *table);
     26void *gfits_get_bintable_column_data_by_Ncol (FTable *table, int Nfield, char *type, off_t *Nrow, int *Ncol, char nativeOrder);
     27void FFPRINT (char *buffer, int *nbuffer, double value);
     28
    729int main (int argc, char **argv) {
     30
     31  if (0) {
     32
     33    int Nout;
     34    int Nbuffer = 0;
     35    ALLOCATE_PTR (buffer, char, NBUFFER);
     36    memset (buffer, 0, NBUFFER);
     37   
     38    srand48(1);
     39
     40    FILE *f1 = fopen ("test1.txt", "w");
     41    FILE *f2 = fopen ("test2.txt", "w");
     42
     43    int i;
     44    for (i = 0; i < 20; i++) {
     45      float v = 1000*(drand48() - 0.5);
     46
     47      FFPRINT (buffer, &Nbuffer, v);
     48      FPRINT (stdout, "\n");
     49
     50      fprintf (f1, "%+.6e\n", v);
     51    }
     52
     53    fwrite (buffer, 1, Nbuffer, f2);
     54    fclose (f1);
     55    fclose (f2);
     56
     57    exit (0);
     58  }
    859
    960  if (argc != 2) usage ();
     
    1364  Header header;
    1465  table.header = &header;
     66  table.buffer = NULL;
    1567
    1668  FILE *f = fopen (input, "r");
     
    56108    fprintf (stdout, "\n");
    57109
    58     int Nrows = 1000;
     110    if (strcmp(exttype, "BINTABLE")) {
     111      off_t Nbytes = gfits_data_size (&header);
     112      gfits_free_header (&header);
     113      fseeko (f, Nbytes, SEEK_CUR);
     114      Nsection ++;
     115      continue;
     116    }
     117
     118    // need to reset this on each pass?
     119    myAssert (!table.buffer, "oops");
     120
     121    // total number of bytes in the data section (including pad at the end)
     122    off_t Nbytes = gfits_data_size (&header);
     123   
     124    off_t Ntotal = header.Naxis[1];
     125    int Nrows = NROWS;
    59126    int start = 0;
    60     while (start < header.Naxis[1]) {
    61       if (!gfits_fread_ftable_range (f, FALSE, &table, start, Nrows)) {
     127    while (start < Ntotal) {
     128      if (!gfits_fread_ftable_range (f, FALSE, TRUE, &table, start, Nrows)) {
    62129        fprintf (stderr, "error reading table for extension %d\n", Nsection);
    63130        exit (1);
    64131      }
    65132      fprintf (stderr, "read "OFF_T_FMT" rows\n", header.Naxis[1]);
     133
     134      print_data (&table);
     135
    66136      start += Nrows;
    67     }
     137      Nbytes -= header.Naxis[0]*header.Naxis[1]*abs(header.bitpix / 8);
     138      header.Naxis[1] = Ntotal;
     139    }
     140
     141    gfits_free_table (&table);
     142    gfits_free_header (&header);
     143
     144    // move the pointer to the end of this data segment
     145    fseeko (f, Nbytes, SEEK_CUR);
    68146    Nsection ++;
    69147  }
     148  gfits_free_header (&header);
     149
    70150  fclose (f);
     151
     152  ohana_memdump (TRUE);
     153
    71154  exit (0);
    72155}
    73156
    74157void usage () {
    75     fprintf (stderr, "USAGE: pspsconvert [-h] (table.fits) (outroot)\n");
    76     exit (2);
     158  fprintf (stderr, "USAGE: pspsconvert [-h] (table.fits) (outroot)\n");
     159  exit (2);
    77160}
     161
     162typedef enum {
     163  TYPE_NONE, 
     164  TYPE_CHAR, 
     165  TYPE_BYTE, 
     166  TYPE_SHORT,
     167  TYPE_INT,   
     168  TYPE_INT64,
     169  TYPE_FLOAT,
     170  TYPE_DOUBLE
     171} TypeInt;
     172
     173void print_data (FTable *table) {
     174
     175  int i, j, Nf;
     176
     177  // print out the columns (need to swap first)
     178
     179  Header *header = table->header;
     180  int Ny = table->header->Naxis[1];
     181
     182  int Nfields;
     183  if (!gfits_scan (header, "TFIELDS", "%d", 1, &Nfields)) return;
     184
     185  ALLOCATE_PTR (Nrow, off_t,  Nfields);
     186  ALLOCATE_PTR (Ncol, int,    Nfields);
     187  ALLOCATE_PTR (data, void *, Nfields);
     188  ALLOCATE_PTR (type, char *, Nfields);
     189  ALLOCATE_PTR (Type, int,    Nfields);
     190
     191  // first, extract the table data:
     192  for (i = 0; i < Nfields; i++) {
     193    ALLOCATE (type[i], char, 16);
     194    data[i] = gfits_get_bintable_column_data_by_Ncol (table, i + 1, type[i], &Nrow[i], &Ncol[i], FALSE);
     195    myAssert (Nrow[i] == Ny, "oops");
     196    Type[i] = TYPE_NONE;
     197    if (!strcmp (type[i], "char"))    Type[i] = TYPE_CHAR;
     198    if (!strcmp (type[i], "byte"))    Type[i] = TYPE_BYTE;
     199    if (!strcmp (type[i], "short"))   Type[i] = TYPE_SHORT;
     200    if (!strcmp (type[i], "int"))     Type[i] = TYPE_INT;
     201    if (!strcmp (type[i], "int64_t")) Type[i] = TYPE_INT64;
     202    if (!strcmp (type[i], "float"))   Type[i] = TYPE_FLOAT;
     203    if (!strcmp (type[i], "double"))  Type[i] = TYPE_DOUBLE;
     204    myAssert (Type[i] != TYPE_NONE, "oops");
     205  }
     206
     207  // convert the type characters to an enum for speed here
     208  // could also generate pointers of all types to the data;
     209
     210# if (TO_BUFFER)
     211  int Nout;
     212# endif
     213  int Nbuffer = 0;
     214  ALLOCATE_PTR (buffer, char, NBUFFER);
     215  memset (buffer, 0, NBUFFER);
     216
     217  for (i = 0; i < Ny; i++) {
     218    for (Nf = 0; Nf < Nfields; Nf++) {
     219      switch (Type[Nf]) {
     220        case TYPE_CHAR: {
     221          char line[1024];
     222          char *value = data[Nf];
     223          memset (line, 0, 128);
     224          memcpy (line, &value[i*Ncol[Nf]], Ncol[Nf]);
     225          FPRINT (stdout, "%s", line);
     226          break;
     227        }
     228        case TYPE_BYTE: {
     229          char *value = data[Nf];
     230          for (j = 0; j < Ncol[Nf]; j++) {
     231            FPRINT (stdout, "%hhd", value[i*Ncol[Nf] + j]);
     232          }
     233          break;
     234        }
     235        case TYPE_SHORT: {
     236          short *value = data[Nf];
     237          for (j = 0; j < Ncol[Nf]; j++) {
     238            FPRINT (stdout, "%hd", value[i*Ncol[Nf] + j]);
     239          }
     240          break;
     241        }
     242        case TYPE_INT: {
     243          int *value = data[Nf];
     244          for (j = 0; j < Ncol[Nf]; j++) {
     245            FPRINT (stdout, "%d", value[i*Ncol[Nf] + j]);
     246          }
     247          break;
     248        }
     249        case TYPE_INT64: {
     250          int64_t *value = data[Nf];
     251          for (j = 0; j < Ncol[Nf]; j++) {
     252            FPRINT (stdout, "%lld", (long long int) value[i*Ncol[Nf] + j]);
     253          }
     254          break;
     255        }
     256        case TYPE_FLOAT: {
     257          float *value = data[Nf];
     258          for (j = 0; j < Ncol[Nf]; j++) {
     259            FFPRINT (buffer, &Nbuffer, value[i*Ncol[Nf] + j]);
     260            // FPRINT (stdout, "%e", value[i*Ncol[Nf] + j]);
     261          }
     262          break;
     263        }
     264        case TYPE_DOUBLE: {
     265          double *value = data[Nf];
     266          for (j = 0; j < Ncol[Nf]; j++) {
     267            FFPRINT (buffer, &Nbuffer, value[i*Ncol[Nf] + j]);
     268            // FPRINT (stdout, "%e", value[i*Ncol[Nf] + j]);
     269          }
     270          break;
     271        }
     272        default:
     273          myAbort ("impossible");
     274      }
     275      if (Nf < Nfields - 1) FPRINT (stdout, ",");
     276      if (Nbuffer >= NBUFFER - 2*NMAX) {
     277        fwrite (buffer, 1, Nbuffer, stdout);
     278        Nbuffer = 0;
     279        memset (buffer, 0, NMAX);
     280      }
     281    }
     282    FPRINT (stdout, "\n");
     283  }
     284# if (TO_BUFFER)
     285  fwrite (buffer, 1, Nbuffer, stdout);
     286# endif
     287
     288  free (buffer);
     289
     290  for (i = 0 ; i < Nfields; i++) {
     291    free (type[i]);
     292    free (data[i]);
     293  }
     294  free (Nrow);
     295  free (Ncol);
     296  free (data);
     297  free (type);
     298  free (Type);
     299
     300  return;
     301}
     302
     303# define SWAP_BYTE {                                    \
     304    char tmp;                                           \
     305    tmp = Pin[0]; Pin[0] = Pin[1]; Pin[1] = tmp; }
     306# define SWAP_WORD {                                    \
     307    char tmp;                                           \
     308    tmp = Pin[0]; Pin[0] = Pin[3]; Pin[3] = tmp;        \
     309    tmp = Pin[1]; Pin[1] = Pin[2]; Pin[2] = tmp; }
     310# define SWAP_DBLE {                                    \
     311    char tmp;                                           \
     312    tmp = Pin[0]; Pin[0] = Pin[7]; Pin[7] = tmp;        \
     313    tmp = Pin[1]; Pin[1] = Pin[6]; Pin[6] = tmp;        \
     314    tmp = Pin[2]; Pin[2] = Pin[5]; Pin[5] = tmp;        \
     315    tmp = Pin[3]; Pin[3] = Pin[4]; Pin[4] = tmp; }
     316
     317void *gfits_get_bintable_column_data_by_Ncol (FTable *table, int Nfield, char *type, off_t *Nrow, int *Ncol, char nativeOrder) {
     318
     319  int i;
     320  char tlabel[256], field[256], format[256], tmpline[64];
     321  char *Pin, *Pout, *array;
     322  double Bscale, Bzero;
     323
     324  Header *header = table->header;
     325
     326  int Nfields;
     327  if (!gfits_scan (header, "TFIELDS", "%d", 1, &Nfields)) return NULL;
     328
     329  if (Nfield < 1) return NULL;
     330  if (Nfield > Nfields) return NULL;
     331
     332  Bscale = 1;
     333  Bzero  = 0;
     334
     335  /* get column keywords */
     336  snprintf (field, 256, "TTYPE%d", Nfield);
     337  gfits_scan (header, field, "%s", 1, tlabel);
     338  snprintf (field, 256, "TSCAL%d", Nfield);
     339  gfits_scan (header, field, "%lf", 1, &Bscale);
     340  snprintf (field, 256, "TZERO%d", Nfield);
     341  gfits_scan (header, field, "%lf", 1, &Bzero);
     342  snprintf (field, 256, "TFORM%d", Nfield);
     343  gfits_scan (header, field, "%s", 1, format);
     344
     345  int Nval, Nbytes;
     346  if (!gfits_bintable_format (format, type, &Nval, &Nbytes)) return (NULL);
     347 
     348  /* check existing table dimensions */
     349  off_t Nx, Ny;
     350  gfits_scan (header, "NAXIS1",  OFF_T_FMT, 1,  &Nx);
     351  gfits_scan (header, "NAXIS2",  OFF_T_FMT, 1,  &Ny);
     352
     353  /* scan columns to find insert point */
     354  int Nstart = 0;
     355  for (i = 1; i < Nfield; i++) {
     356    snprintf (field, 256, "TFORM%d", i);
     357    gfits_scan (header, field, "%s", 1, format);
     358    int Nv, Nb;
     359    gfits_bintable_format (format, tmpline, &Nv, &Nb);
     360    Nstart += Nv*Nb;
     361  }
     362
     363  /* extract bytes from table into array */
     364  ALLOCATE (array, char, Nbytes*Nval*Ny);
     365  Pin  = table[0].buffer + Nstart;
     366  Pout = array;
     367  for (i = 0; i < Ny; i++, Pin += Nx, Pout += Nval*Nbytes) {
     368    memcpy (Pout, Pin, Nval*Nbytes);
     369  }
     370
     371  // NOTE: we have already copied the data to 'array',
     372  Pin  = array;
     373  Pout = array;
     374  int directCopy = (Bzero == 0.0) && (Bscale == 1.0);
     375
     376  /* convert data in-situ with correct type, byte swap and Bzero/Bscale */
     377  if (!strcmp (type, "char")) {
     378    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     379      if (!directCopy) { *(char *)Pout = *(char *)Pin*Bscale + Bzero; }
     380    }
     381  }
     382  if (!strcmp (type, "byte")) {
     383    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     384      if (!directCopy) { *(char *)Pout = *(char *)Pin*Bscale + Bzero; }
     385    }
     386  }
     387  if (!strcmp (type, "short")) {
     388    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     389# ifdef BYTE_SWAP
     390      if (!nativeOrder) { SWAP_BYTE; }
     391# endif
     392      if (!directCopy) { *(short *)Pout = *(short *)Pin*Bscale + Bzero; }
     393    } 
     394  }
     395  if (!strcmp (type, "int")) {
     396    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     397# ifdef BYTE_SWAP
     398      if (!nativeOrder) { SWAP_WORD; }
     399# endif
     400      if (!directCopy) { *(int *)Pout = *(int *)Pin*Bscale + Bzero; }
     401    }
     402  }
     403  if (!strcmp (type, "int64_t")) {
     404    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     405# ifdef BYTE_SWAP
     406      if (!nativeOrder) { SWAP_DBLE; }
     407# endif
     408      if (!directCopy) { *(int64_t *)Pout = *(int64_t *)Pin*Bscale + Bzero; }
     409    }
     410  }
     411  if (!strcmp (type, "float")) {
     412    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     413# ifdef BYTE_SWAP
     414      if (!nativeOrder) { SWAP_WORD; }
     415# endif
     416      if (!directCopy) { *(float *)Pout = *(float *)Pin*Bscale + Bzero; }
     417    }
     418  }
     419  if (!strcmp (type, "double")) {
     420    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
     421# ifdef BYTE_SWAP
     422      if (!nativeOrder) { SWAP_DBLE; }
     423# endif
     424      if (!directCopy) { *(double *)Pout = *(double *)Pin*Bscale + Bzero; }
     425    }
     426  }
     427
     428  // check that we supplied a valid type
     429
     430  *Ncol = Nval;
     431  *Nrow = Ny;
     432  return (array);
     433}
     434
     435/**
     436        char *ptr = line;
     437        while (*ptr) {
     438          if (*ptr == '\n') {
     439            *ptr = 0;
     440            break;
     441          }
     442          ptr ++;
     443        }
     444
     445 **/
     446
     447void FFPRINT (char *buffer, int *nbuffer, double value) {
     448
     449  // I want a fast way to print a float in %e format.
     450
     451  // use a fixed format
     452  // +F.ffffffe+NN
     453
     454  int Nbuffer = *nbuffer;
     455
     456  if (value == 0.0) {
     457    strcpy (&buffer[Nbuffer], "0.0+e00");
     458    Nbuffer += 7;
     459    *nbuffer = Nbuffer;
     460    return;
     461  }                                             
     462
     463  int isPositiveValue = (value > 0.0);
     464
     465  double pvalue = fabs(value);
     466
     467  int isPositiveExp = (pvalue >= 1.0);
     468
     469  int iExponent = (int)(log10(pvalue));
     470  if (!isPositiveExp) iExponent --;
     471 
     472  double power = pow (10.0, (double) iExponent);
     473
     474  double Mantissa = pvalue / power;
     475
     476  int iMantissa = Mantissa * 10000000;
     477 
     478  char output[14];
     479
     480  // +1.234567e+01 -> 1234567
     481  // 0123456789012 == 6543210
     482
     483  output[0] = isPositiveValue ? '+' : '-';
     484
     485  int roundUp = (iMantissa % 10 > 4);
     486  // fprintf (stderr, "%f : %d : %d\n", value, iMantissa, roundUp);
     487
     488  iMantissa = iMantissa / 10;
     489  if (roundUp) iMantissa ++;
     490
     491  int i;
     492  for (i = 0; i < 8; i++) {
     493    if (i == 6) {
     494      output[8-i] = '.';
     495      continue;
     496    }
     497    output[8-i] = '0' + iMantissa % 10;
     498    iMantissa = iMantissa / 10;
     499  }
     500   
     501  output[9] = 'e';
     502  output[10] = isPositiveExp ? '+' : '-';
     503
     504  for (i = 0; i < 2; i++) {
     505    output[12-i] = '0' + iExponent % 10;
     506    iExponent = iExponent / 10;
     507  }
     508  output[13] = 0;
     509 
     510  memcpy (&buffer[Nbuffer], output, 14);
     511  Nbuffer += 13;
     512  *nbuffer = Nbuffer;
     513}
Note: See TracChangeset for help on using the changeset viewer.