IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 24, 2020, 3:19:09 PM (6 years ago)
Author:
eugene
Message:

adding string-type vectors to opihi

File:
1 edited

Legend:

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

    r40519 r41269  
    3535
    3636// vector types
    37 enum {COLTYPE_NONE, COLTYPE_FLT, COLTYPE_INT, COLTYPE_TIME, COLTYPE_CHAR};
     37enum {COLTYPE_NONE, COLTYPE_FLT, COLTYPE_INT, COLTYPE_TIME, COLTYPE_CHAR, COLTYPE_STR};
    3838
    3939static int      Nvec     = 0;
     
    9191    gprint (GP_ERR, "     -skip N : skip N lines before reading\n");
    9292    gprint (GP_ERR, "     column names may include a type: name:type\n");
    93     gprint (GP_ERR, "       type is int, float, char, time\n");
     93    gprint (GP_ERR, "       type is int, float, char, str, time\n");
    9494    gprint (GP_ERR, "       for char, values are placed into a list $name:0 - $name:n\n");
     95    gprint (GP_ERR, "       for str, values are placed into a string-typed vector\n");
    9596    gprint (GP_ERR, "       for time, values are human-readable date/time strings YYYY/MM/DD,hh:mm:ss\n");
    9697    gprint (GP_ERR, "       the resulting vector is a float based on the TIMEFORMAT, TIMEREF values\n");
     
    152153      if (!strcasecmp(ptr, "int"))   { coltype[i] = COLTYPE_INT; }
    153154      if (!strcasecmp(ptr, "char"))  { coltype[i] = COLTYPE_CHAR; }
     155      if (!strcasecmp(ptr, "str"))   { coltype[i] = COLTYPE_STR; }
    154156      if (!strcasecmp(ptr, "time"))  { coltype[i] = COLTYPE_TIME; }
    155157      if (!coltype[i]) goto bad_colname;
     
    213215        // note that COLTYPE_TIME is a type of float
    214216        ResetVector (vec[i], OPIHI_FLT, NELEM);
     217        break;
     218      case COLTYPE_STR:
     219        ResetVector (vec[i], OPIHI_STR, NELEM);
    215220        break;
    216221      case COLTYPE_CHAR:
     
    311316              break;
    312317            }
     318          case COLTYPE_STR:
     319            {
     320              // I need to get an isolated word in 'value' with the string value of this field
     321              char *ptr = IsCSV ? ptrparse_csv (col[i], c0) : ptrparse (col[i], c0);
     322              char *value = NULL;
     323              if (IsCSV) {
     324                char *end = parse_nextword_csv (ptr);
     325                if (end) {
     326                  value = end ? strncreate (ptr, end - ptr) : strcreate (ptr);
     327                }
     328              } else {
     329                value = thisword(ptr);
     330              }
     331              vec[i][0].elements.Str[Nelem] = value; // needs to be freed later.
     332              break;
     333            }
    313334          case COLTYPE_FLT:
    314335            readStatus = IsCSV ? dparse_csv (&dvalue, col[i], c0) : dparse (&dvalue, col[i], c0);
     
    348369              ResetVector (vec[i], OPIHI_FLT, NELEM);
    349370              break;
     371            case COLTYPE_STR:
     372              ResetVector (vec[i], OPIHI_STR, NELEM);
     373              break;
    350374            case COLTYPE_CHAR:
    351375            default:
     
    369393        ResetVector (vec[i], OPIHI_FLT, Nelem);
    370394        break;
     395      case COLTYPE_STR:
     396        ResetVector (vec[i], OPIHI_STR, Nelem);
     397        break;
    371398      case COLTYPE_CHAR:
    372399        sprintf (varname, "%s:n", listname[i]);
     
    469496  }
    470497
    471   // if CharAsVectors, char fields will be saved as vectors NAME:0 -- NAME:n for n characters
    472   // else char fields will be saved as $NAME:0 - $NAME:m for m rows
     498  // by default, we now store a string-type field as a string-type vector.
     499
     500  // if CharAsList is selected (and My < 10000), char fields will be saved as $NAME:0 - $NAME:m for m rows
     501
     502  // if CharAsVectors is selected, char fields will be saved as vectors NAME:0 -- NAME:n for n characters
     503
    473504  // if (Ny > 10000), force CharAsVectors
    474505  int CharAsVectors = FALSE;
     
    476507    remove_argument (N, &argc, argv);
    477508    CharAsVectors = TRUE;
     509  }
     510  int CharAsList = FALSE;
     511  if ((N = get_argument (argc, argv, "-char-list"))) {
     512    remove_argument (N, &argc, argv);
     513    CharAsList = TRUE;
    478514  }
    479515
     
    651687    if (!FITS_TRANSPOSE) {
    652688      // read string column into a list rather than a vector
    653       if (!strcmp (type, "char") && !CharAsVectors && (Ny < 3000)) {
    654         char *fieldName = argv[i];
    655         char *Ptr = data;
    656         char varname[1024];  // used as a buffer for the names of string fields
    657         for (j = 0; j < Ny; j++) {
    658           set_list_varname (varname, fieldName, j, FALSE);
    659           char *value = strncreate (&Ptr[j*Nval], Nval);
    660           // replace instances of $ with _
    661           char *p = strchr (value, '$');
    662           while (p) {
    663             *p = '_';
    664             p = strchr (p, '$');
     689      if (!strcmp (type, "char")) {
     690        // save char-type field as a List:
     691        if (CharAsList && (Ny < 3000)) {
     692          char *fieldName = argv[i];
     693          char *Ptr = data;
     694          char varname[1024];  // used as a buffer for the names of string fields
     695          for (j = 0; j < Ny; j++) {
     696            set_list_varname (varname, fieldName, j, FALSE);
     697            char *value = strncreate (&Ptr[j*Nval], Nval);
     698            // replace instances of $ with _
     699            char *p = strchr (value, '$');
     700            while (p) {
     701              *p = '_';
     702              p = strchr (p, '$');
     703            }
     704            set_str_variable (varname, value);
     705            free (value);
    665706          }
    666           set_str_variable (varname, value);
    667           free (value);
     707          sprintf (varname, "%s:n", fieldName);
     708          set_int_variable (varname, Ny);
     709          continue;
    668710        }
    669         sprintf (varname, "%s:n", fieldName);
    670         set_int_variable (varname, Ny);
    671         continue;
     711        // save char-type field as a string-type vector:
     712        if (!CharAsVectors) {
     713          Vector *myVector = NULL;
     714          sprintf (name, "%s", argv[i]);
     715          if ((myVector = SelectVector (name, ANYVECTOR, TRUE)) == NULL) ESCAPE ("bad vector name\n");
     716          ResetVector (myVector, OPIHI_STR, Ny);
     717
     718          char *Ptr = data;
     719          for (j = 0; j < Ny; j++) {
     720            myVector[0].elements.Str[j] = strncreate (&Ptr[j*Nval], Nval);
     721            // replace instances of $ with _
     722            char *p = strchr (myVector[0].elements.Str[j], '$');
     723            while (p) { *p = '_'; p = strchr (p, '$'); }
     724          }
     725          continue;
     726        }
    672727      }
    673728
    674729      // define the multifield vector names (Nval vectors x Ny elements)
     730      // CharAsVectors is handled below automatically
    675731      ALLOCATE (vec, Vector *, Nval);
    676732      for (j = 0; j < Nval; j++) {
Note: See TracChangeset for help on using the changeset viewer.