int main(int argc, char **argv) { SCANNED_ARG *scanned; SDDS_TABLE inputPage, *copiedPage, outputPage; long copiedPages; char *inputfile, *outputfile; char **column, **excludeColumn=NULL; int32_t columns; long excludeColumns; char *indParameterName; char **copyColumn; int32_t copyColumns; long verbose; long slopeErrors; long iArg,i; double *indVar; char *indVarUnits; char **intColumn,**slopeColumn,**slopeSigmaColumn; char *Units,*slopeUnits; double *depVar; long order; double *coef,*coefsigma,*weight,*diff,chi; long iCol,iRow,iPage; long rows; double *slope, slope2, slopeAve, slopeSigma; unsigned long pipeFlags, majorOrderFlag; long tmpfile_used, noWarnings; long generateIndex; short columnMajorOrder=-1; copiedPage = NULL; slopeSigmaColumn = NULL; slopeUnits = Units = indVarUnits = NULL; rows = 0; slope = NULL; slope2 = 0; coef = coefsigma = weight = diff = slope = NULL; argc = scanargs(&scanned, argc, argv); if (argc == 1) bomb(NULL, USAGE); inputfile = outputfile = NULL; columns = excludeColumns = 0; column = excludeColumn = NULL; indParameterName = NULL; verbose = 0; slopeErrors = 0; pipeFlags = 0; tmpfile_used=0; noWarnings=0; for (iArg = 1; iArg<argc; iArg++) { if (scanned[iArg].arg_type == OPTION) { delete_chars(scanned[iArg].list[0], "_"); switch (match_string(scanned[iArg].list[0], commandline_option, COMMANDLINE_OPTIONS, UNIQUE_MATCH)) { case CLO_MAJOR_ORDER: majorOrderFlag=0; scanned[iArg].n_items--; if (scanned[iArg].n_items>0 && (!scanItemList(&majorOrderFlag, scanned[iArg].list+1, &scanned[iArg].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_INDEPENDENT_PARAMETER: if (!(indParameterName = scanned[iArg].list[1])) SDDS_Bomb("no string given for option -independentVariable"); break; case CLO_COLUMNS: if (columns) SDDS_Bomb("only one -columns option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -columns syntax"); column = tmalloc(sizeof(*column)*(columns = scanned[iArg].n_items-1)); for (i = 0; i<columns; i++) column[i] = scanned[iArg].list[i+1]; break; case CLO_EXCLUDE: if (excludeColumns) SDDS_Bomb("only one -excludecolumns option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -excludecolumns syntax"); excludeColumn = tmalloc(sizeof(*excludeColumn)*(excludeColumns = scanned[iArg].n_items-1)); for (i = 0; i<excludeColumns; i++) excludeColumn[i] = scanned[iArg].list[i+1]; break; case CLO_VERBOSE: verbose = 1; break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_SLOPE_ERRORS: slopeErrors = 1; break; default: SDDS_Bomb("unrecognized option given"); break; } } else { if (!inputfile) inputfile = scanned[iArg].list[0]; else if (!outputfile) outputfile = scanned[iArg].list[0]; else SDDS_Bomb("too many filenames given"); } } processFilenames("sddsvslopes", &inputfile, &outputfile, pipeFlags, noWarnings, &tmpfile_used); if (!indParameterName) SDDS_Bomb("independentVariable not given"); if (!excludeColumns) { excludeColumn = defaultExcludedColumn; excludeColumns = DEFAULT_EXCLUDED_COLUMNS; } if (verbose) fprintf(stderr,"Reading file %s.\n",inputfile); SDDS_InitializeInput(&inputPage, inputfile); copiedPages = 0; while (SDDS_ReadTable(&inputPage)>0) { if (!copiedPages) { copiedPage = (SDDS_TABLE*)malloc(sizeof(SDDS_TABLE)); rows = SDDS_CountRowsOfInterest(&inputPage); } else { copiedPage = (SDDS_TABLE*)realloc(copiedPage, (copiedPages+1)*sizeof(SDDS_TABLE)); } if (!SDDS_InitializeCopy(&copiedPage[copiedPages], &inputPage, NULL, "m")) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); if (!SDDS_CopyTable(&copiedPage[copiedPages], &inputPage)) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); copiedPages++; } if (copiedPages<2) { fprintf(stderr,"Insufficient data (i.e. number of data pages) to fit a straight line.\n"); exit(1); } switch(SDDS_CheckColumn(&inputPage, "Rootname", NULL, SDDS_STRING, NULL)) { case SDDS_CHECK_WRONGUNITS: case SDDS_CHECK_OKAY: break; default: fprintf(stderr,"Something wrong with column %s.\n", "Rootname"); exit(1); } switch(SDDS_CheckColumn(&inputPage, "Index", NULL, SDDS_LONG, NULL)) { case SDDS_CHECK_WRONGUNITS: case SDDS_CHECK_OKAY: generateIndex = 0; break; case SDDS_CHECK_NONEXISTENT: generateIndex = 1; break; default: fprintf(stderr,"Something wrong with column %s.\n", "Rootname"); exit(1); } /****************\ * make array of independent variable \**************/ indVar = (double*)malloc(sizeof(*indVar)*copiedPages); switch(SDDS_CheckParameter(&inputPage, indParameterName, NULL, SDDS_DOUBLE, NULL)) { case SDDS_CHECK_WRONGUNITS: case SDDS_CHECK_OKAY: break; default: fprintf(stderr,"Something wrong with parameter %s.\n", indParameterName); exit(1); } for (iPage = 0; iPage<copiedPages; iPage++) { if (!SDDS_GetParameter(&copiedPage[iPage],indParameterName,&indVar[iPage])) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } if (!SDDS_GetParameterInformation(&inputPage, "units", &indVarUnits, SDDS_GET_BY_NAME, indParameterName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!indVarUnits) { indVarUnits = (char *) malloc(sizeof(*indVarUnits)); indVarUnits[0] = 0; } /************************************\ * get columns of interest. use set_multicolumn_flags to simply * return new values for array column. \*************************************/ if (!set_multicolumn_flags(&inputPage, &column, &columns, excludeColumn, excludeColumns)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } /************************************\ * make column names for the output \*************************************/ intColumn = (char**)malloc((sizeof(char*)*columns)); slopeColumn = (char**)malloc((sizeof(char*)*columns)); if (slopeErrors) slopeSigmaColumn = (char**)malloc((sizeof(char*)*columns)); for (i=0; i<columns; i++) { intColumn[i] = (char*)malloc((sizeof(char)*(strlen(column[i])+strlen("Intercept")+1))); strcat(strcpy(intColumn[i], column[i]), "Intercept"); slopeColumn[i] = (char*)malloc((sizeof(char)*(strlen(column[i])+strlen("Slope")+1))); strcat(strcpy(slopeColumn[i], column[i]), "Slope"); if (slopeErrors) { slopeSigmaColumn[i] = (char*)malloc((sizeof(char)*(strlen(column[i])+strlen("SlopeSigma")+1))); strcat(strcpy(slopeSigmaColumn[i], column[i]), "SlopeSigma"); } } /************************************\ * Write layout for output file \*************************************/ if (verbose) fprintf(stderr,"Opening file %s.\n",outputfile); if(!SDDS_InitializeOutput(&outputPage,SDDS_BINARY,1, "lsf of sddsvexperiment",NULL,outputfile) || 0>SDDS_DefineParameter(&outputPage, "InputFile", "InputFile", NULL, "InputFile", NULL, SDDS_STRING, 0) || 0>SDDS_DefineParameter(&outputPage, "IndependentVariable", "IndependentVariable", NULL, "IndependentVariable", NULL, SDDS_STRING, 0) || (0>SDDS_DefineColumn(&outputPage,"Index",NULL,NULL,"Rootname index",NULL,SDDS_LONG,0))|| (0>SDDS_DefineColumn(&outputPage,"Rootname",NULL,NULL,NULL,NULL,SDDS_STRING,0))) SDDS_PrintErrors(stderr,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; for (iCol=0; iCol<columns; iCol++) { if (!SDDS_GetColumnInformation(&inputPage, "units", &Units, SDDS_GET_BY_NAME,column[iCol])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!Units) { Units = (char*) malloc(sizeof(*Units)); Units[0] = 0; } if (0>SDDS_DefineColumn(&outputPage, intColumn[iCol], NULL, Units, NULL, NULL, SDDS_DOUBLE,0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* units for slopes columns */ if (strlen(indVarUnits) && strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)*(strlen(Units)+strlen(indVarUnits)+2)); strcat( strcat( strcpy(slopeUnits, Units), "/"), indVarUnits); } if (strlen(indVarUnits) && !strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)*(strlen(indVarUnits)+2)); strcat( strcpy( slopeUnits, "1/"), indVarUnits); } if (!strlen(indVarUnits) && strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)*(strlen(Units)+2)); strcpy( slopeUnits, indVarUnits); } if (!strlen(indVarUnits) && !strlen(Units) ) { slopeUnits = (char*)malloc(sizeof(*slopeUnits)); strcpy( slopeUnits, ""); } if (0>SDDS_DefineColumn(&outputPage, slopeColumn[iCol], NULL, slopeUnits, NULL, NULL, SDDS_DOUBLE,0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (slopeErrors) { if (0>SDDS_DefineColumn(&outputPage, slopeSigmaColumn[iCol], NULL, slopeUnits, NULL, NULL, SDDS_DOUBLE,0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(slopeUnits); } if(!SDDS_WriteLayout(&outputPage) || !SDDS_StartTable(&outputPage,rows) || !SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "InputFile",inputfile?inputfile:"pipe",NULL) || !SDDS_SetParameters(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, 0, "IndependentVariable", indParameterName, NULL) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /************************************\ * Copy columns to output file (usually columns Index and Rootname) \*************************************/ copyColumns = DEFAULT_COPY_COLUMNS; copyColumn = defaultCopyColumn; if (!set_multicolumn_flags(&inputPage, ©Column, ©Columns, NULL, 0)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); exit(1); } if(!SDDS_CopyColumns(&outputPage, &inputPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); depVar = (double*)malloc(sizeof(*depVar)*copiedPages); weight = (double*)malloc(sizeof(*weight)*copiedPages); diff = (double*)malloc(sizeof(*weight)*copiedPages); order=1; coef = (double*)malloc(sizeof(*coef)*(order+1)); coefsigma = (double*)malloc(sizeof(*coefsigma)*(order+1)); if(slopeErrors) slope = (double*)malloc(sizeof(*slope)*copiedPages); for (iCol=0; iCol<columns; iCol++) { for (iPage=0; iPage<copiedPages; iPage++) weight[iPage]=1; if (verbose) fprintf(stderr,"Doing column %s.\n", column[iCol]); for (iRow=0; iRow<rows; iRow++) { for (iPage=0; iPage<copiedPages; iPage++) { if (!SDDS_GetValue(&copiedPage[iPage], column[iCol], iRow, &depVar[iPage])) SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } if (!(lsfn(indVar, depVar, weight, copiedPages, order, coef, coefsigma, &chi, diff))){ fprintf(stderr,"Problem with call to lsfn\n."); exit(1); } if (generateIndex) { if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, iRow, "Index",iRow, intColumn[iCol], coef[0], slopeColumn[iCol], coef[1], NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, iRow, intColumn[iCol], coef[0], slopeColumn[iCol], coef[1], NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if(slopeErrors){ /* recalculate the slope with a subset of points */ slopeAve= slope2 = 0; for (iPage=0; iPage<copiedPages; iPage++) { weight[iPage] = 1e10; if(iPage) weight[iPage-1] = 1; if (!(lsfn(indVar, depVar, weight, copiedPages, order, coef, coefsigma, &chi, diff))){ fprintf(stderr,"Problem with call to lsfn\n."); exit(1); } slope[iPage] = coef[1]; slopeAve += slope[iPage]; slope2 += sqr(slope[iPage]); } slopeSigma = sqrt(slope2/copiedPages - sqr(slopeAve/copiedPages)); if (!SDDS_SetRowValues(&outputPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, iRow, slopeSigmaColumn[iCol], slopeSigma, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } } if( !SDDS_WriteTable(&outputPage)|| SDDS_Terminate(&inputPage) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (iPage=0; iPage<copiedPages; iPage++) { if( !SDDS_Terminate(&copiedPage[iPage]) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if( SDDS_Terminate(&outputPage) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (tmpfile_used && !replaceFileAndBackUp(inputfile, outputfile)) exit(1); SDDS_FreeStringArray(column, columns); free(column); SDDS_FreeStringArray(intColumn, columns); SDDS_FreeStringArray(slopeColumn, columns); free(intColumn); free(slopeColumn); if (slopeErrors) { SDDS_FreeStringArray(slopeSigmaColumn, columns); free(slopeSigmaColumn); } free(copiedPage); free(indVar); free(depVar); if (weight) free(weight); if (diff) free(diff); if (slope) free(slope); if (coef) free(coef); if (coefsigma) free(coefsigma); if (Units) free(Units); if (indVarUnits) free(indVarUnits); free_scanargs(&scanned, argc); return(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); }
/**************************** 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); }