h1_summary.c
changeset 1 2ce4ee911439
parent 0 3c02cce30be8
child 3 d3122367777b
equal deleted inserted replaced
0:ebcbb562b168 1:9a0330997f5f
    80 	struct var *next;
    80 	struct var *next;
    81 	struct var *prev;
    81 	struct var *prev;
    82 };
    82 };
    83 
    83 
    84 static char *time_name = "time";
    84 static char *time_name = "time";
       
    85 static char *time_bounds_name = "time_bounds";
    85 static char *mcsec_name = "mcsec";
    86 static char *mcsec_name = "mcsec";
    86 static char *history_name = "history";
    87 static char *history_name = "history";
    87 static char *nsamples_name = "nsamples";
    88 static char *nsamples_name = "nsamples";
    88 static char *hour_name = "hour", *hour_long_name = "hour of day",
    89 static char *hour_name = "hour", *hour_long_name = "hour of day",
    89 	*hour_units = "hour";
    90 	*hour_units = "hour";
   781 
   782 
   782 	return;
   783 	return;
   783 }
   784 }
   784 
   785 
   785 void copy_metadata(int in_ncid, struct var *in_var_head,
   786 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)
   787 	struct dim **in_dim_idx, int out_ncid, struct var *out_var_head,
       
   788 	double *tbounds)
   787 {
   789 {
   788 	int i, j;
   790 	int i, j;
   789 	float hr[HOURS_PER_DAY];
   791 	float hr[HOURS_PER_DAY];
   790 	struct var *in_vnode, *out_vnode;
   792 	struct var *in_vnode, *out_vnode;
   791 	void *val;
   793 	void *val;
   817 #ifdef DEBUG
   819 #ifdef DEBUG
   818 					printf("Copying last value of \
   820 					printf("Copying last value of \
   819 time-based metadata variable %s\n", in_vnode->name);
   821 time-based metadata variable %s\n", in_vnode->name);
   820 #endif /* DEBUG */
   822 #endif /* DEBUG */
   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));
   823 					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);
   824 					if (!strcmp(in_vnode->name, time_bounds_name))
       
   825 						write_timeslice(out_ncid, out_vnode->ncvarid, out_vnode->nctype, in_vnode->ndims, in_vnode->dimids, in_dim_idx, tbounds, 0);
       
   826 					else
       
   827 						write_timeslice(out_ncid, out_vnode->ncvarid, out_vnode->nctype, in_vnode->ndims, in_vnode->dimids, in_dim_idx, val, 0);
   823 				}
   828 				}
   824 				free(val);
   829 				free(val);
   825 				/*
   830 				/*
   826 				 * Just after the "time" variable, write out
   831 				 * Just after the "time" variable, write out
   827 				 * the "hour" variable values.
   832 				 * the "hour" variable values.
   843 	}
   848 	}
   844 
   849 
   845 	return;
   850 	return;
   846 }
   851 }
   847 
   852 
   848 void open_inout(char *input_fname, char *mean_fname, char *stddev_fname, char *flist, size_t nsamples)
   853 void get_time_bounds(char *first_fname, char *last_fname, double *tbounds)
       
   854 {
       
   855 	int time_dimid, time_bounds_varid;
       
   856 	size_t time_len, time_bounds_index[2];
       
   857 	double bnd1, bnd2;
       
   858 
       
   859 	/* Open first input file */
       
   860 	wrap_nc(nc_open(first_fname, NC_NOWRITE, &input_ncid));
       
   861 	/* Get dimension ID of the time dimension */
       
   862 	wrap_nc(nc_inq_dimid(input_ncid, time_name, &time_dimid));
       
   863 	/* Get length of time dimension */
       
   864 	wrap_nc(nc_inq_dimlen(input_ncid, time_dimid, &time_len));
       
   865 	/* Get variable ID of time_bounds variable */
       
   866 	wrap_nc(nc_inq_varid(input_ncid, time_bounds_name, &time_bounds_varid));
       
   867 	/* Set index for reading the first value of time_bounds from the
       
   868 	 * first timeslice of the first file */
       
   869 	time_bounds_index[0] = time_bounds_index[1] = 0;
       
   870 	/* Read the value */
       
   871 	wrap_nc(nc_get_var1_double(input_ncid, time_bounds_varid,
       
   872 		time_bounds_index, &bnd1));
       
   873 	/* If the first and last file are not the same, close the first one and
       
   874 	 * open the second one */
       
   875 	if (strcmp(first_fname, last_fname)) {
       
   876 		/* Close the first input file */
       
   877 		wrap_nc(nc_close(input_ncid));
       
   878 		/* Open the last input file */
       
   879 		wrap_nc(nc_open(last_fname, NC_NOWRITE, &input_ncid));
       
   880 		/* Get dimension ID of the time dimension */
       
   881 		wrap_nc(nc_inq_dimid(input_ncid, time_name, &time_dimid));
       
   882 		/* Get length of time dimension */
       
   883 		wrap_nc(nc_inq_dimlen(input_ncid, time_dimid, &time_len));
       
   884 		/* Get variable ID of time_bounds variable */
       
   885 		wrap_nc(nc_inq_varid(input_ncid, time_bounds_name,
       
   886 			&time_bounds_varid));
       
   887 	}
       
   888 	/* Set index for reading the second value of time_bounds from the
       
   889 	 * last timeslice of the last file */
       
   890 	time_bounds_index[0] = time_len - 1; time_bounds_index[1] = 1;
       
   891 	/* Read the value */
       
   892 	wrap_nc(nc_get_var1_double(input_ncid, time_bounds_varid,
       
   893 		time_bounds_index, &bnd2));
       
   894 
       
   895 	/* Close the last input file */
       
   896 	wrap_nc(nc_close(input_ncid));
       
   897 
       
   898 	tbounds[0] = bnd1;
       
   899 	tbounds[1] = bnd2;
       
   900 
       
   901 	return;
       
   902 }
       
   903 
       
   904 void open_inout(char *first_fname, char *last_fname, char *mean_fname, char *stddev_fname, char *flist, size_t nsamples)
   849 {
   905 {
   850 	char *mean_history_gatt, *stddev_history_gatt,
   906 	char *mean_history_gatt, *stddev_history_gatt,
   851 		*mean_prefix="Statistical means from history files:",
   907 		*mean_prefix="Statistical means from history files:",
   852 		*stddev_prefix="Statistical standard deviations from history files:";
   908 		*stddev_prefix="Statistical standard deviations from history files:";
       
   909 	double tbounds[2];
   853 
   910 
   854 	/*
   911 	/*
   855 	 * Construct strings for history global attributes for the two output
   912 	 * Construct strings for history global attributes for the two output
   856 	 * files.
   913 	 * files.
   857 	 */
   914 	 */
   864 		exit(2);
   921 		exit(2);
   865 	}
   922 	}
   866 	sprintf(mean_history_gatt, "%s%s", mean_prefix, flist);
   923 	sprintf(mean_history_gatt, "%s%s", mean_prefix, flist);
   867 	sprintf(stddev_history_gatt, "%s%s", stddev_prefix, flist);
   924 	sprintf(stddev_history_gatt, "%s%s", stddev_prefix, flist);
   868 
   925 
   869 	/* Open input file */
   926 	/* The two time_bounds values must be handled explicitly by obtaining
   870 	wrap_nc(nc_open(input_fname, NC_NOWRITE, &input_ncid));
   927 	 * the first one from the first file and the last one from the last
       
   928 	 * file.  These are then passed to copy_metadata() below. */
       
   929 	get_time_bounds(first_fname, last_fname, tbounds);
       
   930 #ifdef DEBUG
       
   931 	fprintf(stderr, "Got back tbounds=%lf,%lf\n", tbounds[0], tbounds[1]);
       
   932 #endif /* DEBUG */
       
   933 
       
   934 	/* Open last input file */
       
   935 	wrap_nc(nc_open(last_fname, NC_NOWRITE, &input_ncid));
   871 	/* Inquire about number of dimensions, variables, global attributes
   936 	/* Inquire about number of dimensions, variables, global attributes
   872 	 * and the ID of the unlimited dimension
   937 	 * and the ID of the unlimited dimension
   873 	 */
   938 	 */
   874 	wrap_nc(nc_inq(input_ncid, &input_ndims, &input_nvars, &input_ngatts,
   939 	wrap_nc(nc_inq(input_ncid, &input_ndims, &input_nvars, &input_ngatts,
   875 		&input_unlimdimid));
   940 		&input_unlimdimid));
   907 	/* End define mode */
   972 	/* End define mode */
   908 	wrap_nc(nc_enddef(mean_ncid));
   973 	wrap_nc(nc_enddef(mean_ncid));
   909 	wrap_nc(nc_enddef(stddev_ncid));
   974 	wrap_nc(nc_enddef(stddev_ncid));
   910 	/* Write out metdata variables that do not depend on "time" */
   975 	/* Write out metdata variables that do not depend on "time" */
   911 	copy_metadata(input_ncid, input_var_head, input_dim_idx, mean_ncid,
   976 	copy_metadata(input_ncid, input_var_head, input_dim_idx, mean_ncid,
   912 		mean_var_head);
   977 		mean_var_head, tbounds);
   913 	copy_metadata(input_ncid, input_var_head, input_dim_idx, stddev_ncid,
   978 	copy_metadata(input_ncid, input_var_head, input_dim_idx, stddev_ncid,
   914 		stddev_var_head);
   979 		stddev_var_head, tbounds);
   915 
   980 
   916 	wrap_nc(nc_close(input_ncid));
   981 	wrap_nc(nc_close(input_ncid));
   917 
   982 
   918 	return;
   983 	return;
   919 }
   984 }
  1511 	 * for the two output files based on the *last* input files.  The
  1576 	 * for the two output files based on the *last* input files.  The
  1512 	 * last file is used because some metadata variables will contain
  1577 	 * last file is used because some metadata variables will contain
  1513 	 * only values from the last time slice from the period over which
  1578 	 * only values from the last time slice from the period over which
  1514 	 * the statistics are computed.
  1579 	 * the statistics are computed.
  1515 	 */
  1580 	 */
  1516 	open_inout(ifnames[(nifnames-1)], mfname, sfname, flist, nsamples);
  1581 	open_inout(ifnames[0], ifnames[(nifnames-1)], mfname, sfname, flist,
       
  1582 		nsamples);
  1517 
  1583 
  1518 	compute_stats(nifnames, ifnames, nsamples);
  1584 	compute_stats(nifnames, ifnames, nsamples);
  1519 
  1585 
  1520 	/* Close the two output files */
  1586 	/* Close the two output files */
  1521 	wrap_nc(nc_close(mean_ncid));
  1587 	wrap_nc(nc_close(mean_ncid));