/*************************************************************************\ * 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. \*************************************************************************/ /* file: utils.c * contents: utility routines for SDDS applications * M. Borland, 1995 $Log: SDDSutils.c,v $ Revision 1.16 2011/12/06 16:29:11 shang moved getMatchingSDDSNames to SDDSlib Revision 1.15 2005/11/29 18:28:54 shang added getSDDSMatchingNames() Revision 1.14 2003/09/02 19:16:01 soliday Cleaned up code for Linux. Revision 1.13 2002/11/25 23:20:50 borland Fixed bug resulting in freeing of same memory twice in multiplyColumnUnits(). Revision 1.12 2002/10/17 15:50:52 soliday Fixed a memory allocation error. Revision 1.11 2002/08/14 17:12:35 soliday Added Open License Revision 1.10 2001/10/25 21:58:11 borland Moved greatestProductOfSmallPrimes() and makeFreqUnits() from sddsfft.c to this location for use by sddsfft.c and sddsnaff.c . Revision 1.6 2000/04/13 18:00:17 soliday Removed break statements after return statements. Revision 1.5 1999/09/14 18:28:50 soliday replace_stringn is no longer defined locally Revision 1.4 1999/05/25 19:01:33 soliday Removed compiler warning on linux. Revision 1.3 1999/05/05 14:37:27 borland Fixed bug in SDDS_CompareParameterValues (wasn't compare string data correctly). * Revision 1.2 1996/05/29 21:42:15 borland * Added new program sddsrowstats. Improved error messages in SDDSutils.c * routine (expandColumnPairNames). * * Revision 1.1 1995/09/06 14:55:43 saunders * First test release of SDDS1.5 * */ #include "mdb.h" #include "SDDS.h" #include "SDDSutils.h" long appendToStringArray(char ***item, long items, char *newItem) { if (!(*item = SDDS_Realloc(*item, sizeof(**item)*(items+1)))) SDDS_Bomb("allocation failure in appendToStringArray"); if (!SDDS_CopyString((*item)+items, newItem)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } return items+1; } long expandColumnPairNames(SDDS_DATASET *SDDSin, char ***name, char ***errorName, long names, char **excludeName, long excludeNames, long typeMode, long typeValue) { long i, j, names1, errorNames1, names2; char **name1, **errorName1, **name2, **errorName2; if (!names || !*name) return 0; name2 = errorName1 = errorName2 = NULL; names2 = errorNames1 = 0; for (i=0; i0?1:0 ; case SDDS_DOUBLE: ddiff = *((double*)param1) - *((double*)param2); return ddiff<0 ? -1 : ddiff>0?1:0 ; case SDDS_LONG: ldiff = *((long*)param1) - *((long*)param2); return ldiff<0 ? -1 : ldiff>0?1:0 ; case SDDS_SHORT: ldiff = *((short*)param1) - *((short*)param2); return ldiff<0 ? -1 : ldiff>0?1:0 ; case SDDS_CHARACTER: cdiff = (short)*((char*)param1) - (short)*((char*)param2); return cdiff<0 ? -1 : cdiff>0?1:0 ; case SDDS_STRING: return strcmp(*(char**)param1, *(char**)param2); default: SDDS_SetError("Problem doing data comparison--invalid data type (SDDS_CompareParameterValues)"); SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); exit(1); } } char *makeFrequencyUnits(SDDS_DATASET *SDDSin, char *indepName) { char *timeUnits; char *units; long reciprocal = 0, end; if (SDDS_GetColumnInformation(SDDSin, "units", &timeUnits, SDDS_GET_BY_NAME, indepName)!=SDDS_STRING) return 0; if (timeUnits) { while (1) { end = strlen(timeUnits) - 1; if (timeUnits[0]=='(' && timeUnits[end]==')') { timeUnits[end] = 0; strslide(timeUnits, 1); } else if (timeUnits[0]=='1' && timeUnits[1]=='/' && timeUnits[2]=='(' && timeUnits[end]==')') { timeUnits[end] = 0; strslide(timeUnits, 3); reciprocal = !reciprocal; } else break; } } if (!timeUnits || SDDS_StringIsBlank(timeUnits)) { units = tmalloc(sizeof(*units)*1); units[0] = 0; return units; } if (reciprocal) { if (!SDDS_CopyString(&units, timeUnits)) return NULL; return units; } units = tmalloc(sizeof(*units)*(strlen(timeUnits)+5)); if (strchr(timeUnits, ' ')) sprintf(units, "1/(%s)", timeUnits); else sprintf(units, "1/%s", timeUnits); return units; } #define MAXPRIMES 8 long greatestProductOfSmallPrimes(long rows) /* doesn't really find the greatest product, but simply a large one */ { long bestResult = 0, result; static long prime[MAXPRIMES] = {2, 3, 5, 7, 11, 13, 17, 19}; long nPrimes; for (nPrimes=1; nPrimes<=MAXPRIMES; nPrimes++) { if ((result = greatestProductOfSmallPrimes1(rows, prime, nPrimes))>bestResult && result<=rows) bestResult = result; } if (bestResult==0) SDDS_Bomb("couldn't find acceptable number of rows for truncation/padding"); return bestResult; } long greatestProductOfSmallPrimes1(long rows, long *primeList, long nPrimes) { long iprime, product, remains, bestFactor=0; double remainder, smallestRemainder; remains = rows; product = 1; while (remains>2) { smallestRemainder = LONG_MAX; for (iprime=0; iprime