00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 char *module_name = "CookbookRecipe:07";
00016 char *version_id = "1.0";
00017
00018 #include <jsoc_main.h>
00019
00020 ModuleArgs_t module_args[] = {
00021 {ARG_STRING, "ds", "drms.images", "name of data series"},
00022 {ARG_INT, "cols", "0", "number of columns in image (default (rows)"},
00023 {ARG_INT, "rows", "0", "number of rows in image (default: cols)"},
00024 {ARG_NUME, "dist", "uniform", "distribution for pseudo-randoms",
00025 "uniform, dblexp, normal"},
00026 {ARG_INT, "seed", "0", "initial seed for pseudo-random number generator"},
00027 {ARG_FLAG, "v", "", "verbose flag"},
00028 {ARG_END}
00029 };
00030
00031 enum dist_type {UNIFORM, DBL_EXP, NORMAL};
00032
00033 float next_rand (int distrib) {
00034 double val0, val1, rmod, gf;
00035 static double sav;
00036 static int avail = 0;
00037
00038 switch (distrib) {
00039 case (UNIFORM):
00040 return 2 * drand48 () - 1.0;
00041 case (DBL_EXP):
00042 val0 = -log (drand48 ());
00043 val1 = (drand48 () < 0.5) ? -0.5 : +0.5;
00044 return val0 * val1;
00045 case (NORMAL):
00046 if (avail) {
00047 avail = 0;
00048 return sav;
00049 }
00050 rmod = 2.0;
00051 while (rmod >= 1.0) {
00052 val0 = 2 * drand48 () - 1.0;
00053 val1 = 2 * drand48 () - 1.0;
00054 rmod = val0 * val0 + val1 * val1;
00055 }
00056 gf = sqrt (-2.0 * log (rmod) / rmod);
00057 sav = gf * val0;
00058 avail = 1;
00059 return gf * val1;
00060 default:
00061 return 0.0;
00062 }
00063 }
00064
00065 void rand_fill_array (float *data, int cols, int rows, int df) {
00066 double xr;
00067 int n, col, row, pr;
00068 int colsM1 = cols - 1;
00069 double kf1 = 1.0, kf2 = 0.5, kf3 = 1.0 / 3.0, kf4 = 0.25, kf5 = 0.2;
00070
00071 n = row = 0;
00072 data[n++] = next_rand (df);
00073 for (col = 1; col < colsM1; col++, n++) {
00074 data[n] = kf1 * data[n-1] + next_rand (df);
00075 }
00076 data[n++] = kf2 * (data[n-1] + data[n+1-cols]) + next_rand (df);
00077
00078 row++;
00079 for (; row < rows; row++) {
00080 pr = n - cols;
00081 data[n++] = kf3 * (data[n-1] + data[pr] + data[pr+1]) + next_rand (df);
00082 pr++;
00083 for (col = 1; col < colsM1; col++, n++, pr++) {
00084 data[n] = kf4 * (data[pr-1] + data[pr] + data[pr+1] + data[n-1]) +
00085 next_rand (df);
00086 }
00087 data[n++] = kf5 * (data[pr-1] + data[pr] + data[pr+1] + data[n-1] + data[n+1-cols]) +
00088 next_rand (df);
00089 }
00090 }
00091
00092 int DoIt (void) {
00093 CmdParams_t *params = &cmdparams;
00094 DRMS_Record_t *record;
00095 DRMS_Segment_t *record_segment;
00096 DRMS_Array_t *data_array;
00097 float *data;
00098 int axes[2];
00099 int status = 0;
00100
00101 char *series = strdup (params_get_str (params, "ds"));
00102 int cols = params_get_int (params, "cols");
00103 int rows = params_get_int (params, "rows");
00104 int distrib = params_get_int (params, "dist");
00105 long int seed = params_get_int (params, "seed");
00106 int verbose = params_isflagset (params, "v");
00107
00108 if (cols <= 0) cols = rows;
00109 if (rows <= 0) rows = cols;
00110 if (rows <= 0) {
00111 fprintf (stderr, "Error: at least one of cols and rows must be specified\n");
00112 return 1;
00113 }
00114
00115 axes[0] = cols;
00116 axes[1] = rows;
00117 data = (float *)malloc (cols * rows * sizeof (float));
00118 data_array = drms_array_create (DRMS_TYPE_FLOAT, 2, axes, (void *)data,
00119 &status);
00120
00121 srand48 (seed);
00122 rand_fill_array (data, cols, rows, distrib);
00123
00124 if (verbose) {
00125 int col, row, n;
00126 int mincol, maxcol, minrow, maxrow;
00127 float minval, maxval;
00128
00129 minval = maxval = data[0];
00130 mincol = minrow = maxcol = maxrow = 0;
00131 for (n = 0, row = 0; row < rows; row++) {
00132 for (col = 0; col < cols; col++, n++) {
00133 if (data[n] < minval) {
00134 minval = data[n];
00135 mincol = col;
00136 minrow = row;
00137 }
00138 if (data[n] > maxval) {
00139 maxval = data[n];
00140 maxcol = col;
00141 maxrow = row;
00142 }
00143 }
00144 }
00145 printf ("min @ [%d, %d] : %f\n", mincol, minrow, minval);
00146 printf ("max @ [%d, %d] : %f\n", maxcol, maxrow, maxval);
00147 }
00148
00149 record = drms_create_record (drms_env, series, DRMS_PERMANENT, &status);
00150 if (!record) {
00151 fprintf (stderr, "Error: Unable to create record in series %s\n", series);
00152 fprintf (stderr, " Does series exist and is it writeable by you?\n");
00153 return 1;
00154 }
00155 record_segment = drms_segment_lookupnum (record, 0);
00156
00157 data_array->bscale = 1.0;
00158 data_array->bzero = 0.0;
00159 status = drms_segment_write (record_segment, data_array, 0);
00160 if (status) {
00161 fprintf (stderr, "Error writing data to segment 0 of series %s\n", series);
00162 fprintf (stderr, " series may not have appropriate structure\n");
00163 drms_free_array (data_array);
00164 drms_close_record (record, DRMS_FREE_RECORD);
00165 return 1;
00166 }
00167 drms_close_record (record, DRMS_INSERT_RECORD);
00168 return 0;
00169 }
00170
00171
00172
00173
00174