h1_summary.c
changeset 1 2ce4ee911439
parent 0 3c02cce30be8
child 3 d3122367777b
     1.1 --- a/h1_summary.c	Wed Sep 26 17:16:40 2007 -0400
     1.2 +++ b/h1_summary.c	Mon Oct 01 15:12:14 2007 -0400
     1.3 @@ -82,6 +82,7 @@
     1.4  };
     1.5  
     1.6  static char *time_name = "time";
     1.7 +static char *time_bounds_name = "time_bounds";
     1.8  static char *mcsec_name = "mcsec";
     1.9  static char *history_name = "history";
    1.10  static char *nsamples_name = "nsamples";
    1.11 @@ -783,7 +784,8 @@
    1.12  }
    1.13  
    1.14  void copy_metadata(int in_ncid, struct var *in_var_head,
    1.15 -	struct dim **in_dim_idx, int out_ncid, struct var *out_var_head)
    1.16 +	struct dim **in_dim_idx, int out_ncid, struct var *out_var_head,
    1.17 +	double *tbounds)
    1.18  {
    1.19  	int i, j;
    1.20  	float hr[HOURS_PER_DAY];
    1.21 @@ -819,7 +821,10 @@
    1.22  time-based metadata variable %s\n", in_vnode->name);
    1.23  #endif /* DEBUG */
    1.24  					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));
    1.25 -					write_timeslice(out_ncid, out_vnode->ncvarid, out_vnode->nctype, in_vnode->ndims, in_vnode->dimids, in_dim_idx, val, 0);
    1.26 +					if (!strcmp(in_vnode->name, time_bounds_name))
    1.27 +						write_timeslice(out_ncid, out_vnode->ncvarid, out_vnode->nctype, in_vnode->ndims, in_vnode->dimids, in_dim_idx, tbounds, 0);
    1.28 +					else
    1.29 +						write_timeslice(out_ncid, out_vnode->ncvarid, out_vnode->nctype, in_vnode->ndims, in_vnode->dimids, in_dim_idx, val, 0);
    1.30  				}
    1.31  				free(val);
    1.32  				/*
    1.33 @@ -845,11 +850,63 @@
    1.34  	return;
    1.35  }
    1.36  
    1.37 -void open_inout(char *input_fname, char *mean_fname, char *stddev_fname, char *flist, size_t nsamples)
    1.38 +void get_time_bounds(char *first_fname, char *last_fname, double *tbounds)
    1.39 +{
    1.40 +	int time_dimid, time_bounds_varid;
    1.41 +	size_t time_len, time_bounds_index[2];
    1.42 +	double bnd1, bnd2;
    1.43 +
    1.44 +	/* Open first input file */
    1.45 +	wrap_nc(nc_open(first_fname, NC_NOWRITE, &input_ncid));
    1.46 +	/* Get dimension ID of the time dimension */
    1.47 +	wrap_nc(nc_inq_dimid(input_ncid, time_name, &time_dimid));
    1.48 +	/* Get length of time dimension */
    1.49 +	wrap_nc(nc_inq_dimlen(input_ncid, time_dimid, &time_len));
    1.50 +	/* Get variable ID of time_bounds variable */
    1.51 +	wrap_nc(nc_inq_varid(input_ncid, time_bounds_name, &time_bounds_varid));
    1.52 +	/* Set index for reading the first value of time_bounds from the
    1.53 +	 * first timeslice of the first file */
    1.54 +	time_bounds_index[0] = time_bounds_index[1] = 0;
    1.55 +	/* Read the value */
    1.56 +	wrap_nc(nc_get_var1_double(input_ncid, time_bounds_varid,
    1.57 +		time_bounds_index, &bnd1));
    1.58 +	/* If the first and last file are not the same, close the first one and
    1.59 +	 * open the second one */
    1.60 +	if (strcmp(first_fname, last_fname)) {
    1.61 +		/* Close the first input file */
    1.62 +		wrap_nc(nc_close(input_ncid));
    1.63 +		/* Open the last input file */
    1.64 +		wrap_nc(nc_open(last_fname, NC_NOWRITE, &input_ncid));
    1.65 +		/* Get dimension ID of the time dimension */
    1.66 +		wrap_nc(nc_inq_dimid(input_ncid, time_name, &time_dimid));
    1.67 +		/* Get length of time dimension */
    1.68 +		wrap_nc(nc_inq_dimlen(input_ncid, time_dimid, &time_len));
    1.69 +		/* Get variable ID of time_bounds variable */
    1.70 +		wrap_nc(nc_inq_varid(input_ncid, time_bounds_name,
    1.71 +			&time_bounds_varid));
    1.72 +	}
    1.73 +	/* Set index for reading the second value of time_bounds from the
    1.74 +	 * last timeslice of the last file */
    1.75 +	time_bounds_index[0] = time_len - 1; time_bounds_index[1] = 1;
    1.76 +	/* Read the value */
    1.77 +	wrap_nc(nc_get_var1_double(input_ncid, time_bounds_varid,
    1.78 +		time_bounds_index, &bnd2));
    1.79 +
    1.80 +	/* Close the last input file */
    1.81 +	wrap_nc(nc_close(input_ncid));
    1.82 +
    1.83 +	tbounds[0] = bnd1;
    1.84 +	tbounds[1] = bnd2;
    1.85 +
    1.86 +	return;
    1.87 +}
    1.88 +
    1.89 +void open_inout(char *first_fname, char *last_fname, char *mean_fname, char *stddev_fname, char *flist, size_t nsamples)
    1.90  {
    1.91  	char *mean_history_gatt, *stddev_history_gatt,
    1.92  		*mean_prefix="Statistical means from history files:",
    1.93  		*stddev_prefix="Statistical standard deviations from history files:";
    1.94 +	double tbounds[2];
    1.95  
    1.96  	/*
    1.97  	 * Construct strings for history global attributes for the two output
    1.98 @@ -866,8 +923,16 @@
    1.99  	sprintf(mean_history_gatt, "%s%s", mean_prefix, flist);
   1.100  	sprintf(stddev_history_gatt, "%s%s", stddev_prefix, flist);
   1.101  
   1.102 -	/* Open input file */
   1.103 -	wrap_nc(nc_open(input_fname, NC_NOWRITE, &input_ncid));
   1.104 +	/* The two time_bounds values must be handled explicitly by obtaining
   1.105 +	 * the first one from the first file and the last one from the last
   1.106 +	 * file.  These are then passed to copy_metadata() below. */
   1.107 +	get_time_bounds(first_fname, last_fname, tbounds);
   1.108 +#ifdef DEBUG
   1.109 +	fprintf(stderr, "Got back tbounds=%lf,%lf\n", tbounds[0], tbounds[1]);
   1.110 +#endif /* DEBUG */
   1.111 +
   1.112 +	/* Open last input file */
   1.113 +	wrap_nc(nc_open(last_fname, NC_NOWRITE, &input_ncid));
   1.114  	/* Inquire about number of dimensions, variables, global attributes
   1.115  	 * and the ID of the unlimited dimension
   1.116  	 */
   1.117 @@ -909,9 +974,9 @@
   1.118  	wrap_nc(nc_enddef(stddev_ncid));
   1.119  	/* Write out metdata variables that do not depend on "time" */
   1.120  	copy_metadata(input_ncid, input_var_head, input_dim_idx, mean_ncid,
   1.121 -		mean_var_head);
   1.122 +		mean_var_head, tbounds);
   1.123  	copy_metadata(input_ncid, input_var_head, input_dim_idx, stddev_ncid,
   1.124 -		stddev_var_head);
   1.125 +		stddev_var_head, tbounds);
   1.126  
   1.127  	wrap_nc(nc_close(input_ncid));
   1.128  
   1.129 @@ -1513,7 +1578,8 @@
   1.130  	 * only values from the last time slice from the period over which
   1.131  	 * the statistics are computed.
   1.132  	 */
   1.133 -	open_inout(ifnames[(nifnames-1)], mfname, sfname, flist, nsamples);
   1.134 +	open_inout(ifnames[0], ifnames[(nifnames-1)], mfname, sfname, flist,
   1.135 +		nsamples);
   1.136  
   1.137  	compute_stats(nifnames, ifnames, nsamples);
   1.138