IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 17, 2014, 10:21:10 AM (12 years ago)
Author:
eugene
Message:

various changes due to mods to average.d and measure.d; new parsing concept for photocode-related fields

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.data/read_vectors.c

    r36375 r37049  
    33FILE *f = (FILE *) NULL;
    44char filename[2048];
     5
     6void read_vectors_cleanup ();
    57
    68int datafile (int argc, char **argv) {
     
    2325// vector types
    2426enum {COLTYPE_NONE, COLTYPE_FLT, COLTYPE_INT, COLTYPE_TIME, COLTYPE_CHAR};
    25 static int FITS_TRANSPOSE;
     27
     28static int      Nvec     = 0;
     29static Vector **vec      = NULL;
     30static char   **listname = NULL;
     31static int     *col      = NULL;
     32static int     *coltype  = NULL;
     33static char    *buffer   = NULL;
    2634
    2735int read_vectors (int argc, char **argv) {
     
    2937  int TimeFormat;
    3038  time_t TimeReference;
    31   int i, j, Nskip, Narg, Nvec, *col, IsCSV, VERBOSE;
    32   int Nbytes, Nstart, NELEM, Nelem, nread, *coltype;
     39  int i, j, Nskip, Narg, IsCSV, VERBOSE;
     40  int Nbytes, Nstart, NELEM, Nelem, nread;
    3341  char *colstr, *c0, *c1, *extname;
    34   Vector **vec;
    35 
    36   char *buffer = NULL;
    37 
    38   FITS_TRANSPOSE = FALSE;
    39   if ((Narg = get_argument (argc, argv, "-transpose"))) {
    40     remove_argument (Narg, &argc, argv);
    41     FITS_TRANSPOSE = TRUE;
    42   }
     42  char varname[1024];  // used as a buffer for the names of string fields
    4343
    4444  /* auto-sense table type */
     
    8888
    8989  Nvec = (argc - 1) / 2;
     90  ALLOCATE (listname, char *, Nvec);
    9091  ALLOCATE (vec, Vector *, Nvec);
    9192  ALLOCATE (col, int, Nvec);
    9293  ALLOCATE (coltype, int, Nvec);
     94  for (i = 0; i < Nvec; i++) {
     95    listname[i] = NULL;
     96    vec[i] = NULL;
     97  }
    9398
    9499  for (i = 0; i < Nvec; i++) {
     
    115120    }
    116121
    117     if ((vec[i] = SelectVector (argv[2*i + 1], ANYVECTOR, TRUE)) == NULL) {
    118       gprint (GP_ERR, "USAGE: read name N name N ...\n");
    119       free (vec);
    120       free (col);
    121       return (FALSE);   
     122    if (coltype[i] == COLTYPE_CHAR) {
     123      listname[i] = strcreate (argv[2*i + 1]);
     124    } else {
     125      if ((vec[i] = SelectVector (argv[2*i + 1], ANYVECTOR, TRUE)) == NULL) {
     126        gprint (GP_ERR, "USAGE: read name N name N ...\n");
     127        read_vectors_cleanup();
     128        return (FALSE);   
     129      }
    122130    }
    123131
     
    144152    bad_colname:
    145153      gprint (GP_ERR, "USAGE: read name N name N ...\n");
    146       free (vec);
    147       free (col);
     154      read_vectors_cleanup();
    148155      return (FALSE);   
    149156    }
     
    153160  NELEM = 1000;
    154161  for (i = 0; i < Nvec; i++) {
    155     if ((coltype[i] == COLTYPE_INT) || (coltype[i] == COLTYPE_CHAR)) {
    156       ResetVector (vec[i], OPIHI_INT, NELEM);
    157     } else {
    158       // note that COLTYPE_TIME is a type of float
    159       ResetVector (vec[i], OPIHI_FLT, NELEM);
     162    switch (coltype[i]) {
     163      case COLTYPE_INT:
     164        ResetVector (vec[i], OPIHI_INT, NELEM);
     165        break;
     166      case COLTYPE_FLT:
     167      case COLTYPE_TIME:
     168        // note that COLTYPE_TIME is a type of float
     169        ResetVector (vec[i], OPIHI_FLT, NELEM);
     170        break;
     171      case COLTYPE_CHAR:
     172      default:
     173        break;
    160174    }
    161175  }
     
    166180    if (scan_line_maxlen (f, buffer, 0x10000) == EOF) {
    167181      gprint (GP_ERR, "problem reading file %s\n", filename);
    168       free (vec);
    169       free (col);
     182      read_vectors_cleanup();
    170183      return FALSE;
    171184    }
     
    213226      for (i = 0; i < Nvec; i++) {
    214227        int ivalue;
    215         char cvalue;
    216228        double dvalue;
    217229        time_t tvalue;
     
    224236            break;
    225237          case COLTYPE_CHAR:
    226             readStatus = IsCSV ? charparse_csv (&cvalue, col[i], c0) : charparse (&cvalue, col[i], c0);
    227             vec[i][0].elements.Int[Nelem] = readStatus ? cvalue : 0;
    228             break;
     238            {
     239              // I need to get an isolated word in 'value' with the string value of this field
     240              char *ptr = IsCSV ? ptrparse_csv (col[i], c0) : ptrparse (col[i], c0);
     241              char *value = NULL;
     242              if (IsCSV) {
     243                char *end = parse_nextword_csv (ptr);
     244                if (end) {
     245                  value = end ? strncreate (ptr, end - ptr) : strcreate (ptr);
     246                }
     247              } else {
     248                value = thisword(ptr);
     249              }
     250              set_list_varname (varname, listname[i], Nelem, FALSE);
     251              set_str_variable (varname, value);
     252              free (value);
     253              break;
     254            }
    229255          case COLTYPE_FLT:
    230256            readStatus = IsCSV ? dparse_csv (&dvalue, col[i], c0) : dparse (&dvalue, col[i], c0);
     
    256282        NELEM += 1000;
    257283        for (i = 0; i < Nvec; i++) {
    258           if (coltype[i] == COLTYPE_INT) {
    259             REALLOCATE (vec[i][0].elements.Int, opihi_int, NELEM);
    260           } else {
    261             REALLOCATE (vec[i][0].elements.Flt, opihi_flt, NELEM);
     284          switch (coltype[i]) {
     285            case COLTYPE_INT:
     286              ResetVector (vec[i], OPIHI_INT, NELEM);
     287              break;
     288            case COLTYPE_FLT:
     289            case COLTYPE_TIME:
     290              ResetVector (vec[i], OPIHI_FLT, NELEM);
     291              break;
     292            case COLTYPE_CHAR:
     293            default:
     294              break;
    262295          }
    263296        }
     
    266299    }
    267300  }
     301  // set the final vector / list length
    268302  for (i = 0; i < Nvec; i++) {
    269     if (coltype[i] == COLTYPE_INT) {
    270       REALLOCATE (vec[i][0].elements.Int, opihi_int, MAX (Nelem,1));
    271     } else {
    272       REALLOCATE (vec[i][0].elements.Flt, opihi_flt, MAX (Nelem,1));
    273     }
    274     vec[i][0].Nelements = Nelem;
    275   }
    276  
    277   free (vec);
    278   free (col);
    279   if (buffer) free (buffer);
     303    switch (coltype[i]) {
     304      case COLTYPE_INT:
     305        ResetVector (vec[i], OPIHI_INT, Nelem);
     306        break;
     307      case COLTYPE_FLT:
     308      case COLTYPE_TIME:
     309        ResetVector (vec[i], OPIHI_FLT, Nelem);
     310        break;
     311      case COLTYPE_CHAR:
     312        sprintf (varname, "%s:n", listname[i]);
     313        set_int_variable (varname, Nelem);
     314        break;
     315      default:
     316        break;
     317    }
     318  }
     319  read_vectors_cleanup();
    280320  return (TRUE);
    281 
    282321}
    283322
    284 # define ESCAPE(MSG) { \
    285   gprint (GP_ERR, "%s\n", MSG); \
     323# define ESCAPE(...) {          \
     324  gprint (GP_ERR, __VA_ARGS__); \
    286325  if (CCDKeyword != NULL) free (CCDKeyword); \
    287326  gfits_free_table  (&table); \
     
    292331
    293332  off_t Nbytes;
    294   int i, j, k, N, Nextend, Ny, Binary, vecType, padIfShort;
     333  int i, j, N, Nextend, Ny, Binary, vecType, padIfShort;
    295334  char type[16], ID[80], *CCDKeyword;
    296335  FTable table;
    297336  Header header;
    298337  Vector **vec;
     338  int FITS_TRANSPOSE;
    299339
    300340  table.buffer = NULL;
    301341  header.buffer = NULL;
     342
     343  FITS_TRANSPOSE = FALSE;
     344  if ((N = get_argument (argc, argv, "-transpose"))) {
     345    remove_argument (N, &argc, argv);
     346    FITS_TRANSPOSE = TRUE;
     347  }
    302348
    303349  CCDKeyword = NULL;
     
    328374  // }
    329375
    330   if (argc < 2) ESCAPE ("USAGE: read -fits extension [-extnum] [-keyword key] name name ...");
    331 
    332   if (f == NULL) ESCAPE ("file not found");
     376  if (argc < 2) ESCAPE ("USAGE: read -fits extension [-extnum] [-keyword key] name name ...\n");
     377
     378  if (f == NULL) ESCAPE ("file not found\n");
    333379  fseeko (f, 0LL, SEEK_SET);
    334380  table.header = &header;
     
    338384    // first extension is PHU, cannot be a table.
    339385    // Nextend counts from 0 for first extension
    340     if (!gfits_load_header (f, &header)) ESCAPE ("error reading primary header for file");
     386    if (!gfits_load_header (f, &header)) ESCAPE ("error reading primary header for file\n");
    341387    Nbytes = gfits_data_size (&header);
    342388    fseeko (f, Nbytes, SEEK_CUR);
     
    344390
    345391    for (i = 0; i < Nextend; i++) {
    346       if (!gfits_load_header (f, &header)) ESCAPE ("extension not found");
     392      if (!gfits_load_header (f, &header)) ESCAPE ("extension %d not found\n", i);
    347393      Nbytes = gfits_data_size (&header);
    348394      /* skip the prior data buffers */
     
    350396      gfits_free_header (&header);
    351397    }
    352     if (!gfits_load_header (f, &header)) ESCAPE ("error reading header for extension");
    353     if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension");
     398    if (!gfits_load_header (f, &header)) ESCAPE ("error reading header for extension %d\n", Nextend);
     399    if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension %d\n", Nextend);
    354400
    355401  } else {
     
    382428        continue;
    383429      }
    384       if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension");
     430      if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension\n");
    385431      break;
    386432    }
     
    391437  gfits_scan (&header, "XTENSION", "%s", 1, type);
    392438  if (strcmp (type, "BINTABLE") && strcmp (type, "TABLE")) {
    393     ESCAPE ("specified extension is not a table\n");
     439    ESCAPE ("specified extension %s is not a table\n", type);
    394440  }
    395441  Binary = !strcmp (type, "BINTABLE");
     
    418464       
    419465    if (!FITS_TRANSPOSE) {
     466      // read string column into a list rather than a vector
     467      if (!strcmp (type, "char")) {
     468        char *fieldName = argv[i];
     469        char *Ptr = data;
     470        char varname[1024];  // used as a buffer for the names of string fields
     471        for (j = 0; j < Ny; j++) {
     472          set_list_varname (varname, fieldName, j, FALSE);
     473          char *value = strncreate (&Ptr[j*Nval], Nval);
     474          // replace instances of $ with _
     475          char *p = strchr (value, '$');
     476          while (p) {
     477            *p = '_';
     478            p = strchr (p, '$');
     479          }
     480          set_str_variable (varname, value);
     481          free (value);
     482        }
     483        sprintf (varname, "%s:n", fieldName);
     484        set_int_variable (varname, Ny);
     485        continue;
     486      }
     487
    420488      // define the multifield vector names (Nval vectors x Ny elements)
    421489      ALLOCATE (vec, Vector *, Nval);
     
    429497      }
    430498
    431       if (!strcmp (type, "char")) {
    432         char *Ptr = data;
    433         for (j = 0; j < Ny; j++) {
    434           for (k = 0; k < Nval; k++, Ptr++) {
    435             vec[k][0].elements.Int[j] = *Ptr;
    436           }
    437         }
    438       }
    439       if (!strcmp (type, "short")) {
    440         short *Ptr = data;
    441         for (j = 0; j < Ny; j++) {
    442           for (k = 0; k < Nval; k++, Ptr++) {
    443             vec[k][0].elements.Int[j] = *Ptr;
    444           }
    445         }
    446       }
    447       if (!strcmp (type, "int")) {
    448         int *Ptr = data;
    449         for (j = 0; j < Ny; j++) {
    450           for (k = 0; k < Nval; k++, Ptr++) {
    451             vec[k][0].elements.Int[j] = *Ptr;
    452           }
    453         }
    454       }
    455       if (!strcmp (type, "int64_t")) {
    456         int64_t *Ptr = data;
    457         for (j = 0; j < Ny; j++) {
    458           for (k = 0; k < Nval; k++, Ptr++) {
    459             vec[k][0].elements.Int[j] = *Ptr;
    460           }
    461         }
    462       }
    463       if (!strcmp (type, "float")) {
    464         float *Ptr = data;
    465         for (j = 0; j < Ny; j++) {
    466           for (k = 0; k < Nval; k++, Ptr++) {
    467             vec[k][0].elements.Flt[j] = *Ptr;
    468           }
    469         }
    470       }
    471       if (!strcmp (type, "double")) {
    472         double *Ptr = data;
    473         for (j = 0; j < Ny; j++) {
    474           for (k = 0; k < Nval; k++, Ptr++) {
    475             vec[k][0].elements.Flt[j] = *Ptr;
    476           }
    477         }
    478       }
     499      if (!VectorAssignData (vec, type, data, Ny, Nval)) ESCAPE ("bad column type %s", type);
     500
    479501    } else {
    480502      // define the multifield vector names (Ny vectors x Nval elements)
     
    489511      }
    490512
    491       if (!strcmp (type, "char")) {
    492         char *Ptr = data;
    493         for (j = 0; j < Ny; j++) {
    494           for (k = 0; k < Nval; k++, Ptr++) {
    495             vec[j][0].elements.Int[k] = *Ptr;
    496           }
    497         }
    498       }
    499       if (!strcmp (type, "short")) {
    500         short *Ptr = data;
    501         for (j = 0; j < Ny; j++) {
    502           for (k = 0; k < Nval; k++, Ptr++) {
    503             vec[j][0].elements.Int[k] = *Ptr;
    504           }
    505         }
    506       }
    507       if (!strcmp (type, "int")) {
    508         int *Ptr = data;
    509         for (j = 0; j < Ny; j++) {
    510           for (k = 0; k < Nval; k++, Ptr++) {
    511             vec[j][0].elements.Int[k] = *Ptr;
    512           }
    513         }
    514       }
    515       if (!strcmp (type, "int64_t")) {
    516         int64_t *Ptr = data;
    517         for (j = 0; j < Ny; j++) {
    518           for (k = 0; k < Nval; k++, Ptr++) {
    519             vec[j][0].elements.Int[k] = *Ptr;
    520           }
    521         }
    522       }
    523       if (!strcmp (type, "float")) {
    524         float *Ptr = data;
    525         for (j = 0; j < Ny; j++) {
    526           for (k = 0; k < Nval; k++, Ptr++) {
    527             vec[j][0].elements.Flt[k] = *Ptr;
    528           }
    529         }
    530       }
    531       if (!strcmp (type, "double")) {
    532         double *Ptr = data;
    533         for (j = 0; j < Ny; j++) {
    534           for (k = 0; k < Nval; k++, Ptr++) {
    535             vec[j][0].elements.Flt[k] = *Ptr;
    536           }
    537         }
    538       }
     513      if (!VectorAssignDataTranspose (vec, type, data, Ny, Nval)) ESCAPE ("bad column type %s", type);
    539514    }
    540515    free (data);
     
    546521  return (TRUE);
    547522}
     523
     524void read_vectors_cleanup () {
     525
     526  int i;
     527
     528  if (col) free (col);
     529  if (coltype) free (coltype);
     530  if (buffer) free (buffer);
     531  if (listname) {
     532    for (i = 0; i < Nvec; i++) {
     533      if (listname[i]) free (listname[i]);
     534    }
     535    free (listname);
     536  }
     537  if (vec) free (vec);
     538}
Note: See TracChangeset for help on using the changeset viewer.