void finalize() { DEBUG("finalizing libdthread"); initialized = false; //xrun::finalize(); fprintf(stderr, "\nStatistics information:\n"); PRINT_TIMER(serial); PRINT_COUNTER(commit); PRINT_COUNTER(twinpage); PRINT_COUNTER(suspectpage); PRINT_COUNTER(slowpage); PRINT_COUNTER(dirtypage); PRINT_COUNTER(lazypage); PRINT_COUNTER(shorttrans); }
void finalize() { DEBUG("finalizing libtthread"); memory->closeProtection(); initialized = false; #ifdef DEBUG_ENABLED fprintf(stderr, "\nStatistics information:\n"); PRINT_TIMER(serial); PRINT_COUNTER(commit); PRINT_COUNTER(twinpage); PRINT_COUNTER(suspectpage); PRINT_COUNTER(slowpage); PRINT_COUNTER(dirtypage); PRINT_COUNTER(lazypage); PRINT_COUNTER(shorttrans); tthread::log *log = new(logbuf)tthread::log; log->print(); #endif // DEBUG_ENABLED }
/************************************************************************** * Main program to fit a line to the data. **************************************************************************/ int main(int argc, char *argv[]) { int Iteration; /* Declare a LinearFit data structure */ LinearFit DataSet; /* Declare timers */ DECLARE_TIMER(InputTimer); DECLARE_TIMER(CalculationTimer); /* Constant defining the number of times to perform calculations */ #define NUM_REPETITIONS (100) /* Variables to hold the coefficients of the least-square linear fit */ double A, B; /* Temporary variables to hold data point read from file */ double X, Y; /* "Boolean" variable to indicate all data has been read. */ int Done; /* Declare an input file of type FILE pointer */ FILE *InputFile; /* Check that a command line argument was provided */ if (argc != 1) { /* Start timer */ START_TIMER(InputTimer); /* Perform the calculations many times */ for (Iteration=0; Iteration < NUM_REPETITIONS ; Iteration++) { /* Open input file for reading -- it should be a valid filename */ InputFile = fopen(argv[1], "r"); /* Start with minimally sized arrays */ DataSet.Size = 1; /* Allocate the arrays */ DataSet.Data_X = (double *)malloc(sizeof(double) * DataSet.Size); DataSet.Data_Y = (double *)malloc(sizeof(double) * DataSet.Size); /* Initialize the index where the next data point will go */ DataSet.NextElement = 0; /* Read all of the data from the file */ do { /* Read X,Y data point and if read did not go beyond end-of-file, add it to the data set */ if (fscanf(InputFile, "%lf %lf", &X, &Y) != EOF) { /* Add the data point */ AddPoint(&DataSet, X, Y); Done = 0; } /* if() */ else { /* Set the flag indicating that all the data is gone */ Done = 1; } /* if...else() */ } while (!Done); /* Stop Input Timer */ STOP_TIMER(InputTimer); /* Start Calculation Timer */ START_TIMER(CalculationTimer); /* Save the constant value and the linear coefficient */ CalculateCoefficients(&DataSet, &A, &B); /* Stop Calculation Timer */ STOP_TIMER(CalculationTimer); /* Start Input Timer */ START_TIMER(InputTimer); /* Return dynamic memory to the heap */ free(DataSet.Data_X); free(DataSet.Data_Y); /* Disconnect the input file from the stream */ fclose(InputFile); } /* Stop the timer */ STOP_TIMER(InputTimer); /* Print out the line that fits the data set. */ printf("The line is: Y = %g * X + %g\n", A, B); /* Print measured times */ PRINT_TIMER(InputTimer); PRINT_TIMER(CalculationTimer); } /* if() */ else { /* Display program usage information */ printf("Usage: %s Filename\n", argv[0]); } /* if...else() */ return 0; } /* main() */
bool BkgdObsSplineLoader::loadBkgdObs(QList<real> &bgIn) { // Turn Debug on if there are problems with the vertical spline interpolation, // Eventually this should be replaced with the internal spline code // SplineD::Debug(1); // Geographic functions // GeographicLib::TransverseMercatorExact tm = GeographicLib::TransverseMercatorExact::UTM(); real referenceLon = configHash->value("ref_lon").toFloat(); int time; real lat, lon, alt, u, v, w, t, qv, rhoa, qr; real Pi = acos(-1); bool debugDomain = isTrue("debug_domain"); // bgZ = -32768.; TODO. What was that about? // background is in km, ROI is gridpoints iROI = configHash->value("i_background_roi").toFloat() / iincr; jROI = configHash->value("j_background_roi").toFloat() / jincr; maxGridDist = 3.0; QString interp_mode = configHash->value("bg_interpolation"); if (interp_mode == "Cressman") maxGridDist = 1.0; if (frameVector.size() == 0) { std::cout << "Frame Vector is not initialized." << std::endl; return false; } std::cout << "Loading background onto Gaussian mish with " << iROI << " grid length radius of influence in i direction" << std::endl; std::cout << "and " << jROI << " grid length radius of influence in j direction" << std::endl; QDateTime startTime = frameVector.front().getTime(); QDateTime endTime = frameVector.back().getTime(); std::cout << "Start time: " << startTime.toTime_t() << " " << startTime.toString("yyyy-MM-dd-HH:mm:ss").toLatin1().data() << std::endl; std::cout << "End time: " << endTime.toTime_t() << " " << endTime.toString("yyyy-MM-dd-HH:mm:ss").toLatin1().data() << std::endl; int timeProblem = 0; int domainProblem = 0; int radiusProblem = 0; int levelProblem = 0; int splineProblem = 0; std::cout << "iROI: " << iROI << ", jROI: " << jROI << ", maxGridDist: " << maxGridDist << std::endl; std::cout << "imin: " << imin << ", iincr: " << iincr << std::endl; std::cout << "jmin: " << jmin << ", jincr: " << jincr << std::endl; std::cout << "kmin: " << kmin << ", kincr: " << kincr << std::endl; while( bkgdAdapter->next(time, lat, lon, alt, u, v, w, t, qv, rhoa, qr) ) { int tci; if (! timeCheck(time, startTime, endTime, tci)) { timeProblem++; continue; } real Um = frameVector[tci].getUmean(); real Vm = frameVector[tci].getVmean(); // Get the X, Y & Z real tcX, tcY, metX, metY; projection.Forward(referenceLon, frameVector[tci].getLat() , frameVector[tci].getLon(), tcX, tcY); projection.Forward(referenceLon, lat, lon , metX, metY); bgX = (metX - tcX) / 1000.; bgY = (metY - tcY) / 1000.; real heightm = (alt > 10.0) ? alt : 10; // don't want to take log of zero below... bgZ = heightm / 1000.; bgRadius = sqrt(bgX * bgX + bgY * bgY); bgTheta = 180.0 * atan2(bgY, bgX) / Pi; if (configHash->value("allow_negative_angles") != "true") if (bgTheta < 0) bgTheta += 360.0; // Make sure the ob is in the Interpolation domain if (runMode == RUN_MODE_XYZ) { if ((bgX < (imin - iincr - (iROI * iincr * maxGridDist))) or (bgX > (imax + iincr + (iROI * iincr * maxGridDist))) or (bgY < (jmin - jincr - (jROI * jincr * maxGridDist))) or (bgY > (jmax + jincr + (jROI * jincr * maxGridDist))) or (bgZ < kmin)) { //Allow for higher values for interpolation purposes domainProblem++; if (debugDomain) std::cout << "bgX: " << bgX << " bgY: " << bgY << " bgZ: " << bgZ << std::endl; continue; } } else if (runMode == RUN_MODE_RTZ) { if ((bgRadius < (imin - iincr - (iROI * iincr * maxGridDist))) or (bgRadius > (imax + iincr + (iROI * iincr * maxGridDist))) or (bgTheta < jmin - jincr - (jROI * jincr * maxGridDist)) or (bgTheta > jmax + jincr + (jROI * jincr * maxGridDist)) or (bgZ < kmin)) { //Exceeding the Theta domain only makes sense for sectors domainProblem++; continue; } real cylUm = (Um * bgX + Vm * bgY) / bgRadius; real cylVm = (-Um * bgY + Vm * bgX) / bgRadius; Um = cylUm; Vm = cylVm; } // Reference states real rhoBar = refstate->getReferenceVariable(ReferenceVariable::rhoaref, heightm); real qBar = refstate->getReferenceVariable(ReferenceVariable::qvbhypref, heightm); real tBar = refstate->getReferenceVariable(ReferenceVariable::tempref, heightm); real rho = rhoa + rhoa * qv / 1000.; real rhou = rho * (u - Um); real rhov = rho * (v - Vm); real rhow = rho * w; real tprime = t - tBar; qv = refstate->bhypTransform(qv); real qvprime = qv - qBar; real rhoprime = (rhoa - rhoBar) * 100; real logZ = log(bgZ); if (configHash->value("qr_variable") == "qr") qr = refstate->bhypTransform(qr); // We assume here that the background precipitation field is always zero // real qr = 0.; if (runMode == RUN_MODE_XYZ) { bgIn << bgX << bgY << logZ << time << rhou << rhov << rhow << tprime << qvprime << rhoprime << qr ; } else if (runMode == RUN_MODE_RTZ) bgIn << bgRadius << bgTheta << logZ << time << rhou << rhov << rhow << tprime << qvprime << rhoprime << qr ; // Push values for an entire column, then solve for the spline if ( (logheights.size() != 0) && (logZ <= logheights.back()) ) { // Not first level, not same column, Solve for the spline if (logheights.size() == 1) { std::cerr << "Error at " << lat << ", " << lon << ", " << bgZ << std::endl << "Only one level found in background spline setup. " << std::endl << "Please check Background.in to ensure sorting by descending Z coordinate and re-run." << std::endl; levelProblem++; return false; } splineSolver(0); // This will also clear logheights, uBG, vBG, ... } logheights.push_back(logZ); uBG.push_back(rhou); vBG.push_back(rhov); wBG.push_back(rhow); tBG.push_back(tprime); qBG.push_back(qvprime); rBG.push_back(rhoprime); zBG.push_back(qr); } // each obs std::cout << "timeProblem: " << timeProblem << ", domainProblem: " << domainProblem << ", radiusProblem: " << radiusProblem << ", levelProblem: " << levelProblem << ", splineProblem: " << splineProblem << std::endl; if (!logheights.size()) { // Error reading in the background field std::cout << "No background estimates read in. " << "Please check the time and location of your background field.\n"; #if 0 std::cout << "Observation window: " << tcstart.toStdString() << " to " << tcend.toStdString() << "\n"; std::cout << "Background time: " << bgTimestring.toStdString() << "\n"; #endif return false; } // std::cout << "------------------------2\n"; // Solve for the last spline if (logheights.size() == 1) { std::cerr << "Only one level found in background spline setup. " << "Please check Background.in to ensure sorting by Z coordinate and re-run." << std::endl; return false; } // Exponential interpolation in horizontal, b-Spline interpolation on log height in vertical splineSolver(2); // This will also clear logheights, uBG, vBG, ... int numbgObs = bgIn.size() / 11; // 11 entries per ob if (isTrue("debug_bgIn")) dumpBgIn(0, numbgObs, bgIn); QVector<int> emptybg; // TODO Do we need to check interpolation and fill hole for KD too? // bgWeight is a byproduct of calling the spline solver, so that's not available. // what about fill holes? It depends on emptyBg which is also a byproduct of the spline solver. // Check interpolation if (numbgObs > 0) { START_TIMER(timeci); for (int ki = -1; ki < (kdim); ki++) { for (int kmu = -1; kmu <= 1; kmu += 2) { real kPos = kmin + kincr * (ki + (0.5 * sqrt(1. / 3.) * kmu + 0.5)); for (int ii = -1; ii < (idim); ii++) { for (int imu = -1; imu <= 1; imu += 2) { real iPos = imin + iincr * (ii + (0.5 * sqrt(1. / 3.) * imu + 0.5)); for (int ji = -1; ji < (jdim); ji++) { for (int jmu = -1; jmu <= 1; jmu += 2) { real jPos = jmin + jincr * (ji + (0.5 * sqrt(1. / 3.) * jmu + 0.5)); int bgI = (ii + 1) * 2 + (imu + 1) / 2; int bgJ = (ji + 1) * 2 + (jmu + 1) / 2; int bgK = (ki + 1) * 2 + (kmu + 1) / 2; int bIndex = numVars * (idim + 1) * 2 * (jdim + 1) * 2 * bgK + numVars * (idim + 1) * 2 * bgJ + numVars * bgI; for (unsigned int var = 0; var < numVars; var++) { if (bgWeights[bIndex] != 0) { bgU[bIndex + var] /= bgWeights[bIndex]; } else { emptybg.push_back(bIndex); if (emptybg.size() < 15) { std::cout << "Empty background mish for variable " << var << " at " << iPos << ", " << jPos << ", " << kPos << std::endl; } else if (emptybg.size() == 15) { std::cout << "Too many empty mish points, will no longer report.\n"; } } } if (configHash->value("qr_variable") == "dbz") { if (bgU[bIndex + 6] > 0) { real dbzavg = 10 * log10(bgU[bIndex + 6]); bgU[bIndex + 6] = (dbzavg + 35.) * 0.1; } else { bgU[bIndex + 6] = 0.0; } } } } } } } } fillHoles(emptybg); PRINT_TIMER("Interpolation check", timeci); } else { std::cout << "No background observations loaded" << std::endl; return false; } std::cout << numbgObs << " background observations loaded" << std::endl; if (isTrue("debug_bgU")) { dumpBgU(0, uStateSize, bgU); } if (configHash->contains("debug_bgU_nc")) bgu2nc(configHash->value("debug_bgU_nc").toLatin1().data(), bgU); return true; }