00001 #include "drms.h"
00002 #include "drms_names.h"
00003 #include "jsoc_main.h"
00004
00005 ModuleArgs_t module_args[] = {
00006 {ARG_FLOAT, "alpha", "", "multiplicative scaling"},
00007 {ARG_FLOAT, "beta", "", "additive offset: data -> (alpha * data) + beta"},
00008 {ARG_END}
00009 };
00010
00011 char *module_name = "scalesegments";
00012 char *version_id = "1.8";
00013
00014
00015
00016
00017 int DoIt () {
00018 char keyname[10];
00019 int i, j, status, dostat;
00020 arraylen_t n;
00021 arraylen_t k;
00022 arraylen_t count;
00023 char *rsname;
00024 DRMS_RecordSet_t *oldrs, *newrs;
00025 DRMS_Array_t *array;
00026 DRMS_Segment_t *oldseg, *newseg;
00027 HIterator_t newit, oldit;
00028 double alpha, beta, *data, sum;
00029
00030 alpha = cmdparams_get_double (&cmdparams, "alpha", &status);
00031 beta = cmdparams_get_double (&cmdparams, "beta", &status);
00032
00033 i = 1;
00034 while ((rsname = cmdparams_getarg (&cmdparams, i++))) {
00035
00036 oldrs = drms_open_records(drms_env, rsname, &status);
00037
00038 if (oldrs) {
00039
00040 newrs = drms_create_records (drms_env, oldrs->n,
00041 oldrs->records[0]->seriesinfo->seriesname,
00042 DRMS_PERMANENT, &status);
00043 if (status) {
00044 fprintf (stderr, "Create records failed.\n");
00045 return status;
00046 }
00047
00048 if (oldrs->n > 0 ) {
00049
00050 for (j=0; j<oldrs->n; j++) {
00051
00052 hiter_new(&oldit, &((oldrs->records[j])->segments));
00053 hiter_new(&newit, &((newrs->records[j])->segments));
00054
00055 while ((oldseg = (DRMS_Segment_t *)hiter_getnext (&oldit)) ) {
00056 newseg = (DRMS_Segment_t *)hiter_getnext (&oldit);
00057
00058 sprintf(keyname, "datamean[%d]", newseg->info->segnum);
00059 if (drms_keyword_lookup (oldrs->records[j], keyname, 1)) dostat = 1;
00060 else dostat = 0;
00061
00062 array = drms_segment_read (oldseg, DRMS_TYPE_DOUBLE, &status);
00063 if (status) {
00064 printf("ERROR: drms_segment_read failed with status %d\n", status);
00065 return status;
00066 }
00067
00068 n = drms_array_count (array);
00069
00070 data = array->data;
00071 count = 0;
00072 sum = 0.0;
00073 for (k=0; k<n; k++) {
00074 if (data[k] != DRMS_MISSING_DOUBLE) {
00075 count++;
00076 data[k] = alpha*data[k] + beta;
00077 if (dostat) sum += data[k];
00078 }
00079 }
00080
00081 if (dostat) {
00082 sprintf (keyname, "datamean[%d]", newseg->info->segnum);
00083 drms_setkey_double (newrs->records[j], keyname, sum/count);
00084 }
00085
00086 status = drms_segment_write (newseg, array,0);
00087 if (status) {
00088 fprintf (stderr,"ERROR: drms_segment_write failed with error "
00089 "code %d.\n",status);
00090 return status;
00091 }
00092 drms_free_array(array);
00093 }
00094 }
00095 drms_close_records (oldrs, DRMS_FREE_RECORD);
00096 drms_close_records (newrs, DRMS_INSERT_RECORD);
00097 }
00098 }
00099 else printf ("drms_open_records failed with error code %d.\n", status);
00100 }
00101 return 0;
00102 }