void mafAddIRows(char *mafIn, char *twoBitIn, char *mafOut, char *nBedFile) /* mafAddIRows - Filter out maf files. */ { FILE *f = mustOpen(mafOut, "w"); struct twoBitFile *twoBit = twoBitOpen(twoBitIn); struct mafAli *mafList, *maf; struct mafFile *mf = mafOpen(mafIn); struct hash *bedHash = newHash(6); if (nBedFile != NULL) { struct lineFile *lf = lineFileOpen(nBedFile, TRUE); char *row[1]; while (lineFileRow(lf, row)) { addBed(row[0], bedHash); } lineFileClose(&lf); } speciesHash = newHash(6); mafList = readMafs(mf); mafWriteStart(f, mf->scoring); mafFileFree(&mf); chainStrands(strandHeads, bedHash); bridgeSpecies(mafList, speciesList); fillHoles(mafList, speciesList, twoBit); for(maf = mafList; maf ; maf = maf->next) mafWrite(f, maf); }
void FastThreePhase::decode() { makeColor(); int n = width * height; threePhaseWrap.setThreshold((unsigned char) rangeThreshold); threePhaseWrap.makeWrappedPhase(phasePixels, qualityPixels, graySequence[0], graySequence[1], graySequence[2]); // this is used by getStart() to determine the center for(int i = 0; i < n; i++) ready[i] = qualityPixels[i] != LABEL_BACKGROUND; partialQualityMap.makeQualityMap(phasePixels, qualityPixels); scanlineOffset.makeOffset(phasePixels, qualityPixels, (char*) offsetPixels, getStart(), phasePersistence); for(int i = 0; i < n; i++) mask[i] = qualityPixels[i] != LABEL_UNWRAPPED; for(int i = 0; i < n; i++) { int cur = ((char*) offsetPixels)[i]; phase[i] = (cur * 256) + phasePixels[i]; phase[i] *= .002f; // this should be handled by skew/scale instead } makeDepth(); if(maxHoleSize > 0) fillHoles(); }
void meshsurf3::buildSurface( std::vector<vec3d> &vertices, std::vector<vec3d> &normals, std::vector<vec3i> &faces, const levelset3 *fluid, const levelset3 *solid, bool enclose, FLOAT64 dpx, uint iteration, bool doFit ) { // Save a reference to the mesher const mesher3 &g = *this->g; tick(); dump("Computing levelset for %d nodes...", g.nodes.size()); values.clear(); values.resize(g.nodes.size()); if( values.empty() ) { email::print("Mesher Uninitialized !\n"); email::send(); exit(0); } // Compute levelset for each node PARALLEL_FOR for( uint n=0; n<g.nodes.size(); n++ ) { FLOAT64 phi = fluid->evalLevelset(g.nodes[n]); if( enclose ) phi = fmax(phi,-dpx-solid->evalLevelset(g.nodes[n])); values[n] = phi; } dump("Done. Took %s.\n",stock("meshsurf_levelset_sample")); // Fill holes fillHoles(values,solid,g.nodes,g.elements,g.node2node,fluid->dx); // Calculate intersections on edges tick(); dump("Computing cut edges for each tet..."); std::vector<vec3d> cutpoints(g.edges.size()); std::vector<int> cutIndices(g.edges.size()); uint index = 0; for( int n=0; n<g.edges.size(); n++ ) { FLOAT64 levelsets[2]; for( uint m=0; m<2; m++ ) levelsets[m] = values[g.edges[n][m]]; if( copysign(1.0,levelsets[0]) * copysign(1.0,levelsets[1]) <= 0 ) { FLOAT64 det = levelsets[0]-levelsets[1]; if( fabs(det) < 1e-16 ) det = copysign(1e-16,det); FLOAT64 a = fmin(0.99,fmax(0.01,levelsets[0]/det)); vec3d p = (1.0-a)*g.nodes[g.edges[n][0]]+a*g.nodes[g.edges[n][1]]; cutpoints[n] = p; cutIndices[n] = index++; } else { cutIndices[n] = -1; } } // Copy them to packed array vertices.clear(); vertices.resize(index); index = 0; for( uint n=0; n<cutIndices.size(); n++ ) { if( cutIndices[n] >= 0 ) vertices[index++] = cutpoints[n]; } dump("Done. Took %s.\n",stock()); // Stitch faces (marching tet) tick(); dump("Stitching edges..."); std::vector<std::vector<uint> > node_faces; node_faces.resize(g.nodes.size()); faces.clear(); for( uint n=0; n<g.elements.size(); n++ ) { std::vector<uint> nv; for( uint m=0; m<g.element_edges[n].size(); m++ ) { uint idx = g.element_edges[n][m]; if( cutIndices[idx] >= 0 ) { nv.push_back(cutIndices[idx]); } } if( nv.size() == 4 ) { // Triangulate the veritces int idx0 = -1; int idx1 = -1; for( uint m=0; m<g.element_edges[n].size(); m++ ) { if( cutIndices[g.element_edges[n][m]] >= 0 ) { idx0 = g.element_edges[n][m]; break; } } // If found one intersection edge if( idx0 >= 0 ) { for( uint m=0; m<g.element_edges[n].size(); m++ ) { uint opID = g.element_edges[n][m]; if( cutIndices[opID] >= 0 && isOpEdge(idx0,opID)) { idx1 = opID; break; } } // If found opposite intersection edge if( idx1 >= 0 ) { vec3i v; v[0] = cutIndices[idx0]; v[1] = cutIndices[idx1]; for( uint m=0; m<g.element_edges[n].size(); m++ ) { uint idx2 = g.element_edges[n][m]; if( cutIndices[idx2] >= 0 && idx2 != idx0 && idx2 != idx1 ) { v[2] = cutIndices[idx2]; faces.push_back(v); for( uint m=0; m<g.elements[n].size(); m++ ) { node_faces[g.elements[n][m]].push_back(faces.size()-1); } } } } else { dump( "Opposite edge was not found !\n"); exit(0); } } else { dump( "Ground edge was not found !\n"); exit(0); } } else if( nv.size() == 3 ) { faces.push_back(vec3i(nv[0],nv[1],nv[2])); for( uint m=0; m<g.elements[n].size(); m++ ) { node_faces[g.elements[n][m]].push_back(faces.size()-1); } } else if( nv.size() == 0 ) { // Empty surface. Do nothing... } else { dump( "\nBad stitch encounrtered: Element - edge count = %d.\n", g.element_edges[n].size() ); dump( "Unknown set found N(%e,%e,%e,%e) = %d.\n", values[g.elements[n][0]], values[g.elements[n][1]], values[g.elements[n][2]], values[g.elements[n][3]], nv.size()); for( uint k=0; k<g.element_edges[n].size(); k++ ) { uint vidx[2] = { g.edges[g.element_edges[n][k]][0], g.edges[g.element_edges[n][k]][1] }; dump( "Edge info[%d] = edge(%d,%d) = (%e,%e) = (%f,%f)\n", k, vidx[0], vidx[1], values[vidx[0]], values[vidx[1]], copysign(1.0,values[vidx[0]]), copysign(1.0,values[vidx[1]]) ); } } } dump("Done. Took %s.\n",stock("meshsurf_stitch_mesh")); // Find closest position tick(); dump("Finding closest surface position at surface tets..."); surfacePos.clear(); surfacePos.resize(g.nodes.size()); for( uint n=0; n<g.nodes.size(); n++ ) { FLOAT64 dist = 1e8; vec3d p = g.nodes[n]; for( uint m=0; m<node_faces[n].size(); m++ ) { vec3d p0 = vertices[faces[node_faces[n][m]][0]]; vec3d p1 = vertices[faces[node_faces[n][m]][1]]; vec3d p2 = vertices[faces[node_faces[n][m]][2]]; vec3d out = g.nodes[n]; vec3d src = out; FLOAT64 d = fmax(1e-8,point_triangle_distance(src,p0,p1,p2,out)); if( d < dist ) { p = out; dist = d; } } if( dist < 1.0 ) { values[n] = (values[n]>=0 ? 1.0 : -1.0) * dist; surfacePos[n].push_back(g.nodes[n]); surfacePos[n].push_back(p); } else { values[n] = (values[n]>=0 ? 1.0 : -1.0) * 1e8; } } dump("Done. Took %s.\n",stock("meshsurf_find_close_pos1")); if( extrapolate_dist ) { // Fast march tick(); std::vector<fastmarch3<FLOAT64>::node3 *> fnodes(g.nodes.size()); for( uint n=0; n<g.nodes.size(); n++ ) fnodes[n] = new fastmarch3<FLOAT64>::node3; for( uint n=0; n<g.nodes.size(); n++ ) { vec3d p = g.nodes[n]; bool fixed = fabs(values[n]) < 1.0; fnodes[n]->p = p; fnodes[n]->fixed = fixed; fnodes[n]->levelset = values[n]; fnodes[n]->value = 0.0; fnodes[n]->p2p.resize(g.node2node[n].size()); for( uint m=0; m<g.node2node[n].size(); m++ ) { fnodes[n]->p2p[m] = fnodes[g.node2node[n][m]]; } } fastmarch3<FLOAT64>::fastMarch(fnodes,extrapolate_dist,-extrapolate_dist,1); // Pick up values for( uint n=0; n<g.nodes.size(); n++ ) { values[n] = fnodes[n]->levelset; delete fnodes[n]; } stock("meshsurf_fastmarch"); } // Compute normal tick(); dump("Computing normals..."); computeNormals(vertices,normals,faces,cutIndices); dump("Done. Took %s.\n",stock("meshsurf_normal")); // Flip facet rotation if necessary flipFacet(vertices,normals,faces,0.1*fluid->dx); #if 1 // Fit surface if( doFit ) { tick(); dump("Fitting %d surface vertices...", vertices.size() ); for( uint k=0; k<1; k++ ) { smoothMesh(vertices,normals,faces,solid,dpx,1); PARALLEL_FOR for( uint n=0; n<vertices.size(); n++ ) { vec3d out = vertices[n]; if( solid->evalLevelset(out) > dpx && fluid->getClosestSurfacePos(out) ) { vertices[n] = out; } } } dump("Done. Took %s.\n",stock("meshsurf_fit")); } #endif // Smooth mesh #if 1 tick(); dump("Smoothing faces..."); smoothMesh(vertices,normals,faces,solid,dpx,iteration); dump("Done. Took %s.\n",stock("meshsurf_smooth")); #endif // Find closest position again tick(); dump("Finding closest surface position at surface tets again..."); surfacePos.clear(); surfacePos.resize(g.nodes.size()); for( uint n=0; n<g.nodes.size(); n++ ) { FLOAT64 dist = 1e8; vec3d p = g.nodes[n]; for( uint m=0; m<node_faces[n].size(); m++ ) { vec3d p0 = vertices[faces[node_faces[n][m]][0]]; vec3d p1 = vertices[faces[node_faces[n][m]][1]]; vec3d p2 = vertices[faces[node_faces[n][m]][2]]; vec3d out = g.nodes[n]; vec3d src = out; FLOAT64 d = fmax(1e-8,point_triangle_distance(src,p0,p1,p2,out)); if( d < dist ) { p = out; dist = d; } } if( dist < 1.0 ) { values[n] = (values[n]>=0 ? 1.0 : -1.0) * dist; } } dump("Done. Took %s.\n",stock("meshsurf_find_close_pos2")); // Flip again flipFacet(vertices,normals,faces,0.1*fluid->dx); }
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; }