/*************************************************************************\ * Copyright (c) 2002 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * This file is distributed subject to a Software License Agreement found * in the file LICENSE that is included with this distribution. \*************************************************************************/ /* program sddsregroup.c * read a sddsfile with pages of equal number of rows and produce * an output file with reprouped pages. * If the input file has m pages of n rows each then * the output file had n pages of m rows each. * The parameters of the input file may become extra columns in the output file. * These extra columns will be duplicated in all pages. * On the command line, one may specify which columns of the input file * becomes parameters in the output file. $Log: sddsregroup.c,v $ Revision 1.15 2005/11/04 22:46:17 soliday Updated code to be compiled by a 64 bit processor. Revision 1.14 2002/08/14 17:12:52 soliday Added Open License Revision 1.13 2002/01/16 18:50:02 soliday Added missing SDDS_Terminate and free functions. Revision 1.12 2001/01/10 19:35:45 soliday Standardized usage message. Revision 1.11 1999/09/14 18:32:09 soliday init_stats is no longer defined locally Revision 1.10 1999/05/25 19:14:20 soliday Removed compiler warning on linux. Revision 1.9 1999/01/06 19:54:54 borland Fixed the version number in the usage message. Revision 1.8 1998/07/11 07:20:24 emery Changed error message for unequal numbers of rows in pages. Revision 1.7 1998/07/11 06:56:22 emery Make program exits when not all pages have the same number of rows. Revision 1.6 1997/01/31 22:56:51 emery Shortened some lines by inserting a . * Revision 1.5 1996/05/22 23:23:00 emery * indented the source code. * * Revision 1.4 1996/02/13 20:40:39 emery * Use "m" as the filemode parameter (used to be NULL) for InitializeCopy function, * made necessary by changes made to the InitializeCopy function. * * Revision 1.3 1995/09/06 14:57:03 saunders * First test release of SDDS1.5 * */ #include "mdb.h" #include "scan.h" #include "match_string.h" #include "matlib.h" #include "SDDS.h" #define CLO_NEWCOLUMNS 0 #define CLO_NEWPARAMETERS 1 #define CLO_WARNING 2 #define CLO_VERBOSE 3 #define CLO_PIPE 4 #define COMMANDLINE_OPTIONS 5 char *commandline_option[COMMANDLINE_OPTIONS] = { "newcolumns", "newparameters", "warning", "verbose", "pipe" }; static char *USAGE = "sddsregroup [-pipe=[input][,output]]\n\ [-newparameters=[,...]]\n\ [-newcolumns=[,...]] [-warning] [-verbose]\n\n\ Re-organizes the data in the input file by taking single rows from each page of the inputfile\n\ to form single pages of the output file.\n\ pipe reads input from and/or writes output to a pipe.\n\ inputfile sdds file with m pages of n rows each.\n\ outputfile sdds file with n pages of m rows each.\n\ newparameters specifies which columns of the input file will become\n\ parameters in the output file. By default no new parameters\n\ are created, and all columns of the input file are transfered\n\ to the output file.\n\ newcolumns specifies which parameters of the input file will become\n\ columns in the output file. The columns will necessarily be\n\ duplicated in all pages. By default all parameters\n\ values are lost.\n\n\ Program by Louis Emery, ANL (This is version 2, July 1998.) \n"; int main(int argc, char **argv) { SCANNED_ARG *s_arg; SDDS_TABLE inputPage, *inputPages, outputPage; char *inputfile, *outputfile; char *InputDescription, *InputContents; char *OutputDescription, *OutputContents; char **InputParameters, **InputColumns, **ColToParNames, **ParToColNames; char **OutputColumns; int32_t NInputParameters, NInputColumns; long NColToPar, NParToCol, NColToCol; long NOutputColumns; long NInputRows, NOutputRows, NInputPages, NOutputPages; long *ColToColInputIndex, *ColToParInputIndex, *ParToColInputIndex; long *ColToColOutputIndex, *ColToParOutputIndex, *ParToColOutputIndex; long pageIncrement = 20; long i, i_arg, j; long ipage, row; long verbose, warning; unsigned long pipeFlags; long tmpfile_used, noWarnings; inputPages = NULL; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc == 1) bomb(NULL, USAGE); inputfile = outputfile = NULL; InputDescription = InputContents = NULL; OutputDescription = OutputContents = NULL; InputParameters = InputColumns = ColToParNames = ParToColNames = NULL; OutputColumns = NULL; NInputParameters = NInputColumns = NColToPar = NParToCol = NColToCol = 0; NOutputColumns = 0; NInputRows = NOutputRows = NInputPages = NOutputPages = 0; ColToColInputIndex = ColToParInputIndex = ParToColInputIndex = NULL; ColToColOutputIndex = ColToParOutputIndex = ParToColOutputIndex = NULL; verbose = 0; warning = 0; pipeFlags = 0; tmpfile_used = 0; noWarnings = 0; for (i_arg = 1; i_argmatch_string(InputColumns[i], ColToParNames, NColToPar, EXACT_MATCH)) { if(0>SDDS_TransferColumnDefinition(&outputPage, &inputPages[0], InputColumns[i], InputColumns[i])) SDDS_PrintErrors(stdout, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); ColToColInputIndex[j] = SDDS_GetColumnIndex(&inputPages[0], InputColumns[i]); ColToColOutputIndex[j] = SDDS_GetColumnIndex(&outputPage, InputColumns[i]); j++; } } if(j != NColToCol) SDDS_Bomb("Error: Something went wrong with counting the columns. Report to author."); if(!SDDS_WriteLayout(&outputPage)) SDDS_PrintErrors(stdout, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); /*******************************\ * fill pages in the output file * \*******************************/ NOutputPages = NInputRows; NOutputRows = NInputPages; for( ipage = 0; ipage < NOutputPages; ipage++){ if(verbose) fprintf(stderr,"Starting page %ld...\n", ipage); SDDS_StartTable(&outputPage, NOutputRows); /* set parameters */ for( i = 0; i < NColToPar; i++){ if(!SDDS_SetParameters(&outputPage, SDDS_SET_BY_INDEX|SDDS_PASS_BY_REFERENCE, ColToParOutputIndex[i], SDDS_GetValueByAbsIndex(&inputPages[0], ColToParInputIndex[i], ipage, NULL), -1)) SDDS_PrintErrors(stdout, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } /* set columns */ for(i = 0;i