Ejemplo n.º 1
0
void
CInterval::
init()
{
  bool integral = isIntegral();

  timeType_ = TimeType::SECONDS;

  startTime_.year   = 0;
  startTime_.month  = 0;
  startTime_.day    = 0;
  startTime_.hour   = 0;
  startTime_.minute = 0;
  startTime_.second = 0;

  // Ensure min/max are in the correct order
  double min = std::min(data_.start, data_.end);
  double max = std::max(data_.start, data_.end);

  if      (isIntegral()) {
    min = std::floor(min);
    max = std::ceil (max);
  }
  else if (isDate()) {
    int y = timeLengthToYears  (min, max);
    int m = timeLengthToMonths (min, max);
    int d = timeLengthToDays   (min, max);

    startTime_.year  = timeToYear   (min);
    startTime_.month = timeToMonths (min);
    startTime_.day   = timeToDays   (min);

    min = 0;

    if      (y >= 5) {
      //std::cout << "years\n";
      timeType_ = TimeType::YEARS;
      max       = y;
    }
    else if (m >= 3) {
      //std::cout << "months\n";
      timeType_ = TimeType::MONTHS;
      max       = m;
    }
    else if (d >= 4) {
      //std::cout << "days\n";
      timeType_ = TimeType::DAYS;
      max       = d;
    }

    integral = true;
  }
  else if (isTime()) {
    int h = timeLengthToHours  (min, max);
    int m = timeLengthToMinutes(min, max);
    int s = timeLengthToSeconds(min, max);

    startTime_.hour   = timeToHours  (min);
    startTime_.minute = timeToMinutes(min);
    startTime_.second = timeToSeconds(min);

    min = 0;

    if      (h >= 12) {
      //std::cout << "hours\n";
      timeType_ = TimeType::HOURS;
      max       = h;
    }
    else if (m >= 10) {
      //std::cout << "minutes\n";
      timeType_ = TimeType::MINUTES;
      max       = m;
    }
    else {
      //std::cout << "seconds\n";
      timeType_ = TimeType::SECONDS;
      max       = s;
    }

    integral = true;
  }

  //---

  // use fixed increment
  double majorIncrement = this->majorIncrement();

  if (majorIncrement > 0.0 && (! isDate() && ! isTime())) {
    calcData_.start     = min;
    calcData_.end       = max;
    calcData_.increment = majorIncrement;

    calcData_.numMajor =
      CMathRound::RoundNearest((calcData_.end - calcData_.start)/calcData_.increment);
    calcData_.numMinor = 5;

    return;
  }

  //---

  if (data_.numMajor > 0) {
    goodTicks_.opt = data_.numMajor;
    goodTicks_.min = std::max(goodTicks_.opt/10, 1);
    goodTicks_.max = goodTicks_.opt*10;
  }
  else {
    goodTicks_ = GoodTicks();
  }

  //---

  calcData_.numMajor = -1;
  calcData_.numMinor = 5;

  //---

  calcData_.increment = data_.increment;

  if (calcData_.increment <= 0.0) {
    calcData_.increment = initIncrement(min, max, integral);

    //---

    // Calculate other test increments
    for (int i = 0; i < numIncrementTests; i++) {
      // disable non-integral increments for integral
      if (integral && ! isInteger(incrementTests[i].factor)) {
        incrementTests[i].incFactor = 0.0;
        continue;
      }

      // disable non-log increments for log
      if (isLog() && ! incrementTests[i].isLog) {
        incrementTests[i].incFactor = 0.0;
        continue;
      }

      incrementTests[i].incFactor = calcData_.increment*incrementTests[i].factor;
    }

    //---

    // Test each increment in turn (Set default start/end to force update)
    int tickIncrement = this->tickIncrement();

    GapData axisGapData;

    for (int i = 0; i < numIncrementTests; i++) {
      // skip disable tests
      if (incrementTests[i].incFactor <= 0.0)
        continue;

      // if tick increment set then skip if not multiple of increment
      if (tickIncrement > 0) {
        if (! isInteger(incrementTests[i].incFactor))
          continue;

        int incFactor1 = int(incrementTests[i].incFactor);

        if (incFactor1 % tickIncrement != 0)
          continue;
      }

      // test factor, ticks and update best
      if (testAxisGaps(min, max,
                       incrementTests[i].incFactor,
                       incrementTests[i].numTicks,
                       axisGapData)) {
        //axisGapData.print("  Best) ");
      }
    }

    //---

    calcData_.start     = axisGapData.start;
    calcData_.end       = axisGapData.end;
    calcData_.increment = axisGapData.increment;
    calcData_.numMinor  = (! isLog() ? axisGapData.numMinor : 10);
  }
  else {
    calcData_.start    = min;
    calcData_.end      = max;
    calcData_.numMinor = 5;
  }

  calcData_.numMajor =
    CMathRound::RoundNearest((calcData_.end - calcData_.start)/calcData_.increment);

  if      (isDate()) {
    if      (timeType_ == TimeType::YEARS) {
      calcData_.numMinor = 12;
    }
    else if (timeType_ == TimeType::MONTHS) {
      calcData_.numMinor = 4;
    }
    else if (timeType_ == TimeType::DAYS) {
      calcData_.numMinor = 4;
    }
  }
  else if (isTime()) {
    if      (timeType_ == TimeType::HOURS) {
      calcData_.numMinor = 6;
    }
    else if (timeType_ == TimeType::MINUTES) {
      calcData_.numMinor = 12;
    }
    else if (timeType_ == TimeType::SECONDS) {
      calcData_.numMinor = 12;
    }
  }
}
Ejemplo n.º 2
0
int main( int argc, char *argv[]) {
	int currentChar = 0; //markes current char in file
	char number[256]; 	 //char array holding current processing number
	int numberInt = 0; 	 //int version of currently held number (converted from number array
	int i = 0;
	int j = 0;
	double time = 0;    //initial time (t_stop)
	double timeDiff = 0;   // t - t_stop
	int beta = 0; 		   // beta detections
	double netBeta = 0;	   // beta detections in picoCurries
	int alpha = 0;	       // alpha detections
	double netAlpha = 0;   // alpha detections in picoCurries
	int CFC = 0;  	       // Clean Filter Count
	const double ALPHACALIBRATION = 1.63;
	const double BETACALIBRATION = 1.15;

	//make sure there is only one argument: the filename
	//aka simple error handeling
	if (argc != 2) {
		printf("Usage: %s dataset\n", argv[0]);
	} else {
		FILE *data = fopen(argv[1], "r");
		FILE *write = fopen(strcat(argv[1], "Activity"),"w");

		if ((data == 0) || (write == 0)) {
			printf("Could not open/find file\n");
			return(1);
		} else {
			/* 
			This right here is the section to parse the file. As I suck with that stuff, this looks pretty ugly. Basically, it parses the file character by character by using fgetc(). Each time this is called, the next character is pulled from the file. If the chacter is a '#', it will ignore the rest of the line (by pulling more characters until a '\n' is passed, which is why typing the datasheets with Windows won't work).

			When it gets past the commented lines, it will take the first number it sees and sets it to the initial time. 

			Then with each line it calculates the time difference, the net alphaBeta, the net beta, and the corrected alpha and beta numbers, prints these to file as well as to screen. It then repeats until the End Of File is reached.
			*/
			//get characters until end of file is reached
			while((currentChar = fgetc(data)) != EOF){
				if(currentChar == '#'){

				//ignore lines beginning with '#'
					while((currentChar = fgetc(data)) != '\n'){
					}

				}else if((currentChar != ',') && (currentChar != '\n')) {

				// get the number up to the ',' and append to char array
					number[i] = currentChar;
					i++;

				}else if((currentChar == ',') || (currentChar == '\n') || (currentChar == ' ')){

				//if end of number: reset counter, calculate, and print data to file
					number[i] = '\0';
					i = 0;

					//copy number into a better sized array
					char numberCopy[strlen(number)+1];
					for(j = 0; j <= (strlen(numberCopy)+1);j++){
						numberCopy[j] = number[j];
					}

					//convert to int
					numberInt = atol(numberCopy,NULL);

					if (time == 0){

					//if no initial time, set it and print beginnings of file
						time = timeToHours(numberInt);
						fprintf(write,"# Date: %s\n# t_stop = %d (HHMMSS)\n",argv[1],numberInt);

					} else if ((timeDiff == 0) && (CFC == 0) && (alpha == 0) && (beta == 0)){

					//find the time difference between the data time and intial time
						timeDiff = (timeToHours(numberInt)) - time;

						//in case time recorded was on new day
						if(timeDiff < 0){timeDiff += 24;}

					} else if ((beta == 0) && (CFC == 0) && (alpha == 0)) {

						//set the beta number
						beta = numberInt;

					} else if ((CFC == 0) && (alpha == 0)) {

						//set the clean filter count
						CFC = numberInt;

					} else if (alpha == 0) {
					//since this is the last number (if the format is followed correctly), we set the alpha value and calculate the net beta number and net alpha number

						alpha = numberInt;
						netBeta = ((double)(beta - CFC - alpha))*BETACALIBRATION;
						netAlpha = (double)alpha * ALPHACALIBRATION;

						//reinitilize variables, print to file, and print to screen
						fprintf(write,"%f,%.2f,%f,%.2f\n",timeDiff,netAlpha,timeDiff,netBeta);
						printf("%.2f,%.2f,%.2f\n",timeDiff,netAlpha,netBeta);
						timeDiff = 0;
						beta = 0;
						alpha = 0;
						CFC = 0;
					}
				}
			}
		}
		//close the files
		fclose(data);
		fclose(write);
	}
	return(0);
}