IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 26, 2006, 12:09:48 PM (20 years ago)
Author:
Paul Price
Message:

Adding psStatsFromString and psStatsToString.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/math/psStats.c

    r7986 r7989  
    1616 * use ->min and ->max (PS_STAT_USE_RANGE)
    1717 *
    18  *  @version $Revision: 1.179 $ $Name: not supported by cvs2svn $
    19  *  @date $Date: 2006-07-26 04:21:39 $
     18 *  @version $Revision: 1.180 $ $Name: not supported by cvs2svn $
     19 *  @date $Date: 2006-07-26 22:09:48 $
    2020 *
    2121 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2424#include <stdlib.h>
    2525#include <stdio.h>
    26 #include <string.h>
    2726#include <stdarg.h>
    2827#include <float.h>
    2928#include <math.h>
    3029#include <limits.h>
     30#include <strings.h>
    3131
    3232/*****************************************************************************/
     
    4545#include "psAssert.h"
    4646#include "psMathUtils.h"
     47#include "psList.h"
     48#include "psString.h"
    4749
    4850#include "psErrorText.h"
     
    21242126}
    21252127
     2128psStats *psStatsFromString(const char *string)
     2129{
     2130    psList *subStrings = psStringSplit(string, " ,;", false); // List of sub-strings
     2131    if (!subStrings || psListLength(subStrings) == 0) {
     2132        // Nothing here
     2133        psError(PS_ERR_BAD_PARAMETER_VALUE, false, "No string to parse for statistics: %s\n", string);
     2134        psFree(subStrings);
     2135        return NULL;
     2136    }
     2137    psStats *stats = psStatsAlloc(0);   // Generate empty stats structure
     2138    psListIterator *iterator = psListIteratorAlloc(subStrings, PS_LIST_HEAD, false); // Iterator
     2139    psString statString;                // Statistic string, from iteration
     2140
     2141    #define READ_STAT(NAME, SYMBOL) \
     2142    if (strcasecmp(statString, NAME) == 0) { \
     2143        stats->options |= SYMBOL; \
     2144    }
     2145
     2146    while ((statString = psListGetAndIncrement(iterator))) {
     2147        // This might look a little weird if automatically formated,
     2148        // but it's legal C once the pre-processing has been done, and
     2149        // it reads a lot better than a whole heap of "if-then-else" statements.
     2150        READ_STAT("MEAN",     PS_STAT_SAMPLE_MEAN) else
     2151            READ_STAT("STDEV",    PS_STAT_SAMPLE_STDEV) else
     2152                READ_STAT("MEDIAN",   PS_STAT_SAMPLE_MEDIAN) else
     2153                    READ_STAT("QUARTILE", PS_STAT_SAMPLE_QUARTILE) else
     2154                        READ_STAT("SAMPLE_MEAN",     PS_STAT_SAMPLE_MEAN) else
     2155                            READ_STAT("SAMPLE_STDEV",    PS_STAT_SAMPLE_STDEV) else
     2156                                READ_STAT("SAMPLE_MEDIAN",   PS_STAT_SAMPLE_MEDIAN) else
     2157                                    READ_STAT("SAMPLE_QUARTILE", PS_STAT_SAMPLE_QUARTILE) else
     2158                                        READ_STAT("ROBUST",          PS_STAT_ROBUST_MEDIAN) else
     2159                                            READ_STAT("ROBUST_MEDIAN",   PS_STAT_ROBUST_MEDIAN) else
     2160                                                READ_STAT("ROBUST_STDEV",    PS_STAT_ROBUST_STDEV) else
     2161                                                    READ_STAT("ROBUST_QUARTILE", PS_STAT_ROBUST_QUARTILE) else
     2162                                                        READ_STAT("FITTED",       PS_STAT_FITTED_MEAN) else
     2163                                                            READ_STAT("FITTED_MEAN",  PS_STAT_FITTED_MEAN) else
     2164                                                                READ_STAT("FITTED_STDEV", PS_STAT_ROBUST_STDEV) else
     2165                                                                    READ_STAT("CLIPPED",       PS_STAT_CLIPPED_MEAN) else
     2166                                                                        READ_STAT("CLIPPED_MEAN",  PS_STAT_CLIPPED_MEAN) else
     2167                                                                            READ_STAT("CLIPPED_STDEV", PS_STAT_CLIPPED_STDEV) else {
     2168                                                                                psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Can't interpret string as statistic: %s\n",
     2169                                                                                        statString);
     2170                                                                                psFree(iterator);
     2171                                                                                psFree(subStrings);
     2172                                                                                psFree(stats);
     2173                                                                                return NULL;
     2174                                                                            }
     2175    }
     2176
     2177    psFree(iterator);
     2178    psFree(subStrings);
     2179    return stats;
     2180}
     2181
     2182psString psStatsToString(const psStats *stats)
     2183{
     2184    psString string = NULL;             // String to return
     2185
     2186    #define WRITE_STAT(NAME, SYMBOL) \
     2187    if (stats->options & SYMBOL) { \
     2188        psStringAppend(&string, ",%s", NAME); \
     2189    }
     2190
     2191    // Same list as above (for psStatsFromString), but with repeat symbols removed
     2192    WRITE_STAT("SAMPLE_MEAN",     PS_STAT_SAMPLE_MEAN);
     2193    WRITE_STAT("SAMPLE_STDEV",    PS_STAT_SAMPLE_STDEV);
     2194    WRITE_STAT("SAMPLE_MEDIAN",   PS_STAT_SAMPLE_MEDIAN);
     2195    WRITE_STAT("SAMPLE_QUARTILE", PS_STAT_SAMPLE_QUARTILE);
     2196    WRITE_STAT("ROBUST_MEDIAN",   PS_STAT_ROBUST_MEDIAN);
     2197    WRITE_STAT("ROBUST_STDEV",    PS_STAT_ROBUST_STDEV);
     2198    WRITE_STAT("ROBUST_QUARTILE", PS_STAT_ROBUST_QUARTILE);
     2199    WRITE_STAT("FITTED_MEAN",  PS_STAT_FITTED_MEAN);
     2200    WRITE_STAT("FITTED_STDEV", PS_STAT_ROBUST_STDEV);
     2201    WRITE_STAT("CLIPPED_MEAN",  PS_STAT_CLIPPED_MEAN);
     2202    WRITE_STAT("CLIPPED_STDEV", PS_STAT_CLIPPED_STDEV);
     2203
     2204    return string;
     2205}
Note: See TracChangeset for help on using the changeset viewer.