Exemplo n.º 1
0
int main(int argc, char **argv)
{
  FILE *outfile;
  SDDS_TABLE SDDS_table;
  SDDS_LAYOUT *layout;
  COLUMN_DEFINITION *coldef;
  PARAMETER_DEFINITION *pardef;
  ARRAY_DEFINITION *arraydef;
  long list_request;
  char *input, *output;
  long i, j, i_arg, append_units, nrows, ntable;
  SCANNED_ARG *s_arg;
  char *text, *contents, *delimiter;
  long verbose=0, all=0, nvariableparms=0, excel=0, line=0;
  void *data;
  unsigned long pipeFlags;
#ifdef USE_XLS
  workbook *w;
  worksheet *ws;
#endif
  int ret;
  char sheet[256];
  char buffer[5];

  SDDS_RegisterProgramName(argv[0]);
  
  list_request = -1;
  input = output = NULL;
  append_units = 0;
  delimiter = DELIMITER;
  pipeFlags = 0;
  
  argc = scanargs(&s_arg, argc, argv);
  if (argc==1)
    bomb(NULL, USAGE);
  
  for (i_arg=1; i_arg<argc; i_arg++) {
    if (s_arg[i_arg].arg_type==OPTION) {
      switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
      case SET_DELIMITER:
	if (s_arg[i_arg].n_items<2)
	  SDDS_Bomb("Invalid -delimiter syntax");
	delimiter = s_arg[i_arg].list[1];
	break;
      case SET_ALL:
	all = 1;
	break;
      case SET_EXCEL:
#ifdef USE_XLS
	excel = 1;
#else
        SDDS_Bomb("-excel option is not available becuase sdds2spreadsheet was not compiled with xlslib support");
#endif
	break;
      case SET_VERBOSE:
	verbose = 1;
	break;
      case SET_PIPE:
	if (!processPipeOption(s_arg[i_arg].list+1, 
			       s_arg[i_arg].n_items-1, &pipeFlags))
	  SDDS_Bomb("invalid -pipe syntax");
	break;
      default:
	SDDS_Bomb(strcat("Unknown option: ", s_arg[i_arg].list[0]));
	break;
      }
    }
    else {
      if (input==NULL)
	input = s_arg[i_arg].list[0];
      else if (output==NULL)
	output = s_arg[i_arg].list[0];
      else
	SDDS_Bomb("too many filenames");
    }
  }
  
  
  processFilenames("sdds2spreadsheet", &input, &output, pipeFlags, 0, NULL);
  
  if (output) {
    if (!excel) {
      outfile=fopen(output,"w");
      if(!outfile)
        SDDS_Bomb(strcat("Can't open output file ", output));
    }
  }
  else {
    if (excel) {
      SDDS_Bomb("-pipe=out and -excel options cannot be used together");
    }
    outfile = stdout;
  }
  
  if (input && !excel)
    fprintf(outfile,"Created from SDDS file: %s\n", input);
  if (!SDDS_InitializeInput(&SDDS_table, input)) {
    SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
    exit(1);
  }
  layout = &SDDS_table.layout;
  /* Description */    
  if(verbose && input) 
    fprintf(stderr, "\nfile %s is in SDDS protocol version %" PRId32 "\n", 
	    input, layout->version);

  if (!SDDS_GetDescription(&SDDS_table, &text, &contents)) 
    SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
  
  if (text) 
    if (verbose) 
      fprintf(stderr, "description: %s\n", text);

  if (!excel)
    fprintf(outfile,"%s%s\n",text?text:"No description",delimiter);
  
  if (contents)
    if (verbose) 
      fprintf(stderr, "contents: %s\n", contents);

  if (!excel)
    fprintf(outfile,"%s%s\n",contents?contents:"No description",delimiter);
  
  if (layout->data_mode.mode==SDDS_ASCII) {
    if (verbose) {
      fprintf(stderr, "\ndata is ASCII with %" PRId32 " lines per row and %" PRId32 " additional header lines expected.\n",
	      layout->data_mode.lines_per_row, layout->data_mode.additional_header_lines);
      fprintf(stderr, "row counts: %s    \n",
	      layout->data_mode.no_row_counts?"no":"yes");
    }
  } 
  else if (verbose) 
    fprintf(stderr, "\ndata is binary\n");

  /* Columns */    
  if (layout->n_columns) {
    if(verbose) {
      fprintf(stderr, "\n%" PRId32 " columns of data:\n", layout->n_columns);
      fprintf(stderr, "NAME            UNITS           SYMBOL          FORMAT          TYPE    FIELD  DESCRIPTION\n");
      fprintf(stderr, "                                                                        LENGTH\n");
    }
    if (all && !excel) 
      fprintf(outfile,
	      "\nColumns%s\nName%sUnits%sSymbol%sFormat%sType%sField Length%sDescription%s\n",
	      delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, 
	      delimiter);
    for (i=0; i<layout->n_columns; i++) {
      coldef = layout->column_definition+i;
      if(verbose) 
	fprintf(stderr, "%-15s %-15s %-15s %-15s %-7s %-7" PRId32 " %s\n",
		coldef->name, coldef->units?coldef->units:"", 
                coldef->symbol?coldef->symbol:"",
		coldef->format_string?coldef->format_string:"", 
                SDDS_type_name[coldef->type-1], 
                coldef->field_length,  
		coldef->description?coldef->description:"");
      if(all && !excel) 
	fprintf(outfile,"%s%s%s%s%s%s%s%s%s%s%-7" PRId32 "%s%s%s\n",
		coldef->name, delimiter,
		coldef->units?coldef->units:"", delimiter,
		coldef->symbol?coldef->symbol:"", delimiter,
		coldef->format_string?coldef->format_string:"", delimiter,
		SDDS_type_name[coldef->type-1], delimiter,
		coldef->field_length,  delimiter,
		coldef->description?coldef->description:"", delimiter);
    }
  }
  /* Parameters */    
  if (layout->n_parameters) {
    if (verbose) {
      fprintf(stderr, "\n%" PRId32 " parameters:\n", layout->n_parameters);
      fprintf(stderr, 
	      "NAME                UNITS               SYMBOL              TYPE                DESCRIPTION\n");
    }
    if(all && !excel) 
      fprintf(outfile,"\nParameters%s\nName%sFixedValue%sUnits%sSymbol%sType%sDescription%s\n",
	      delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter);
    for (i=0; i<layout->n_parameters; i++) {
      pardef = layout->parameter_definition+i;
      if(!pardef->fixed_value) {
	nvariableparms++;
	if(!all) continue;
      }
      if(verbose) fprintf(stderr, "%-19s %-19s %-19s %-19s %s\n",
			  pardef->name, pardef->units?pardef->units:"", pardef->symbol?pardef->symbol:"", 
			  SDDS_type_name[pardef->type-1], pardef->description?pardef->description:"");
      if (!excel) {
        if(all) fprintf(outfile,"%s%s%s%s%s%s%s%s%s%s%s%s\n",
                        pardef->name, delimiter,
                        pardef->fixed_value?pardef->fixed_value:"", delimiter,
                        pardef->units?pardef->units:"", delimiter,
                        pardef->symbol?pardef->symbol:"", delimiter,
                        SDDS_type_name[pardef->type-1], delimiter,
                        pardef->description?pardef->description:"", delimiter);
        else fprintf(outfile,"%s%s%s%s%s\n",
                     pardef->name, delimiter, delimiter,
                     pardef->fixed_value?pardef->fixed_value:"", delimiter);
      }
    }
  }
  /* Arrays */    
  if (layout->n_arrays && all) {
    if(verbose) {
      fprintf(stderr, "\n%" PRId32 " arrays of data:\n", layout->n_arrays);
      fprintf(stderr, "NAME            UNITS           SYMBOL"
	      "          FORMAT  TYPE            FIELD   GROUP           DESCRIPTION\n");
      fprintf(stderr, "                                                                        LENGTH  NAME\n");
    }
    if (!excel) {
      fprintf(outfile,"\nArrays%s\nName%sUnits%sSymbol%sFormat%sType%sField Length%sGroup Name%sDescription%s\n", delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter, delimiter);
    }
    for (i=0; i<layout->n_arrays; i++) {
      arraydef = layout->array_definition+i;
      if(verbose) fprintf(stderr, "%-15s %-15s %-15s %-7s %-8s*^%-5" PRId32 " %-7" PRId32 " %-15s %s\n",
			  arraydef->name, arraydef->units, arraydef->symbol,
			  arraydef->format_string, SDDS_type_name[arraydef->type-1], arraydef->dimensions,
			  arraydef->field_length,  arraydef->group_name, arraydef->description);
      if (!excel) {
        fprintf(outfile,"%s%s%s%s%s%s%s%s%s*^%-5" PRId32 "%s%-7" PRId32 "%s%s%s%s%s\n",
                arraydef->name, delimiter,
                arraydef->units, delimiter,
                arraydef->symbol, delimiter,
                arraydef->format_string, delimiter,
                SDDS_type_name[arraydef->type-1], arraydef->dimensions, delimiter,
                arraydef->field_length,  delimiter,
                arraydef->group_name, delimiter,
                arraydef->description, delimiter);
      }
    }
  }
  /* Associates */    
  /*
    if (layout->n_associates && all) {
    if(verbose) {
            fprintf(stderr, "\n%ld associates:\n", layout->n_associates);
            fprintf(stderr, "SDDS  FILENAME            PATH                          CONTENTS            DESCRIPTION\n");
            }
        fprintf(outfile,"\nAssociates%s\nType%sFilename%sPath%sContents%sDescription%s\n",
	  delimiter, delimiter, delimiter, delimiter, delimiter, delimiter);
	for (i=0; i<layout->n_associates; i++)
          if(verbose) fprintf(stderr, "%-5s %-19s %-29s %-19s %s\n",
	    layout->associate_definition[i].sdds?"SDDS":"Non-SDDS",
	    layout->associate_definition[i].filename, layout->associate_definition[i].path,
	    layout->associate_definition[i].contents, layout->associate_definition[i].description);
	  fprintf(outfile,"%s%s%s%s%s%s%s%s%s%s\n",
	    layout->associate_definition[i].sdds?"yes":"no", delimiter,
	    layout->associate_definition[i].filename, delimiter,
	    layout->associate_definition[i].path, delimiter,
	    layout->associate_definition[i].contents, delimiter,
	    layout->associate_definition[i].description, delimiter);
    }
*/

#ifdef USE_XLS
  w = xlsNewWorkbook();
#ifdef __APPLE__
  xlsWorkbookIconvInType(w, "UCS-4-INTERNAL");
#endif
#endif
/* Process tables */    
  while ((ntable=SDDS_ReadTable(&SDDS_table))>0) {
    line=0;
#ifdef USE_XLS
    if (excel) {
      sprintf(sheet, "Sheet%d", ntable);
      ws = xlsWorkbookSheet(w, sheet);
    } else
      fprintf(outfile,"\nTable %ld\n",ntable);
#else
    fprintf(outfile,"\nTable %ld\n",ntable);  
#endif
    /* Variable parameters */
    if (nvariableparms) {
      for (i=0; i<layout->n_parameters; i++) {
	pardef = layout->parameter_definition+i;
	if(pardef->fixed_value) continue;
	if (!(data=SDDS_GetParameter(&SDDS_table, pardef->name, NULL))) {
	  SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
	  exit(1);
	}
#ifdef USE_XLS
        if (excel) {
          xlsWorksheetLabel(ws, line, 0, pardef->name, NULL);
          switch(pardef->type) {
          case SDDS_DOUBLE:
            xlsWorksheetNumberDbl(ws, line, 1, *((double*)data), NULL);
            break;
          case SDDS_FLOAT:
            xlsWorksheetNumberDbl(ws, line, 1, *((float*)data), NULL);  
            break;
          case SDDS_ULONG:
            xlsWorksheetNumberInt(ws, line, 1, *((uint32_t*)data), NULL);  
            break;
          case SDDS_LONG:
            xlsWorksheetNumberInt(ws, line, 1, *((int32_t*)data), NULL);  
            break;
          case SDDS_USHORT:
            xlsWorksheetNumberInt(ws, line, 1, *((unsigned short*)data), NULL);  
            break;
          case SDDS_SHORT:
            xlsWorksheetNumberInt(ws, line, 1, *((short*)data), NULL);  
            break;
          case SDDS_STRING:
            xlsWorksheetLabel(ws, line, 1, *((char**)data), NULL);
            break;
          case SDDS_CHARACTER:
            sprintf(buffer, "%c", *((char*)data));
            xlsWorksheetLabel(ws, line, 1, buffer, NULL);
            break;
          default:
            break;
          }

          line++;
        } else {
#endif
          fprintf(outfile,"%s%s%s",pardef->name,delimiter,delimiter);
          SDDS_PrintTypedValue(data, 0, pardef->type, NULL, outfile, 0);
          fprintf(outfile,"%s\n",delimiter);
#ifdef USE_XLS
        }
#endif
      }
      line++;
    }
    /* Columns */
    if (layout->n_columns) {
      SDDS_SetColumnFlags(&SDDS_table, 1);
      SDDS_SetRowFlags(&SDDS_table, 1);
      if ((nrows=SDDS_CountRowsOfInterest(&SDDS_table))<0) {
	SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
	exit(1);
      }
      for (i=0; i<layout->n_columns; i++) {
	coldef = layout->column_definition+i;
#ifdef USE_XLS
        if (excel) {
          xlsWorksheetLabel(ws, line, i, coldef->name, NULL);
        }
        else
          fprintf(outfile,"%s%s",coldef->name,delimiter);
#else
        fprintf(outfile,"%s%s",coldef->name,delimiter);
#endif
      }
      line++;
      if (!excel)
        fprintf(outfile,"\n");
      if (nrows) {
	for (j=0; j<nrows; j++) {
	  for (i=0; i<layout->n_columns; i++) {
	    coldef = layout->column_definition+i;
	    if (!(data=SDDS_GetValue(&SDDS_table, coldef->name,j,NULL))) {
	      SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
	      exit(1);
	    }
#ifdef USE_XLS
            if (excel) {
              switch(coldef->type) {
              case SDDS_DOUBLE:
                xlsWorksheetNumberDbl(ws, line, i, *((double*)data), NULL);  
                break;
              case SDDS_FLOAT:
                xlsWorksheetNumberDbl(ws, line, i, *((float*)data), NULL);  
                break;
              case SDDS_ULONG:
                xlsWorksheetNumberInt(ws, line, i, *((uint32_t*)data), NULL);  
                break;
              case SDDS_LONG:
                xlsWorksheetNumberInt(ws, line, i, *((int32_t*)data), NULL);  
                break;
              case SDDS_USHORT:
                xlsWorksheetNumberInt(ws, line, i, *((unsigned short*)data), NULL);  
                break;
              case SDDS_SHORT:
                xlsWorksheetNumberInt(ws, line, i, *((short*)data), NULL);  
                break;
              case SDDS_STRING:
                xlsWorksheetLabel(ws, line, i, *((char**)data), NULL);
                break;
              case SDDS_CHARACTER:
                sprintf(buffer, "%c", *((char*)data));
                xlsWorksheetLabel(ws, line, i, buffer, NULL);
                break;
              default:
                break;
              }
            } else {
#endif
              switch(coldef->type) {
              case SDDS_DOUBLE:
                fprintf(outfile, "%.*g", DBL_DIG, *((double*)data));
                break;
              case SDDS_FLOAT:
                fprintf(outfile, "%.*g", FLT_DIG, *((float*)data));
                break;
              default:
                SDDS_PrintTypedValue(data, 0, coldef->type, NULL, outfile, 0);
                break;
              }
              fprintf(outfile,delimiter);
#ifdef USE_XLS
            }
#endif
	  }
          if (!excel)
            fprintf(outfile,"\n");
          line++;
	}
      }
    }
  }
#ifdef USE_XLS
  ret = xlsWorkbookDump(w, output);
  xlsDeleteWorkbook(w);
#endif

  /*	
	QUIT:
  */
  fflush(stdout);
  if (!SDDS_Terminate(&SDDS_table)) {
    SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
    exit(1);
  }
  return(0);
}
Exemplo n.º 2
0
/**************************** main ****************************************/
int main(int argc, char **argv)
{
  FILE *outfile;
  SDDS_TABLE SDDS_table;
  SDDS_LAYOUT *layout;
  COLUMN_DEFINITION *coldef;
  PARAMETER_DEFINITION *pardef;
  ARRAY_DEFINITION *arraydef;
  long list_request;
  char s[256];
  char *input, *output;
  long i, j, i_arg, append_units, nrows, ntable, len;
  SCANNED_ARG *s_arg;
  char *text, *contents, *ss, *ptr, *iformat=FORMAT, *format, *rformat;
  long verbose=0, comments=0, addquotes=1;
  short nexp;
  double dd, ddred;
  float ff, ffred;
  void *data;
  unsigned long pipeFlags;
  
  SDDS_RegisterProgramName(argv[0]);
  
  list_request = -1;
  input = output = NULL;
  append_units = 0;
  pipeFlags = 0;
  
  argc = scanargs(&s_arg, argc, argv);
  if (argc==1)
    bomb(NULL, USAGE);
  
  for (i_arg=1; i_arg<argc; i_arg++) {
    if (s_arg[i_arg].arg_type==OPTION) {
      switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
      case SET_COMMENTS:
	comments = 1;
	break;
      case SET_FORMAT:
	if (s_arg[i_arg].n_items<2)
	  SDDS_Bomb("Invalid -format syntax");
	iformat = s_arg[i_arg].list[1];     /* Input format */
	break;
      case SET_VERBOSE:
	verbose = 1;
	break;
      case SET_PIPE:
	if (!processPipeOption(s_arg[i_arg].list+1, 
			       s_arg[i_arg].n_items-1, &pipeFlags))
	  SDDS_Bomb("invalid -pipe syntax");
	break;
      default:
	SDDS_Bomb(strcat("Unknown option: ", s_arg[i_arg].list[0]));
	break;
      }
    }
    else {
      if (input==NULL)
	input = s_arg[i_arg].list[0];
      else if (output==NULL)
	output = s_arg[i_arg].list[0];
      else
	SDDS_Bomb("too many filenames");
    }
  }
  
  processFilenames("sdds2math", &input, &output, pipeFlags, 0, NULL);
  
  if (output) {
    outfile=fopen(output,"w");
    if(!outfile)
      SDDS_Bomb(strcat("Can't open output file ", output));
  }
  else {
    outfile = stdout;
  }
  
  
  /* Calculate formats for converting to Mathematica convention */    
  len=strlen(iformat);     /* Inputformat */
  format=(char *)calloc(256,sizeof(char));     /* Whole format */
  rformat=(char *)calloc(256,sizeof(char));    /* Part before e */
  strcpy(format,iformat);
  if((ptr=strchr(format,'E'))) *ptr='e';     /* Aren't going to use E or G anyway */
  if((ptr=strchr(format,'G'))) *ptr='g';
  strcpy(rformat,format);
  if((ptr=strpbrk(rformat,"eg"))) *ptr='f';
  
  /*     fprintf(outfile,"Created from SDDS file: %s\n",filename[file]); */
  if (!SDDS_InitializeInput(&SDDS_table, input)) {
    SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
    exit(1);
  }
  layout = &SDDS_table.layout;
  /* Start top level */
  fprintf(outfile,"{");
  /* Description */    
  fprintf(outfile,"{");
  if(verbose) printf("\nfile %s is in SDDS protocol version %" PRId32 "\n", input, layout->version);
  if (!SDDS_GetDescription(&SDDS_table, &text, &contents)) 
    SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
  if (text)
    if(verbose) printf("description: %s\n", text);
  if (text)
    fprintf(outfile,"\"%s\",",text?text:"No description\t");
  if (contents)
    if(verbose) printf("contents: %s\n", contents);
  fprintf(outfile,"\"%s\"",contents?contents:"No description\t");
  if (layout->data_mode.mode==SDDS_ASCII) {
    if(verbose) printf("\ndata is ASCII with %" PRId32 " lines per row and %" PRId32 " additional header lines expected.\n",
		       layout->data_mode.lines_per_row, layout->data_mode.additional_header_lines);
    if(verbose) printf("row counts: %s \n",
		       layout->data_mode.no_row_counts?"no":"yes");
  }
  else
    if(verbose) printf("\ndata is binary\n");
  fprintf(outfile,"},\n");
  /* Columns */    
  fprintf(outfile," {");
  if (layout->n_columns) {
    if(verbose) printf("\n%" PRId32 " columns of data:\n", layout->n_columns);
    if(verbose) printf("NAME            UNITS           SYMBOL          FORMAT          TYPE    FIELD  DESCRIPTION\n");
    if(verbose) printf("                                                                        LENGTH\n");
    for (i=0; i<layout->n_columns; i++) {
      if(i > 0) fprintf(outfile,",\n  ");
      coldef = layout->column_definition+i;
      if(verbose) printf("%-15s %-15s %-15s %-15s %-7s %-7" PRId32 " %s\n",
			 coldef->name, coldef->units, coldef->symbol,
			 coldef->format_string, SDDS_type_name[coldef->type-1], coldef->field_length,  coldef->description);
      fprintf(outfile,"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%" PRId32 ",\"%s\"}",
	      coldef->name?coldef->name:"No name",
	      coldef->units?coldef->units:"",
	      coldef->symbol?coldef->symbol:"",
	      coldef->format_string?coldef->format_string:"",
	      SDDS_type_name[coldef->type-1],
	      coldef->field_length,
	      coldef->description?coldef->description:"No description");
    }
  }
  fprintf(outfile,"},\n");
  /* Parameters */    
  fprintf(outfile," {");
  if (layout->n_parameters) {
    if(verbose) printf("\n%" PRId32 " parameters:\n", layout->n_parameters);
    if(verbose) printf("NAME                UNITS               SYMBOL              TYPE                DESCRIPTION\n");
    for (i=0; i < layout->n_parameters; i++) {
      if(i > 0) fprintf(outfile,",\n  ");
      pardef = layout->parameter_definition+i;
      if(verbose) printf("%-19s %-19s %-19s %-19s %s\n",
			 pardef->name, pardef->units, pardef->symbol, 
			 SDDS_type_name[pardef->type-1], pardef->description);
      fprintf(outfile,"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"}",
	      pardef->name?pardef->name:"No name",
	      pardef->fixed_value?pardef->units:"",
	      pardef->units?pardef->units:"",
	      pardef->symbol?pardef->symbol:"",
	      SDDS_type_name[pardef->type-1],
	      pardef->description?pardef->description:"No description");
    }
  }
  fprintf(outfile,"},\n");
  /* Arrays */    
  fprintf(outfile," {");
  if (layout->n_arrays) {
    if(verbose) printf("\n%" PRId32 " arrays of data:\n", layout->n_arrays);
    if(verbose) printf("NAME            UNITS           SYMBOL"
		       "          FORMAT  TYPE            FIELD   GROUP           DESCRIPTION\n");
    if(verbose) printf("                                                                        LENGTH  NAME\n");
    for (i=0; i<layout->n_arrays; i++) {
      if(i > 0) fprintf(outfile,",\n  ");
      arraydef = layout->array_definition+i;
      if(verbose) printf("%-15s %-15s %-15s %-7s %-8s*^%-5" PRId32 " %-7" PRId32 " %-15s %s\n",
			 arraydef->name, arraydef->units, arraydef->symbol,
			 arraydef->format_string, SDDS_type_name[arraydef->type-1], arraydef->dimensions,
			 arraydef->field_length,  arraydef->group_name, arraydef->description);
      fprintf(outfile,"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s*^%" PRId32 "\",%-7" PRId32 ",\"%s\",\"%s\"}",
	      arraydef->name?arraydef->name:"No name",
	      arraydef->units?arraydef->units:"",
	      arraydef->symbol?arraydef->symbol:"",
	      arraydef->format_string?arraydef->format_string:"",
	      SDDS_type_name[arraydef->type-1], arraydef->dimensions,
	      arraydef->field_length,
	      arraydef->group_name,
	      arraydef->description);
    }
  }
  fprintf(outfile,"},\n");
  /* Associates */    
  fprintf(outfile," {");
  if (layout->n_associates) {
    if(verbose) printf("\n%" PRId32 " associates:\n", layout->n_associates);
    if(verbose) printf("SDDS  FILENAME            PATH                          CONTENTS            DESCRIPTION\n");
    for (i=0; i<layout->n_associates; i++)
      if(i > 0) fprintf(outfile,",\n  ");
    if(verbose) printf("%-5s %-19s %-29s %-19s %s\n",
		       layout->associate_definition[i].sdds?"SDDS":"Non-SDDS",
		       layout->associate_definition[i].filename, layout->associate_definition[i].path,
		       layout->associate_definition[i].contents, layout->associate_definition[i].description);
    fprintf(outfile,"{\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"}",
	    layout->associate_definition[i].sdds?"True":"False",
	    layout->associate_definition[i].filename,
	    layout->associate_definition[i].path,
	    layout->associate_definition[i].contents,
	    layout->associate_definition[i].description);
  }
  fprintf(outfile,"},\n");
  /* Process tables */    
  fprintf(outfile," {");     /* Start of array of tables */
  while ((ntable=SDDS_ReadTable(&SDDS_table))>0) {
    if(ntable > 1) fprintf(outfile,",\n  ");
    if(comments) fprintf(outfile,"(*Table %ld*)",ntable);
    fprintf(outfile,"{\n");     /* Start of this table */
    /* Variable parameters */
    fprintf(outfile,"   {");    /* Start of parameters */
    for (i=0; i<layout->n_parameters; i++) {
      if(i > 0) fprintf(outfile,",\n    ");
      pardef = layout->parameter_definition+i;
      if (!(data=SDDS_GetParameter(&SDDS_table, pardef->name, NULL))) {
	SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
	exit(1);
      }
      /* This parameter */
      if(comments) fprintf(outfile,"(*%s*)",pardef->name);
      addquotes=1;
      switch(pardef->type) {
      case SDDS_DOUBLE:
	dd=*(double *)data;
	sprintf(s,format,dd);
	if((ptr=strchr(s,'e'))) {
	  *ptr=' ';
	  sscanf(s,"%lf %hd",&ddred,&nexp);
	  fprintf(outfile,rformat,ddred);
	  fprintf(outfile,"*10^%d",nexp);
	}
	else {
	  fprintf(outfile,s);
	}
	break;
      case SDDS_FLOAT:
	ff=*(float *)data;
	sprintf(s,format,ff);
	if((ptr=strchr(s,'e'))) {
	  *ptr=' ';
	  sscanf(s,"%f %hd",&ffred,&nexp);
	  fprintf(outfile,rformat,ffred);
	  fprintf(outfile,"*10^%d",nexp);
	}
	else {
	  fprintf(outfile,s);
	}
	break;
      case SDDS_STRING:
	ss=*(char **)data;
	if(*ss == '"') addquotes=0;
	else if(SDDS_StringIsBlank(ss) || SDDS_HasWhitespace(ss)) addquotes=0;
      case SDDS_CHARACTER:
	if(addquotes) fprintf(outfile,"\"");
	SDDS_PrintTypedValue(data, 0, pardef->type, NULL, outfile, 0);
	if(addquotes) fprintf(outfile,"\"");
	break;
      default:
	SDDS_PrintTypedValue(data, 0, pardef->type, NULL, outfile, 0);
	break;
      }
    }
    fprintf(outfile,"},\n");     /* End of parameters */
    /* Columns */
    fprintf(outfile,"   {");     /* Start of data array */
    if (layout->n_columns) {
      SDDS_SetColumnFlags(&SDDS_table, 1);
      SDDS_SetRowFlags(&SDDS_table, 1);
      if ((nrows=SDDS_CountRowsOfInterest(&SDDS_table))<0) {
	SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
	exit(1);
      }
      /* 	    for (i=0; i<layout->n_columns; i++) { */
      /* 		coldef = layout->column_definition+i; */
      /* 		fprintf(outfile,"%s%s",coldef->name,delimiter); */
      /* 	    } */
      /* 	    fprintf(outfile,"\n"); */
      if (nrows) {
	for (j=0; j<nrows; j++) {
	  if(j > 0) fprintf(outfile,",\n    ");
	  fprintf(outfile,"{");     /* Start of row */
	  for (i=0; i<layout->n_columns; i++) {
	    if( i > 0) fprintf(outfile,",");
	    coldef = layout->column_definition+i;
	    if (!(data=SDDS_GetValue(&SDDS_table, coldef->name,j,NULL))) {
	      SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
	      exit(1);
	    }
	    addquotes=1;
	    switch(coldef->type) {
	    case SDDS_DOUBLE:
	      dd=*(double *)data;
	      sprintf(s,format,dd);
	      if((ptr=strchr(s,'e'))) {
		*ptr=' ';
		sscanf(s,"%lf %hd",&ddred,&nexp);
		fprintf(outfile,rformat,ddred);
		fprintf(outfile,"*10^%d",nexp);
	      }
	      else {
		fprintf(outfile,s);
	      }
	      break;
	    case SDDS_FLOAT:
	      ff=*(float *)data;
	      sprintf(s,format,ff);
	      if((ptr=strchr(s,'e'))) {
		*ptr=' ';
		sscanf(s,"%f %hd",&ffred,&nexp);
		fprintf(outfile,rformat,ffred);
		fprintf(outfile,"*10^%d",nexp);
	      }
	      else {
		fprintf(outfile,s);
	      }
	      break;
	    case SDDS_STRING:
	      ss=*(char **)data;
	      if(*ss == '"') addquotes=0;
	      else if(SDDS_StringIsBlank(ss) || SDDS_HasWhitespace(ss)) addquotes=0;
	    case SDDS_CHARACTER:
	      if(addquotes) fprintf(outfile,"\"");
	      SDDS_PrintTypedValue(data, 0, coldef->type, NULL, outfile, 0);
	      if(addquotes) fprintf(outfile,"\"");
	      break;
	    default:
	      SDDS_PrintTypedValue(data, 0, coldef->type, NULL, outfile, 0);
	      break;
	    }
	  }
	  fprintf(outfile,"}");     /* End of row */
	}
      }
    }
    fprintf(outfile,"}");     /* End of data array */
    fprintf(outfile,"}");     /* End of this table */
  }
  fprintf(outfile,"\n }\n");     /* End of array of tables, last major component */
  /* End top level */    
  fprintf(outfile,"}\n");     /* End of top level */
  /*    
	QUIT:
  */
  fflush(stdout);
  if (!SDDS_Terminate(&SDDS_table)) {
    SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
    exit(1);
  }
  return(0);
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
  SCANNED_ARG *s_arg;
  SDDS_DATASET inputPage, outputPage;
  
  char *inputfile, *outputfile;
  char **inputColumnName, **inputStringColumnName, **inputDoubleColumnName;
  char **outputStringColumnName, **outputDoubleColumnName, **matchColumn=NULL;
  long inputRows, inputDoubleColumns, inputStringColumns, indexColumn=0, matchColumns=0, noOldColumnNamesColumn=0;
  long outputRows, outputDoubleColumns, outputStringColumns;
  char **inputParameterName;
  int32_t inputParameters, inputColumns;
  char *inputDescription, *inputContents;
  char *outputDescription;
  long i, i_arg, col;
  char *buffer;
  char **columnOfStrings;
  long buffer_size;
#define BUFFER_SIZE_INCREMENT 16384
  MATRIX *R, *RInv;
  long OldStringColumnsDefined;
  char *inputStringRows, *outputStringRows;
  char **stringArray, *stringParameter;
  long token_length;
  long verbose;
  char format[32];
  long digits;
  char *Symbol, *Root;
  void *parameterPointer;
  long ascii;
  unsigned long pipeFlags, majorOrderFlag;
  long tmpfile_used, noWarnings;
  long ipage=0, columnType;
  char *oldColumnNames, *newColumnNamesColumn;
  short columnMajorOrder=-1;
  
  inputColumnName = outputStringColumnName = outputDoubleColumnName = inputParameterName = NULL;
  outputRows = outputDoubleColumns = outputStringColumns = OldStringColumnsDefined = 0;

  SDDS_RegisterProgramName(argv[0]);

  argc = scanargs(&s_arg, argc, argv);
  if (argc==1) 
    bomb(NULL,  USAGE);

  inputfile = outputfile = NULL;
  verbose = 0;
  Symbol = Root = NULL;
  ascii = 0;
  digits=3;
  pipeFlags = 0;
  tmpfile_used = 0;
  noWarnings = 0;
  oldColumnNames = NULL;
  newColumnNamesColumn = NULL;
  
  for (i_arg=1; i_arg<argc; i_arg++) {
    if (s_arg[i_arg].arg_type==OPTION) {
      switch(match_string(s_arg[i_arg].list[0], commandline_option, COMMANDLINE_OPTIONS, 
                          UNIQUE_MATCH)) {
      case CLO_MAJOR_ORDER:
        majorOrderFlag=0;
        s_arg[i_arg].n_items--;
        if (s_arg[i_arg].n_items>0 &&
            (!scanItemList(&majorOrderFlag, s_arg[i_arg].list+1, &s_arg[i_arg].n_items, 0,
                           "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
                           "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER,
                           NULL)))
          SDDS_Bomb("invalid -majorOrder syntax/values");
        if (majorOrderFlag&SDDS_COLUMN_MAJOR_ORDER)
          columnMajorOrder=1;
        else if (majorOrderFlag&SDDS_ROW_MAJOR_ORDER)
          columnMajorOrder=0;
        break;
      case CLO_MATCH_COLUMN:
        matchColumns = s_arg[i_arg].n_items-1;
        matchColumn = s_arg[i_arg].list+1;
        break;
      case CLO_INDEX_COLUMN:
        indexColumn = 1;
        break;
      case CLO_NO_OLDCOLUMNNAMES:
        noOldColumnNamesColumn = 1;
        break;
      case CLO_VERBOSE:
        verbose=1;
        break;
      case CLO_ASCII:
        ascii=1;
        break;
      case CLO_DIGITS:
        if (!(get_long(&digits, s_arg[i_arg].list[1])))
          bomb("no string given for option -digits", USAGE);
        break;
      case CLO_COLUMNROOT:
        if (!(Root=s_arg[i_arg].list[1]))
          SDDS_Bomb("No root string given");
        break;
      case CLO_SYMBOL:
        if (!(Symbol=s_arg[i_arg].list[1]))
          SDDS_Bomb("No symbol string given");
        break;
      case CLO_PIPE:
        if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags))
          SDDS_Bomb("invalid -pipe syntax");
        break;
      case CLO_OLDCOLUMNNAMES:
        if (!(oldColumnNames=s_arg[i_arg].list[1]))
          SDDS_Bomb("No oldColumnNames string given");
        break;
      case CLO_NEWCOLUMNNAMES:
        if (s_arg[i_arg].n_items!=2 ||
            SDDS_StringIsBlank(newColumnNamesColumn = s_arg[i_arg].list[1]))
          SDDS_Bomb("Invalid -newColumnNames syntax/value");
        break;
      default:
        bomb("unrecognized option given", USAGE);
      }
    }
    else {
      if (!inputfile)
        inputfile = s_arg[i_arg].list[0];
      else if (!outputfile)
        outputfile = s_arg[i_arg].list[0];
      else
        bomb("too many filenames given", USAGE);
    }
  }

  processFilenames("sddstranpose", &inputfile, &outputfile, pipeFlags, noWarnings, &tmpfile_used);
  if (newColumnNamesColumn && Root) 
    SDDS_Bomb("-root and -newColumnNames are incompatible");
  
  if (!SDDS_InitializeInput(&inputPage, inputfile) ||
      !(inputParameterName=(char**)SDDS_GetParameterNames(&inputPage, &inputParameters)) ||
      !SDDS_GetDescription(&inputPage, &inputDescription, &inputContents))
    SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors);
  if (matchColumns) 
    inputColumnName = getMatchingSDDSNames(&inputPage, matchColumn, matchColumns, &inputColumns, SDDS_MATCH_COLUMN); 
  else {
    if (!(inputColumnName=(char**)SDDS_GetColumnNames(&inputPage, &inputColumns)))
      SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors);
  }
  
  inputDoubleColumns=0;
  inputStringColumns=0;
  inputDoubleColumnName=(char**)malloc(inputColumns*sizeof(char*));
  inputStringColumnName=(char**)malloc(inputColumns*sizeof(char*));
  inputRows = 0;
  /***********                                  \
   * read data *
   \***********/
  while (0<SDDS_ReadTable(&inputPage)) {
    ipage ++;
#if defined(DEBUG)
    fprintf(stderr, "working on page %ld\n", ipage);
#endif
    if (ipage==1) {
      SDDS_DeferSavingLayout(1);
      if( !SDDS_SetColumnFlags(&inputPage, 0))
        SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors);
      /* count the string and numerical columns in the input file */
      for (i=0;i<inputColumns;i++) {
        if ( SDDS_NUMERIC_TYPE( columnType = SDDS_GetColumnType( &inputPage, i))) {
          inputDoubleColumnName[inputDoubleColumns]=inputColumnName[i];
          inputDoubleColumns++;
        }
      }
      for (i=0; i<inputPage.layout.n_columns; i++) {
        if (inputPage.layout.column_definition[i].type == SDDS_STRING ) {
          inputStringColumnName[inputStringColumns] = inputPage.layout.column_definition[i].name;
          inputStringColumns++;
        }
      }
      if( !(inputRows=SDDS_CountRowsOfInterest(&inputPage)))
        SDDS_Bomb("No rows in dataset.");
    }
    else {
      /* these statements are executed on the subsequent pages */
      if (inputRows != SDDS_CountRowsOfInterest(&inputPage)) {
        SDDS_Bomb("Datasets don't have the same number of rows.\nProcessing stopped before reaching the end of the input file.");
      }
    }
#if defined(DEBUG)
    fprintf(stderr, "row flags set\n");
#endif
    if (inputDoubleColumns == 0)
      SDDS_Bomb("No numerical columns in file.");
    if ((ipage==1) && verbose) {
      fprintf(stderr, "No. of double/float/integer columns: %ld.\n", inputDoubleColumns);
      fprintf(stderr, "No. of string columns: %ld.\n", inputStringColumns);
      fprintf(stderr, "No. of rows: %ld.\n", inputRows);
    }
    /****************\
     * transpose data *
     \****************/
    if (inputDoubleColumns) {
      if (ipage == 1) {
        m_alloc(&RInv, inputRows, inputDoubleColumns);
        m_alloc(&R, inputDoubleColumns, inputRows);
      }
      for (col=0;col<inputDoubleColumns;col++){
        if (!(R->a[col]=(double*)SDDS_GetColumnInDoubles(&inputPage, inputDoubleColumnName[col]))) {
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        }
      }
      if (verbose) {
        m_show(R, "%9.6le ", "Transpose of input matrix:\n", stdout);
      }
      m_trans(RInv, R);
    }
    /***************************\
     * determine existence of    *
     * transposed string columns *
     \***************************/
    if (ipage == 1) {
      OldStringColumnsDefined=0;
      switch(SDDS_CheckParameter(&inputPage, OLD_STRING_COLUMN_NAMES, NULL, SDDS_STRING, NULL)){
      case SDDS_CHECK_OKAY:
        OldStringColumnsDefined=1;
        break;
      case SDDS_CHECK_NONEXISTENT:
        break;
      case SDDS_CHECK_WRONGTYPE:
      case SDDS_CHECK_WRONGUNITS:
        fprintf(stderr, "Something wrong with parameter OldStringColumns.\n"); 
        exit(1);
        break;
      }
      if (OldStringColumnsDefined){
        /* decompose OldStringColumns into names of string columns for the output file */
        if (!SDDS_GetParameter(&inputPage, OLD_STRING_COLUMN_NAMES, &inputStringRows))
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        if (verbose) {
          fprintf(stderr, "Parameter OldStringColumns: %s.\n", inputStringRows);
        }
        outputStringColumnName=(char**)malloc(sizeof(char*));
        outputStringColumns=0;
        buffer_size=BUFFER_SIZE_INCREMENT;
        buffer=(char*)malloc(sizeof(char)*buffer_size);
        while ( 0 <= (token_length = SDDS_GetToken(inputStringRows, buffer, BUFFER_SIZE_INCREMENT))){
          if (!token_length)
            SDDS_Bomb("A null string was detected in parameter OldStringColumns.\n");
          if (!SDDS_CopyString(&outputStringColumnName[outputStringColumns], buffer))
            SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
          if (verbose) {
            fprintf(stderr, "Output string column: %s\n", outputStringColumnName[outputStringColumns]);
          }
          outputStringColumns++;
        }
      }
    }
    
    /*********************\
     * define output page *
     \*********************/
    if ( ipage == 1 ) {
      outputRows = inputDoubleColumns;
      outputDoubleColumns = inputRows;
      if (inputDescription){
        outputDescription = (char*) malloc( sizeof(char) * (strlen("Transpose of ") + strlen(inputDescription) + 1));
        strcat(strcpy(outputDescription, "Transpose of "), inputDescription);
        if (!SDDS_InitializeOutput(&outputPage, ascii?SDDS_ASCII:SDDS_BINARY, 1, outputDescription, inputContents, outputfile))
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
      }
      else {
        if (!SDDS_InitializeOutput(&outputPage, ascii?SDDS_ASCII:SDDS_BINARY, 1, NULL, NULL, outputfile))
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
      }    
      if (columnMajorOrder!=-1)
        outputPage.layout.data_mode.column_major = columnMajorOrder;
      else
        outputPage.layout.data_mode.column_major = inputPage.layout.data_mode.column_major;
      /***********************************\
       * define names for double columns *
       \***********************************/
      if (!Root && inputStringColumns ) {
        /* use specified string column, or first string column encountered */
        if (!newColumnNamesColumn)
          /* first string column encountered */
          outputDoubleColumnName = (char**) SDDS_GetColumn(&inputPage, inputStringColumnName[0]);
        else {
          /* use specified string column */
          if (SDDS_CheckColumn(&inputPage, newColumnNamesColumn, NULL, SDDS_STRING, stderr)!=SDDS_CHECK_OKAY)
            SDDS_Bomb("column named with -newColumnNames does not exist in input");
          outputDoubleColumnName = (char**)SDDS_GetColumn(&inputPage, newColumnNamesColumn);
        }
        for (i=1; i<inputRows; i++) {
          
          if (match_string(outputDoubleColumnName[i-1], outputDoubleColumnName+i, inputRows-i, EXACT_MATCH)>=0) {
            fprintf(stderr, "Error, duplicate %s found in input file string column %s, can not be used as output column names\n",  outputDoubleColumnName[i-1], newColumnNamesColumn ? newColumnNamesColumn : inputStringColumnName[0]);
            exit(1);
          }
        }
      }
      else {
        /* use command line options to produce column names in the output file */
        outputDoubleColumnName = (char**) malloc( outputDoubleColumns * sizeof(char*) );
        digits = MAX(digits, log10(inputRows) + 1);
        if (!Root){
          Root = (char*) malloc( sizeof(char) * (strlen("Column")+1) );
          strcpy(Root, "Column");
        }
        if (outputDoubleColumns!=1) {
          for ( i=0; i < outputDoubleColumns; i++){
            outputDoubleColumnName[i] = (char*) malloc( sizeof(char) * (strlen(Root)+digits+1));
            sprintf(format, "%s%%0%ldld", Root, digits);
            sprintf(outputDoubleColumnName[i], format, i);
          }
        }
        else {/* only one row to transpose */
          outputDoubleColumnName[0] = (char*) malloc( sizeof(char) * (strlen(Root)+1));
          strcpy( outputDoubleColumnName[0], Root);
        }
      }
      
      
      /*************************\
       * define string columns *
       \*************************/
      if (OldStringColumnsDefined) {
        if (!SDDS_DefineSimpleColumns(&outputPage, outputStringColumns, 
                                      outputStringColumnName, NULL, SDDS_STRING))
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
      }
      else {
        /* by default there should be at least one string column, that of the old column names. */
        if (!noOldColumnNamesColumn) {
          outputStringColumns = 1;
          outputStringColumnName = (char**) malloc( sizeof(char*));
          if (oldColumnNames) {
            /* commanline option specification */
            outputStringColumnName[0] = oldColumnNames;
          }
          else {
            outputStringColumnName[0] = (char*) malloc( sizeof(char) * (strlen("OldColumnNames") + 1));
            strcpy(outputStringColumnName[0], "OldColumnNames");
          }
          if ( 0 > SDDS_DefineColumn(&outputPage, outputStringColumnName[0], NULL, NULL, NULL, NULL, SDDS_STRING, 0))
            SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        }
      }
      if (indexColumn && !SDDS_DefineSimpleColumn(&outputPage, "Index", NULL, SDDS_LONG))
        SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
      /*************************\
       * define double columns *
       \*************************/
      for ( i=0; i < outputDoubleColumns; i++)
        if (Symbol){
          if (0>SDDS_DefineColumn(&outputPage, outputDoubleColumnName[i], Symbol, NULL, NULL, 
                                  NULL, SDDS_DOUBLE, 0))
            SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        }
        else {
          if (0>SDDS_DefineColumn(&outputPage, outputDoubleColumnName[i], NULL, NULL, NULL, 
                                  NULL, SDDS_DOUBLE, 0))
            SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        }
      /********************************\
       * define string parameters       *
       * i.e. transposed string columns *
       \********************************/
      if ( inputStringColumns>1 ) {
        if (0>SDDS_DefineParameter(&outputPage, OLD_STRING_COLUMN_NAMES, 
                                   NULL, NULL, "Transposed string columns", NULL, SDDS_STRING, 
                                   NULL))
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        for ( i=0; i < inputStringColumns; i++){
          if (0>SDDS_DefineParameter(&outputPage, inputStringColumnName[i], NULL, NULL, "Transposed string column data", NULL, 
                                     SDDS_STRING, NULL))
            SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        }
      }
      
      /*************************\
       * transfer parameters not *
       * associated with old     *
       * string columns          *
       \*************************/
      if (inputParameters) {
        for ( i=0; i < inputParameters; i++) {
          if ( (0 > match_string(inputParameterName[i], outputStringColumnName, outputStringColumns, 0) &&
                strcasecmp(inputParameterName[i], OLD_STRING_COLUMN_NAMES)))
            if ( 0 > SDDS_TransferParameterDefinition(&outputPage, &inputPage, inputParameterName[i], NULL))
              SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        }
      }
      
      /***************\
       * write layout *
       \***************/
      SDDS_DeferSavingLayout(0);
      /* if InputFile is not already transfered ot the output file, then create it. */
      switch( SDDS_CheckParameter(&outputPage, "InputFile", NULL, SDDS_STRING, NULL) ) {
      case SDDS_CHECK_NONEXISTENT:
        if (0>SDDS_DefineParameter(&outputPage, "InputFile", NULL, NULL, "Original matrix file", NULL, SDDS_STRING, NULL))
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        break;
      default:
        break;
      }
      if (!SDDS_WriteLayout(&outputPage) )
        SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
    }
#if defined(DEBUG)
    fprintf(stderr, "table layout defined\n");
#endif

    if (!SDDS_StartTable(&outputPage, outputRows) )
      SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
    if (ipage == 1) {
      if (!SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, 
                              "InputFile", inputfile?inputfile:"pipe", NULL))
        SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
    }
    /***************************************\
     * assign string columns    from input *
     * to     string parameters in output  *
     \**************************************/
    if ( inputStringColumns > 1) {
      for ( i=0; i < inputStringColumns; i++){
        columnOfStrings = (char**) SDDS_GetColumn(&inputPage, inputStringColumnName[i]);
        stringParameter = JoinStrings(columnOfStrings, inputRows, BUFFER_SIZE_INCREMENT);
        if ( !SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, 
                                 inputStringColumnName[i], stringParameter, NULL))
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        free(columnOfStrings);
        free(stringParameter);
      }
      outputStringRows = JoinStrings(inputStringColumnName, inputStringColumns, BUFFER_SIZE_INCREMENT);
      if (!SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, 
                              OLD_STRING_COLUMN_NAMES, outputStringRows, NULL))
        SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
    }
    
#if defined(DEBUG)
    fprintf(stderr, "string parameters assigned\n");
#endif
    
    if (inputParameters){
      for ( i=0; i < inputParameters; i++){
        if ( (0 > match_string(inputParameterName[i], outputStringColumnName, outputStringColumns, 0) &&
              strcasecmp(inputParameterName[i], OLD_STRING_COLUMN_NAMES))) {
          parameterPointer = (void*) SDDS_GetParameter(&inputPage, inputParameterName[i], NULL);
          if (!SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, 
                                  inputParameterName[i], parameterPointer, NULL))
            SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
          free(parameterPointer);
        }
      }
    }
#if defined(DEBUG)
    fprintf(stderr, "input parameters assigned\n");
#endif
    
    /**********************************\
     * assign data to                   *
     * output table part of data set    *
     \**********************************/
    if (outputRows) {
      /***************************\
       * assign string column data *
       \***************************/
      if (OldStringColumnsDefined){
        for ( i=0 ; i < outputStringColumns; i++){
          if (!SDDS_GetParameter(&inputPage, outputStringColumnName[i], &stringParameter))
            SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
          stringArray=TokenizeString(stringParameter, outputRows);
          if (!SDDS_SetColumn(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, 
                              stringArray, outputRows, outputStringColumnName[i]))
            SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
        }
      }
      else {
        if (!noOldColumnNamesColumn && !SDDS_SetColumn(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, 
                            inputDoubleColumnName, outputRows, outputStringColumnName[0]))
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
      }
#if defined(DEBUG)
      fprintf(stderr, "string data columns assigned\n");
#endif
      /***************************\
       * assign double column data *
       \***************************/
      for ( i=0 ; i < outputDoubleColumns; i++) /* i is the row index */
        if (!SDDS_SetColumn(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_REFERENCE, 
                            RInv->a[i], outputRows, outputDoubleColumnName[i]))
          SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
      if (indexColumn) {
        for (i=0; i<outputRows; i++)
          if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, i, "Index", i, NULL))
            SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
      }
#if defined(DEBUG)
      fprintf(stderr, "double data columns assigned\n");
#endif
    }
    
#if defined(DEBUG)
    fprintf(stderr, "data assigned\n");
#endif
    if (!SDDS_WriteTable(&outputPage))
      SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
#if defined(DEBUG)
    fprintf(stderr, "data written out\n");
#endif
  }    
  if (inputDoubleColumns) {
    m_free(&RInv);
    m_free(&R);
  }
  if (inputColumnName) {
    SDDS_FreeStringArray(inputColumnName, inputColumns);
    free(inputColumnName);
  }
  if (inputStringColumns)
    free(inputStringColumnName);
  if (inputDescription)
    free(inputDescription);
  if (inputParameterName) {
    SDDS_FreeStringArray(inputParameterName, inputParameters);
    free(inputParameterName);
  }
  if (outputDoubleColumns) {
    SDDS_FreeStringArray(outputDoubleColumnName, outputDoubleColumns);
    free(outputDoubleColumnName);
  }
  
  if (!SDDS_Terminate(&inputPage) || !SDDS_Terminate(&outputPage))
    SDDS_PrintErrors(stdout, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors);
  
  if (tmpfile_used && !replaceFileAndBackUp(inputfile, outputfile)) 
    exit(1);
  return(0);
}
Exemplo n.º 4
0
int main(int argc, char **argv)
{
  SDDS_DATASET SDDSin, SDDSout;
  long i_arg, tmpfileUsed;
  SCANNED_ARG *s_arg;
  char *input, *output, *description_text, *description_contents;
  unsigned long pipeFlags;
  long pageNumber, nonNative = 0;
  char *outputEndianess = NULL;
  char buffer[40];
  
  SDDS_RegisterProgramName(argv[0]);
  argc = scanargs(&s_arg, argc, argv);
  if (argc<2) 
    bomb(NULL, USAGE);

  input = output = description_text = description_contents = NULL;
  pipeFlags = 0;
  for (i_arg=1; i_arg<argc; i_arg++) {
    if (s_arg[i_arg].arg_type==OPTION) {
      delete_chars(s_arg[i_arg].list[0], "_");
      switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
      case SET_PIPE:
        if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags))
          SDDS_Bomb("invalid -pipe syntax");
        break;
      case NONNATIVE:
	nonNative = 1;
        break;
      default:
        fprintf(stderr, "Error (%s): unknown switch: %s\n", argv[0], s_arg[i_arg].list[0]);
        exit(1);
        break;
      }
    }
    else {
      if (input==NULL)
        input = s_arg[i_arg].list[0];
      else if (output==NULL)
        output = s_arg[i_arg].list[0];
      else
        SDDS_Bomb("too many filenames");
    }
  }

  processFilenames("sddsendian", &input, &output, pipeFlags, 0, &tmpfileUsed);

  outputEndianess = getenv("SDDS_OUTPUT_ENDIANESS");

  if (outputEndianess) {
    putenv("SDDS_OUTPUT_ENDIANESS=");
  }

  if (!SDDS_InitializeInput(&SDDSin, input)) {
    SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
    exit(1);
  }
  if (!description_text)
    SDDS_GetDescription(&SDDSin, &description_text, &description_contents);
  if (!SDDS_InitializeCopy(&SDDSout, &SDDSin, output, "w") || 
      !SDDS_SetDataMode(&SDDSout, nonNative?SDDS_BINARY:-SDDS_BINARY) ||
      !SDDS_WriteLayout(&SDDSout)) {
    SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
    exit(1);
  }
  
  if (nonNative) {
    while ((pageNumber=SDDS_ReadNonNativePage(&SDDSin))>=0) {
      if (!SDDS_CopyPage(&SDDSout, &SDDSin) ||
          !SDDS_WritePage(&SDDSout)) {
        SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
        exit(1);
      }
    }
  } else {
    while ((pageNumber=SDDS_ReadPage(&SDDSin))>=0) {
      if (!SDDS_CopyPage(&SDDSout, &SDDSin) ||
          !SDDS_WriteNonNativeBinaryPage(&SDDSout)) {
        SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
        exit(1);
      }
    }
  }

  if (!SDDS_Terminate(&SDDSin) || !SDDS_Terminate(&SDDSout)) {
    SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
    exit(1);
  }
  
  if (tmpfileUsed && !replaceFileAndBackUp(input, output))
    exit(1);

  if (outputEndianess) {
    sprintf(buffer, "SDDS_OUTPUT_ENDIANESS=%s", outputEndianess);
    putenv(buffer);
  }

  return(0);
}