1 |
/* |
2 |
* smpl_08.c $DRMS/proj/cookbook/ |
3 |
* |
4 |
* Calculates primary statistics for the selected segment(s) of selected |
5 |
* records in a DRMS data series |
6 |
* |
7 |
* Populates a data series consisting of variable size images with random |
8 |
* data from a selected distribution function |
9 |
* Illustrates the use of drms_segment_read() and its returned DRMS_Array |
10 |
* struct to manipulate data in the data "cubes" of individual record |
11 |
* segments |
12 |
* The default record set is the (current) last record in the series |
13 |
* created and populated in Recipe 07 |
14 |
* |
15 |
* Usage: |
16 |
* smpl_08 [ds= ] |
17 |
* |
18 |
* Revision history is at end of file. |
19 |
*/ |
20 |
|
21 |
char *module_name = "CookbookRecipe:08"; |
22 |
char *version_id = "1.0"; |
23 |
|
24 |
/* |
25 |
* Bugs: |
26 |
* drms_segment_read seems to fail if storage protocol is bin (or bin.gz) |
27 |
* |
28 |
*/ |
29 |
|
30 |
/* required include, declarations */ |
31 |
#include <jsoc_main.h> |
32 |
#include <math.h> |
33 |
|
34 |
ModuleArgs_t module_args[] = { |
35 |
{ARG_STRING, "ds", "drms.images[:#$]", "data set"}, |
36 |
{ARG_STRING, "seg", "Not Specified", "data segment name (default: all)"}, |
37 |
{} |
38 |
}; |
39 |
|
40 |
void calc_and_print_stats (DRMS_Segment_t *record_segment) { |
41 |
DRMS_Array_t *data_array; /* array struct from segment */ |
42 |
double *data; |
43 |
double sum, sum2, avg, stdev; |
44 |
long long ntot, nv; |
45 |
int n, rank; |
46 |
int status; |
47 |
/* read data from segment into array as doubles |
48 |
(will block until segment is staged) */ |
49 |
data_array = drms_segment_read (record_segment, DRMS_TYPE_DOUBLE, &status); |
50 |
if (status) { |
51 |
/* if drms_segment_read() returns a non-0 status, it probably could |
52 |
not find the file, which may have aged out of the archive, or may |
53 |
not be accessible from the current system, or the segment may have an |
54 |
unsupported protocol. In any case, there's nothing to be done... */ |
55 |
printf ("Not found\n"); |
56 |
return; |
57 |
} |
58 |
rank = data_array->naxis; |
59 |
ntot = 1; |
60 |
for (n = 0; n < rank; n++) ntot *= data_array->axis[n]; |
61 |
data = (double *)data_array->data; |
62 |
nv = 0; |
63 |
sum = sum2 = 0; |
64 |
for (n = 0; n < ntot; n++) { |
65 |
if (isnan (data[n])) continue; |
66 |
nv++; |
67 |
sum += data[n]; |
68 |
sum2 += data[n] * data[n]; |
69 |
} |
70 |
printf ("%ld of %ld valid", nv, ntot); |
71 |
if (nv) { |
72 |
avg = sum / nv; |
73 |
sum2 /= nv; |
74 |
stdev = sqrt (sum2 - avg * avg); |
75 |
printf ("; mean = %11.4e std dev = %11.4e", avg, stdev); |
76 |
} |
77 |
printf ("\n"); |
78 |
drms_free_array (data_array); |
79 |
} |
80 |
/* main module body */ |
81 |
int DoIt () { |
82 |
DRMS_RecordSet_t *drs; /* data set */ |
83 |
DRMS_Record_t *record; /* individual record of the data set */ |
84 |
DRMS_Segment_t *record_segment; /* single data segment of a record */ |
85 |
int recn, rec_ct, segn, seg_ct; |
86 |
int n, seg_selected; |
87 |
int status; |
88 |
/* Get command line arguments */ |
89 |
char *dsspec = strdup (params_get_str (&cmdparams, "ds")); |
90 |
char *seg_name = strdup (params_get_str (&cmdparams, "seg")); |
91 |
|
92 |
seg_selected = strcmp (seg_name, "Not Specified"); |
93 |
drs = drms_open_records (drms_env, dsspec, &status); |
94 |
if (!drs) { |
95 |
fprintf (stderr, "Error: unable to open record set %s\n", dsspec); |
96 |
return 0; |
97 |
} |
98 |
rec_ct = drs->n; |
99 |
if (rec_ct < 1) { |
100 |
fprintf (stderr, "No records in selected set %s\n", dsspec); |
101 |
return 0; |
102 |
} |
103 |
/* loop over all the records in the selected data set */ |
104 |
for (recn = 0; recn < rec_ct; recn++) { |
105 |
if (rec_ct > 1) printf ("record %d:\n", recn); |
106 |
record = drs->records[recn]; |
107 |
seg_ct = drms_record_numsegments (record); |
108 |
if (seg_ct < 1) printf (" no segments in selected record\n"); |
109 |
if (seg_selected) { |
110 |
record_segment = drms_segment_lookup (record, seg_name); |
111 |
if (!record_segment) { |
112 |
fprintf (stderr, "Error, unable to locate segment %s of record %d\n", |
113 |
seg_name, recn); |
114 |
continue; |
115 |
} |
116 |
calc_and_print_stats (record_segment); |
117 |
} else { |
118 |
/* loop over all segments defined for series */ |
119 |
for (segn = 0; segn < seg_ct; segn++) { |
120 |
record_segment = drms_segment_lookupnum (record, segn); |
121 |
if (!record_segment) { |
122 |
fprintf (stderr, "Error, unable to locate segment %d of record %d\n", |
123 |
segn, recn); |
124 |
continue; |
125 |
} |
126 |
printf (" %s\t", record_segment->info->name); |
127 |
if (record_segment->info->protocol == DRMS_PROTOCOL_INVALID || |
128 |
record_segment->info->protocol == DRMS_GENERIC) { |
129 |
printf ("unsupported data protocol; skipped\n"); |
130 |
continue; |
131 |
} |
132 |
calc_and_print_stats (record_segment); |
133 |
} |
134 |
} |
135 |
} |
136 |
|
137 |
return 0; |
138 |
} |
139 |
|
140 |
/* |
141 |
* Revision History |
142 |
* |
143 |
* 08.07.29 Rick Bogart created this file as a heuristic exercise |
144 |
* 09.09.20 " incorporated in cookbook with minor mods |
145 |
*/ |