/*************************************************************************\ * 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: sddssortcolumn * purpose: sorts the columns of an SDDS file * * Hairong Shang, 2003 $Log: sddssortcolumn.c,v $ Revision 1.4 2010/04/15 16:41:10 soliday Fixed some warning messages. Revision 1.3 2009/04/06 18:27:14 shang fixed a bug sorting columns by provided order in a file. Revision 1.2 2006/12/14 22:22:00 soliday Updated a bunch of programs because SDDS_SaveLayout is now called by SDDS_WriteLayout and it is no longer required to be called directly. Also the AutoCheckMode is turned off by default now so I removed calls to SDDS_SetAutoCheckMode that would attempt to turn it off. It is now up to the programmer to turn it on in new programs until debugging is completed and then remove the call to SDDS_SetAutoCheckMode. Revision 1.1 2003/07/09 18:20:59 shang first version sort the columns of an SDDS file in specified order. */ #include "mdb.h" #include "SDDS.h" #include "scan.h" #if defined(_WIN32) #include #define pid_t int #else #if defined(linux) #include #endif #include #endif #define SET_PIPE 0 #define SET_SORTLIST 1 #define SET_DECREASING 2 #define SET_BPMORDER 3 #define SET_SORTWITH 4 #define N_OPTIONS 5 char *option[N_OPTIONS] = { "pipe","sortList","decreasing","bpmOrder","sortWith", } ; char *USAGE = "sddssortcolumn [] [] [-pipe=[input][,output]]\n\ [-sortList=] [-decreasing] \n\ [-bpmOrder] [-sortWith=,column=]\n\n\ -sortList specifiy the order of column names in a list. \n\ -sortWith=,column= \n\ sort the columns of input by the order in the column of \n\ provided by sortWith option. It overwrites other sorting order. \n\ -bpmOrder sort the columns by the assumed bpm position in storage ring. \n\ -decreasing sort the columns in decreasing order, default is increasing.\n\ rearrange the columns of an SDDS input file in specified order. \n\n\ Program by Hairong Shang\n"; static char **ColumnName; static long increasing,bpmOrder; static int32_t columns; long getBPMSubOrder(char *bpmName) { long suborder; /*suborder of bpm: A:P0,A:P1,A:P2,A:P3,A:P4,A:P5, B:P5,B:P4,B:P3,B:P2,B:P1,B:P0,BM:P1, BM:P2,ID:P1, ID:P2 */ if (wild_match(bpmName,"*A:P0*")) suborder=1; else if (wild_match(bpmName,"*A:P1*")) suborder=2; else if (wild_match(bpmName,"*A:P2*")) suborder=3; else if (wild_match(bpmName,"*A:P3*")) suborder=4; else if (wild_match(bpmName,"*A:P4*")) suborder=5; else if (wild_match(bpmName,"*A:P5*")) suborder=6; else if (wild_match(bpmName,"*B:P5*")) suborder=7; else if (wild_match(bpmName,"*B:P4*")) suborder=8; else if (wild_match(bpmName,"*B:P3*")) suborder=9; else if (wild_match(bpmName,"*B:P2*")) suborder=10; else if (wild_match(bpmName,"*B:P1*")) suborder=11; else if (wild_match(bpmName,"*B:P0*")) suborder=12; else if (wild_match(bpmName,"*BM:P1*")) suborder=13; else if (wild_match(bpmName,"*BM:P2*")) suborder=14; else if (wild_match(bpmName,"*ID:P1*")) suborder=15; else if (wild_match(bpmName,"*ID:P2*")) suborder=16; else suborder=17; return suborder; } #if defined(SOLARIS) || defined(LINUX) || defined(_WIN32) int CompareStrings(const void *vindex1, const void *vindex2) #else long CompareStrings(const void *vindex1, const void *vindex2) #endif { static long index1, index2; long comparison,sector1,sector2,subsector1,subsector2; /*type: 0: A:P, 1 B:P, 2 BM, 3 ID bpm */ index1 = *(long*)vindex1; index2 = *(long*)vindex2; if (bpmOrder) { if (sscanf(ColumnName[index1],"S%ld",§or1)!=1) sector1=0; if (sscanf(ColumnName[index2],"S%ld",§or2)!=1) sector2=0; if (sector1==0 && sector2==0) comparison=strcmp(ColumnName[index1], ColumnName[index2]); else { if (sector1>sector2) comparison=1; else if (sector1subsector2) comparison=1; else if (subsector1=0) { sortedColumn[count] = sortList[i]; count++; } } for (i=0;i0) { rows = SDDS_CountRowsOfInterest(&SDDS_input); if (!SDDS_StartPage(&SDDS_output, rows)) { SDDS_SetError("Problem starting output page"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_CopyParameters(&SDDS_output, &SDDS_input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_CopyColumns(&SDDS_output, &SDDS_input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_WritePage(&SDDS_output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDS_input) || !SDDS_Terminate(&SDDS_output)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (tmpfile_used && !replaceFileAndBackUp(input, output)) exit(1); if (parameters) { for (i=0;i