00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 char *module_name = "CookbookRecipe:08";
00022 char *version_id = "1.0";
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <jsoc_main.h>
00032 #include <math.h>
00033
00034 ModuleArgs_t module_args[] = {
00035 {ARG_STRING, "ds", "drms.images[:#$]", "data set"},
00036 {ARG_STRING, "seg", "Not Specified", "data segment name (default: all)"},
00037 {}
00038 };
00039
00040 void calc_and_print_stats (DRMS_Segment_t *record_segment) {
00041 DRMS_Array_t *data_array;
00042 double *data;
00043 double sum, sum2, avg, stdev;
00044 long long ntot, nv;
00045 int n, rank;
00046 int status;
00047
00048
00049 data_array = drms_segment_read (record_segment, DRMS_TYPE_DOUBLE, &status);
00050 if (status) {
00051
00052
00053
00054
00055 printf ("Not found\n");
00056 return;
00057 }
00058 rank = data_array->naxis;
00059 ntot = 1;
00060 for (n = 0; n < rank; n++) ntot *= data_array->axis[n];
00061 data = (double *)data_array->data;
00062 nv = 0;
00063 sum = sum2 = 0;
00064 for (n = 0; n < ntot; n++) {
00065 if (isnan (data[n])) continue;
00066 nv++;
00067 sum += data[n];
00068 sum2 += data[n] * data[n];
00069 }
00070 printf ("%ld of %ld valid", nv, ntot);
00071 if (nv) {
00072 avg = sum / nv;
00073 sum2 /= nv;
00074 stdev = sqrt (sum2 - avg * avg);
00075 printf ("; mean = %11.4e std dev = %11.4e", avg, stdev);
00076 }
00077 printf ("\n");
00078 drms_free_array (data_array);
00079 }
00080
00081 int DoIt () {
00082 DRMS_RecordSet_t *drs;
00083 DRMS_Record_t *record;
00084 DRMS_Segment_t *record_segment;
00085 int recn, rec_ct, segn, seg_ct;
00086 int n, seg_selected;
00087 int status;
00088
00089 char *dsspec = strdup (params_get_str (&cmdparams, "ds"));
00090 char *seg_name = strdup (params_get_str (&cmdparams, "seg"));
00091
00092 seg_selected = strcmp (seg_name, "Not Specified");
00093 drs = drms_open_records (drms_env, dsspec, &status);
00094 if (!drs) {
00095 fprintf (stderr, "Error: unable to open record set %s\n", dsspec);
00096 return 0;
00097 }
00098 rec_ct = drs->n;
00099 if (rec_ct < 1) {
00100 fprintf (stderr, "No records in selected set %s\n", dsspec);
00101 return 0;
00102 }
00103
00104 for (recn = 0; recn < rec_ct; recn++) {
00105 if (rec_ct > 1) printf ("record %d:\n", recn);
00106 record = drs->records[recn];
00107 seg_ct = drms_record_numsegments (record);
00108 if (seg_ct < 1) printf (" no segments in selected record\n");
00109 if (seg_selected) {
00110 record_segment = drms_segment_lookup (record, seg_name);
00111 if (!record_segment) {
00112 fprintf (stderr, "Error, unable to locate segment %s of record %d\n",
00113 seg_name, recn);
00114 continue;
00115 }
00116 calc_and_print_stats (record_segment);
00117 } else {
00118
00119 for (segn = 0; segn < seg_ct; segn++) {
00120 record_segment = drms_segment_lookupnum (record, segn);
00121 if (!record_segment) {
00122 fprintf (stderr, "Error, unable to locate segment %d of record %d\n",
00123 segn, recn);
00124 continue;
00125 }
00126 printf (" %s\t", record_segment->info->name);
00127 if (record_segment->info->protocol == DRMS_PROTOCOL_INVALID ||
00128 record_segment->info->protocol == DRMS_GENERIC) {
00129 printf ("unsupported data protocol; skipped\n");
00130 continue;
00131 }
00132 calc_and_print_stats (record_segment);
00133 }
00134 }
00135 }
00136
00137 return 0;
00138 }
00139
00140
00141
00142
00143
00144
00145