00001 #ident "$Header: /home/cvsuser/cvsroot/JSOC/proj/libs/stats/set_statistics.c,v 1.4 2011/11/15 20:20:38 phil Exp $"
00002
00003 #include <jsoc.h>
00004 #include <jsoc_main.h>
00005 #include "fstats.h"
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #define SETKEY(str,val) \
00025 { \
00026 if (thiskey = drms_keyword_lookup(rec, str, 0)) \
00027 drms_setkey_double(rec, str, val); \
00028 else \
00029 { \
00030 char name[DRMS_MAXNAMELEN]; \
00031 strcpy(name, str); \
00032 strcat(name, segstr); \
00033 if ((thiskey = drms_keyword_lookup(rec, name, 0)) && ((thiskey->info->kwflags & 1) == kKeywordFlag_PerSegment)) \
00034 drms_setkey_double(rec, name, val); \
00035 } \
00036 }
00037
00038 int set_statistics(DRMS_Segment_t *seg, DRMS_Array_t *data, int mode)
00039 {
00040 int status;
00041 DRMS_Record_t *rec;
00042 int i, n, nok, ntot, segnum, nsegs;
00043 double min, max, medn, mean, sig, skew, kurt;
00044 char segstr[10];
00045 DRMS_Keyword_t *thiskey;
00046
00047 if (!seg || !data)
00048 return(DRMS_ERROR_INVALIDDATA);
00049 rec = seg->record;
00050 if (!rec)
00051 return(DRMS_ERROR_INVALIDDATA);
00052 nsegs = rec->segments.num_total;
00053 segnum = seg->info->segnum;
00054
00055 sprintf(segstr, "_%03d", segnum);
00056
00057 for (n=1, i=0; i<data->naxis; i++)
00058 n *= data->axis[i];
00059
00060 ntot = drms_getkey_int(rec, "TOTVALS", &status);
00061 if (status)
00062 {
00063 char name[DRMS_MAXNAMELEN];
00064 strcpy(name, "TOTVALS");
00065 strcat(name, segstr);
00066 if ((thiskey = drms_keyword_lookup(rec, name, 0)) && ((thiskey->info->kwflags & 1) == kKeywordFlag_PerSegment))
00067 ntot = drms_getkey_int(rec, name, &status);
00068 if (status)
00069 ntot = n;
00070 }
00071
00072 if (data->type == DRMS_TYPE_FLOAT)
00073 {
00074 if (mode)
00075 status = fstats2(n, (float *)data->data, &min, &max, &medn, &mean, &sig, &skew, &kurt, &nok);
00076 else
00077 status = fstats(n, (float *)data->data, &min, &max, &medn, &mean, &sig, &skew, &kurt, &nok);
00078 }
00079 else if (data->type == DRMS_TYPE_DOUBLE)
00080 {
00081 if (mode)
00082 status = dstats2(n, (double *)data->data, &min, &max, &medn, &mean, &sig, &skew, &kurt, &nok);
00083 else
00084 status = dstats(n, (double *)data->data, &min, &max, &medn, &mean, &sig, &skew, &kurt, &nok);
00085 }
00086 else return(-1);
00087
00088 if (status)
00089 return(status);
00090
00091 SETKEY("DATAMEAN", mean);
00092 SETKEY("DATAMIN", min);
00093 SETKEY("DATAMAX", max);
00094 SETKEY("DATAMEDN", medn);
00095 SETKEY("DATARMS", sig);
00096 SETKEY("DATASKEW", skew);
00097 SETKEY("DATAKURT", kurt);
00098 SETKEY("DATAVALS", (double)nok);
00099 SETKEY("MISSVALS", (double)(ntot - nok));
00100 return(0);
00101 }