Ejemplo n.º 1
0
int adjust_error_handling(
int args,
int * print_stack,
int * do_next)
{
    char buffer[80];
    int choice;

    for(;;)
    {
        fprintf(stderr, "enter a number:\n");
        fprintf(stderr, "1 = done with error handling\n");
        fprintf(stderr, "2 = %sprint stack on error\n",
            ((*print_stack IS ON) ? "do not " : ""));
        if (args IS 3)
        {
            if (*do_next IS 0)                    /* 0 means continue */
                fprintf(stderr,
                    "3 = stop on error (do not continue)\n");
            else                                  /* if do_next IS 2 -- 2 means stopping on error */
                fprintf(stderr,
                    "3 = continue on error (do not stop)\n");
        }
        else if (args IS 2)
        {
            if (*do_next IS 0)                    /* 0 means continue */
                fprintf(stderr,
                    "3 = mdi on error (do not continue or stop)\n");
            else if (*do_next IS 1)               /* 1 means MDI */
                fprintf(stderr,
                        "3 = stop on error (do not mdi or continue)\n");
            else                                  /* if do_next IS 2 -- 2 means stopping on error */
                fprintf(stderr,
                    "3 = continue on error (do not stop or mdi)\n");
        }
        fprintf(stderr, "enter choice => ");
		fgets(buffer, sizeof(buffer), stdin);

        if (sscanf_s(buffer, "%d", &choice) ISNT 1)
            continue;
        if (choice IS 1)
            break;
        else if (choice IS 2)
            *print_stack SET_TO ((*print_stack IS OFF) ? ON : OFF);
        else if ((choice IS 3) AND (args IS 3))
            *do_next SET_TO ((*do_next IS 0) ? 2 : 0);
        else if ((choice IS 3) AND (args IS 2))
            *do_next SET_TO ((*do_next IS 2) ? 0 : (*do_next + 1));
    }
    return 0;
}
Ejemplo n.º 2
0
void START_SPINDLE_COUNTERCLOCKWISE()
{
    PRINT0("START_SPINDLE_COUNTERCLOCKWISE()\r\n");
    _spindle_turning SET_TO ((_spindle_speed IS 0) ? CANON_STOPPED :
    CANON_COUNTERCLOCKWISE);
}
Ejemplo n.º 3
0
void STRAIGHT_PROBE(
double x, double y, double z
#ifdef AA
, double a                                        /*AA*/
#endif
#ifdef BB
, double b                                        /*BB*/
#endif
#ifdef CC
, double c                                        /*CC*/
#endif
)
{
    double distance;
    double dx, dy, dz;
    double backoff;

    dx SET_TO (_program_position_x - x);
    dy SET_TO (_program_position_y - y);
    dz SET_TO (_program_position_z - z);
    distance SET_TO sqrt((dx * dx) + (dy * dy) + (dz * dz));

    fprintf(_outfile, "%5d ", _line_number++);
    print_nc_line_number();
    fprintf(_outfile, "STRAIGHT_PROBE(%.4f, %.4f, %.4f"
    #ifdef AA
        ", %.4f"                                  /*AA*/
    #endif
    #ifdef BB
        ", %.4f"                                  /*BB*/
    #endif
    #ifdef CC
        ", %.4f"                                  /*CC*/
    #endif
        ")\r\n", x, y, z
    #ifdef AA
        , a                                       /*AA*/
    #endif
    #ifdef BB
        , b                                       /*BB*/
    #endif
    #ifdef CC
        , c                                       /*CC*/
    #endif
        );
    _probe_position_x SET_TO x;
    _probe_position_y SET_TO y;
    _probe_position_z SET_TO z;
#ifdef AA
    _probe_position_a SET_TO a;                   /*AA*/
#endif
#ifdef BB
    _probe_position_b SET_TO b;                   /*BB*/
#endif
#ifdef CC
    _probe_position_c SET_TO c;                   /*CC*/
#endif
    if (distance IS 0)
    {
        _program_position_x SET_TO _program_position_x;
        _program_position_y SET_TO _program_position_y;
        _program_position_z SET_TO _program_position_z;
    }
    else
    {
        backoff SET_TO ((_length_unit_type IS CANON_UNITS_MM) ? 0.254 : 0.01);
        _program_position_x SET_TO (x + (backoff * (dx / distance)));
        _program_position_y SET_TO (y + (backoff * (dy / distance)));
        _program_position_z SET_TO (z + (backoff * (dz / distance)));
    }
#ifdef AA
    _program_position_a SET_TO a;                 /*AA*/
#endif
#ifdef BB
    _program_position_b SET_TO b;                 /*BB*/
#endif
#ifdef CC
    _program_position_c SET_TO c;                 /*CC*/
#endif
}
Ejemplo n.º 4
0
void USE_LENGTH_UNITS(CANON_UNITS in_unit)
{
    if (in_unit IS CANON_UNITS_INCHES)
    {
        PRINT0("USE_LENGTH_UNITS(CANON_UNITS_INCHES)\r\n");
        if (_length_unit_type IS CANON_UNITS_MM)
        {
            _length_unit_type SET_TO CANON_UNITS_INCHES;
            _length_unit_factor SET_TO 25.4;
            _program_origin_x SET_TO (_program_origin_x / 25.4);
            _program_origin_y SET_TO (_program_origin_y / 25.4);
            _program_origin_z SET_TO (_program_origin_z / 25.4);
            _program_position_x SET_TO (_program_position_x / 25.4);
            _program_position_y SET_TO (_program_position_y / 25.4);
            _program_position_z SET_TO (_program_position_z / 25.4);
        }
    }
    else if (in_unit IS CANON_UNITS_MM)
    {
        PRINT0("USE_LENGTH_UNITS(CANON_UNITS_MM)\r\n");
        if (_length_unit_type IS CANON_UNITS_INCHES)
        {
            _length_unit_type SET_TO CANON_UNITS_MM;
            _length_unit_factor SET_TO 1.0;
            _program_origin_x SET_TO (_program_origin_x * 25.4);
            _program_origin_y SET_TO (_program_origin_y * 25.4);
            _program_origin_z SET_TO (_program_origin_z * 25.4);
            _program_position_x SET_TO (_program_position_x * 25.4);
            _program_position_y SET_TO (_program_position_y * 25.4);
            _program_position_z SET_TO (_program_position_z * 25.4);
        }
    }
    else
        PRINT0("USE_LENGTH_UNITS(UNKNOWN)\r\n");
}
	void QPBO<REAL>::TransformToSecondStage(bool copy_trees)
{
	// add non-submodular edges
	Node* i[2];
	Node* j[2];
	Arc* a[2];

	memset(nodes[1], 0, node_num*sizeof(Node));
	node_last[1] = nodes[1] + node_num;

	if (!copy_trees)
	{
		for (i[0]=nodes[0], i[1]=nodes[1]; i[0]<node_last[0]; i[0]++, i[1]++)
		{
			i[1]->first = NULL;
			i[1]->tr_cap = -i[0]->tr_cap;
		}

		for (a[0]=arcs[0], a[1]=arcs[1]; a[0]<arc_max[0]; a[0]+=2, a[1]+=2)
		{
			if (!a[0]->sister) continue;

			code_assert(IsNode0(a[0]->sister->head));
			SET_SISTERS(a[1], a[1]+1);
			if (IsNode0(a[0]->head))
			{
				i[1] = GetMate0(a[0]->sister->head);
				j[1] = GetMate0(a[0]->head);

				SET_FROM(a[1],         j[1]);
				SET_FROM(a[1]->sister, i[1]);
				SET_TO(a[1],         i[1]);
				SET_TO(a[1]->sister, j[1]);
			}
			else
			{
				i[0] = a[0]->sister->head;
				i[1] = GetMate0(i[0]);
				j[1] = a[0]->head;
				j[0] = GetMate1(j[1]);

				SET_FROM(a[0],         i[0]);
				SET_FROM(a[0]->sister, j[1]);
				SET_FROM(a[1],         j[0]);
				SET_FROM(a[1]->sister, i[1]);
				SET_TO(a[1],         i[1]);
				SET_TO(a[1]->sister, j[0]);
			}
			a[1]->r_cap = a[0]->r_cap;
			a[1]->sister->r_cap = a[0]->sister->r_cap;
		}
	}
	else
	{
		for (i[0]=nodes[0], i[1]=nodes[1]; i[0]<node_last[0]; i[0]++, i[1]++)
		{
			i[1]->first = NULL;
			i[1]->tr_cap = -i[0]->tr_cap;
			i[1]->is_sink = i[0]->is_sink ^ 1;
			i[1]->DIST = i[0]->DIST;
			i[1]->TS = i[0]->TS;

			if (i[0]->parent == NULL || i[0]->parent == QPBO_MAXFLOW_TERMINAL) i[1]->parent = i[0]->parent;
			else i[1]->parent = GetMate0(i[0]->parent->sister);
		}

		for (a[0]=arcs[0], a[1]=arcs[1]; a[0]<arc_max[0]; a[0]+=2, a[1]+=2)
		{
			if (!a[0]->sister) continue;

			code_assert(IsNode0(a[0]->sister->head));
			SET_SISTERS(a[1], a[1]+1);
			if (IsNode0(a[0]->head))
			{
				i[1] = GetMate0(a[0]->sister->head);
				j[1] = GetMate0(a[0]->head);

				SET_FROM(a[1],         j[1]);
				SET_FROM(a[1]->sister, i[1]);
				SET_TO(a[1],         i[1]);
				SET_TO(a[1]->sister, j[1]);
			}
			else
			{
				i[0] = a[0]->sister->head;
				i[1] = GetMate0(i[0]);
				j[1] = a[0]->head;
				j[0] = GetMate1(j[1]);

				SET_FROM(a[0],         i[0]);
				SET_FROM(a[0]->sister, j[1]);
				SET_FROM(a[1],         j[0]);
				SET_FROM(a[1]->sister, i[1]);
				SET_TO(a[1],         i[1]);
				SET_TO(a[1]->sister, j[0]);

				mark_node(i[0]);
				mark_node(i[1]);
				mark_node(j[0]);
				mark_node(j[1]);
			}
			a[1]->r_cap = a[0]->r_cap;
			a[1]->sister->r_cap = a[0]->sister->r_cap;
		}
	}

	stage = 1;
}
	void QPBO<REAL>::AddPairwiseTerm(EdgeId e, NodeId _i, NodeId _j, REAL E00, REAL E01, REAL E10, REAL E11)
{
	user_assert(e >= 0 && arcs[0][2*e].sister);
	user_assert(arcs[0][2*e].head==&nodes[0][_i] || arcs[0][2*e].head==&nodes[1][_i] || arcs[0][2*e].head==&nodes[0][_j] || arcs[0][2*e].head==&nodes[1][_j]);
	user_assert(arcs[0][2*e+1].head==&nodes[0][_i] || arcs[0][2*e+1].head==&nodes[1][_i] || arcs[0][2*e+1].head==&nodes[0][_j] || arcs[0][2*e+1].head==&nodes[1][_j]);
	user_assert(_i != _j);

	REAL delta, ci, cj, cij, cji;

	if (stage == 0)
	{
		Arc* a = &arcs[0][2*e];
		Arc* a_rev = &arcs[0][2*e+1];
		code_assert(a->sister==a_rev && a->sister==a_rev);

		Node* i = a_rev->head;
		Node* j = a->head;
		code_assert(IsNode0(i));
		if (i != &nodes[0][_i]) { delta = E01; E01 = E10; E10 = delta; }
		if (IsNode0(j))
		{
			ComputeWeights(E00, E01, E10, E11, ci, cj, cij, cji);
			
			i->tr_cap += ci;
			j->tr_cap += cj;
			a->r_cap += cij;
			a_rev->r_cap += cji;

			if (a->r_cap < 0)
			{
				delta = a->r_cap;
				a->r_cap = 0;
				a_rev->r_cap += delta;
				i->tr_cap -= delta;
				j->tr_cap += delta;
			}
			if (a_rev->r_cap < 0)
			{
				delta = a_rev->r_cap;
				a_rev->r_cap = 0;
				a->r_cap += delta;
				j->tr_cap -= delta;
				i->tr_cap += delta;
			}

			if (a->r_cap < 0)
			{
				all_edges_submodular = false;
				REMOVE_FROM(a, i);
				REMOVE_FROM(a_rev, j);
				SET_TO(a, GetMate0(j));

				delta = a->r_cap;
				i->tr_cap -= delta;
				a->r_cap = -delta;
			}
		}
		else
		{
			j = GetMate1(j);
			ComputeWeights(E01, E00, E11, E10, ci, cj, cij, cji);
			
			i->tr_cap += ci;
			j->tr_cap -= cj;
			a->r_cap += cij;
			a_rev->r_cap += cji;

			if (a->r_cap < 0)
			{
				delta = a->r_cap;
				a->r_cap = 0;
				a_rev->r_cap += delta;
				i->tr_cap -= delta;
				j->tr_cap -= delta;
			}
			if (a_rev->r_cap < 0)
			{
				delta = a_rev->r_cap;
				a_rev->r_cap = 0;
				a->r_cap += delta;
				j->tr_cap += delta;
				i->tr_cap += delta;
			}

			if (a->r_cap < 0)
			{
				SET_FROM(a, i);
				SET_FROM(a_rev, j);
				SET_TO(a, j);

				delta = a->r_cap;
				i->tr_cap -= delta;
				a->r_cap = -delta;
			}
		}
	}
	else
	{
		Arc* a[2] = { &arcs[0][2*e], &arcs[1][2*e] };
		Arc* a_rev[2] = { &arcs[0][2*e+1], &arcs[1][2*e+1] };
		code_assert(a[0]->sister==a_rev[0] && a[1]->sister==a_rev[1] && a[0]==a_rev[0]->sister && a[1]==a_rev[1]->sister);

		Node* i[2] = { a_rev[0]->head, a[1]->head };
		Node* j[2] = { a[0]->head, a_rev[1]->head };
		int k = IsNode0(i[0]) ? 0 : 1;
		if (i[k] != &nodes[0][_i]) { delta = E01; E01 = E10; E10 = delta; }
		if (IsNode0(j[k]))
		{ 
			ComputeWeights(E00, E01, E10, E11, ci, cj, cij, cji);
		}
		else
		{ 
			ComputeWeights(E01, E00, E11, E10, ci, cj, cij, cji);
		};

		// make sure that a[0]->r_cap == a[1]->r_cap and a_rev[0]->r_cap == a_rev[1]->r_cap by pushing flow
		delta = a[1]->r_cap - a[0]->r_cap;
		//a[1]->r_cap -= delta;   // don't do the subtraction - later we'll set explicitly a[1]->r_cap = a[0]->r_cap
		//a[1]->sister->r_cap += delta;
		a_rev[1]->head->tr_cap -= delta;
		a[1]->head->tr_cap     += delta;

		i[0]->tr_cap += ci; i[1]->tr_cap -= ci;
		j[0]->tr_cap += cj; j[1]->tr_cap -= cj;
		a[0]->r_cap += cij;
		a_rev[0]->r_cap += cji;

		if (a[0]->r_cap < 0)
		{
			delta = a[0]->r_cap;
			a[0]->r_cap = 0;
			a_rev[0]->r_cap += delta;
			i[0]->tr_cap -= delta; i[1]->tr_cap += delta;
			j[0]->tr_cap += delta; j[1]->tr_cap -= delta;
		}
		if (a_rev[0]->r_cap < 0)
		{
			delta = a_rev[0]->r_cap;
			a_rev[0]->r_cap = 0;
			a[0]->r_cap += delta;
			j[0]->tr_cap -= delta; j[1]->tr_cap += delta;
			i[0]->tr_cap += delta; i[1]->tr_cap -= delta;
		}

		if (a[0]->r_cap < 0)
		{
			// need to swap submodular <-> supermodular
			SET_TO(a[0], j[1]);
			SET_TO(a_rev[1], j[0]);
			REMOVE_FROM(a_rev[0], j[0]);
			SET_FROM(a_rev[0], j[1]);
			REMOVE_FROM(a[1], j[1]);
			SET_FROM(a[1], j[0]);

			delta = a[0]->r_cap;
			i[0]->tr_cap -= delta; i[1]->tr_cap += delta;
			a[0]->r_cap = -delta;
		}

		a[1]->r_cap = a[0]->r_cap;
		a_rev[1]->r_cap = a_rev[0]->r_cap;
	}

	zero_energy += E00;
}
	typename QPBO<REAL>::EdgeId QPBO<REAL>::AddPairwiseTerm(NodeId _i, NodeId _j, REAL E00, REAL E01, REAL E10, REAL E11)
{
	//printf("%d,%d",_i,node_num);
	user_assert(_i >= 0 && _i < node_num);
	user_assert(_j >= 0 && _j < node_num);
	user_assert(_i != _j);

	REAL ci, cj, cij, cji;

	if (!first_free) 
	{
		reallocate_arcs(2*(GetMaxEdgeNum() + GetMaxEdgeNum()/2));
	}

	EdgeId e = (int)(first_free - arcs[IsArc0(first_free) ? 0 : 1])/2;
	first_free = first_free->next;

	if (stage == 0)
	{
		Arc *a, *a_rev;
		a     = &arcs[0][2*e];
		a_rev = &arcs[0][2*e+1];

		Node* i = nodes[0] + _i;
		Node* j = nodes[0] + _j;

		if (E01 + E10 >= E00 + E11)
		{
			ComputeWeights(E00, E01, E10, E11, ci, cj, cij, cji);

			SET_TO(a, j);
			SET_FROM(a,     i);
			SET_FROM(a_rev, j);

			j->tr_cap += cj;
		}
		else
		{
			all_edges_submodular = false;
			ComputeWeights(E01, E00, E11, E10, ci, cj, cij, cji);

			SET_TO(a, GetMate0(j));
			a->next = NULL;
			a_rev->next = NULL;

			j->tr_cap -= cj;
		}

		SET_SISTERS(a, a_rev);
		SET_TO(a_rev, i);

		i->tr_cap += ci;
		a->r_cap = cij;
		a_rev->r_cap = cji;
	}
	else
	{
		Arc *a[2], *a_rev[2];
		a[0]     = &arcs[0][2*e];
		a_rev[0] = &arcs[0][2*e+1];
		a[1]     = &arcs[1][2*e];
		a_rev[1] = &arcs[1][2*e+1];

		Node* i[2] = { nodes[0] + _i, nodes[1] + _i };
		Node* j[2];

		if (E01 + E10 >= E00 + E11)
		{
			j[0] = nodes[0] + _j; j[1] = nodes[1] + _j;
			ComputeWeights(E00, E01, E10, E11, ci, cj, cij, cji);
		}
		else
		{
			j[1] = nodes[0] + _j; j[0] = nodes[1] + _j;
			ComputeWeights(E01, E00, E11, E10, ci, cj, cij, cji);
		}

		SET_SISTERS(a[0], a_rev[0]);
		SET_SISTERS(a[1], a_rev[1]);

		SET_TO(a[0],     j[0]);
		SET_TO(a_rev[0], i[0]);
		SET_TO(a[1],     i[1]);
		SET_TO(a_rev[1], j[1]);

		SET_FROM(a[0],     i[0]);
		SET_FROM(a_rev[0], j[0]);
		SET_FROM(a[1],     j[1]);
		SET_FROM(a_rev[1], i[1]);

		i[0]->tr_cap += ci; i[1]->tr_cap -= ci;
		j[0]->tr_cap += cj; j[1]->tr_cap -= cj;
		a[0]->r_cap = a[1]->r_cap = cij;
		a_rev[0]->r_cap = a_rev[1]->r_cap = cji;
	}

	zero_energy += E00;

	return e;
}
Ejemplo n.º 8
0
int main (int argc, char ** argv)
{
    int status;
    int choice;
    int do_next;                                  /* 0=continue, 1=mdi, 2=stop */
    int block_delete;
    char buffer[80];
    int tool_flag;
    int gees[RS274NGC_ACTIVE_G_CODES];
    int ems[RS274NGC_ACTIVE_M_CODES];
    double sets[RS274NGC_ACTIVE_SETTINGS];
    char default_name[] SET_TO "rs274ngc.var";
    int print_stack;

    if (argc > 3)
    {
        fprintf(stderr, "Usage \"%s\"\n", argv[0]);
        fprintf(stderr, "   or \"%s <input file>\"\n", argv[0]);
        fprintf(stderr, "   or \"%s <input file> <output file>\"\n", argv[0]);
        exit(1);
    }

    do_next SET_TO 2;                             /* 2=stop */
    block_delete SET_TO OFF;
    print_stack SET_TO OFF;
    tool_flag SET_TO 0;
    strcpy_s(_parameter_file_name, sizeof(_parameter_file_name), default_name);
    _outfile SET_TO stdout;                       /* may be reset below */

    for(; ;)
    {
        fprintf(stderr, "enter a number:\n");
        fprintf(stderr, "1 = start interpreting\n");
        fprintf(stderr, "2 = choose parameter file ...\n");
        fprintf(stderr, "3 = read tool file ...\n");
        fprintf(stderr, "4 = turn block delete switch %s\n",
            ((block_delete IS OFF) ? "ON" : "OFF"));
        fprintf(stderr, "5 = adjust error handling...\n");
        fprintf(stderr, "enter choice => ");
		fgets(buffer, sizeof(buffer), stdin);

        if (sscanf_s(buffer,"%d", &choice) ISNT 1)
            continue;
        if (choice IS 1)
            break;
        else if (choice IS 2)
        {
            if (designate_parameter_file(_parameter_file_name,sizeof(_parameter_file_name)) ISNT 0)
                exit(1);
        }
        else if (choice IS 3)
        {
            if (read_tool_file("") ISNT 0)
                exit(1);
            tool_flag SET_TO 1;
        }
        else if (choice IS 4)
            block_delete SET_TO ((block_delete IS OFF) ? ON : OFF);
        else if (choice IS 5)
            adjust_error_handling(argc, &print_stack, &do_next);
    }
    fprintf(stderr, "executing\n");
    if (tool_flag IS 0)
    {
        if (read_tool_file("rs274ngc.tool_default") ISNT 0)
            exit(1);
    }

    if (argc IS 3)
    {
        fopen_s(&_outfile ,argv[2], "w");
        if (_outfile IS NULL)
        {
            fprintf(stderr, "could not open output file %s\n", argv[2]);
            exit(1);
        }
    }

    if ((status SET_TO rs274ngc_init()) ISNT RS274NGC_OK)
    {
        report_error(status, print_stack);
        exit(1);
    }

    if (argc IS 1)
        status SET_TO interpret_from_keyboard(block_delete, print_stack);
    else                                          /* if (argc IS 2 or argc IS 3) */
    {
        status SET_TO rs274ngc_open(argv[1]);
        if (status ISNT RS274NGC_OK)              /* do not need to close since not open */
        {
            report_error(status, print_stack);
            exit(1);
        }
        status SET_TO interpret_from_file(do_next, block_delete, print_stack);
        rs274ngc_file_name(buffer, 5);            /* called to exercise the function */
        rs274ngc_file_name(buffer, 79);           /* called to exercise the function */
        rs274ngc_close();
    }
    rs274ngc_line_length();                       /* called to exercise the function */
    rs274ngc_sequence_number();                   /* called to exercise the function */
    rs274ngc_active_g_codes(gees);                /* called to exercise the function */
    rs274ngc_active_m_codes(ems);                 /* called to exercise the function */
    rs274ngc_active_settings(sets);               /* called to exercise the function */
    rs274ngc_exit();                              /* saves parameters */
    exit(status);
}
Ejemplo n.º 9
0
Archivo: uwerr.c Proyecto: etmc/cvc
int uwerr (char* append) {

  const double epsilon = 2.0e-16;

  int i, n, label;
  int ndata, Wmax, W, Wopt, k;
  double **a_b, *a_bb, *a_proj, a_bb_proj;
  double *F_b, *F_bb, *F_bw;
  double *Gamma_F, C_F, C_Fopt, v_Fbb, dv_Fbb, tau, *tau_int;
  double *f_alpha, *h_alpha, *m_alpha, *data_ptr, func_res;
  double value, dvalue, ddvalue, tau_intbb, dtau_intbb;
  double chisqr, Qval, *p_r, p_r_mean, p_r_var, delta, lobd, *bins;
  char filename[80], format[80];
  FILE *ofs;

  printf("[uwerr] The following arguments have been read:\n");
  printf("[uwerr] nalpha   = %d\n", nalpha);
  printf("[uwerr] nreplica = %d\n", nreplica);
  for(i=0; i<nreplica; i++) {
    printf("[uwerr] n_r(%2d)  = %d\n", i, n_r[i]);
  }
  printf("[uwerr] npara    = %d\n", npara);
  for(i=0; i<npara; i++) {
    printf("[uwerr] para(%2d) = %e\n", i, para[i]);
  }
  printf("[uwerr] ipo      = %d\n", ipo);
  printf("[uwerr] s_tau    = %e\n", s_tau);
  printf("[uwerr] obsname  = %s\n", obsname);
  printf("[uwerr] append   = %s\n", append);

  fprintf(stdout, "[uwerr]: Starting ...\n");

  /*************************************************************
   * check if combination of values in ipo an func are allowed *
   *************************************************************/
  label = ipo;
  if(ipo>0 && func!=NULL) {
    ipo = 0;
  }
  else if ( ipo==0 && func==NULL) {
    fprintf(stdout, "[uwerr] illegal values of func and ipo, return");
    return(1);
  }

  fprintf(stdout, "[uwerr]: checked ipo and func\n");

  /* ndata - total number of rows in data */
  for( i=1, ndata = *n_r; i<nreplica; ndata += *(n_r + i++) );
  /* Wmax - longest possible summation index + 1 */
  MIN_INT(n_r, nreplica, &Wmax);

  fprintf(stdout, "[uwerr]: have ndata and Wmax ready\n");

  /*******************
   * allocate memory *
   *******************/
  F_b     = (double *)calloc(nreplica, sizeof(double));
  F_bb    = (double *)calloc(1, sizeof(double));
  F_bw    = (double *)calloc(1, sizeof(double));
  Gamma_F = (double *)calloc(Wmax, sizeof(double));  
  tau_int = (double *)calloc(Wmax, sizeof(double));
  if (ipo==0 && func!=NULL) /* only necessary in case of derived quantity */ {
    a_b  = (double**)calloc(nreplica, sizeof(double*));
    a_bb = (double *)calloc(nalpha,   sizeof(double));
    for(n=0; n<nreplica; n++) { *(a_b+n)=(double*)calloc(nalpha, sizeof(double)); }
  }

  fprintf(stdout, "[uwerr]: allocated memory\n");

  /*********************************************************************
   * calculate estimators for primary observable/derived quantity      *
   *********************************************************************/
  if(ipo>0 && func==NULL)     /* here estimators for one of the prim. observables */ {
    data_ptr = *(data+ipo-1); /* points to column of ipo in data */
    for(n=0; n<nreplica; n++) {
      ARITHMEAN(data_ptr, *(n_r+n), F_b+n); /* arithmetic mean for replia */
      data_ptr = data_ptr + *(n_r+n);       /* pointer set to beginning of next replia */
      /* test */
      fprintf(stdout, "[uwerr] F_b(%d) = %18.16e\n", n, *(F_b+n));
    }
    ARITHMEAN(*(data+ipo-1), ndata, F_bb);  /* mean including all data for ipo */
    /* test */
    fprintf(stdout, "[uwerr] F_bn = %18.16e\n", *F_bb);
  }
  else if (ipo==0 && func!=NULL) {          /* estimators for derived quantity */
    /* calculate means per replica and total mean */
    for(i=0; i<nalpha; i++) {
      data_ptr = *(data+i);
      for(n=0; n<nreplica; n++) {
	ARITHMEAN(data_ptr, *(n_r+n), *(a_b+n)+i);
	data_ptr += *(n_r+n);
      }
      ARITHMEAN(*(data+i), ndata, a_bb+i);
    }
    /* calculate estimators per replica for derived quatity */
    for(n=0; n<nreplica; n++) {
      func(nalpha, *(a_b+n), npara, para, F_b+n);         /* est. for means per replicum */
    }
    func(nalpha, a_bb, npara, para, F_bb);                /* est. for total mean */
  }
  /* in case of more than one replica 
     calculate weighed mean of F_b's with weights n_r */
  if(nreplica > 1) {
    WEIGHEDMEAN(F_b, nreplica, F_bw, n_r);
    /* test */
    fprintf(stdout, "[uwerr] F_bw = %18.16e\n", *F_bw);
  }

  fprintf(stdout, "[uwerr]: have estimators ready\n");

  /***********************************************
   * calculate projection of data and mean value *
   ***********************************************/
  if(ipo>0 && func==NULL) {
    a_proj = *(data + ipo - 1); /* data is projectet to itself in case of prim.
				   observable */
    a_bb_proj = *F_bb;          /* projected mean is total mean */
  }
  else if (ipo==0 && func!=NULL) {
    f_alpha = (double *)calloc(nalpha, sizeof(double));
    h_alpha = (double *)calloc(nalpha, sizeof(double));
    m_alpha = (double *)calloc(ndata, sizeof(double));
    a_proj  = (double *)calloc(ndata, sizeof(double));
    
    /* calculate derivatives of func with respect to A_alpha */
    for(i=0; i<nalpha; i++) { /* loop over all prim. observables */
      SET_TO(h_alpha, nalpha, 0.0); 
      STDDEV(*(data+i), ndata, h_alpha+i);
      /* test */
      fprintf(stdout, "[uwerr] halpha = %18.16e\n", *(h_alpha+i));
      if(*(h_alpha+i)==0.0) {
	fprintf(stdout, "[uwerr] Warning: no fluctuation in primary observable %d\n", i);
	*(f_alpha + i) = 0.0;
      }
      else {
	ADD_ASSIGN(m_alpha, a_bb, h_alpha, nalpha);
	func(nalpha, m_alpha, npara, para, &func_res);
	*(f_alpha+i) = func_res;
	SUB_ASSIGN(m_alpha, a_bb, h_alpha, nalpha);
	func(nalpha, m_alpha, npara, para, &func_res);
	*(f_alpha+i) -= func_res;
	*(f_alpha+i) = *(f_alpha+i) / (2.0 * *(h_alpha+i));
      }
    }
    SET_TO(a_proj, ndata, 0.0);
    a_bb_proj = 0.0;
    for(i=0; i<nalpha; i++) {
      for(n=0; n<ndata; n++) { 
	*(a_proj + n) = *(a_proj + n) + ( *(*(data+i)+n) ) * ( *(f_alpha+i) );
      }
      a_bb_proj = a_bb_proj + *(a_bb+i) * (*(f_alpha+i));
    }
    free(m_alpha);
    free(f_alpha);
    free(h_alpha);
    for(n=0; n<nreplica; n++) { free(*(a_b+n)); }
    free(a_b);
    free(a_bb);
  }

  fprintf(stdout, "[uwerr]: have projected data ready\n");

  /**********************************************************************
   * calculate error, error of the error; automatic windowing condition *
   **********************************************************************/

  /* (1) Gamma_F(t), t=0,...,Wmax */
  SET_TO(Gamma_F, Wmax, 0.0);  
  SET_TO(tau_int, Wmax, 0.0);
  for(i=0,v_Fbb=0.0; i<ndata; i++) {
    v_Fbb = v_Fbb + SQR( (*(a_proj+i) - a_bb_proj) );
  }
  v_Fbb /= (double)ndata;
  C_F      = v_Fbb;
  *Gamma_F = v_Fbb;
  /* test */
  fprintf(stdout, "[uwerr] a_bb_proj  = %18.16e\n", a_bb_proj);
  fprintf(stdout, "[uwerr] Gamma_F(%1d) = %18.16e\n", 0, *Gamma_F);
  if (*Gamma_F==0.0) {
    fprintf(stderr, "[uwerr] ERROR, no fluctuations; return\n");
    strcpy(filename, obsname);
    strcat(filename,"_uwerr");
    ofs = fopen(filename, append);
    if ((void*)ofs==NULL) {
      fprintf(stderr, "[uwerr] Could not open file %s\n", filename);
      return(1);
    }
    fprintf(ofs, "%d\t%18.16e\t%18.16e\t%18.16e\t%18.16e\t%18.16e\t%18.16e\t"\
	    "%18.16e\t%18.16e\n", label, *F_bb, 0.0, 0.0, 0.0, \
	    0.0, -1.0, 0.0, 0.0);
    if (fclose(ofs)!=0) {
      fprintf(stderr, "[uwerr] Could not close file %s\n", filename);
      return(1);
    }
    return(-5);
  }
  *tau_int = 0.5; 
  for(W=1; W<Wmax-1; W++) {
    /* calculate Gamma_F(W) */
    data_ptr = a_proj;
    for(n=0; n<nreplica; n++) {
      for(i=0; i<(*(n_r+n)-W); i++) {
	*(Gamma_F+W) += (*(data_ptr+i) - a_bb_proj) * (*(data_ptr+i+W) - a_bb_proj);
      }
      data_ptr = data_ptr + *(n_r+n);
    }
    *(Gamma_F+W) = *(Gamma_F+W) / (double)(ndata-nreplica*W);
    /* test */
    fprintf(stdout, "[uwerr] Gamma_F(%d) = %18.16e\n", W, *(Gamma_F+W));
    C_F = C_F + 2.0 * *(Gamma_F+W);
    *tau_int = C_F / (2.0*v_Fbb);
    if(*tau_int < 0.5) {
      fprintf(stdout, "[uwerr] Warning: tau_int < 0.5; tau set to %f\n", TINY);
      tau = TINY;
    }
    else {
      tau = s_tau / log( (*tau_int+0.5) / (*tau_int-0.5) );
    }
    /* test */
    fprintf(stdout, "[uwerr] tau(%d) = %18.16e\n", W, tau);
    if( exp(-(double)W / tau) - tau / sqrt((double)(W*ndata)) < 0.0 ) {
      Wopt = W;
      /* test */
      fprintf(stdout, "[uwerr] Wopt = %d\n", Wopt);
      break;
     }
  }

  if(W==Wmax-1) {
    fprintf(stdout, "[uwerr] windowing condition failed after W = %d\n", W);
    return(1);
  }
  else {
    SUM(Gamma_F+1, Wopt, &C_Fopt);
    C_Fopt = 2.0 * C_Fopt + *Gamma_F;
    /* test */
    fprintf(stdout, "[uwerr] before: C_Fopt = %18.16e\n", C_Fopt);
    for(W=0; W<=Wopt; W++) {
      *(Gamma_F+W) = *(Gamma_F+W) + C_Fopt/((double)ndata);
    }
    SUM(Gamma_F+1, Wopt, &C_Fopt);
    C_Fopt = 2.0 * C_Fopt + *Gamma_F;
    /* test */
    fprintf(stdout, "[uwerr] after: C_Fopt = %18.16e\n", C_Fopt);
    v_Fbb = *Gamma_F;
    *tau_int = 0.5*v_Fbb;
    for(W=1; W<=Wopt; W++) *(tau_int+W) = *(tau_int+W-1) + *(Gamma_F+W);
    for(W=0; W<=Wopt; W++) *(tau_int+W) /= v_Fbb;
  }

  fprintf(stdout, "[uwerr]: perfomed automatic windowing\n");

  /***********************************
   * bias cancellation of mean value *
   ***********************************/
  if(nreplica > 1 ) {
    *F_bb = ( (double)nreplica * *F_bb - *F_bw ) / ((double)(nreplica-1));
  }

  fprintf(stdout, "[uwerr]: leading bias cancelled\n");


  /**************************
   * calculation of results *
   **************************/
  value = *F_bb;
  dvalue = sqrt(C_Fopt/((double)ndata));
  ddvalue = dvalue * sqrt((Wopt + 0.5)/ndata);
  tau_intbb = C_Fopt / (2.0 * v_Fbb);
  dtau_intbb = sqrt( 2.0 * ( 2.0*Wopt-3.0*tau_intbb + 1 + \
			     1/(4.0*tau_intbb))/((double)ndata) ) * tau_intbb;
  dv_Fbb = sqrt(2.0*(tau_intbb + 1/(4.0*tau_intbb)) / (double)ndata) * v_Fbb;

  /*******************************************
   * consistency checks in case nreplica > 0 *
   *******************************************/
  if(nreplica>1) {
  /* (1) calculate Q-value <---> determine goodness of the fit 
     F_b(n) = F_bw = const. */
    chisqr = 0.0;
    for(n=0; n<nreplica; n++) {
      chisqr = chisqr + SQR( *(F_b+n) - *F_bw ) / (C_Fopt/(double)(*(n_r+n)));
    }
    /* test */
    fprintf(stdout, "[uwerr]: chisqr = %18.16e\n", chisqr);
    fprintf(stdout, "[uwerr]: n      = %d     \n", (nreplica-1)/2);

    Qval = 1.0 - incomp_gamma(chisqr/2.0, (nreplica-1)/2);
  
  /* (2) inspection of p_r's defined below in a histogramm */
    p_r = (double *)calloc(nreplica, sizeof(double));
    for(n=0; n<nreplica; n++) {
      *(p_r+n) = (*(F_b+n) - *F_bw) / \
	(dvalue*sqrt(((double)ndata/(double)(*(n_r+n)))-1.0));
    }
    ARITHMEAN(p_r, nreplica, &p_r_mean);
    VAR(p_r, nreplica, &p_r_var);
    k = 1 + (int)rint(log((double)nreplica)/log(2.0));
    strcpy(filename, obsname);
    strcat(filename, "_uwerr_hist");
    ofs = fopen(filename, append);
    fprintf(ofs, "# mean of p_r's:\tp_r_mean = %8.6e\n" \
	    "# variance of p_r's:\tp_r_var = %8.6e\n",  \
	    p_r_mean, p_r_var);
    strcpy(format, "%%dst p_r(%2d) = %18.16e\n");
    for(n=0; n<nreplica; n++) {
      fprintf(ofs, format, n, *(p_r+n));
    }
    if(k<3) /* not enough classes for a meaningful histogramm */ {
      fprintf(ofs, "# [uwerr]: k = %d is to small\n", k);
    }
    else {
      ABS_MAX_DBL(p_r, nreplica, &lobd); /* max{|p_r's|} */
      lobd = lobd *(1.0+TINY);
      delta = 2.0*lobd/(double)k;        /* expected distribution around mean=0 */
      lobd = -lobd;                      /* lower boundary of abscissa */
      bins = (double *)calloc(k, sizeof(double)); /* contains number of entries */
      SET_TO(bins, k, 0.0);                       /* for each class */
      for(n=0; n<nreplica; n++) /* inc. bins(i) by 1, if p_r(n) is in class i */ {
	i = (int)((*(p_r+n) - lobd)/delta);
	*(bins + i) = *(bins + i) + 1.0;
      }
      fprintf(ofs, "# number of entries:\tnreplica = %d\n" \
	      "# number of classes:\tk = %d\n"             \
	      "# lower boundary:\tlobd = %8.6e\n"          \
	      "# bin width:\tdelta = %8.6e\n",             \
	      nreplica, k, lobd, delta);
      strcpy(format, "%%hst %18.16e\t%18.16e\n");
      for(i=0; i<k; i++) {
	fprintf(ofs, format, lobd+((double)i+0.5)*delta, *(bins+i));
      }
    }
    fclose(ofs);
  }

  /**************************
   * output                 *
   **************************/

  /* (1) value, dvalue, ... */  
  strcpy(filename, obsname);
  strcat(filename,"_uwerr");
  ofs = fopen(filename, append);
  if ((void*)ofs==NULL) {
    fprintf(stderr, "[uwerr] Could not open file %s\n", filename);
    return(1);
  }
  strcpy(format, "%d\t%18.16e\t%18.16e\t%18.16e\t%18.16e\t%18.16e\t%18.16e\t%18.16e\t%18.16e\n");
  fprintf(ofs, format, label, value, dvalue, ddvalue, tau_intbb, dtau_intbb, Qval, v_Fbb, dv_Fbb);
  if (fclose(ofs)!=0) {
    fprintf(stderr, "[uwerr] Could not close file %s\n", filename);
    return(1);
  }

  /* (2) Gamma_F */
  strcpy(filename, obsname);
  strcat(filename, "_uwerr_Gamma");
  ofs = fopen(filename, append);
  if ((void*)ofs==NULL) {
    fprintf(stderr, "[uwerr] Could not open file %s\n", filename);
    return(1);
  }
  strcpy(format, "%d\t%18.16e\n");
  fprintf(ofs, "# obsname = %s \t ipo = %d", obsname, ipo);
  for(W=0; W<=Wopt; W++) {
    fprintf(ofs, format, W, *(Gamma_F+W));
  }
  if (fclose(ofs)!=0) {
    fprintf(stderr, "[uwerr] Could not close file %s\n", filename);
    return(1);
  }

  /* (3) tau_int */
  strcpy(filename, obsname);
  strcat(filename, "_uwerr_tauint");
  ofs = fopen(filename, append);
  fprintf(ofs, "# obsname = %s \t ipo = %d", obsname, ipo);
  for(W=0; W<=Wopt; W++) {
    fprintf(ofs, format, W, *(tau_int+W));
  }
  fclose(ofs);

  fprintf(stdout, "[uwerr]: output written\n");

  /*****************************
   * free allocated disk space *
   *****************************/
  free(F_b);
  free(F_bb);
  free(F_bw);
  free(Gamma_F);
  free(tau_int);
  if(ipo==0 && func!=NULL) {
    free(a_proj);
  }

  return(0);

}