Пример #1
0
float simple_liner_regression(np::ndarray a, np::ndarray b, np::ndarray c) {
	int nd1 = a.get_nd();
	int nd2 = b.get_nd();
	if (nd1 != 1 || nd2 != 1)
		throw std::runtime_error("a and b must be 1-dimensional");

	if ( (a.get_dtype() != np::dtype::get_builtin<double>()) ||
			(b.get_dtype() != np::dtype::get_builtin<double>()) )
		throw std::runtime_error("a and b must be float64 array");

	size_t N = a.shape(0);
	if ( N != b.shape(0) )
		throw std::runtime_error(" a and b must be same size");

	double *p = reinterpret_cast<double *>(a.get_data());
	std::vector<float> x;
	for(int i=0;i<N;i++) x.push_back(*p++);

	double *q = reinterpret_cast<double *>(b.get_data());
	std::vector<float> y;
	for(int i=0;i<N;i++) y.push_back(*q++);

	// 回帰系数の計算
	float a1 = calc_covariance(x,y) / calc_variance(x);
	float a0 = calc_mean(y) - a1 * calc_mean(x);

	double *r = reinterpret_cast<double *>(c.get_data());
	*r = a0; r++;
	*r = a1;
}
Пример #2
0
int main(int argc, char *argv[]){

    // Variables for directory handling
    DIR *directory;
    struct dirent *file_struct;

    // Variables for file handling
    char *dic_path = argv[1];
    char file_path[500];

    // Variables for feature handling
    double gray_histo_array[100];
    double h_entropy_array[100];
    int feature_count = 0;
    double gray_mean, h_mean, gray_stdDev, h_stdDev, correlation;

    // Checks argument and opens training directory
    if(argc != 2){
        printf("Wrong number of arguments");
        printf("Usage: $ %s directory-path", argv[0]);
        exit(EXIT_FAILURE);
    }

    if( (directory = opendir(dic_path)) == NULL){
        printf("Error while opening directory %s", dic_path);
        exit(EXIT_FAILURE);
    }

    /* Iterates throught all files in training directory
       and extract the features 
    */
    while( file_struct = readdir(directory) )
        // . and .. are no valid file names
        if( strcmp("..", file_struct -> d_name) && strcmp(".", file_struct -> d_name) ){
            // Constructs file absolute path
            strcpy(file_path, dic_path);
            strcat(file_path, file_struct -> d_name);
            // Calculates features
            extract_features(file_path, &gray_histo_array[feature_count], &h_entropy_array[feature_count]);
            ++feature_count;
        }

    closedir(directory);

    gray_mean = calc_mean(gray_histo_array, feature_count);
    h_mean = calc_mean(h_entropy_array, feature_count);

    gray_stdDev = calc_stdDev(gray_histo_array, feature_count, gray_mean);
    h_stdDev = calc_stdDev(h_entropy_array, feature_count, h_mean);

    correlation = calc_correlation(gray_histo_array, h_entropy_array, gray_mean, h_mean, gray_stdDev, h_stdDev, feature_count);

    printf("Mean: %f and %f\n", gray_mean, h_mean);
    printf("Standart dev: %f and %f\n", gray_stdDev, h_stdDev);
    printf("Correlation %f\n", correlation);
    printf("Class name : %f , %f , %f , %f , %f \n", gray_mean, h_mean, gray_stdDev, h_stdDev, correlation);

    return 0;
}
Пример #3
0
int corr_white_peterson(Fluxrec *flux1, Fluxrec *flux2, int npoints, float *r)
{
  int i;              /* Looping variable */
  float mean1,mean2;  /* Straight means of each curve */
  float rms1,rms2;    /* RMS scatter about means of each curve */
  Fluxrec *fp1,*fp2;  /* Pointers to navigate flux1 and flux2 */

  /*
   * First calculate mean and rms of the two curves
   */

  if(calc_mean(flux1,npoints,&mean1,&rms1)) {
    fprintf(stderr,"ERROR: corr_white_peterson\n");
    return 1;
  }
  if(calc_mean(flux2,npoints,&mean2,&rms2)) {
    fprintf(stderr,"ERROR: corr_white_peterson\n");
    return 1;
  }

  /*
   * Initialize the linear correlation coefficient
   */

  *r = 0.0;

  /*
   * Now step through the curves, calculating the
   *  sum of the products of the fluxes decreased by the local mean.
   */

  for(i=0,fp1=flux1,fp2=flux2; i<npoints; i++,fp1++,fp2++)
    *r += (fp1->flux - mean1) * (fp2->flux - mean2);

  /*
   * Now normalize according to White and Peterson.
   */

  *r /= (npoints * rms1 * rms2);

  return 0;
}
Пример #4
0
double calc_standard_deviation(double *values, int nvalues)
{
    double mean, rms, stdev;

    if (nvalues == 0)
	return .0;

    rms = calc_root_mean_square(values, nvalues);
    mean = calc_mean(values, nvalues);

    stdev = sqrt(pow(rms, 2) - pow(mean, 2));
    return stdev;
}
Пример #5
0
static treeptr reroot(treeptr ptree, sint nseqs)
{

   treeptr p, rootnode, rootptr;
   float   diff, mindiff = 0.0, mindepth = 1.0, maxdist;
   sint   i;
   Boolean first = TRUE;

/*
  find the difference between the means of leaf->node
  distances on the left and on the right of each node
*/
   rootptr = ptree;
   for (i=0; i<ntotal; i++)
     {
        p = ptrs[i];
        if (p->parent == NULL)
           diff = calc_root_mean(p, &maxdist);
        else
           diff = calc_mean(p, &maxdist, nseqs);

        if ((diff == 0) || ((diff > 0) && (diff < 2 * p->dist)))
          {
              if ((maxdist < mindepth) || (first == TRUE))
                 {
                    first = FALSE;
                    rootptr = p;
                    mindepth = maxdist;
                    mindiff = diff;
                 }
           }

     }

/*
  insert a new node as the ancestor of the node which produces the shallowest
  tree.
*/
   if (rootptr == ptree)
     {
        mindiff = rootptr->left->dist + rootptr->right->dist;
        rootptr = rootptr->right;
     }
   rootnode = insert_root(rootptr, mindiff);
  
   diff = calc_root_mean(rootnode, &maxdist);

   return(rootnode);
}
Пример #6
0
float mean(np::ndarray a) {
	int nd = a.get_nd();
	if (nd != 1)
		throw std::runtime_error("a must be 1-dimensional");

	if (a.get_dtype() != np::dtype::get_builtin<double>())
		throw std::runtime_error("a must be float64 array");
	
	size_t N = a.shape(0);
	double *p = reinterpret_cast<double *>(a.get_data());
	std::vector<float> x;
	for(int i=0;i<N;i++) x.push_back(*p++);

	return calc_mean(x);
}
Пример #7
0
double calc_std(double a[], int length){
    double mean;
    mean = calc_mean(a, length);

    int denumerator = length - 1;

    if (length == 1)
        denumerator = length;

    double temp = 0.0;

    for (int i = 0; i < length; ++i) {
        temp += pow(a[i] - mean, 2);
    }

    return sqrt(temp / denumerator);
}
Пример #8
0
void AdjustSync::AutosyncOffset()
{
	const float mean = calc_mean( s_fAutosyncOffset, s_fAutosyncOffset+OFFSET_SAMPLE_COUNT );
	const float stddev = calc_stddev( s_fAutosyncOffset, s_fAutosyncOffset+OFFSET_SAMPLE_COUNT );

	RString sAutosyncType;
	switch( GAMESTATE->m_SongOptions.GetCurrent().m_AutosyncType )
	{
	case SongOptions::AUTOSYNC_SONG:
		sAutosyncType = AUTOSYNC_SONG;
		break;
	case SongOptions::AUTOSYNC_MACHINE:
		sAutosyncType = AUTOSYNC_MACHINE;
		break;
	default:
		ASSERT(0);
	}

	if( stddev < .03f )  // If they stepped with less than .03 error
	{
		switch( GAMESTATE->m_SongOptions.GetCurrent().m_AutosyncType )
		{
		case SongOptions::AUTOSYNC_SONG:
			GAMESTATE->m_pCurSong->m_Timing.m_fBeat0OffsetInSeconds += mean;
			break;
		case SongOptions::AUTOSYNC_MACHINE:
			PREFSMAN->m_fGlobalOffsetSeconds.Set( PREFSMAN->m_fGlobalOffsetSeconds + mean );
			break;
		default:
			ASSERT(0);
		}

		SCREENMAN->SystemMessage( AUTOSYNC_CORRECTION_APPLIED.GetValue() );
	}
	else
	{
		SCREENMAN->SystemMessage( AUTOSYNC_CORRECTION_NOT_APPLIED.GetValue() );
	}

	s_iAutosyncOffsetSample = 0;
	s_fStandardDeviation = stddev;
}
Пример #9
0
static double calc_corr(TYPE_WIGARRAY *x, TYPE_WIGARRAY *y, int num){
  int i;
  double dx, dy;

  static double mx=-1, xx=0;
  if(mx==-1){
    mx = calc_mean(num, x);
    for(i=0; i<num; i++){
      dx = x[i] - mx;
      xx += dx * dx;
    }
    xx = sqrt(xx);
  }

  double yy=0, xy=0;
  static int sumy=-1;
  if(sumy==-1){
    sumy=0;
    for(i=0; i<num; i++) sumy += y[i];
  }else{
    for(i=0; i<5; i++){
      sumy -= *(y-i);
      sumy += y[num-i-1];
    }
  }
  double my = sumy/(double)num;
  for(i=0; i<num; i++){
    dx = x[i] - mx;
    dy = y[i] - my;
    yy += dy * dy;
    xy += dx * dy;
  }
  yy = sqrt(yy);

  return(xy / (xx*yy));
}
Пример #10
0
int main(int argc, char **argv)
{
	int      i, j;
	int      flag = 0;
	int      dim, size;
	double **data;
	double  *mean;
	double **sigma;
	double **corr;
	FILE    *fp;

	if(argc < 2){
		fprintf(stderr, "\n[使用法] : corrmat <file> (-s)\n");
		exit(1);
	}

	/* 出力フラグを調べる */
	if(is_opt(argc, argv, "-s"))
		flag = 1;
	
	/* 次元数とサンプル数を調べる */
	get_size(argv[1], &dim, &size);
	data = new_double_matrix(size, dim);

	/* データを読み込む */
	if((fp = fopen(argv[1], "r")) == NULL){
		fprintf(stderr, "\n[エラー] : ファイル %s が開けません\n", argv[1]);
		exit(1);
	}
	
	for(i = 0; i < size; i++)
		for(j = 0; j < dim; j++)
			fscanf(fp, "%lf", &data[i][j]);

	fclose(fp);

	/* 平均を計算する */
	mean = calc_mean(data, size, dim);

	/* 共分散行列を計算する */
	sigma = calc_cov_mat(data, mean, size, dim);

	/* 相関係数行列を計算する */
	corr = calc_corr_mat(sigma, dim);

	/* 結果を出力 */
	if(!flag)
		printf("Correlation Matrix:\n");

	for(i = 0; i < dim; i++){
		for(j = 0; j < dim; j++){
			printf("%#+.2f", corr[i][j]);
			if(j < dim - 1)
				printf(" ");
		}
		printf("\n");
		fflush(stdout);
	}

	/* 終了処理 */
	free_double_matrix(data);
	free_double_vector(mean);
	free_double_matrix(sigma);
	free_double_matrix(corr);

	exit(0);
}
Пример #11
0
/*-------------------------------------------------------------------------------------------*/
int cross_correlation(struct Map_info *Map, double passWE, double passNS)
    /*
       Map: Vector map from which cross-crorrelation will take values
       passWE: spline step in West-East direction
       passNS: spline step in North-South direction

       RETURN:
       TRUE on success
       FALSE on failure
     */
{
    int bilin = TRUE;		/*booleans */
    int nsplx, nsply, nparam_spl, ndata;
    double *mean, *rms, *stdev;

    /* double lambda[PARAM_LAMBDA] = { 0.0001, 0.001, 0.01, 0.1, 1.0, 10.0 }; */	/* Fixed values (by the moment) */
    double lambda[PARAM_LAMBDA] = { 0.0001, 0.001, 0.005, 0.01, 0.02, 0.05 };	/* Fixed values (by the moment) */
    /* a more exhaustive search:
    #define PARAM_LAMBDA 11
    double lambda[PARAM_LAMBDA] = { 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0 }; */

    double *TN, *Q, *parVect;	/* Interpolation and least-square vectors */
    double **N, **obsVect;	/* Interpolation and least-square matrix */

    struct Point *observ;
    struct Stats stat_vect;

    /*struct line_pnts *points; */
    /*struct line_cats *Cats; */
    struct Cell_head region;

    G_get_window(&region);

    extern int bspline_field;
    extern char *bspline_column;
    dbCatValArray cvarr;

    G_debug(5,
	    "CrossCorrelation: Some tests using different lambda_i values will be done");

    ndata = Vect_get_num_lines(Map);

    if (ndata > NDATA_MAX)
	G_warning(_("%d are too many points. "
		    "The cross validation would take too much time."), ndata);

    /*points = Vect_new_line_struct (); */
    /*Cats = Vect_new_cats_struct (); */

    /* Current region is read and points recorded into observ */
    observ = P_Read_Vector_Region_Map(Map, &region, &ndata, 1024, 1);
    G_debug(5, "CrossCorrelation: %d points read in region. ", ndata);
    G_verbose_message(_("%d points read in region"),
		      ndata);

    if (ndata > 50)
	G_warning(_("Maybe it takes too long. "
		    "It will depend on how many points you are considering."));
    else
	G_debug(5, "CrossCorrelation: It shouldn't take too long.");

    if (ndata > 0) {		/* If at least one point is in the region */
	int i, j, lbd;		/* lbd: lambda index */
	int BW;	
	double mean_reg, *obs_mean;

	int nrec, ctype = 0, verbosity;
	struct field_info *Fi;
	dbDriver *driver_cats;

	mean = G_alloc_vector(PARAM_LAMBDA);	/* Alloc as much mean, rms and stdev values as the total */
	rms = G_alloc_vector(PARAM_LAMBDA);	/* number of parameter used used for cross validation */
	stdev = G_alloc_vector(PARAM_LAMBDA);

	verbosity = G_verbose(); /* store for later reset */

	/* Working with attributes */
	if (bspline_field > 0) {
	    db_CatValArray_init(&cvarr);

	    Fi = Vect_get_field(Map, bspline_field);
	    if (Fi == NULL)
	      G_fatal_error(_("Database connection not defined for layer %d"),
			    bspline_field);

	    driver_cats =
		db_start_driver_open_database(Fi->driver, Fi->database);
	    G_debug(1, _("CrossCorrelation: driver=%s db=%s"), Fi->driver,
		    Fi->database);

	    if (driver_cats == NULL)
		G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
			      Fi->database, Fi->driver);

	    nrec =
		db_select_CatValArray(driver_cats, Fi->table, Fi->key,
				      bspline_column, NULL, &cvarr);
	    G_debug(3, "nrec = %d", nrec);

	    ctype = cvarr.ctype;
	    if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
		G_fatal_error(_("Column type not supported"));

	    if (nrec < 0)
		G_fatal_error(_("No records selected from table <%s> "),
			      Fi->table);

	    G_debug(1, "%d records selected from table",
		    nrec);

	    db_close_database_shutdown_driver(driver_cats);
	}

	/* Setting number of splines as a function of WE and SN spline steps */
	nsplx = ceil((region.east - region.west) / passWE);
	nsply = ceil((region.north - region.south) / passNS);
	nparam_spl = nsplx * nsply;	/* Total number of splines */

	if (nparam_spl > 22900)
	    G_fatal_error(_("Too many splines (%d x %d). "
			    "Consider changing spline steps \"ew_step=\" \"ns_step=\"."),
			  nsplx, nsply);

	BW = P_get_BandWidth(bilin, nsply);
	/**/
	/*Least Squares system */
	N = G_alloc_matrix(nparam_spl, BW);	/* Normal matrix */
	TN = G_alloc_vector(nparam_spl);	/* vector */
	parVect = G_alloc_vector(nparam_spl);	/* Parameters vector */
	obsVect = G_alloc_matrix(ndata, 3);	/* Observation vector */
	Q = G_alloc_vector(ndata);		/* "a priori" var-cov matrix */

	obs_mean = G_alloc_vector(ndata);
	stat_vect = alloc_Stats(ndata);

	for (lbd = 0; lbd < PARAM_LAMBDA; lbd++) {	/* For each lambda value */

	    G_message(_("Beginning cross validation with "
		        "lambda_i=%.4f ... (%d of %d)"), lambda[lbd],
		      lbd+1, PARAM_LAMBDA);

	    /*
	       How the cross correlation algorithm is done:
	       For each cycle, only the first ndata-1 "observ" elements are considered for the 
	       interpolation. Within every interpolation mean is calculated to lowering edge 
	       errors. The point left out will be used for an estimation. The error between the 
	       estimation and the observation is recorded for further statistics.
	       At the end of the cycle, the last point, that is, the ndata-1 index, and the point 
	       with j index are swapped.
	     */
	    for (j = 0; j < ndata; j++) {	/* Cross Correlation will use all ndata points */
		double out_x, out_y, out_z;	/* This point is left out */

		for (i = 0; i < ndata; i++) {	/* Each time, only the first ndata-1 points */
		    double dval;		/* are considered in the interpolation */

		    /* Setting obsVect vector & Q matrix */
		    Q[i] = 1;	/* Q=I */
		    obsVect[i][0] = observ[i].coordX;
		    obsVect[i][1] = observ[i].coordY;

		    if (bspline_field > 0) {
			int cat, ival, ret;

			/*type = Vect_read_line (Map, points, Cats, observ[i].lineID); */
			/*if ( !(type & GV_POINTS ) ) continue; */

			/*Vect_cat_get ( Cats, bspline_field, &cat ); */
			cat = observ[i].cat;

			if (cat < 0)
			    continue;

			if (ctype == DB_C_TYPE_INT) {
			    ret =
				db_CatValArray_get_value_int(&cvarr, cat,
							     &ival);
			    obsVect[i][2] = ival;
			    obs_mean[i] = ival;
			}
			else {	/* DB_C_TYPE_DOUBLE */
			    ret =
				db_CatValArray_get_value_double(&cvarr, cat,
								&dval);
			    obsVect[i][2] = dval;
			    obs_mean[i] = dval;
			}
			if (ret != DB_OK) {
			    G_warning(_("No record for point (cat = %d)"),
				      cat);
			    continue;
			}
		    }
		    else {
			obsVect[i][2] = observ[i].coordZ;
			obs_mean[i] = observ[i].coordZ;
		    }
		}		/* i index */

		/* Mean calculation for every point less the last one */
		mean_reg = calc_mean(obs_mean, ndata - 1);

		for (i = 0; i < ndata; i++)
		    obsVect[i][2] -= mean_reg;

		/* This is left out */
		out_x = observ[ndata - 1].coordX;
		out_y = observ[ndata - 1].coordY;
		out_z = obsVect[ndata - 1][2];

		if (bilin) {	/* Bilinear interpolation */
		    normalDefBilin(N, TN, Q, obsVect, passWE, passNS, nsplx,
				   nsply, region.west, region.south,
				   ndata - 1, nparam_spl, BW);
		    nCorrectGrad(N, lambda[lbd], nsplx, nsply, passWE,
				 passNS);
		}
		else {		/* Bicubic interpolation */
		    normalDefBicubic(N, TN, Q, obsVect, passWE, passNS, nsplx,
				     nsply, region.west, region.south,
				     ndata - 1, nparam_spl, BW);
		    nCorrectGrad(N, lambda[lbd], nsplx, nsply, passWE,
				 passNS);
		}

		/* 
		   if (bilin) interpolation (&interp, P_BILINEAR);
		   else interpolation (&interp, P_BICUBIC);
		 */
		G_set_verbose(G_verbose_min());
		G_math_solver_cholesky_sband(N, parVect, TN, nparam_spl, BW);
		G_set_verbose(verbosity);

		/* Estimation of j-point */
		if (bilin)
		    stat_vect.estima[j] =
			dataInterpolateBilin(out_x, out_y, passWE, passNS,
					     nsplx, nsply, region.west,
					     region.south, parVect);

		else
		    stat_vect.estima[j] =
			dataInterpolateBilin(out_x, out_y, passWE, passNS,
					     nsplx, nsply, region.west,
					     region.south, parVect);

		/* Difference between estimated and observated i-point */
		stat_vect.error[j] = out_z - stat_vect.estima[j];
		G_debug(1, "CrossCorrelation: stat_vect.error[%d]  =  %lf", j,
			stat_vect.error[j]);

		/* Once the last value is left out, it is swapped with j-value */
		observ = swap(observ, j, ndata - 1);

		G_percent(j, ndata, 2);
	    }

	    mean[lbd] = calc_mean(stat_vect.error, stat_vect.n_points);
	    rms[lbd] =
		calc_root_mean_square(stat_vect.error, stat_vect.n_points);
	    stdev[lbd] =
		calc_standard_deviation(stat_vect.error, stat_vect.n_points);

	    G_message(_("Mean = %.5lf"), mean[lbd]);
	    G_message(_("Root Mean Square (RMS) = %.5lf"),
		      rms[lbd]);
	    G_message("---");
	}			/* ENDFOR each lambda value */

	G_free_matrix(N);
	G_free_vector(TN);
	G_free_vector(Q);
	G_free_matrix(obsVect);
	G_free_vector(parVect);
#ifdef nodef
	/*TODO: if the minimum lambda is wanted, the function declaration must be changed */
	/* At this moment, consider rms only */
	rms_min = find_minimum(rms, &lbd_min);
	stdev_min = find_minimum(stdev, &lbd_min);

	/* Writing some output */
	G_message(_("Different number of splines and lambda_i values have "
		    "been taken for the cross correlation"));
	G_message(_("The minimum value for the test (rms=%lf) was "
		    "obtained with: lambda_i = %.3f"),
		  rms_min,
		  lambda[lbd_min]);

	*lambda_min = lambda[lbd_min];
#endif

	G_message(_("Table of results:"));
	fprintf(stdout, _("    lambda |       mean |        rms |\n"));
	for (lbd = 0; lbd < PARAM_LAMBDA; lbd++) {
	    fprintf(stdout, " %9.5f | %10.4f | %10.4f |\n", lambda[lbd],
		    mean[lbd], rms[lbd]);
	}
	
	G_free_vector(mean);
	G_free_vector(rms);
    }				/* ENDIF (ndata > 0) */
    else
	G_warning(_("No point lies into the current region"));

    G_free(observ);
    return TRUE;
}
Пример #12
0
int plot_resid(Fluxrec *flux, int npoints, float *a, int ncoeff,
	       float *meanrms)
{
  int i,j;              /* Looping variables */
  int no_error=1;       /* Flag set to 0 on error */
  float ytmp;           /* Value of fitted function at a given day */
  float mean;           /* Mean of residual curve */
  float *polyx=NULL;    /* Polynomial in x */
  Fluxrec *resid=NULL;  /* Residual curve */
  Fluxrec *rptr;        /* Pointer to navigate resid */
  Fluxrec *fptr;        /* Pointer to navigate flux */

  /*
   * Allocate memory
   */

  if(!(polyx = new_array(ncoeff,1))) {
    fprintf(stderr,"ERROR: plot_resid\n");
    return 1;
  }

  if(!(resid = new_fluxrec(npoints)))
    no_error = 0;

  /*
   * Loop through flux, at each step compute the value of the function
   *  and then subtract it from the flux.
   */

  if(no_error) {
    for(i=0,fptr=flux,rptr=resid; i<npoints; i++,fptr++,rptr++) {

    /*
     * Compute the function value at this step
     */

    sinpoly(fptr->day,polyx-1,ncoeff);
    ytmp = 0;
    for(j=0; j<ncoeff; j++)
      ytmp += a[j] * polyx[j];

    /*
     * Now set resid->flux to be the difference between flux->flux and the
     *  fitted function
     */

    *rptr = *fptr;
    rptr->flux -= ytmp;
    }
  }

  /*
   * Now plot the points
   */

  if(no_error)
    if(plot_lcurve(resid,npoints,"Days","Residual Flux Density",""))
      no_error = 0;

  /*
   * Draw a line at 0.0 residual
   */

  if(no_error) {
    cpgsci(2);
    cpgslw(5);
    cpgmove(-999,0.0);
    cpgdraw(999,0.0);
    cpgslw(1);
    cpgsci(1);
  }

  /*
   * Calculate mean and rms
   */

  if(no_error) {
    if(calc_mean(resid,npoints,&mean,meanrms))
      no_error = 0;
    else {
      *meanrms /= sqrt(npoints);
      printf("\nplot_resid: Mean of residuals = %8.4f. ",mean);
      printf("RMS uncertainty in mean = %6.4f\n",*meanrms);
    }
  }

  /*
   * Clean up and exit
   */

  cpgslw(1);
  cpgsci(1);
  polyx = del_array(polyx);
  resid = del_fluxrec(resid);

  if(no_error)
    return 0;
  else {
    fprintf(stderr,"ERROR: plot_resid\n");
    return 1;
  }
}
Пример #13
0
int call_xcorr_fft(Fluxrec *flux[], int size, char *filename, int nbad, 
		   FILE *logfp, int doprint)
{
  int i;                       /* Looping variable */
  int no_error=1;              /* Flag set to 0 on error */
  int corsize;                 /* Length of correlation curves */
  float meanba,meanbc,meanbd;  /* Mean values of cross-correlation curves */
  float rmsba,rmsbc,rmsbd;     /* RMS values of cross-correlation curves */
  float norm=1.0;              /* Normalization for curves */
  float normday;               /* Day from which normalization value comes */
  Fluxrec best_ba;             /* Highest cross-correlation for B-A */
  Fluxrec best_bc;             /* Highest cross-correlation for B-C */
  Fluxrec best_bd;             /* Highest cross-correlation for B-D */
  Fluxrec *zmean[N08]={NULL};  /* Zero-mean light curves */
  Fluxrec *acorrb=NULL;        /* B autocorrelation */
  Fluxrec *corrbc=NULL;        /* B-C cross-correlation */
  Fluxrec *corrba=NULL;        /* B-A cross-correlation */
  Fluxrec *corrbd=NULL;        /* B-D cross-correlation */
  Fluxrec *cp1,*cp2,*cp3;      /* Pointers to navigate cross-corr arrays */
  FILE *ofp=NULL;              /* Output file */

  /*
   * Initialize the best lag containers to something ridiculous.  In this
   *  function the lag will be contained in the day member of the Fluxrec
   *  structure and the cross-correlation coefficient will be contained 
   *  in the flux member.
   */

  best_ba.flux = -999.0;
  best_bc.flux = -999.0;
  best_bd.flux = -999.0;

  /*
   * Normalize the curves since we are looking for correlations in
   *  fractional variations and then subtract 1 to create a zero-mean
   *  light curve.  This helps by getting rid of the DC component in
   *  the cross-correlation.
   */

  printf("\nCalculating cross-correlations....\n\n");
  for(i=0; i<N08; i++) {
    if(no_error)
      if(!(zmean[i] = norm_zero_mean(flux[i],size)))
	no_error = 0;
  }

  /*
   * First do an autocorrelation on curve B and use the peak
   *  value (at zero lag) to normalize the cross-correlation
   *  curves.  
   */

  if(no_error)
    if(!(acorrb = do_corr(zmean[1],zmean[1],size,&corsize,nbad)))
      no_error = 0;

  if(no_error) {
    norm = -9999.0;
    for(i=0,cp1=acorrb; i<corsize; i++,cp1++) {
      if(cp1->flux > norm) {
	norm = cp1->flux;
	normday = cp1->day;
      }
    }
    printf("\ncall_xcorr_fft: Normalization = %f from day %5.1f\n\n",
	   norm,normday);
  }

  /*
   * Call the cross-correlation routines for each pair of
   *  zero-mean light curves.
   */

  if(no_error)
    if(!(corrba = do_corr(zmean[1],zmean[0],size,&corsize,nbad)))
      no_error = 0;

  if(no_error)
    if(!(corrbc = do_corr(zmean[1],zmean[2],size,&corsize,nbad)))
      no_error = 0;

  if(no_error)
    if(!(corrbd = do_corr(zmean[1],zmean[3],size,&corsize,nbad)))
      no_error = 0;

  /*
   * Open output file
   */

  if(doprint && no_error)
    if((ofp = fopen(filename,"w")) == NULL) {
      fprintf(stderr,"ERROR: call_xcorr_fft.  Cannot open %s\n",filename);
      no_error = 0;
    }

  /*
   * Print cross-correlation curves to output files and also find
   *  lags with highest cross-correlation values.
   */

  if(no_error) {
    if(doprint) {
      fprintf(ofp,"#  Lag    Corr_ba   Corr_bc   Corr_bd\n");
      fprintf(ofp,"#------- --------- --------- ---------\n");
    }
    for(i=0,cp1=corrba,cp2=corrbc,cp3=corrbd; i<corsize; 
	i++,cp1++,cp2++,cp3++) {
      cp1->flux /= norm;
      cp2->flux /= norm;
      cp3->flux /= norm;
      if(doprint)
	fprintf(ofp,"%8.3f %9f %9f %9f\n",
	      -cp1->day,cp1->flux,cp2->flux,cp3->flux);
      if(cp1->flux > best_ba.flux)
	best_ba = *cp1;
      if(cp2->flux > best_bc.flux)
	best_bc = *cp2;
      if(cp3->flux > best_bd.flux)
	best_bd = *cp3;
    }
  }

  /*
   * Find rms (and mean) of cross-correlation curves.  This can be used
   *  to assess the significance of the highest peaks in the curves.
   */

  if(no_error) {
    calc_mean(corrba,corsize,&meanba,&rmsba);
    calc_mean(corrbc,corsize,&meanbc,&rmsbc);
    calc_mean(corrbd,corsize,&meanbd,&rmsbd);
#if 0
    printf(" call_xcorr_fft: B-A xcorr curve mean = %f, rms = %f\n",
	   meanba,rmsba);
    printf(" call_xcorr_fft: B-C xcorr curve mean = %f, rms = %f\n",
	   meanbc,rmsbc);
    printf(" call_xcorr_fft: B-D xcorr curve mean = %f, rms = %f\n\n",
	   meanbd,rmsbd);
#endif
  }

  /*
   * Print best lags to logfile, if it exists.  Take the negative of
   *  best_b?.day because if B leads the other light curve, the
   *  cross-correlation will return a negative number for the best-fit
   *  lags.  Since we know/hope that B leads all other values and we
   *  are looking for the amount that the other curves lag B, just
   *  take the negative of best_b?.day.
   */

  if(logfp && no_error) {
    fprintf(logfp,"# Cross correlation results\n");
    fprintf(logfp,"%7.2f %f %f ",-best_ba.day,best_ba.flux,rmsba);
    fprintf(logfp,"%7.2f %f %f ",-best_bc.day,best_bc.flux,rmsbc);
    fprintf(logfp,"%7.2f %f %f\n",-best_bd.day,best_bd.flux,rmsbd);
  }

  /*
   * Also print output to screen
   */

  if(no_error) {
    printf(" call_xcorr_fff: B-A best lag = %7.2f d (%f = %6.2f sigma)\n",
	   -best_ba.day,best_ba.flux,(best_ba.flux / rmsba));
    printf(" call_xcorr_fft: B-C best lag = %7.2f d (%f = %6.2f sigma)\n",
	   -best_bc.day,best_bc.flux,(best_bc.flux / rmsbc));
    printf(" call_xcorr_fft: B-D best lag = %7.2f d (%f = %6.2f sigma)\n",
	   -best_bd.day,best_bd.flux,(best_bd.flux / rmsbd));
  }

  /*
   * Clean up
   */

  for(i=0; i<N08; i++)
    zmean[i] = del_fluxrec(zmean[i]);
  corrbc = del_fluxrec(corrbc);
  corrba = del_fluxrec(corrba);
  corrbd = del_fluxrec(corrbd);
  if(ofp)
    fclose(ofp);

  if(no_error)
    return 0;
  else {
    fprintf(stderr,"ERROR: call_xcorr_fft\n");
    return 1;
  }
}
Пример #14
0
int main(int argc, char **argv)
{
	int      i, j;
	int      flag = 0;
	int      dim, size;
	double **data;
	double  *mean;
	double **sigma;
	FILE    *fp;

	if(argc < 2){
		fprintf(stderr, "\n[使用法] : meancov <file> (-s)\n");
		exit(1);
	}

	/* 出力フラグを調べる */
	if(is_opt(argc, argv, "-s"))
		flag = 1;
	
	/* 次元数とサンプル数を調べる */
	get_size(argv[1], &dim, &size);
	data = new_double_matrix(size, dim);

	/* データを読み込む */
	if((fp = fopen(argv[1], "r")) == NULL){
		fprintf(stderr, "\n[エラー] : ファイル %s が開けません\n", argv[1]);
		exit(1);
	}
	
	for(i = 0; i < size; i++)
		for(j = 0; j < dim; j++)
			fscanf(fp, "%lf", &data[i][j]);

	fclose(fp);

	/* 平均を計算する */
	mean = calc_mean(data, size, dim);

	/* 共分散行列を計算する */
	sigma = calc_cov_mat(data, mean, size, dim);

	/* 結果を出力 */
	if(!flag)
		fprintf(stdout, "Mean :\n");

	for(i = 0; i < dim; i++){
		fprintf(stdout, "%#+.6g", mean[i]);
		if(i != dim - 1)
			fprintf(stdout, " ");
	}
	
	if(!flag)
		fprintf(stdout, "\n\n");
	else
		fprintf(stdout, "\n");

	if(!flag)
		fprintf(stdout, "Covariance :\n");

	for(i = 0; i < dim; i++){
		for(j = 0; j < dim; j++){
			fprintf(stdout, "%#+.6g", sigma[i][j]);
			if(j != dim - 1)
				fprintf(stdout, " ");
		}
		fprintf(stdout, "\n");
	}

	/* 終了処理 */
	free_double_matrix(data);
	free_double_vector(mean);
	free_double_matrix(sigma);

	exit(0);
}
Пример #15
0
int main(int argc, char *argv[])
{
    char *datafile;

    hid_t h5file;
    hid_t input;
    herr_t status;

    int caps;
    int capid;
    cap_t *cap;

    int step;
    int n, i, j, k;
    int nodex, nodey, nodez;
	float radius_inner;
	float radius_outer;
	
	
	int nodex_redu=0;
	int nodey_redu=0;
	int nodez_redu=0;
	int initial=0;
	int timestep=-1;
	
    int current_t=0;
    int timesteps=0;
	
	int cell_counter=0;
	int cell_counter_surface=0;
	
    int *steps;
    char *endptr;

    field_t *coord;
    field_t *velocity;
    field_t *temperature;
    field_t *viscosity;
	
	//Bottom
	field_t *botm_coord;
	field_t *botm_hflux;
	field_t *botm_velo;
	field_t *botm_topo;
	
	//Surface
	field_t *surf_coord;
	field_t *surf_hflux;
	field_t *surf_velo;
	field_t *surf_topo;
	
	///////////////////////////////////////////////
	int bottom=false;
	int surface=false;
	int topo=false;
	int ascii=false;
	
    /************************************************************************
     * Parse command-line parameters.                                       *
     ************************************************************************/

    /*
     * HDF5 file must be specified as first argument.
     */
    
    if (argc < 2)
    {
        fprintf(stderr, "Usage: run with -h to get help\n", argv[0]);
        return EXIT_FAILURE;
    }

    char c;
    char *hdf_filepath;
    char *output_filepath;
    extern char *optarg;
    extern int optind, optopt;
	int errflg=0;
    while ((c = getopt(argc, argv, ":p:o:i:t:x:y:z:bscah?")) != -1) {
        switch(c) {
        		case 'p':
					hdf_filepath = optarg;
					break;
        
				case 'o':
            		output_filepath = optarg;
                	printf("Got output filepath\n");
            		break;
								
        		case 'i':
					initial = atoi(optarg);
					printf("Initial: %d\n",initial);
					break;
				
				case 't':
					timesteps = atoi(optarg);
					printf("Timesteps: %d\n", timesteps);
					//Inclusive
					timesteps++;
					break;
				
				case 'x':
					nodex_redu=atoi(optarg);
					break;
				
				case 'y':
					nodey_redu=atoi(optarg);
					break;
				
				case 'z':
					nodez_redu=atoi(optarg);
					break;
				
				
				case ':':       /* missing operand */
                    fprintf(stderr,
                            "Option -%c requires an operand\n", optopt);
                    errflg++;
                	break;
				////////////////////
				
				case 'b':
					bottom=true;
					printf("Create Bottom");
					break;
				
				case 's':
					surface=true;
					printf("Create Surface");
					break;
				
				case 'c':
					topo=true;
					printf("Create Topography");	
					break;
				
				case 'a':
					ascii=true;
					break;
				
				case '?':
            		errflg++;
					print_help();
				break;
        	}
    }
		
    for ( ; optind < argc; optind++) {
        if (access(argv[optind], R_OK)) {
 			printf("Geht\n");
			}
	}

	
	printf("Opening HDF file...\n");
	
    h5file = H5Fopen(hdf_filepath, H5F_ACC_RDONLY, H5P_DEFAULT);
    if (h5file < 0)
    {
        fprintf(stderr, "Could not open HDF5 file \"%s\"\n", argv[1]);
        return EXIT_FAILURE;
    }

    


    /************************************************************************
     * Get mesh parameters.                                                 *
     ************************************************************************/

    /* Read input group */
    input = H5Gopen(h5file, "input");
    if (input < 0)
    {
        fprintf(stderr, "Could not open /input group in \"%s\"\n", argv[1]);
        status = H5Fclose(h5file);
        return EXIT_FAILURE;
    }
	
	
    status = get_attribute_str(input, "datafile", &datafile);
    status = get_attribute_int(input, "nproc_surf", &caps);
    status = get_attribute_int(input, "nodex", &nodex);
    status = get_attribute_int(input, "nodey", &nodey);
    status = get_attribute_int(input, "nodez", &nodez);
	status = get_attribute_float(input,"radius_inner",&radius_inner);
	status = get_attribute_float(input,"radius_outer",&radius_outer);
	
	//Bound input params against hdf
	if(nodex_redu<nodex & nodex_redu>0)
	{
	nodex = nodex_redu;
	}
	if(nodey_redu<nodey & nodey_redu>0)
	{
	nodey = nodey_redu;
	}
	if(nodez_redu<nodez & nodez_redu>0)
	{
	nodez = nodez_redu;
	}
	
	
	printf("Nodex: %d\n",nodex);
	printf("Nodey: %d\n",nodey);
	printf("Nodez: %d\n",nodez);
	printf("Caps: %d\n",caps);
    /* Release input group */
    status = H5Gclose(input);
	

    /************************************************************************
     * Create fields using cap00 datasets as a template.                    *
     ************************************************************************/

    cap         = open_cap(h5file, 0);
    coord       = open_field(cap, "coord");
    velocity    = open_field(cap, "velocity");
    temperature = open_field(cap, "temperature");
    viscosity   = open_field(cap, "viscosity");
    
	
	/*Create fields bottom and surface*/
	botm_coord = open_field(cap,"botm/coord");
	botm_hflux = open_field(cap,"botm/heatflux");
	botm_velo = open_field(cap,"botm/velocity");
	botm_topo = open_field(cap,"botm/topography");
	
	surf_coord = open_field(cap,"surf/coord");
	surf_hflux = open_field(cap,"surf/heatflux");
	surf_velo = open_field(cap,"surf/velocity");
	surf_topo = open_field(cap,"surf/topography");
	
	status      = close_cap(cap);

	
    /************************************************************************
     * Output requested data.                                               *
     ************************************************************************/
	int iterations=0;
    /* Iterate over timesteps */
    for(current_t = initial; current_t < timesteps; current_t++)
    {
		
		printf("\nProcessing timestep: %d\n",current_t);
      
		
		coordinates_t ordered_coordinates[((nodex*nodey*nodez)*caps)];
		coordinates_t ordered_velocity[((nodex*nodey*nodez)*caps)*3];		
		float ordered_temperature[(nodex*nodey*nodez)*caps];
		float ordered_viscosity[(nodex*nodey*nodez)*caps];
		hexahedron_t connectivity[((nodex-1)*(nodey-1)*(nodez-1))*caps];
		
		
		coordinates_t ordered_botm_coords[(nodex*nodey*caps)];
		float ordered_botm_hflux[(nodex*nodey*caps)];
		coordinates_t ordered_botm_velocity[(nodex*nodey*caps)];
		
		
		coordinates_t ordered_surf_coords[(nodex*nodey*caps)];
		float ordered_surf_hflux[(nodex*nodey*caps)];
		coordinates_t ordered_surf_velocity[(nodex*nodey*caps)];
		
		vtk_pixel_t connectivity_surface[(nodex*nodey*caps)];
		
		//Holds single coordinate		
		coordinates_t coordinate;
		
		//Holds single vector
		coordinates_t velocity_vector;
				
        /* Iterate over caps */
		
		for(capid = 0; capid < caps; capid++)
        {
            cap = open_cap(h5file, capid);
			printf("Processing cap %d of %d\n",capid+1,caps);
            //snprintf(filename, (size_t)99, "%s.cap%02d.%d", datafile, capid, step);
            //fprintf(stderr, "Writing %s\n", filename);

            //file = fopen(filename, "w");
            //fprintf(file, "%d x %d x %d\n", nodex, nodey, nodez);

            /* Read data from HDF5 file. */
            read_field(cap, coord, 0);
            read_field(cap, velocity, current_t);
            read_field(cap, temperature, current_t);
            read_field(cap, viscosity, current_t);
			
			
			
    		/*Counts iterations*/
    		n = 0;
			
			//Number of nodes per cap
			int nodes=nodex*nodey*nodez;
			
	        /* Traverse data in Citcom order */
            for(j = 0; j < nodey; j++)
            {	
                for(i = 0; i < nodex; i++)
                {
			       for(k = 0; k < nodez; k++)
                    {       
								//Coordinates						
								coordinate.x = coord->data[3*n+0];
                                coordinate.y = coord->data[3*n+1];
                                coordinate.z = coord->data[3*n+2];
								coordinate = rtf_to_xyz(coordinate);
								ordered_coordinates[n+(capid*nodes)].x = coordinate.x;
								ordered_coordinates[n+(capid*nodes)].y = coordinate.y;
								ordered_coordinates[n+(capid*nodes)].z = coordinate.z;
								
								//Velocity
                        	    velocity_vector.x = velocity->data[3*n+0];
                                velocity_vector.y = velocity->data[3*n+1];
                                velocity_vector.z = velocity->data[3*n+2];
						
								velocity_vector = velocity_to_cart(velocity_vector,coordinate);
								
								ordered_velocity[n+(capid*nodes)].x = velocity_vector.x;
								ordered_velocity[n+(capid*nodes)].y = velocity_vector.y;
								ordered_velocity[n+(capid*nodes)].z = velocity_vector.z;
						
								//Temperature
                                ordered_temperature[n+(capid*nodes)] = temperature->data[n];
						
								//Viscosity
                                ordered_viscosity[n+(capid*nodes)] = viscosity->data[n];
								
								n++;
                    }
                }
            }

			//Create connectivity
			if(iterations==0)
			{
				//For 3d Data 
            	int i=1;    //Counts X Direction
            	int j=1;    //Counts Y Direction
            	int k=1;    //Counts Z Direction
    		
            	for(n=0; n<((nodex*nodey*nodez)-(nodex*nodez));n++)
					{
						
                		if ((i%nodez)==0)   //X-Values
							{
                    		j++;                 //Count Y-Values
        					}
                		if ((j%nodex)==0)
							{
                    		k++;                 //Count Z-Values
                  			}
							
                		if (((i%nodez) != 0) && ((j%nodex) != 0))            //Check if Box can be created
							{
							//Create Connectivity
                    		connectivity[cell_counter].c1 = n+(capid*(nodes));
							connectivity[cell_counter].c2 = connectivity[cell_counter].c1+nodez;
                    		connectivity[cell_counter].c3 = connectivity[cell_counter].c2+nodez*nodex;
                    		connectivity[cell_counter].c4 = connectivity[cell_counter].c1+nodez*nodex;
                    		connectivity[cell_counter].c5 = connectivity[cell_counter].c1+1;
                    		connectivity[cell_counter].c6 = connectivity[cell_counter].c5+nodez;
                    		connectivity[cell_counter].c7 = connectivity[cell_counter].c6+nodez*nodex;
                    		connectivity[cell_counter].c8 = connectivity[cell_counter].c5+nodez*nodex;
							cell_counter++;
							}                   	
                i++;
				
      			}
			}
			

			
			
			//Bottom and Surface
			
			if(bottom==true){
				
				/*Read Bottom data from HDF5 file.*/
				read_field(cap,botm_coord,0);
				read_field(cap,botm_hflux,current_t);
				read_field(cap,botm_velo,current_t);
				read_field(cap,botm_topo,current_t);
				float botm_mean=0.0;	
				if(topo=true)
					{
					botm_mean = calc_mean(botm_topo,nodex,nodey);
					}					
				for(n=0;n<nodex*nodey;n++)
				{
				//Coordinates						
				coordinate.x = botm_coord->data[2*n+0];
    			coordinate.y = botm_coord->data[2*n+1];
    				if(topo==true)
					{
					coordinate.z = radius_inner+(botm_topo->data[n]-botm_mean)*
						(pow(10.0,21.0)/(pow(6371000,2)/pow(10,-6))/3300*10)/1000;
					//printf("Z: %f\n",coordinate.z);
					}
					else
					{
					coordinate.z = radius_inner;
					}
					
				coordinate = rtf_to_xyz(coordinate);
				ordered_botm_coords[n+(capid*nodex*nodey)].x = coordinate.x;
				ordered_botm_coords[n+(capid*nodex*nodey)].y = coordinate.y;
				ordered_botm_coords[n+(capid*nodex*nodey)].z = coordinate.z;
								
				ordered_botm_hflux[n+((capid)*nodex*nodey)] = botm_hflux->data[n];
					
				velocity_vector.x = botm_velo->data[3*n+0];
				velocity_vector.y = botm_velo->data[3*n+1];
				velocity_vector.z = botm_velo->data[3*n+2];
					
				velocity_vector = velocity_to_cart(velocity_vector,coordinate);
					
				ordered_botm_velocity[n+(capid*nodex*nodey)].x = velocity_vector.x;
				ordered_botm_velocity[n+(capid*nodex*nodey)].y = velocity_vector.y;
				ordered_botm_velocity[n+(capid*nodex*nodey)].z = velocity_vector.z;
								
				}		
	
	
			}
			
			if(surface==true)
			{
		
				/*Read Surface data from HDF5 file.*/
				read_field(cap,surf_coord,0);
				read_field(cap,surf_hflux,current_t);
				read_field(cap,surf_velo,current_t);
				read_field(cap,surf_topo,current_t);
				float surf_mean=0.0;
				if(topo=true)
					{
						
					surf_mean = calc_mean(surf_topo,nodex,nodey);
					}					
				for(n=0;n<nodex*nodey;n++)
				{
				//Coordinates						
				coordinate.x = surf_coord->data[2*n+0];
    			coordinate.y = surf_coord->data[2*n+1];
    				if(topo==true)
					{
					coordinate.z = radius_outer+(surf_topo->data[n]-surf_mean)*
						(pow(10.0,21.0)/(pow(6371000,2)/pow(10,-6))/3300*10)/1000;
					//printf("Z: %f\n",coordinate.z);
					}
					else
					{
					coordinate.z = radius_outer;
					}
					
				coordinate = rtf_to_xyz(coordinate);
				ordered_surf_coords[n+(capid*nodex*nodey)].x = coordinate.x;
				ordered_surf_coords[n+(capid*nodex*nodey)].y = coordinate.y;
				ordered_surf_coords[n+(capid*nodex*nodey)].z = coordinate.z;
								
				ordered_surf_hflux[n+((capid)*nodex*nodey)] = botm_hflux->data[n];
					
				velocity_vector.x = botm_velo->data[3*n+0];
				velocity_vector.y = botm_velo->data[3*n+1];
				velocity_vector.z = botm_velo->data[3*n+2];
					
				velocity_vector = velocity_to_cart(velocity_vector,coordinate);
					
				ordered_surf_velocity[n+(capid*nodex*nodey)].x = velocity_vector.x;
				ordered_surf_velocity[n+(capid*nodex*nodey)].y = velocity_vector.y;
				ordered_surf_velocity[n+(capid*nodex*nodey)].z = velocity_vector.z;
								
				}		
				
			}

			
			//Create connectivity information 2d
			
			
			if(iterations==0){
				if(surface==true | bottom==true)
					{
					
            		for(n=0;n<(nodex*nodey)-nodey;n++)
						{
                    	if ((n+1)%nodey!=0){
							connectivity_surface[cell_counter_surface].c1 = n+(capid*((nodex)*(nodey)));
                        	connectivity_surface[cell_counter_surface].c2 = connectivity_surface[cell_counter_surface].c1+1;
							connectivity_surface[cell_counter_surface].c3 = connectivity_surface[cell_counter_surface].c1+nodey;
							connectivity_surface[cell_counter_surface].c4 = connectivity_surface[cell_counter_surface].c3+1;          
							cell_counter_surface++;
							}
						}
					}
				}
			
			close_cap(cap);
        }
		
    iterations++;
		
	//Write data to file
	write_vtk_shell(ordered_coordinates, connectivity, ordered_temperature, 
					   ordered_viscosity, ordered_velocity, nodex, nodey, nodez,
					   current_t, radius_inner,caps,ascii);
	
	if(bottom==true){
		write_vtk_surface(ordered_botm_coords,connectivity_surface,ordered_botm_hflux,
						  ordered_botm_velocity,current_t,nodex,nodey,nodez,caps,"bottom");
	}
	
	
	if(surface==true){
		write_vtk_surface(ordered_surf_coords,connectivity_surface,ordered_surf_hflux,
						  ordered_surf_velocity,current_t,nodex,nodey,nodez,caps,"surface");
	}
		
	}//end timesteps loop

    /* Release resources. */

    status = close_field(coord);
    status = close_field(velocity);
    status = close_field(temperature);
    status = close_field(viscosity);
    status = H5Fclose(h5file);

    return EXIT_SUCCESS;
}