9 * Loop through one month of hourly history tapes from the Community Land Model
10 * and generate monthly statistics (means and standard deviations) of fields
17 #define HOURS_PER_DAY 24
18 #define MIN_PER_HOUR 60
19 #define SEC_PER_MIN 60
20 #define SEC_PER_HOUR (MIN_PER_HOUR * SEC_PER_MIN)
22 static char *metadata_vars[] = {
28 "hour", /* new metadata variable to be added to output files */
58 struct text_att *next;
59 struct text_att *prev;
84 static char *time_name = "time";
85 static char *mcsec_name = "mcsec";
86 static char *history_name = "history";
87 static char *nsamples_name = "nsamples";
88 static char *hour_name = "hour", *hour_long_name = "hour of day",
90 static char *cell_method_name = "cell_method";
92 static int nmvars = sizeof(metadata_vars)/sizeof(*metadata_vars);
93 static int input_ncid, input_ndims, input_nvars, input_ngatts, input_unlimdimid;
94 static struct text_att *input_gatt_head = NULL;
95 static struct dim *input_dim_head = NULL, **input_dim_idx;
96 static struct var *input_var_head = NULL;
97 /* translation stuff */
98 static int *idid2mdid, *idid2sdid; /* dimension IDs */
100 static int mean_ncid, mean_ndims, mean_nvars, mean_ngatts, mean_unlimdimid;
101 static int mean_hour_dimid; /* special notes */
102 static struct dim *mean_dim_head = NULL;
103 static struct var *mean_var_head = NULL;
104 static int stddev_ncid, stddev_ndims, stddev_nvars, stddev_ngatts, stddev_unlimdimid;
105 static int stddev_hour_dimid; /* special notes */
106 static struct dim *stddev_dim_head = NULL;
107 static struct var *stddev_var_head = NULL;
109 char is_metadata(char *name)
113 for (i = 0; i < nmvars && strcmp(name, metadata_vars[i]); i++);
122 struct dim *dimlist_find_by_name(struct dim *head, char *name)
125 struct dim *p = NULL;
129 for (i = 0 ; i == 0 || node != head; i++) {
130 if (!strcmp(node->name, name))
141 struct var *varlist_find_by_name(struct var *head, char *name)
148 for (i = 0 ; (i == 0 || node != head) && strcmp(node->name, name); i++)
150 if (i && node == head)
159 void gattlist_add_node(struct text_att **headp, char *name, char *value)
161 struct text_att *head, *node;
165 if (!(node = (struct text_att *)malloc(sizeof(struct text_att)))) {
166 perror("gattlist_add_node");
170 if (!(node->name = strdup(name))) {
171 perror("gattlist_add_node");
174 if (!(node->value = strdup(value))) {
175 perror("gattlist_add_node");
187 node->prev = head->prev;
188 /* set this after setting node->prev from here */
190 /* set this after having set node->prev */
191 node->prev->next = node;
197 struct dim *dimlist_add_node(struct dim **headp, int dimid, char *name, size_t len)
199 struct dim *head, *node;
203 if (!(node = (struct dim *)malloc(sizeof(struct dim)))) {
204 perror("dimlist_add_node");
209 if (!(node->name = strdup(name))) {
210 perror("dimlist_add_node");
223 node->prev = head->prev;
224 /* set this after setting node->prev from here */
226 /* set this after having set node->prev */
227 node->prev->next = node;
233 void varlist_add_node(struct var **headp, int ncvarid, char *name,
234 nc_type nctype, int ndims, int *dimids, int natts, char FillFlag,
238 struct var *head, *node;
242 if (!(node = (struct var *)malloc(sizeof(struct var)))) {
243 perror("varlist_add_node");
247 node->ncvarid = ncvarid;
248 if (!(node->name = strdup(name))) {
249 perror("varlist_add_node");
252 node->nctype = nctype;
254 if (!(node->dimids = (int *)malloc(ndims * sizeof(int)))) {
255 perror("varlist_add_node");
258 for (i = 0; i < ndims; i++) node->dimids[i] = dimids[i];
260 node->metadata = is_metadata(name);
261 node->FillFlag = FillFlag;
262 node->FillValue = FillValue;
272 node->prev = head->prev;
273 /* set this after setting node->prev from here */
275 /* set this after having set node->prev */
276 node->prev->next = node;
282 void varlist_print(struct var *headp)
287 printf("Beginning of Variable List\n");
291 for (j = 0; j == 0 || node != headp; j++) {
292 printf("Variable %d (ptr=%ld):\n", j, (long)node);
293 printf("\tncvarid = %d\n", node->ncvarid);
294 printf("\tname = %s\n", node->name);
295 printf("\tnctype = %d\n", node->nctype);
296 printf("\tndims = %d\n", node->ndims);
297 printf("\tdimids =");
298 for (i = 0; i < node->ndims; i++)
299 printf(" %d", node->dimids[i]);
301 printf("\tnatts = %d\n", node->natts);
302 printf("\tmetadata = %d\n", (int)node->metadata);
303 printf("\tnext = %ld\n", (long)node->next);
304 printf("\tprev = %ld\n", (long)node->prev);
309 printf("\tList undefined\n");
312 printf("End of Variable List\n");
317 void wrap_nc(int status)
319 if (status != NC_NOERR) {
320 fprintf(stderr, "netCDF error: %s\n", nc_strerror(status));
327 void get_dimensions(int ncid, int ndims, struct dim **dim_headp, struct dim ***in_dim_idxp)
330 char name[NC_MAX_NAME+1];
332 struct dim **in_dim_idx;
334 if (!(*in_dim_idxp = (struct dim **)malloc(ndims * sizeof(struct dim *)))) {
335 perror("*in_dim_idxp");
338 in_dim_idx = *in_dim_idxp;
340 for (i = 0; i < ndims; i++) {
341 wrap_nc(nc_inq_dim(ncid, i, name, &len));
342 in_dim_idx[i] = dimlist_add_node(dim_headp, i, name, len);
347 void get_gatts(int ncid, int ngatts, struct text_att **gatt_headp)
350 char name[NC_MAX_NAME+1], *value;
354 for (i = 0; i < ngatts; i++) {
355 wrap_nc(nc_inq_attname(ncid, NC_GLOBAL, i, name));
356 wrap_nc(nc_inq_att(ncid, NC_GLOBAL, name, &xtype, &len));
357 if (xtype != NC_CHAR) {
358 fprintf(stderr, "Global attribute %s is not of type NC_CHAR\n", name);
361 if (!(value = (char *)malloc((len+1)*sizeof(char)))) {
362 perror("get_gatts: value");
365 wrap_nc(nc_get_att_text(ncid, NC_GLOBAL, name, value));
366 value[(len+1-1)] = '\0';
367 gattlist_add_node(gatt_headp, name, value);
368 free(value); /* because gattlist_add_node() duplicates it */
374 void get_vars(int ncid, int nvars, struct var **var_headp)
376 int i, ndims, dimids[NC_MAX_VAR_DIMS], natts;
379 char name[NC_MAX_NAME+1], *fill_att_name = "_FillValue", FillFlag;
380 nc_type xtype, atype;
382 for (i = 0; i < nvars; i++) {
383 wrap_nc(nc_inq_var(ncid, i, name, &xtype, &ndims, dimids,
385 /* Check for _FillValue */
388 if (nc_inq_att(ncid, i, fill_att_name, &atype, &len)
390 if (atype == NC_FLOAT && len) {
391 wrap_nc(nc_get_att_float(ncid, i,
392 fill_att_name, &FillValue));
396 varlist_add_node(var_headp, i, name, xtype, ndims, dimids,
397 natts, FillFlag, FillValue);
403 int put_dimensions(struct dim *in_dim_head, int in_ndims, int in_unlimdimid,
404 size_t nsamples, int **in2outp, int out_ncid,
405 struct dim **out_dim_headp, int *out_unlimdimidp, int *out_hour_dimidp)
408 * Define dimensions on new files and build translation tables between
411 int j, dimid, ndims, *in2out;
417 if (!(*in2outp = (int *)malloc((in_ndims+1)*sizeof(int)))) {
418 perror("put_dimensions: in2outp");
425 for (j = 0; j == 0 || dnode != in_dim_head; j++) {
426 if (dnode->dimid == in_unlimdimid)
430 wrap_nc(nc_def_dim(out_ncid, dnode->name, len, &dimid));
431 dimlist_add_node(out_dim_headp, dimid, dnode->name, len);
433 in2out[dnode->dimid] = dimid;
434 if (dnode->dimid == in_unlimdimid)
435 *out_unlimdimidp = dimid;
437 * Just after the "time" dimension, add the new
438 * "nsamples" and "hour" dimensions.
440 if (!strcmp(dnode->name, time_name)) {
441 wrap_nc(nc_def_dim(out_ncid, nsamples_name, nsamples, &dimid));
442 dimlist_add_node(out_dim_headp, dimid, nsamples_name, nsamples);
445 wrap_nc(nc_def_dim(out_ncid, hour_name, HOURS_PER_DAY, &dimid));
446 dimlist_add_node(out_dim_headp, dimid, hour_name, HOURS_PER_DAY);
448 /* Track hour dimid for out files */
449 *out_hour_dimidp = dimid;
456 fprintf(stderr, "WARNING: No dimensions defined!\n");
462 int put_gatts(struct text_att *in_gatt_head, int out_ncid, char *out_history)
465 * Write out global attributes matching those of the input file.
466 * Change history attribute to the string passed in as an argument.
468 int j, hflag = 0, ngatts;
470 struct text_att *anode;
475 anode = in_gatt_head;
476 for (j = 0; j == 0 || anode != in_gatt_head; j++) {
477 if (!strcmp(anode->name, history_name)) {
482 value = anode->value;
483 wrap_nc(nc_put_att_text(out_ncid, NC_GLOBAL, anode->name, strlen(value), value));
487 /* If no history attribute on input, add one to the output */
489 wrap_nc(nc_put_att_text(out_ncid, NC_GLOBAL, history_name, strlen(out_history), out_history));
494 fprintf(stderr, "WARNING: No global attributes defined!\n");
500 int translate_dimids(struct dim **in_dim_idx, char metadata, int in_ndims, int *in_dimids, int *in2out, int *out_dimids, int hour_dimid)
503 * Translate between input dimension IDs and those for the output file.
504 * For normal time-based variables, add a new dimension for hour of
505 * day. For metadata variables, do not add new dimensions, even if
506 * it is time-based. Return (possibly new) number of dimensions.
510 for (i = ndims = 0; i < in_ndims; i++, ndims++) {
511 out_dimids[ndims] = in2out[in_dimids[i]];
512 if (!strcmp((in_dim_idx[in_dimids[i]])->name, time_name)
515 out_dimids[ndims] = hour_dimid;
522 int copy_att(char metadata, int stat_type, int in_natts, int in_ncid,
523 int in_varid, int out_ncid, int out_varid)
526 * Copy the attributes of the input variable to those of the output
527 * variable. If the variable is not a metadata variable, ensure that
528 * the cell_method attribute is set either to "time: mean" or
529 * "time: standard_deviation", depending on the output file type.
533 char name[NC_MAX_NAME+1], cmflag = 0;
534 char *cell_methods[] = { "time: mean", "time: standard_deviation" };
536 for (i = natts = 0; i < in_natts; i++, natts++) {
537 wrap_nc(nc_inq_attname(in_ncid, in_varid, i, name));
538 if (!strcmp(name, cell_method_name) && !metadata) {
539 wrap_nc(nc_put_att_text(out_ncid, out_varid, cell_method_name, strlen(cell_methods[stat_type]), cell_methods[stat_type]));
543 wrap_nc(nc_copy_att(in_ncid, in_varid, name, out_ncid, out_varid));
546 * If no cell_method attribute was specified for a non-metadata
547 * variable on the input file, add the appropriate cell_method anyway
548 * on the output file.
550 if (!cmflag && !metadata) {
551 wrap_nc(nc_put_att_text(out_ncid, out_varid, cell_method_name, strlen(cell_methods[stat_type]), cell_methods[stat_type]));
558 int define_vars(int in_ncid, struct dim **in_dim_idx, struct var *in_var_head,
559 int *in2out, int out_ncid, int hour_dimid, struct var **out_var_headp,
563 * Define variables on output file and copy attributes from input file.
564 * Return number of variables defined.
566 int j, varid, nvars, ndims, dimids[NC_MAX_VAR_DIMS], natts;
574 * March through input variables creating (mostly) the same
575 * variables on the output file.
577 for (j = 0; j == 0 || vnode != in_var_head; j++) {
578 ndims = translate_dimids(in_dim_idx, vnode->metadata, vnode->ndims, vnode->dimids, in2out, dimids, hour_dimid);
579 wrap_nc(nc_def_var(out_ncid, vnode->name, vnode->nctype, ndims, dimids, &varid));
580 natts = copy_att(vnode->metadata, stat_type, vnode->natts, in_ncid, vnode->ncvarid, out_ncid, varid);
581 varlist_add_node(out_var_headp, varid, vnode->name, vnode->nctype, ndims, dimids, natts, vnode->FillFlag, vnode->FillValue);
584 * Just after the "time" variable, add the new "hour"
585 * variable for hour of day.
587 if (!strcmp(vnode->name, time_name)) {
589 dimids[0] = hour_dimid;
590 wrap_nc(nc_def_var(out_ncid, hour_name, NC_FLOAT, ndims, dimids, &varid));
591 wrap_nc(nc_put_att_text(out_ncid, varid, "long_name", strlen(hour_long_name), hour_long_name));
592 wrap_nc(nc_put_att_text(out_ncid, varid, "units", strlen(hour_units), hour_units));
593 varlist_add_node(out_var_headp, varid, hour_name, NC_FLOAT, ndims, dimids, 2, 0, 0.0);
601 fprintf(stderr, "WARNING: No variables defined!\n");
607 void *alloc_var(nc_type nctype, size_t len)
613 if (!(val = (float *)malloc((len) * sizeof(float)))) {
614 perror("alloc_var: val");
619 if (!(val = (int *)malloc((len) * sizeof(int)))) {
620 perror("alloc_var: val");
625 if (!(val = (double *)malloc((len) * sizeof(double)))) {
626 perror("alloc_var: val");
631 if (!(val = (char *)malloc(((len)+1) * sizeof(char)))) {
632 perror("alloc_var: val");
637 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
644 void *read_var(int ncid, int varid, nc_type nctype, int ndims, int *dimids, struct dim **dim_idx)
647 size_t len = (size_t)1;
650 /* Figure out the total size */
651 for (i = 0; i < ndims; i++) len *= (dim_idx[dimids[i]])->len;
653 val = alloc_var(nctype, len);
657 wrap_nc(nc_get_var_float(ncid, varid, val));
660 wrap_nc(nc_get_var_int(ncid, varid, val));
663 wrap_nc(nc_get_var_double(ncid, varid, val));
666 wrap_nc(nc_get_var_text(ncid, varid, val));
669 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
676 void *read_timeslice(int ncid, int varid, nc_type nctype, int ndims, int *dimids, struct dim **dim_idx, size_t tslice)
679 size_t len = (size_t)1, start[NC_MAX_VAR_DIMS], count[NC_MAX_VAR_DIMS];
682 /* Make sure time is really the first dimension */
683 if (strcmp((dim_idx[dimids[0]])->name, time_name)) {
684 fprintf(stderr, "read_timeslice: %s is not first dimension of variable!\n", time_name);
688 * Figure out the total size for the slice (start at second dimension)
689 * and build start/count vectors.
693 for (i = 1; i < ndims; i++) {
695 count[i] = (dim_idx[dimids[i]])->len;
696 len *= (dim_idx[dimids[i]])->len;
699 val = alloc_var(nctype, len);
703 wrap_nc(nc_get_vara_float(ncid, varid, start, count, val));
706 wrap_nc(nc_get_vara_int(ncid, varid, start, count, val));
709 wrap_nc(nc_get_vara_double(ncid, varid, start, count, val));
712 wrap_nc(nc_get_vara_text(ncid, varid, start, count, val));
715 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
722 void write_var(int ncid, int varid, nc_type nctype, void *val)
726 wrap_nc(nc_put_var_float(ncid, varid, val));
729 wrap_nc(nc_put_var_int(ncid, varid, val));
732 wrap_nc(nc_put_var_double(ncid, varid, val));
735 wrap_nc(nc_put_var_text(ncid, varid, val));
738 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
745 void write_timeslice(int ncid, int varid, nc_type nctype, int ndims, int *dimids, struct dim **dim_idx, void *val, size_t tslice)
748 size_t start[NC_MAX_VAR_DIMS], count[NC_MAX_VAR_DIMS];
750 /* Make sure time is really the first dimension */
751 if (strcmp((dim_idx[dimids[0]])->name, time_name)) {
752 fprintf(stderr, "write_timeslice: %s is not first dimension of variable!\n", time_name);
756 /* Build start/count vectors */
759 for (i = 1; i < ndims; i++) {
761 count[i] = (dim_idx[dimids[i]])->len;
766 wrap_nc(nc_put_vara_float(ncid, varid, start, count, val));
769 wrap_nc(nc_put_vara_int(ncid, varid, start, count, val));
772 wrap_nc(nc_put_vara_double(ncid, varid, start, count, val));
775 wrap_nc(nc_put_vara_text(ncid, varid, start, count, val));
778 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
785 void copy_metadata(int in_ncid, struct var *in_var_head,
786 struct dim **in_dim_idx, int out_ncid, struct var *out_var_head)
789 float hr[HOURS_PER_DAY];
790 struct var *in_vnode, *out_vnode;
793 for (i = 0; i < HOURS_PER_DAY; i++) hr[i] = (float)i;
796 in_vnode = in_var_head;
798 * March through input variables to stuff metadata values into
799 * the new output files. NOTE: Time-based metadata variables
800 * should contain only the last (time-slice) value (from all
803 for (j = 0; j == 0 || in_vnode != in_var_head; j++) {
804 if (in_vnode->metadata) {
805 out_vnode = varlist_find_by_name(out_var_head, in_vnode->name);
806 if (strcmp((input_dim_idx[in_vnode->dimids[0]])->name, time_name)) {
807 /* time is not the first dimension */
809 printf("Copying metadata variable %s\n",
812 val = read_var(in_ncid, in_vnode->ncvarid, in_vnode->nctype, in_vnode->ndims, in_vnode->dimids, in_dim_idx);
813 write_var(out_ncid, out_vnode->ncvarid, out_vnode->nctype, val);
816 /* time is the first dimension */
818 printf("Copying last value of \
819 time-based metadata variable %s\n", in_vnode->name);
821 val = read_timeslice(in_ncid, in_vnode->ncvarid, in_vnode->nctype, in_vnode->ndims, in_vnode->dimids, in_dim_idx, ((input_dim_idx[in_vnode->dimids[0]])->len - 1));
822 write_timeslice(out_ncid, out_vnode->ncvarid, out_vnode->nctype, in_vnode->ndims, in_vnode->dimids, in_dim_idx, val, 0);
826 * Just after the "time" variable, write out
827 * the "hour" variable values.
829 if (!strcmp(in_vnode->name, time_name)) {
830 out_vnode = varlist_find_by_name(out_var_head, hour_name);
831 write_var(out_ncid, out_vnode->ncvarid,
832 out_vnode->nctype, hr);
837 printf("Skipping variable %s\n",
841 in_vnode = in_vnode->next;
848 void open_inout(char *input_fname, char *mean_fname, char *stddev_fname, char *flist, size_t nsamples)
850 char *mean_history_gatt, *stddev_history_gatt,
851 *mean_prefix="Statistical means from history files:",
852 *stddev_prefix="Statistical standard deviations from history files:";
855 * Construct strings for history global attributes for the two output
858 if (!(mean_history_gatt = (char *)malloc((strlen(mean_prefix) + strlen(flist)+1)*sizeof(char)))) {
859 perror("open_inout:mean_history_gatt");
862 if (!(stddev_history_gatt = (char *)malloc((strlen(stddev_prefix) + strlen(flist)+1)*sizeof(char)))) {
863 perror("open_inout:stddev_history_gatt");
866 sprintf(mean_history_gatt, "%s%s", mean_prefix, flist);
867 sprintf(stddev_history_gatt, "%s%s", stddev_prefix, flist);
869 /* Open input file */
870 wrap_nc(nc_open(input_fname, NC_NOWRITE, &input_ncid));
871 /* Inquire about number of dimensions, variables, global attributes
872 * and the ID of the unlimited dimension
874 wrap_nc(nc_inq(input_ncid, &input_ndims, &input_nvars, &input_ngatts,
876 /* Grab dimension IDs and lengths from input file */
877 get_dimensions(input_ncid, input_ndims, &input_dim_head, &input_dim_idx);
878 /* Grab desired global attributes from input file */
879 get_gatts(input_ncid, input_ngatts, &input_gatt_head);
880 /* Grab variable IDs and attributes from input file */
881 get_vars(input_ncid, input_nvars, &input_var_head);
883 varlist_print(input_var_head);
885 /* Create netCDF files for monthly means and standard deviations */
886 /* Create new netCDF files */
887 wrap_nc(nc_create(mean_fname, NC_NOCLOBBER, &mean_ncid));
888 wrap_nc(nc_create(stddev_fname, NC_NOCLOBBER, &stddev_ncid));
889 /* Define dimensions */
890 mean_ndims = put_dimensions(input_dim_head, input_ndims,
891 input_unlimdimid, nsamples, &idid2mdid, mean_ncid,
892 &mean_dim_head, &mean_unlimdimid, &mean_hour_dimid);
893 stddev_ndims = put_dimensions(input_dim_head, input_ndims,
894 input_unlimdimid, nsamples, &idid2sdid, stddev_ncid,
895 &stddev_dim_head, &stddev_unlimdimid, &stddev_hour_dimid);
896 /* Define variables and their attributes */
897 mean_nvars = define_vars(input_ncid, input_dim_idx, input_var_head,
898 idid2mdid, mean_ncid, mean_hour_dimid, &mean_var_head,
900 stddev_nvars = define_vars(input_ncid, input_dim_idx, input_var_head,
901 idid2sdid, stddev_ncid, stddev_hour_dimid, &stddev_var_head,
903 /* Store global attributes */
904 mean_ngatts = put_gatts(input_gatt_head, mean_ncid, mean_history_gatt);
905 stddev_ngatts = put_gatts(input_gatt_head, stddev_ncid,
906 stddev_history_gatt);
907 /* End define mode */
908 wrap_nc(nc_enddef(mean_ncid));
909 wrap_nc(nc_enddef(stddev_ncid));
910 /* Write out metdata variables that do not depend on "time" */
911 copy_metadata(input_ncid, input_var_head, input_dim_idx, mean_ncid,
913 copy_metadata(input_ncid, input_var_head, input_dim_idx, stddev_ncid,
916 wrap_nc(nc_close(input_ncid));
921 size_t count_samples(int nifnames, char **input_fnames)
924 char name[NC_MAX_NAME+1];
927 for (i = 0; i < nifnames; i++) {
928 /* Open input file */
929 wrap_nc(nc_open(input_fnames[i], NC_NOWRITE, &input_ncid));
931 * Inquire about number of dimensions, variables, global
932 * attributes and the ID of the unlimited dimension
934 wrap_nc(nc_inq(input_ncid, &input_ndims, &input_nvars,
935 &input_ngatts, &input_unlimdimid));
936 wrap_nc(nc_inq_dim(input_ncid, input_unlimdimid, name, &len));
937 if (strcmp(name, time_name)) {
938 fprintf(stderr, "%s is not the unlimited dimension for file %s!\n", time_name, input_fnames[i]);
942 printf("%ld samples in %s\n", (long)len, input_fnames[i]);
944 wrap_nc(nc_close(input_ncid));
950 void alloc_means_stddevs(size_t d1, size_t d2, double ***meansp, double ***stddevsp, size_t ***cell_samplesp)
953 * Allocate space for arrays of means and standard deviations by
957 size_t **cell_samples;
958 double **means, **stddevs;
960 if (!(*meansp = (double **)malloc(HOURS_PER_DAY * sizeof(double *)))) {
961 perror("alloc_means_stddevs:*meansp");
964 if (!(*stddevsp = (double **)malloc(HOURS_PER_DAY * sizeof(double *)))) {
965 perror("alloc_means_stddevs:*stddevsp");
968 if (!(*cell_samplesp = (size_t **)malloc(HOURS_PER_DAY * sizeof(size_t *)))) {
969 perror("alloc_means_stddevs:*cell_samplesp");
975 cell_samples = *cell_samplesp;
977 for (i = 0; i < HOURS_PER_DAY; i++) {
978 if (!(means[i] = (double *)malloc(d1 * d2 * sizeof(double)))) {
979 perror("alloc_means_stddevs:means[i]");
982 if (!(stddevs[i] = (double *)malloc(d1 * d2 * sizeof(double)))) {
983 perror("alloc_means_stddevs:stddevs[i]");
986 if (!(cell_samples[i] = (size_t *)malloc(d1 * d2 * sizeof(size_t)))) {
987 perror("alloc_means_stddevs:cell_samples[i]");
995 void init_means_stddevs(size_t d1, size_t d2, double **means, double **stddevs, size_t **cell_samples, float FillValue)
999 for (hr = 0; hr < HOURS_PER_DAY; hr++) {
1000 for (i = 0; i < d1; i++) {
1001 #pragma _CRI concurrent
1002 for (j = 0; j < d2; j++) {
1004 means[hr][pos] = FillValue;
1005 stddevs[hr][pos] = FillValue;
1006 cell_samples[hr][pos] = 0;
1014 void reset_cell_samples(size_t d1, size_t d2, size_t **cell_samples)
1018 for (hr = 0; hr < HOURS_PER_DAY; hr++) {
1019 for (i = 0; i < d1; i++) {
1020 #pragma _CRI concurrent
1021 for (j = 0; j < d2; j++) {
1023 cell_samples[hr][pos] = 0;
1031 void free_means_stddevs(double **means, double **stddevs, size_t **cell_samples)
1034 * Free space from arrays of means and standard deviations by
1039 for (i = 0; i < HOURS_PER_DAY; i++) {
1042 free(cell_samples[i]);
1052 void add_to_means_float(float *val, int sec, size_t d1, size_t d2,
1053 char FillFlag, float FillValue, double **means, size_t **cell_samples)
1057 hr = (int)floor((double)sec/(double)SEC_PER_HOUR);
1059 for (i = 0; i < d1; i++) {
1060 #pragma _CRI concurrent
1061 for (j = 0; j < d2; j++) {
1063 if (!FillFlag || (FillFlag && val[pos] != FillValue)) {
1064 if (cell_samples[hr][pos] == 0)
1065 means[hr][pos] = (double)val[pos];
1067 means[hr][pos] += (double)val[pos];
1068 ++cell_samples[hr][pos];
1076 void add_to_means_double(double *val, int sec, size_t d1, size_t d2,
1077 char FillFlag, float FillValue, double **means, size_t **cell_samples)
1081 hr = (int)floor((double)sec/(double)SEC_PER_HOUR);
1083 for (i = 0; i < d1; i++) {
1084 #pragma _CRI concurrent
1085 for (j = 0; j < d2; j++) {
1087 if (!FillFlag || (FillFlag && val[pos] != FillValue)) {
1088 if (cell_samples[hr][pos] == 0)
1089 means[hr][pos] = val[pos];
1091 means[hr][pos] += val[pos];
1092 ++cell_samples[hr][pos];
1101 void divide_means(size_t d1, size_t d2, double **means, size_t **cell_samples)
1105 for (hr = 0; hr < HOURS_PER_DAY; hr++) {
1106 for (i = 0; i < d1; i++) {
1107 #pragma _CRI concurrent
1108 for (j = 0; j < d2; j++) {
1110 if (cell_samples[hr][pos] != 0) {
1111 means[hr][pos] /= (double)cell_samples[hr][pos];
1120 void add_to_stddevs_float(float *val, int sec, size_t d1, size_t d2,
1121 char FillFlag, float FillValue, double **means, double **stddevs,
1122 size_t **cell_samples)
1127 hr = (int)floor((double)sec/(double)SEC_PER_HOUR);
1129 for (i = 0; i < d1; i++) {
1130 #pragma _CRI concurrent
1131 for (j = 0; j < d2; j++) {
1133 if (!FillFlag || (FillFlag && val[pos] != FillValue
1134 && means[hr][pos] != FillValue)) {
1135 delta = means[hr][pos] - (double)val[pos];
1136 if (cell_samples[hr][pos] == 0)
1137 stddevs[hr][pos] = delta * delta;
1139 stddevs[hr][pos] += delta * delta;
1140 ++cell_samples[hr][pos];
1148 void add_to_stddevs_double(double *val, int sec, size_t d1, size_t d2,
1149 char FillFlag, float FillValue, double **means, double **stddevs,
1150 size_t **cell_samples)
1155 hr = (int)floor((double)sec/(double)SEC_PER_HOUR);
1157 for (i = 0; i < d1; i++) {
1158 #pragma _CRI concurrent
1159 for (j = 0; j < d2; j++) {
1161 if (!FillFlag || (FillFlag && val[pos] != FillValue
1162 && means[hr][pos] != FillValue)) {
1163 delta = means[hr][pos] - val[pos];
1164 if (cell_samples[hr][pos] == 0)
1165 stddevs[hr][pos] = delta * delta;
1167 stddevs[hr][pos] += delta * delta;
1168 ++cell_samples[hr][pos];
1177 void divide_sqrt_stddevs(size_t d1, size_t d2, double **stddevs, size_t **cell_samples)
1181 for (hr = 0; hr < HOURS_PER_DAY; hr++) {
1182 for (i = 0; i < d1; i++) {
1183 #pragma _CRI concurrent
1184 for (j = 0; j < d2; j++) {
1186 if (cell_samples[hr][pos] != 0) {
1187 stddevs[hr][pos] /= (double)cell_samples[hr][pos];
1188 stddevs[hr][pos] = sqrt(stddevs[hr][pos]);
1197 void alloc_all_samples(size_t d1, size_t d2, size_t nsamples, nc_type nctype, void ***valsp, int **mcsecp)
1202 /* Allocate space for all timeslices */
1203 if (!(*valsp = (void **)malloc(nsamples * sizeof(void *)))) {
1204 perror("alloc_all_samples:*valsp");
1208 for (i = 0; i < nsamples; i++) {
1209 vals[i] = alloc_var(nctype, (d1 * d2));
1211 if (!(*mcsecp = (int *)malloc(nsamples * sizeof(int)))) {
1212 perror("alloc_all_samples:*mcsecp");
1219 void init_all_samples(size_t d1, size_t d2, size_t nsamples, nc_type nctype, void **vals, int *mcsec)
1222 float **fvals = NULL;
1223 double **dvals = NULL;
1227 fvals = (float **)vals;
1230 dvals = (double **)vals;
1233 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
1237 for (i = 0; i < nsamples; i++) {
1238 for (j = 0; j < (d1 * d2); j++) {
1241 fvals[i][j] = (float)0.0;
1244 dvals[i][j] = (double)0.0;
1247 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
1257 void free_all_samples(size_t nsamples, void **vals, int *mcsec)
1261 for (i = 0; i < nsamples; i++)
1262 if (vals[i]) free(vals[i]);
1270 void read_all_samples(int nifnames, char **input_fnames, size_t d1, size_t d2, size_t nsamples, char *var_name, nc_type nctype, int level, int ndims, void **vals, int *mcsec)
1274 size_t len, nslice = 0, start[NC_MAX_VAR_DIMS], count[NC_MAX_VAR_DIMS];
1275 char name[NC_MAX_NAME+1];
1277 for (i = 0; i < nifnames; i++) {
1279 printf("\tOpening %s", input_fnames[i]);
1280 if (ndims > 3) printf(" to retrieve level %d\n", level);
1282 /* Open input file */
1283 wrap_nc(nc_open(input_fnames[i], NC_NOWRITE, &input_ncid));
1285 * Inquire about number of dimensions, variables, global
1286 * attributes and the ID of the unlimited dimension
1288 wrap_nc(nc_inq(input_ncid, &input_ndims, &input_nvars,
1289 &input_ngatts, &input_unlimdimid));
1290 wrap_nc(nc_inq_dim(input_ncid, input_unlimdimid, name, &len));
1291 if (strcmp(name, time_name)) {
1292 fprintf(stderr, "%s is not the unlimited dimension for file %s!\n", time_name, input_fnames[i]);
1295 /* Make sure we don't run off the end of allocated space */
1296 if ((nslice+len) > nsamples) {
1297 fprintf(stderr, "Number of time slices exceeds previous count of %ld!\n", (long)nsamples);
1300 /* Get seconds of day variable */
1301 wrap_nc(nc_inq_varid(input_ncid, mcsec_name, &varid));
1302 wrap_nc(nc_get_var_int(input_ncid, varid, &mcsec[nslice]));
1303 /* Get variable ID for requested variable */
1304 wrap_nc(nc_inq_varid(input_ncid, var_name, &varid));
1305 /* Retrieve time slice of desired variable */
1306 for (ts = 0; ts < len; ts++) {
1309 start[(ndims-2)] = 0;
1310 count[(ndims-2)] = d1;
1311 start[(ndims-1)] = 0;
1312 count[(ndims-1)] = d2;
1319 wrap_nc(nc_get_vara_float(input_ncid, varid, start, count, vals[nslice]));
1322 wrap_nc(nc_get_vara_double(input_ncid, varid, start, count, vals[nslice]));
1325 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
1331 /* Close input file */
1332 wrap_nc(nc_close(input_ncid));
1338 void compute(size_t d1, size_t d2, size_t nsamples, nc_type nctype, void **vals, int *mcsec, char FillFlag, float FillValue, int stat_type, double **means, double **stddevs, size_t **cell_samples)
1342 for (i = 0; i < nsamples; i++) {
1345 if (stat_type == MEAN_TYPE)
1346 add_to_means_float(vals[i], mcsec[i], d1, d2, FillFlag, FillValue, means, cell_samples);
1348 add_to_stddevs_float(vals[i], mcsec[i], d1, d2, FillFlag, FillValue, means, stddevs, cell_samples);
1351 if (stat_type == MEAN_TYPE)
1352 add_to_means_double(vals[i], mcsec[i], d1, d2, FillFlag, FillValue, means, cell_samples);
1354 add_to_stddevs_double(vals[i], mcsec[i], d1, d2, FillFlag, FillValue, means, stddevs, cell_samples);
1357 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
1362 /* Divide sums by number of samples */
1363 if (stat_type == MEAN_TYPE)
1364 divide_means(d1, d2, means, cell_samples);
1366 divide_sqrt_stddevs(d1, d2, stddevs, cell_samples);
1371 float *double_to_float(size_t len, double *dvar)
1376 if (!(fvar = (float *)malloc(len * sizeof(float)))) {
1377 perror("double_to_float:fvar");
1381 for (i = 0; i < len; i++)
1382 fvar[i] = (float)dvar[i];
1387 void write_var_hours(int ncid, int varid, nc_type nctype, size_t d1, size_t d2,
1388 int level, int ndims, double **var_hours)
1390 /* Output dimensions should be one larger than input dimensions */
1392 size_t start[NC_MAX_VAR_DIMS], count[NC_MAX_VAR_DIMS];
1395 if (nctype == NC_FLOAT) {
1396 if (!(fvar = (float *)malloc(d1 * d2 * sizeof(float)))) {
1397 perror("write_var_hours:fvar");
1402 for (hr = 0; hr < HOURS_PER_DAY; hr++) {
1404 count[0] = 1; /* time */
1406 count[1] = 1; /* hour */
1407 start[(ndims-2)] = 0;
1408 count[(ndims-2)] = d1;
1409 start[(ndims-1)] = 0;
1410 count[(ndims-1)] = d2;
1417 for (i = 0; i < (d1 * d2); i++)
1418 fvar[i] = (float)var_hours[hr][i];
1419 wrap_nc(nc_put_vara_float(ncid, varid, start, count, fvar));
1422 wrap_nc(nc_put_vara_double(ncid, varid, start, count, var_hours[hr]));
1425 fprintf(stderr, "netCDF external data type %d not supported\n", nctype);
1430 if (nctype == NC_FLOAT)
1436 void compute_stats(int nifnames, char **input_fnames, size_t nsamples)
1438 int j, k, nlevels, *mcsec;
1439 size_t d1, d2, **cell_samples;
1440 double **means, **stddevs;
1441 struct var *in_vnode, *out_vnode;
1444 if (input_var_head) {
1445 in_vnode = input_var_head;
1446 /* March through non-metadata variables to compute stats */
1447 for (j = 0; j == 0 || in_vnode != input_var_head; j++) {
1448 if (!in_vnode->metadata) {
1449 /* Make sure time is really the first dimension */
1450 if (strcmp((input_dim_idx[in_vnode->dimids[0]])->name, time_name)) {
1451 fprintf(stderr, "compute_stats: %s is not first dimension of variable %s!\n", time_name, in_vnode->name);
1454 /* Figure out input dimensions */
1455 if (in_vnode->ndims < 3 || in_vnode->ndims > 4) {
1456 fprintf(stderr, "compute_stats: %s has %d dimensions!\n", in_vnode->name, in_vnode->ndims);
1459 /* Assume 2D output; find dimensions */
1460 d1 = (input_dim_idx[in_vnode->dimids[(in_vnode->ndims-2)]])->len;
1461 d2 = (input_dim_idx[in_vnode->dimids[(in_vnode->ndims-1)]])->len;
1462 if (in_vnode->ndims == 3)
1465 nlevels = (input_dim_idx[in_vnode->dimids[1]])->len;
1466 /* Allocate working space for means and stddevs */
1467 alloc_means_stddevs(d1, d2, &means, &stddevs, &cell_samples);
1468 init_means_stddevs(d1, d2, means, stddevs, cell_samples, in_vnode->FillValue);
1469 /* Allocate and initize space for entire field across all files */
1470 alloc_all_samples(d1, d2, nsamples, in_vnode->nctype, &vals, &mcsec);
1471 init_all_samples(d1, d2, nsamples, in_vnode->nctype, vals, mcsec);
1472 printf("Computing statistics for %s\n",
1474 /* Compute means and stddevs, then write them */
1475 for (k = 0; k < nlevels; k++) {
1476 /* Read the entire fields from all files */
1477 read_all_samples(nifnames, input_fnames, d1, d2, nsamples, in_vnode->name, in_vnode->nctype, k, in_vnode->ndims, vals, mcsec);
1479 compute(d1, d2, nsamples, in_vnode->nctype, vals, mcsec, in_vnode->FillFlag, in_vnode->FillValue, MEAN_TYPE, means, stddevs, cell_samples);
1480 /* Find corresponding output variable on the mean output file */
1481 out_vnode = varlist_find_by_name(mean_var_head, in_vnode->name);
1482 /* Write out the means for this variable */
1483 write_var_hours(mean_ncid, out_vnode->ncvarid, out_vnode->nctype, d1, d2, k, out_vnode->ndims, means);
1484 /* Zero out cell_samples so they can be used as a flag again for computing stddevs */
1485 reset_cell_samples(d1, d2, cell_samples);
1486 /* Compute stddevs using means */
1487 compute(d1, d2, nsamples, in_vnode->nctype, vals, mcsec, in_vnode->FillFlag, in_vnode->FillValue, STDDEV_TYPE, means, stddevs, cell_samples);
1488 /* Find corresponding output variable on the stddev output file */
1489 out_vnode = varlist_find_by_name(stddev_var_head, in_vnode->name);
1490 /* Write out stddevs for this variable */
1491 write_var_hours(stddev_ncid, out_vnode->ncvarid, out_vnode->nctype, d1, d2, k, out_vnode->ndims, stddevs);
1494 /* Free all samples */
1495 free_all_samples(nsamples, vals, mcsec);
1496 /* Free working space for means and stddevs */
1497 free_means_stddevs(means, stddevs, cell_samples);
1499 in_vnode = in_vnode->next;
1505 void usage(char *program)
1507 fprintf(stderr, "Usage: %s -m mean.nc -s stddev.nc hist_file1.nc [ hist_file2.nc ... ]\n", program);
1508 fprintf(stderr, "WARNING: It is assumed that the list of input files is specified in time order!\n");
1512 int main(int argc, char **argv)
1514 int i, ic, nifnames;
1515 size_t len, pos, nsamples;
1516 char *mfname, *sfname, **ifnames, *flist;
1519 mfname = sfname = flist = NULL;
1523 printf("Number of metadata variables (nmvars) = %d\n", nmvars);
1527 /* check command-line flags and switches */
1528 while ((ic = getopt(argc, argv, "m:s:")) != -1) {
1531 if (!(mfname = strdup(optarg))) {
1537 if (!(sfname = strdup(optarg))) {
1546 fprintf(stderr, "Output file name for writing means required.\n");
1550 fprintf(stderr, "Output file name for writing standard deviations required.\n");
1554 if (optind < argc) {
1555 if (!(ifnames = (char **)malloc((argc-optind+1)*sizeof(char *)))) {
1559 for (i = optind; i < argc; i++) {
1560 if (!(ifnames[nifnames++] = strdup(argv[i]))) {
1561 perror("ifnames[nifnames++]");
1567 fprintf(stderr, "At least one input file name is required.\n");
1573 * Build list of input files to be included in the global history
1574 * attribute on the two outputs files.
1576 for (i = len = 0; i < nifnames; i++)
1577 len += strlen(ifnames[i]);
1578 len += nifnames + 1; /* space in front of every name + null terminator */
1579 if (!(flist = (char *)malloc(len * sizeof(char)))) {
1583 for (i = pos = 0; i < nifnames; pos += (strlen(ifnames[i])+1), i++)
1584 sprintf(&flist[pos], " %s", ifnames[i]);
1586 printf("flist=%s\n", flist);
1590 /* Open every file to count up the number of time samples. */
1591 nsamples = count_samples(nifnames, ifnames);
1593 printf("Number of samples across all files = %ld\n", (long)nsamples);
1598 * Open the *last* input file and the two output files (for mean and
1599 * standard deviation). Define dimensions, variables, and attributes
1600 * for the two output files based on the *last* input files. The
1601 * last file is used because some metadata variables will contain
1602 * only values from the last time slice from the period over which
1603 * the statistics are computed.
1605 open_inout(ifnames[(nifnames-1)], mfname, sfname, flist, nsamples);
1607 compute_stats(nifnames, ifnames, nsamples);
1609 /* Close the two output files */
1610 wrap_nc(nc_close(mean_ncid));
1611 wrap_nc(nc_close(stddev_ncid));