IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39328 for trunk


Ignore:
Timestamp:
Feb 4, 2016, 5:04:49 PM (10 years ago)
Author:
eugene
Message:

add features to get table columns; add help; add -join option to list (opposite of -split)

Location:
trunk/Ohana/src/opihi
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.basic/list.c

    r38972 r39328  
    9393      set_str_variable (line, argv[i+3]);
    9494    }
    95     sprintf (line, "%s:n", argv[1]);
     95    snprintf (line, 1024, "%s:n", argv[1]);
    9696    set_int_variable (line, i);
     97
     98    return (TRUE);
     99  }
     100
     101  // return an error if -add is given with no other args
     102  if ((argc > 2) && (!strcmp (argv[2], "-join"))) {
     103    if (argc != 4) {
     104      gprint (GP_ERR, "USAGE: list (root) -join (variable)\n");
     105      return (FALSE);
     106    }
     107   
     108    int isFound;
     109
     110    snprintf (line, 1024, "%s:n", argv[1]);
     111    int nList = get_int_variable (line, &isFound);
     112    if (!isFound) {
     113      gprint (GP_ERR, "list %s not found\n", argv[1]);
     114      return FALSE;
     115    }
     116
     117    char tmpline[1024], output[1024];
     118    memset (tmpline, 0, 1024);
     119    memset (output, 0, 1024);
     120
     121    for (i = 0; i < nList; i++) {
     122      snprintf (line, 1024, "%s:%d", argv[1], i);
     123      char *word = get_variable (line);
     124      if (!word) break;
     125      if (i > 0) {
     126        snprintf (output, 1024, "%s %s", tmpline, word);
     127      } else {
     128        snprintf (output, 1024, "%s", word);
     129      }
     130      strcpy (tmpline, output);
     131    }
     132    set_str_variable (argv[3], output);
    97133
    98134    return (TRUE);
     
    118154   
    119155    // old list must exist, or give an error
    120     sprintf (line, "%s:n", argv[3]);
     156    snprintf (line, 1024, "%s:n", argv[3]);
    121157    N = get_int_variable (line, &found);
    122158    if (!found) {
     
    127163     
    128164
    129     sprintf (line, "%s:n", argv[1]);
     165    snprintf (line, 1024, "%s:n", argv[1]);
    130166    set_int_variable (line, N);
    131167    for (i = 0; i < N; i++) {
    132       sprintf (line, "%s:%d", argv[3], i);
     168      snprintf (line, 1024, "%s:%d", argv[3], i);
    133169      value = get_variable (line);
    134       // sprintf (line, "%s:%d", argv[1], i);
     170      // snprintf (line, 1024, "%s:%d", argv[1], i);
    135171      set_list_varname (line, argv[1], i, EXCEL_STYLE);
    136172      set_str_variable (line, value);
     
    146182    }
    147183   
    148     sprintf (line, "%s:n", argv[1]);
     184    snprintf (line, 1024, "%s:n", argv[1]);
    149185    N = get_int_variable (line, &found);
    150186    for (i = 0; i < argc - 3; i++) {
    151       // sprintf (line, "%s:%d", argv[1], N + i);
     187      // snprintf (line, 1024, "%s:%d", argv[1], N + i);
    152188      set_list_varname (line, argv[1], N + i, EXCEL_STYLE);
    153189      set_str_variable (line, argv[i+3]);
    154190    }
    155     sprintf (line, "%s:n", argv[1]);
     191    snprintf (line, 1024, "%s:n", argv[1]);
    156192    set_int_variable (line, N + i);
    157193
     
    170206    char line2[1024];
    171207
    172     sprintf (line, "%s:n", argv[1]);
     208    snprintf (line, 1024, "%s:n", argv[1]);
    173209    N = get_int_variable (line, &found);
    174210    for (i = 0; i < N; i++) {
    175       // sprintf (line, "%s:%d", argv[1], i);
     211      // snprintf (line, 1024, "%s:%d", argv[1], i);
    176212      set_list_varname (line, argv[1], i, EXCEL_STYLE);
    177213      value = get_variable (line);
     
    180216        free (value);
    181217        for (j = i + 1; j < N; j++) {
    182           // sprintf (line2, "%s:%d", argv[1], j);
     218          // snprintf (line, 10242, "%s:%d", argv[1], j);
    183219          set_list_varname (line2, argv[1], j, EXCEL_STYLE);
    184220          next_value = get_variable (line2);
     
    187223        }
    188224        DeleteNamedScalar (line);
    189         sprintf (line2, "%s:n", argv[1]);
     225        snprintf (line, 10242, "%s:n", argv[1]);
    190226        set_int_variable (line2, N - 1);
    191227        return (TRUE);
     
    238274      depth --;
    239275      if (depth < 0) { /* we hit the last "END", loop is done */
    240         sprintf (line, "%s:n", argv[1]);
     276        snprintf (line, 1024, "%s:n", argv[1]);
    241277        set_int_variable (line, i);
    242278        free (input);
     
    246282
    247283    if (*input) {
    248       // sprintf (line, "%s:%d", argv[1], i);
     284      // snprintf (line, 1024, "%s:%d", argv[1], i);
    249285      set_list_varname (line, argv[1], i, EXCEL_STYLE);
    250286      set_str_variable (line, input);
     
    310346  free (val);
    311347   
    312   sprintf (line, "%s:n", listname);
     348  snprintf (line, 1024, "%s:n", listname);
    313349  set_int_variable (line, i);
    314350  return (TRUE);
     
    328364  // otherwise save all glob matches
    329365  if (globList.gl_pathc == 0) {
    330     sprintf (line, "%s:n", listname);
     366    snprintf (line, 1024, "%s:n", listname);
    331367    set_int_variable (line, 0);
    332368    return TRUE;
     
    338374    set_str_variable (line, globList.gl_pathv[i]);
    339375  }
    340   sprintf (line, "%s:n", listname);
     376  snprintf (line, 1024, "%s:n", listname);
    341377  set_int_variable (line, Nfile);
    342378  return (TRUE);
     
    371407    FREE (new);
    372408  }
    373   sprintf (line, "%s:n", argv[1]);
     409  snprintf (line, 1024, "%s:n", argv[1]);
    374410  set_int_variable (line, nWords);
    375411
  • trunk/Ohana/src/opihi/cmd.data/read_vectors.c

    r39227 r39328  
    88void read_vectors_cleanup ();
    99int read_table_sizes (Header *header);
     10int read_table_fields (Header *header, int isCompressed, int VERBOSE);
     11void list_extnames (void);
    1012
    1113int datafile (int argc, char **argv) {
     
    8789    gprint (GP_ERR, "       for time, values are human-readable date/time strings YYYY/MM/DD,hh:mm:ss\n");
    8890    gprint (GP_ERR, "       the resulting vector is a float based on the TIMEFORMAT, TIMEREF values\n");
     91
     92    gprint (GP_ERR, "     -fits options:\n");
     93    gprint (GP_ERR, "       -transpose : read rows as columns and vice-versa\n");
     94
     95    gprint (GP_ERR, "       -keyword (FIELD) : use FIELD to identify the extension (default: EXTNAME)\n");
     96    gprint (GP_ERR, "       -pad-if-short : allow reading of broken files by padding missing data\n");
     97    gprint (GP_ERR, "       -sizes : return a list of table size information ($table:Nx, $table:Ny, $table:$Nfields)  \n");
     98    gprint (GP_ERR, "       -list-fields : return a list of table columns ($tfields:0 to $tfields:n) (use -v to see the list)\n");
     99    gprint (GP_ERR, "       -extnum (N) : use the number to find the Nth extension (ignore EXTNAME)\n");
     100    gprint (GP_ERR, "       -range (start) (Nrows) : read the specified portion of the file\n");
     101    gprint (GP_ERR, "        -v : verbose \n");
     102    gprint (GP_ERR, "        -char-vectors : char fields will be saved as vectors NAME:0 -- NAME:n for n characters \n");
     103
    89104    return (FALSE);
    90105  }
     
    393408  }
    394409
     410  int listFields = FALSE;
     411  if ((N = get_argument (argc, argv, "-list-fields"))) {
     412    remove_argument (N, &argc, argv);
     413    listFields = TRUE;
     414  }
     415
    395416  int Nextend = -1;
    396417  if ((N = get_argument (argc, argv, "-extnum"))) {
     
    432453  // }
    433454
    434   if ((argc < 2) && !getSizes) ESCAPE ("USAGE: read -fits extension [-extnum] [-keyword key] name name ...\n");
    435   if ((argc != 1) && getSizes) ESCAPE ("USAGE: read -sizes -fits extension [-extnum] (does not read data values)\n");
     455  int listExtnames = FALSE;
     456  if (!strcmp(extname, "-list")) {
     457    listExtnames = TRUE;
     458  }
     459
     460  if ((argc < 2) && !getSizes && !listFields && !listExtnames) ESCAPE ("USAGE: read -fits extension [-extnum] [-keyword key] name name ...\n");
     461  if ((argc != 1) && getSizes)   ESCAPE ("USAGE: read -sizes -fits extension [-extnum] (does not read data values)\n");
     462  if ((argc != 1) && listFields) ESCAPE ("USAGE: read -list-fields -fits extension [-extnum] (does not read data values)\n");
     463  if ((argc != 1) && listExtnames) ESCAPE ("USAGE: read -list-fields -fits extension [-extnum] (does not read data values)\n");
    436464
    437465  if (f == NULL) ESCAPE ("file not found\n");
    438466  fseeko (f, 0LL, SEEK_SET);
    439467  table.header = &header;
     468
     469  if (listExtnames) {
     470    list_extnames ();
     471    return TRUE;
     472  }
    440473
    441474  /**** find the appropriate extension and read header (if extname is a number, use count) ****/
     
    491524  if (getSizes && !IsCompressed) {
    492525    read_table_sizes (&header);
     526    if (CCDKeyword != NULL) free (CCDKeyword);
     527    gfits_free_header (&header);
     528    return TRUE;
     529  }
     530  if (listFields) {
     531    read_table_fields (&header, IsCompressed, VERBOSE);
    493532    if (CCDKeyword != NULL) free (CCDKeyword);
    494533    gfits_free_header (&header);
     
    676715}
    677716
     717// read -fits foo -sizes -- Nx, Ny, Nfields -> $table:Nx, $table:Ny, $table:$Nfields
     718// read -fits foo -fields
     719
     720int read_table_fields (Header *header, int isCompressed, int VERBOSE) {
     721 
     722  int i, Nfields;
     723  char field[32], varname[32], type[80], comment[80], format[80], unit[80], null[80], nval[80];
     724
     725  gfits_scan (header, "TFIELDS", "%d", 1, &Nfields);
     726
     727  set_int_variable ("table:Nx", header->Naxis[0]);
     728  set_int_variable ("table:Ny", header->Naxis[1]);
     729  set_int_variable ("table:Nfields", Nfields);
     730  set_int_variable ("tfields:n", Nfields);
     731
     732  for (i = 1; i <= Nfields; i++) {
     733    memset (field,   0, 32);
     734    memset (type,    0, 80);
     735    memset (comment, 0, 80);
     736    memset (format,  0, 80);
     737    memset (unit,    0, 80);
     738    memset (null,    0, 80);
     739    memset (nval,    0, 80);
     740
     741    snprintf (field, 32, "TTYPE%d", i);
     742    gfits_scan (header, field, "%s", 1, type);
     743   
     744    snprintf (varname, 32, "tfields:%d", i - 1);
     745    set_str_variable (varname, type);
     746
     747    gfits_scan_alt (header, field, "%C", 1, comment);
     748
     749    if (!isCompressed) {
     750      snprintf (field, 32, "TFORM%d", i);
     751    } else {
     752      snprintf (field, 32, "ZFORM%d", i);
     753    }
     754    gfits_scan (header, field, "%s", 1, format);
     755
     756    snprintf (field, 32, "TUNIT%d", i);
     757    gfits_scan (header, field, "%s", 1, unit);
     758    snprintf (field, 32, "TNULL%d", i);
     759    if (!gfits_scan (header, field, "%s", 1, null)) strcpy (null, "none");
     760    snprintf (field, 32, "TNVAL%d", i);
     761    if (!gfits_scan (header, field, "%s", 1, nval)) strcpy (nval, "none");
     762
     763    if (VERBOSE) gprint (GP_LOG, "%-18s %-18s %-6s %-8s %-8s %-32s\n", type, unit, format, null, nval, comment);
     764  }
     765
     766
     767  return TRUE;
     768}
     769
     770void list_extnames (void) {
     771
     772  off_t Nbytes, Nelem;
     773  int i, Naxis, Ncomp, extend, status;
     774  char extname[82], exttype[82], axisname[32];
     775  Header header;
     776
     777  fseeko (f, 0, SEEK_SET);
     778
     779  gprint (GP_LOG, "%-30s %-15s NAXIS NAXIS(i)...\n", "extname", "datatype");
     780
     781  Ncomp = 0;
     782  while (gfits_fread_header (f, &header)) {
     783    /* extract the EXTNAME for this component (set to PHU for 0th component) */
     784    status = gfits_scan (&header, "EXTNAME", "%s", 1, extname);
     785    if (!status) {
     786      if (Ncomp == 0) {
     787        strcpy (extname, "PHU");
     788      } else {
     789        strcpy (extname, "UNKNOWN");
     790      }
     791    }
     792    gprint (GP_LOG, "%-30s ", extname);
     793
     794    /* extract the datatype for this component (IMAGE for 0th component) */
     795    if (Ncomp == 0) {
     796      strcpy (exttype, "IMAGE");
     797    } else {
     798      status = gfits_scan (&header, "XTENSION", "%s", 1, exttype);
     799      if (!status) {
     800        strcpy (exttype, "UNKNOWN");
     801      }
     802    }
     803    gprint (GP_LOG, "%-15s ", exttype);
     804
     805    /* extract the rank of the component */
     806    status = gfits_scan (&header, "NAXIS",  "%d", 1, &Naxis);
     807    if (!status) {
     808      gprint (GP_ERR, "component %d is missing Naxis!\n", Ncomp);
     809      Ncomp ++;
     810      continue;
     811    }
     812    gprint (GP_LOG, " %4d", Naxis);
     813
     814    /* extract the individual axes */
     815    for (i = 0; i < Naxis; i++) {
     816      sprintf (axisname, "NAXIS%d", i+1);
     817      status = gfits_scan (&header, axisname,  OFF_T_FMT, 1,  &Nelem);
     818      if (!status) {
     819        gprint (GP_ERR, "missing %s\n", axisname);
     820      }
     821      gprint (GP_LOG, " "OFF_T_FMT,  Nelem);
     822    }
     823    gprint (GP_LOG, "\n");
     824
     825    /* are extensions identified? (we will scan for them anyway) */
     826    if (Ncomp == 0) {
     827      extend = FALSE;
     828      gfits_scan_alt (&header, "EXTEND", "%t", 1, &extend);
     829      if (!extend) {
     830        gprint (GP_ERR, "no extensions listed in file\n");
     831      }
     832    }
     833
     834    Nbytes = gfits_data_size (&header);
     835    fseeko (f, Nbytes, SEEK_CUR);
     836
     837    Ncomp ++;
     838  }
     839  return;
     840}
Note: See TracChangeset for help on using the changeset viewer.