Esempio n. 1
0
float MaxDeviation(float yieldRef,float yield[], int var0, int nvars){
  float diff;
  float maxdeviation=0; //
  for (int i=var0; i<=nvars;i++){
    //  cout << " tested "<< yield[i] <<endl;
    diff= Deviation(yieldRef,yield[i]);
    // cout << diff << " is the relative deviation" << endl;
    if ( fabs(diff)>maxdeviation) {
      maxdeviation=diff; //cout << 100*tmp  << " % relative deviation kept" <<endl;
    }else {}
  }
  return maxdeviation;
}
Esempio n. 2
0
float ppAlphaSyst(float yieldRef, float VarS[], float VarB[],float ppyieldRef,float ppyield){
  //compute (max tail,gaus + max bkgd) as a systematic
  // substitute alpha_pbpb with alpha_pp
  
  float s1, s2, s3;
  s1= Deviation(ppyieldRef,ppyield);//variations 1,2
  s2= MaxDeviation(yieldRef,VarS,2,5);// variations 2,3,4,5
  if (s1<s2){s1=s2;}
  s3= MaxDeviation(yieldRef,VarB,0,1);//bkgd vars 0,1! tricky 
  //cout<<setprecision(3)<< 100*s1 << "\\% & "<< 100*s3<<"\\% & "<< 100*MaxSyst(yieldRef,VarS,VarB,5,2)<< "\\% & ";
  //  return sqrt(pow(s1,2)+pow(s2,2)+pow(s3,2));
  return sqrt(pow(s1,2)+pow(s3,2));
  
}
Vector2<Real> BiQuadToSqr<Real>::Transform (const Vector2<Real>& P)
{
    Vector2<Real> A = mP00 - P;
    Real AB = A.DotPerp(mB);
    Real AC = A.DotPerp(mC);

    // 0 = ac*bc+(bc^2+ac*bd-ab*cd)*s+bc*bd*s^2 = k0 + k1*s + k2*s^2
    Real k0 = AC*mBC;
    Real k1 = mBC*mBC + AC*mBD - AB*mCD;
    Real k2 = mBC*mBD;

    if (Math<Real>::FAbs(k2) >= Math<Real>::ZERO_TOLERANCE)
    {
        // The s-equation is quadratic.
        Real inv = ((Real)0.5)/k2;
        Real discr = k1*k1 - ((Real)4)*k0*k2;
        Real root = Math<Real>::Sqrt(Math<Real>::FAbs(discr));

        Vector2<Real> result0;
        result0.X() = (-k1 - root)*inv;
        result0.Y() = AB/(mBC + mBD*result0.X());
        Real deviation0 = Deviation(result0);
        if (deviation0 == (Real)0)
        {
            return result0;
        }

        Vector2<Real> result1;
        result1.X() = (-k1 + root)*inv;
        result1.Y() = AB/(mBC + mBD*result1.X());
        Real deviation1 = Deviation(result1);
        if (deviation1 == (Real)0)
        {
            return result1;
        }

        if (deviation0 <= deviation1)
        {
            if (deviation0 <= Math<Real>::ZERO_TOLERANCE)
            {
                return result0;
            }
        }
        else
        {
            if (deviation1 <= Math<Real>::ZERO_TOLERANCE)
            {
                return result1;
            }
        }
    }
    else
    {
        // The s-equation is linear.
        Vector2<Real> result;

        result.X() = -k0/k1;
        result.Y() = AB/(mBC + mBD*result.X());
        Real deviation = Deviation(result);
        if (deviation <= Math<Real>::ZERO_TOLERANCE)
        {
            return result;
        }
    }

    // Point is outside the quadrilateral, return invalid.
    return Vector2<Real>(Math<Real>::MAX_REAL, Math<Real>::MAX_REAL);
}
Vector2<Real> BiQuadToSqr<Real>::Transform (const Vector2<Real>& rkP)
{
    Vector2<Real> kA = m_kP00 - rkP;
    Real fAB = kA.DotPerp(m_kB);
    Real fAC = kA.DotPerp(m_kC);

    // 0 = ac*bc+(bc^2+ac*bd-ab*cd)*s+bc*bd*s^2 = k0 + k1*s + k2*s^2
    Real fK0 = fAC*m_fBC;
    Real fK1 = m_fBC*m_fBC + fAC*m_fBD - fAB*m_fCD;
    Real fK2 = m_fBC*m_fBD;

    if (Math<Real>::FAbs(fK2) >= Math<Real>::ZERO_TOLERANCE)
    {
        // s-equation is quadratic
        Real fInv = ((Real)0.5)/fK2;
        Real fDiscr = fK1*fK1 - ((Real)4.0)*fK0*fK2;
        Real fRoot = Math<Real>::Sqrt(Math<Real>::FAbs(fDiscr));

        Vector2<Real> kResult0;
        kResult0.X() = (-fK1 - fRoot)*fInv;
        kResult0.Y() = fAB/(m_fBC + m_fBD*kResult0.X());
        Real fDeviation0 = Deviation(kResult0);
        if (fDeviation0 == (Real)0.0)
        {
            return kResult0;
        }

        Vector2<Real> kResult1;
        kResult1.X() = (-fK1 + fRoot)*fInv;
        kResult1.Y() = fAB/(m_fBC + m_fBD*kResult1.X());
        Real fDeviation1 = Deviation(kResult1);
        if (fDeviation1 == (Real)0.0)
        {
            return kResult1;
        }

        if (fDeviation0 <= fDeviation1)
        {
            if (fDeviation0 <= Math<Real>::ZERO_TOLERANCE)
            {
                return kResult0;
            }
        }
        else
        {
            if (fDeviation1 <= Math<Real>::ZERO_TOLERANCE)
            {
                return kResult1;
            }
        }
    }
    else
    {
        // s-equation is linear
        Vector2<Real> kResult;

        kResult.X() = -fK0/fK1;
        kResult.Y() = fAB/(m_fBC + m_fBD*kResult.X());
        Real fDeviation = Deviation(kResult);
        if (fDeviation <= Math<Real>::ZERO_TOLERANCE)
        {
            return kResult;
        }
    }

    // point is outside the quadrilateral, return invalid
    return Vector2<Real>(Math<Real>::MAX_REAL,Math<Real>::MAX_REAL);
}
Esempio n. 5
0
int main(int argc, char* argv[])
{

    programname = copystring(Basename(argv[0]));

    argc--, argv++;
    while (argc && argv[0][0] == '-') {
        while (*++*argv)
            switch(**argv) {
	    case 'p':
                pflag++;
                break;
	    case 'e':
		eflag++;
                epsfwidth = WidthInPoints(*argv + 1);
                goto nextarg;
	    case 'd':
		dflag++;
                goto nextarg;
	    case 'i':
		switch( *(*argv + 1) ) {
		  case '-':
		    iflag = -1;
		  case '+':
		  default:
		    iflag = 1;
		}
                goto nextarg;
	    case 'g':
		gflag++;
		goto nextarg;
	    case 'y':
		yflag++;
		goto nextarg;
	    case 'b':
		bflag++;
		goto nextarg;
	    case 's':
		sflag++;
		goto nextarg;
	    case 'm':
		mflag++;
		TWENTY = atoi(*argv + 1);
		if (TWENTY > DEFAULT_TWENTY)
		    Usage(*argv-1);
		goto nextarg;
	    case 't':
		tflag++;
		THRESHOLD_PERCENT = (floatish) atof(*argv + 1);
		if (THRESHOLD_PERCENT < 0 || THRESHOLD_PERCENT > 5)
		    Usage(*argv-1);
		goto nextarg;
	    case 'c':
		cflag++;
		goto nextarg;
	    case '?':
	    default:
		Usage(*argv-1);
            }
nextarg: ;
        argc--, argv++;
    }

    hpfile = "stdin";
    psfile = "stdout";

    hpfp = stdin;
    psfp = stdout;

    filter = argc < 1;



    if (!filter) {
	pathName = copystring(argv[0]);
	DropSuffix(pathName, ".hp");
	baseName = copystring(Basename(pathName));

        hpfp  = Fp(pathName, &hpfile, ".hp", "r"); 

        // I changed these two lines to use 'pathName' instead of
        // 'baseName'.  This means that the .ps and .aux files get put in
        // the same directory as the .hp file.  This solved Valgrind bugt
        // #117686.  --njn 
//	psfp  = Fp(baseName, &psfile, ".ps", "w"); 
	psfp  = Fp(pathName, &psfile, ".ps", "w"); 

//	if (pflag) auxfp = Fp(baseName, &auxfile, ".aux", "r");
	if (pflag) auxfp = Fp(pathName, &auxfile, ".aux", "r");
    }

    GetHpFile(hpfp);

    if (!filter && pflag) GetAuxFile(auxfp);


    TraceElement();          /* Orders on total, Removes trace elements (tflag) */

    if (dflag) Deviation();  /* ReOrders on deviation */

    if (iflag) Identorder(iflag); /* ReOrders on identifier */

    if (pflag) Reorder();    /* ReOrders on aux file */

    if (TWENTY) TopTwenty(); /* Selects top twenty (mflag) */

    Dimensions();

    areabelow = AreaBelow();

    Scale();

    PutPsFile();

    if (!filter) {
        auxfp = Fp(baseName, &auxfile, ".aux", "w");
	PutAuxFile(auxfp);
    } 

    return(0);
}
Esempio n. 6
0
File: Main.c Progetto: 23Skidoo/ghc
int main(int argc, char *argv[])
{

    programname = copystring(Basename(argv[0]));

    argc--, argv++;
    while (argc && argv[0][0] == '-') {
        while (*++*argv)
            switch(**argv) {
	    case 'p':
                pflag++;
                break;
	    case 'e':
		eflag++;
                epsfwidth = WidthInPoints(*argv + 1);
                goto nextarg;
	    case 'd':
		dflag++;
                goto nextarg;
	    case 'i':
		switch( *(*argv + 1) ) {
		  case '-':
		    iflag = -1;
		    break;
		  case '+':
		  default:
		    iflag = 1;
		}
                goto nextarg;
	    case 'g':
		gflag++;
		goto nextarg;
	    case 'y':
		yflag++;
		goto nextarg;
	    case 'b':
		bflag++;
		goto nextarg;
	    case 's':
		sflag++;
		goto nextarg;
	    case 'm':
		mflag++;
		TWENTY = atoi(*argv + 1);
		// only 20 keys fit on a page
		if (TWENTY > DEFAULT_TWENTY) 
		   multipageflag++;
		goto nextarg;
	    case 'M':
	        multipageflag++;
                goto nextarg;
	    case 't':
		tflag++;
		THRESHOLD_PERCENT = (floatish) atof(*argv + 1);
		if (THRESHOLD_PERCENT < 0 || THRESHOLD_PERCENT > 5)
		    Usage(*argv-1);
		goto nextarg;
	    case 'c':
		cflag++;
		goto nextarg;
	    case '?':
	    default:
		Usage(*argv-1);
            }
nextarg: ;
        argc--, argv++;
    }

    hpfile = "stdin";
    psfile = "stdout";

    hpfp = stdin;
    psfp = stdout;

    filter = argc < 1;



    if (!filter) {
	pathName = copystring(argv[0]);
	DropSuffix(pathName, ".hp");
#if defined(_WIN32)
	DropSuffix(pathName, ".exe");
#endif
	baseName = copystring(Basename(pathName));
        
        hpfp  = Fp(pathName, &hpfile, ".hp", "r"); 
	psfp  = Fp(baseName, &psfile, ".ps", "w"); 

	if (pflag) auxfp = Fp(baseName, &auxfile, ".aux", "r");
    }

    GetHpFile(hpfp);

    if (!filter && pflag) GetAuxFile(auxfp);


    TraceElement();          /* Orders on total, Removes trace elements (tflag) */

    if (dflag) Deviation();  /* ReOrders on deviation */

    if (iflag) Identorder(iflag); /* ReOrders on identifier */

    if (pflag) Reorder();    /* ReOrders on aux file */

    /* Selects top bands (mflag) - can be more than 20 now */
    if (TWENTY != 0) TopTwenty(); 

    Dimensions();

    areabelow = AreaBelow();

    Scale();

    PutPsFile();

    if (!filter) {
        auxfp = Fp(baseName, &auxfile, ".aux", "w");
	PutAuxFile(auxfp);
    } 

    return(0);
}
Esempio n. 7
0
int WriteTrajectories(int argc, char **argv, struct control *pcontrol, data **P)
/* function calulating and writing trajectories */
{
  int GoOn, i;
  double x1, x2, azim, dist_plus, dist_minus, deviation, deviation_start;
  double azim_plus, azim_minus;  
  double start_azim[2];
  struct traj traj;
  struct point nextPoint_plus;         // next point in direction azim
  struct point nextPoint_minus;        // next point in reverse direction 
  struct sort *NN;
  struct data *Point; 
  FILE *trajstart, *out;
  char line[MaxCharInLine] = " ";
  void NextPoint(struct point *nextPoint, double azim, const double c, double x1, double x2); 
  FILE *PrepareFileOut(int argc, char **argv, struct control *pcontrol, const char *ext);
  double UpdateAzim(struct control *pcontrol, sort *NN, data *Point, data **P);
  int BoundaryCheck(struct point *actualpoint, struct control *pcontrol);
  double Distance(struct point *p1, struct point *p2);
  double Deviation(struct control *pcontrol, data *Point);
  int AllocateBin(struct control *pcontrol);
  int CalcSegmentArea(struct control *pcontrol);

  
  // allocate memory for gridding datapoint and initialise the array pointers 
  // needed later with NULL 
  if ((Point = new data) == NULL) {
      fprintf(stderr, "ERROR: not enough memory for griddatapoint\n");
      return -1;
  } 
  Point->nn = NULL;
  Point->dnn = NULL;
  Point->wnn = NULL;
  Point->ann = NULL;
  Point->rbin = NULL;
  Point->abin = NULL;

  // allocate memory for nearest neighbour search array
  NN = (struct sort *) malloc(sizeof(struct sort)*pcontrol->NoPoints);
  if ( NN == NULL )  {
     fprintf(stderr, "ERROR: not enough memory for determing nearest neighbours while gridding\n");
     return -1;
  }

  // allocate memory for bin array if normalization of weights is set
  if (pcontrol->DistanceWeight == ntwf || pcontrol->DistanceWeight == nnid)  {
     // check if bin has not been allocated before, we initialize bin with NULL
     if (pcontrol->bin == NULL) {   // allocate
	if (AllocateBin(pcontrol) < 0)  {
	   fprintf(stderr,"ERROR: occured in function AllocateBin \n");
	   return -1;
	}
     }
     // calculate the area of the segments if necesary
     if (pcontrol->SegArea == NULL)  {  // not yet calculated
	if (CalcSegmentArea(pcontrol) < 0)  {
	   fprintf(stderr, "ERROR: error occured in function CalcSegmentArea\n");
	   return -1;
	}
     }
  }



  if (pcontrol->InteractiveTrajectories)  {
     trajstart = stdin;
     fprintf(stdout, "    ENTER startpoints for trajectories (lon lat), give END to stop\n");
  } else {   
     // open the file with the startpoints for the trajectories and
     // skip first line, this line is a headerline
     if ( (trajstart = fopen(pcontrol->FileTrajecStart, "r")) == NULL) {
        fprintf(stderr, "ERROR: could not open file %s for reading starting points of trajectories \n", pcontrol->FileTrajecStart);
        return -1;
     } 
     fgets(line, MaxCharInLine-1, trajstart);                          // skip header line
  }
  
  // open output file and write header with information in the file
  if ( (out = PrepareFileOut(argc, argv, pcontrol, trjs )) == NULL)  {
     fprintf(stderr, "ERROR: occured in function PrepareFileOut\n");
     return -1;
  }
   
  // loop over all points in FileTrajecStart
  while ( (fgets(line, MaxCharInLine-1, trajstart) != NULL) && (strncmp(line, "END", 3) != 0) ) {
    sscanf(line, " %lf %lf ", &x1, &x2);   
    Point->x1 = d2r*x1;
    Point->x2 = d2r*x2;
    if (pcontrol->verbose) 
        fprintf(stdout, "    following trajectory starting at %.3f deg longitude %.3f deg latitude\n", x1, x2 ); 
    // calculate the azimuth (the predictor for the direction) for the starting point.
    // function returns values between -pi/2 and pi/2 if smoothing was possible,
    // if no neighbours were found, function returns pi. When an error occured,
    // function returns -pi.
    if ( (azim = UpdateAzim(pcontrol, NN, Point, P)) < (-0.5)*pi)  {
        fprintf(stderr, "ERROR: occured in function UpdateAzim \n");
        return -1;
    } else if (azim > 0.5 * pi) {    // no smoothing was possible
        if(pcontrol->verbose) fprintf(stdout, "    lost this trajectory, follow next ...\n");
        if (pcontrol->InteractiveTrajectories) fprintf(stdout, "     new startpoint for trajectory, give END to stop calculation\n");
        continue; // stop following this trajectory, read new startpoints... 
    }   
    // calculate the deviation at the starting point
    deviation_start = Deviation(pcontrol, Point);
    
    // atan returns values between -pi/2 and pi/2, we want positive values
    // between 0 and 2pi (in radians!!).
    // We have two azimuths, one in the 'positive' direction, the second in the 
    // reverse direction. They are stored in the array start_azim
    start_azim[0] = azim >= 0.0 ? azim : (pi + azim);            // plus direction
    start_azim[1] = azim >= 0.0 ? (azim + pi) : (2*pi + azim);   // minus direction

    for (i=0; i<=1; i++)  {
       // write the values of the start point in struct Point (this struct is used for 
       // calculating the updated azimuth, and for calculating the nextPoint).
       // For i=0 this values are already stored in Point, we need it for the second run.
       Point->x1 = d2r*x1;
       Point->x2 = d2r*x2;

       // store the values of start point in structure traj, write the startpoint to outfile
       // if i==0 follow start_azim_plus direction, else follow start_azim_minus direction
       traj.p.x1 = d2r*x1;
       traj.p.x2 = d2r*x2;       
       traj.p.azim = start_azim[i];
       
      
       if (pcontrol->AdditionalInfo) {
          fprintf(out, "> %d \n %lf %lf %lf\n", i, x1, x2, deviation_start);
       } else {
          fprintf(out, "> %d \n %lf %lf \n", i, x1, x2); 
       }

       // follow direction of traj.p.azim, calculate next point in this direction
       NextPoint(&nextPoint_plus, traj.p.azim, pcontrol->TrajStep, Point->x1, Point->x2);

       // write the location of nextPoint_plus in traj.a and in Point
       traj.a.x1 = Point->x1 = nextPoint_plus.x1;
       traj.a.x2 = Point->x2 = nextPoint_plus.x2;
       
       // write the location of the next Point to outfile
       if (pcontrol->AdditionalInfo) {
          deviation = Deviation(pcontrol, Point);
          fprintf(out, " %lf %lf %lf \n", r2d*Point->x1, r2d*Point->x2, deviation); 
       } else {
          fprintf(out, " %lf %lf\n", r2d*Point->x1, r2d*Point->x2); 
       }
       
       
       // calculate the azimuth for this second point
       if ( (azim = UpdateAzim(pcontrol, NN, Point, P)) < (-0.5)*pi)  {
           fprintf(stderr, "ERROR: occured in function UpdateAzim \n");
           return -1;
       } else if (azim > 0.5 * pi) {    // no smoothing was possible, no neighbours
           if(pcontrol->verbose) fprintf(stdout, "    lost this trajectory, follow next ...\n");
           if (i==1 && pcontrol->InteractiveTrajectories) 
              fprintf(stdout, "     new startpoint for trajectory, give END to stop calculation\n");
           continue; // follow trajectory in the other direction 
       }   
 
       // follow this trajectory until map boundaries are reached 
       // what happens if no next point can be calculated (-A option) VERO
       do {               
          // atan returns values between -pi/2 and pi/2, we want positive values
          // between 0 and 2pi (in radians!!)
          azim_plus = azim >= 0.0 ? azim : (pi + azim);
          azim_minus = azim >= 0.0 ? (azim + pi) : (2*pi + azim);
          NextPoint(&nextPoint_plus, azim_plus, pcontrol->TrajStep , traj.a.x1, traj.a.x2);
          NextPoint(&nextPoint_minus, azim_minus, pcontrol->TrajStep , traj.a.x1, traj.a.x2);
       
          // decide which point is the furthest away from the location used two steps before
          dist_plus = Distance(&nextPoint_plus, &(traj.p));
          dist_minus = Distance(&nextPoint_minus, &(traj.p));
       
          if (dist_plus > dist_minus) {   // dist_plus is the point we want
              // now we use traj.a as traj.p and nextPoint_plus as traj.a
              traj.p.x1 = traj.a.x1;
              traj.p.x2 = traj.a.x2;
              traj.p.azim = azim_plus;
              traj.a.x1 = Point->x1 = nextPoint_plus.x1;
              traj.a.x2 = Point->x2 = nextPoint_plus.x2;
              if (pcontrol->AdditionalInfo) {
                 deviation = Deviation(pcontrol, Point);
                 fprintf(out, " %lf %lf %lf\n", r2d*Point->x1, r2d*Point->x2, deviation);
              } else {
                 fprintf(out, " %lf %lf\n", r2d*Point->x1, r2d*Point->x2);    
              }
              
          } else {                        // dist_minus is the point we want                           
              // now we use traj.a as traj.p and nextPoint_minus as traj.a

              traj.p.x1 = traj.a.x1;
              traj.p.x2 = traj.a.x2;
              traj.p.azim = azim_minus;
              traj.a.x1 = Point->x1 = nextPoint_minus.x1;
              traj.a.x2 = Point->x2 = nextPoint_minus.x2;
              if (pcontrol->AdditionalInfo) {
                 deviation = Deviation(pcontrol, Point);
                 fprintf(out, " %lf %lf %lf\n", r2d*Point->x1, r2d*Point->x2, deviation);
              } else {
                 fprintf(out, " %lf %lf\n", r2d*Point->x1, r2d*Point->x2);    
              }

          }
       
          // calculate the azimuth for the 'new' actual point
          if ( (azim = UpdateAzim(pcontrol, NN, Point, P)) < (-0.5)*pi)  {
              fprintf(stderr, "ERROR: occured in function UpdateAzim \n");
              return -1;
          } else if (azim > 0.5 * pi) {    // no smoothing was possible, no neighbours
              if(pcontrol->verbose) fprintf(stdout, "    lost this trajectory at point %lf %lf, follow next ...\n",r2d*Point->x1, r2d*Point->x2 );
              break; // break do loop, stop following this trajectory in this direction ... 
          } 
          
          GoOn = BoundaryCheck(&(traj.a), pcontrol);
 
       } while(GoOn);
       
     }
     
     fflush(out);
     
     if (pcontrol->InteractiveTrajectories) {
        fprintf(stdout, "    new startpoint for trajectory, give END to stop calculation\n");
     }   

  }
  fclose(trajstart);
  fclose(out);
  free(NN);
  return 0;
  
}