Exemplo n.º 1
0
//
// Reads variable data from dump file
//
bool DataVar::initFromFile(const string& filename, const_DomainChunk_ptr dom)
{
    cleanup();
    
#if ESYS_HAVE_NETCDF
    NcError ncerr(NcError::silent_nonfatal);    
    NcFile* input = new NcFile(filename.c_str());
    if (!input->is_valid()) {
        cerr << "Could not open input file " << filename << "." << endl;
        delete input;
        return false;
    }

    NcDim* dim;
    NcAtt* att;

    att = input->get_att("type_id");
    int typeID = att->as_int(0);
    if (typeID != 2) {
        cerr << "WARNING: Only expanded data supported!" << endl;
        delete input;
        return false;
    }

    att = input->get_att("rank");
    rank = att->as_int(0);

    dim = input->get_dim("num_data_points_per_sample");
    ptsPerSample = dim->size();

    att = input->get_att("function_space_type");
    funcSpace = att->as_int(0);

    centering = dom->getCenteringForFunctionSpace(funcSpace);

    dim = input->get_dim("num_samples");
    numSamples = dim->size();

#ifdef _DEBUG
    cout << varName << ":\t" << numSamples << " samples,  "
        << ptsPerSample << " pts/s,  rank: " << rank << endl;
#endif

    domain = dom;
    NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
    if (nodes == NULL) {
        delete input;
        return false;
    }

    meshName = nodes->getName();
    siloMeshName = nodes->getFullSiloName();
    initialized = true;

    size_t dimSize = 1;
    vector<long> counts;

    if (rank > 0) {
        dim = input->get_dim("d0");
        int d = dim->size();
        shape.push_back(d);
        counts.push_back(d);
        dimSize *= d;
    }
    if (rank > 1) {
        dim = input->get_dim("d1");
        int d = dim->size();
        shape.push_back(d);
        counts.push_back(d);
        dimSize *= d;
    }
    if (rank > 2) {
        cerr << "WARNING: Rank " << rank << " data is not supported!\n";
        initialized = false;
    }
 
    if (initialized && numSamples > 0) {
        sampleID.insert(sampleID.end(), numSamples, 0);
        NcVar* var = input->get_var("id");
        var->get(&sampleID[0], numSamples);

        size_t dataSize = dimSize*numSamples*ptsPerSample;
        counts.push_back(ptsPerSample);
        counts.push_back(numSamples);
        float* tempData = new float[dataSize];
        var = input->get_var("data");
        var->get(tempData, &counts[0]);

        const float* srcPtr = tempData;
        for (size_t i=0; i < dimSize; i++, srcPtr++) {
            float* c = averageData(srcPtr, dimSize);
            dataArray.push_back(c);
        }
        delete[] tempData;

        initialized = reorderSamples();
    }

    delete input;
#endif // ESYS_HAVE_NETCDF

    return initialized;
}
Exemplo n.º 2
0
bool
NetworkObject::loadNetCDF(QString flnm)
{
  m_fileName = flnm;

  NcError err(NcError::verbose_nonfatal);

  NcFile ncfFile(flnm.toLatin1().data(), NcFile::ReadOnly);
  NcAtt *att;
  NcVar *var;

  // ---- get gridsize -----
  att = ncfFile.get_att("gridsize");
  m_nX = att->as_int(0);
  m_nY = att->as_int(1);
  m_nZ = att->as_int(2);
  //------------------------

  // ---- get vertex centers -----
  var = ncfFile.get_var("vertex_centers");
  if (!var)
    var = ncfFile.get_var("vertex_center");
  if (!var)
    var = ncfFile.get_var("vertex_centres");
  if (!var)
    var = ncfFile.get_var("vertex_centre");
  
  int nv = var->get_dim(0)->size();
  float *vc = new float [3*nv];
  var->get(vc, nv, 3);
  m_vertexCenters.resize(nv);
  for(int i=0; i<nv; i++)
    m_vertexCenters[i] = Vec(vc[3*i+0], vc[3*i+1], vc[3*i+2]);
  delete [] vc;
  //------------------------


  // ---- get edges -----
  var = ncfFile.get_var("edge_neighbours");
  int ne = var->get_dim(0)->size();
  int *ed = new int [2*ne];
  var->get(ed, ne, 2);
  m_edgeNeighbours.resize(ne);
  for(int i=0; i<ne; i++)
    m_edgeNeighbours[i] = qMakePair(ed[2*i], ed[2*i+1]);
  delete [] ed;
  //------------------------


  Vec bmin = m_vertexCenters[0];
  Vec bmax = m_vertexCenters[0];
  for(int i=0; i<m_vertexCenters.count(); i++)
    {
      bmin = StaticFunctions::minVec(bmin, m_vertexCenters[i]);
      bmax = StaticFunctions::maxVec(bmax, m_vertexCenters[i]);
    }
  m_centroid = (bmin + bmax)/2;
  
  m_enclosingBox[0] = Vec(bmin.x, bmin.y, bmin.z);
  m_enclosingBox[1] = Vec(bmax.x, bmin.y, bmin.z);
  m_enclosingBox[2] = Vec(bmax.x, bmax.y, bmin.z);
  m_enclosingBox[3] = Vec(bmin.x, bmax.y, bmin.z);
  m_enclosingBox[4] = Vec(bmin.x, bmin.y, bmax.z);
  m_enclosingBox[5] = Vec(bmax.x, bmin.y, bmax.z);
  m_enclosingBox[6] = Vec(bmax.x, bmax.y, bmax.z);
  m_enclosingBox[7] = Vec(bmin.x, bmax.y, bmax.z);
    


//  QStringList vatt, eatt;
  int nvars = ncfFile.num_vars();
//  for (int i=0; i < nvars; i++)
//    {
//      var = ncfFile.get_var(i);      
//      QString attname = var->name();
//      attname.toLower();
//      if (attname.contains("vertex_") &&
//	  ( attname != "vertex_centers" ||
//	    attname != "vertex_centres"))
//	vatt.append(attname);
//      else if (attname.contains("edge_") &&
//	       attname != "edge_neighbours")
//	eatt.append(attname);
//    }

  m_vertexAttribute.clear();
  m_edgeAttribute.clear();

  m_vertexRadiusAttribute = -1;
  m_edgeRadiusAttribute = -1;

  int vri = 0;
  int eri = 0;
  for (int i=0; i < nvars; i++)
    {
      var = ncfFile.get_var(i);      
      QString attname = var->name();
      attname.toLower();

      if (attname.contains("vertex_") &&
	  attname != "vertex_center" &&
	  attname != "vertex_centre" &&
	  attname != "vertex_centers" &&
	  attname != "vertex_centres")
	{
	  if (attname == "vertex_radius")
	    m_vertexRadiusAttribute = vri;
	  vri++;

	  QVector<float> val;
	  val.clear();

	  if (var->type() == ncByte || var->type() == ncChar)
	    {
	      uchar *v = new uchar[nv];
	      var->get((ncbyte *)v, nv);
	      for(int j=0; j<nv; j++)
		val.append(v[j]);
	      delete [] v;
	    }
	  else if (var->type() == ncShort)
	    {
	      short *v = new short[nv];
	      var->get((short *)v, nv);
	      for(int j=0; j<nv; j++)
		val.append(v[j]);
	      delete [] v;
	    }
	  else if (var->type() == ncInt)
	    {
	      int *v = new int[nv];
	      var->get((int *)v, nv);
	      for(int j=0; j<nv; j++)
		val.append(v[j]);
	      delete [] v;
	    }
	  else if (var->type() == ncFloat)
	    {
	      float *v = new float[nv];
	      var->get((float *)v, nv);
	      for(int j=0; j<nv; j++)
		val.append(v[j]);
	      delete [] v;
	    }

	  if (val.count() > 0)
	    m_vertexAttribute.append(qMakePair(attname, val));
	}
      else if (attname.contains("edge_") &&
	       attname != "edge_neighbours")
	{
	  if (attname == "edge_radius")
	    m_edgeRadiusAttribute = eri;
	  eri++;

	  QVector<float> val;
	  val.clear();

	  if (var->type() == ncByte || var->type() == ncChar)
	    {
	      uchar *v = new uchar[ne];
	      var->get((ncbyte *)v, ne);
	      for(int j=0; j<ne; j++)
		val.append(v[j]);
	      delete [] v;
	    }
	  else if (var->type() == ncShort)
	    {
	      short *v = new short[ne];
	      var->get((short *)v, ne);
	      for(int j=0; j<ne; j++)
		val.append(v[j]);
	      delete [] v;
	    }
	  else if (var->type() == ncInt)
	    {
	      int *v = new int[ne];
	      var->get((int *)v, ne);
	      for(int j=0; j<ne; j++)
		val.append(v[j]);
	      delete [] v;
	    }
	  else if (var->type() == ncFloat)
	    {
	      float *v = new float[ne];
	      var->get((float *)v, ne);
	      for(int j=0; j<ne; j++)
		val.append(v[j]);
	      delete [] v;
	    }

	  if (val.count() > 0)
	    m_edgeAttribute.append(qMakePair(attname, val));
	}
    }

  ncfFile.close();
  
  if (!Global::batchMode())
    {
      QString str;
      str = QString("Grid Size : %1 %2 %3\n").arg(m_nX).arg(m_nY).arg(m_nZ);
      str += QString("Vertices : %1\n").arg(m_vertexCenters.count());
      str += QString("Edges : %1\n").arg(m_edgeNeighbours.count());
      str += QString("\n");
      str += QString("Vertex Attributes : %1\n").arg(m_vertexAttribute.count());
      for(int i=0; i<m_vertexAttribute.count(); i++)
	str += QString(" %1\n").arg(m_vertexAttribute[i].first);
      str += QString("\n");
      str += QString("Edge Attributes : %1\n").arg(m_edgeAttribute.count());
      for(int i=0; i<m_edgeAttribute.count(); i++)
	str += QString(" %1\n").arg(m_edgeAttribute[i].first);
      
      QMessageBox::information(0, "Network loaded", str);
    }

  m_Vatt = 0;
  m_Eatt = 0;
  m_Vminmax.clear();
  m_Eminmax.clear();
  m_userVminmax.clear();
  m_userEminmax.clear();

  for(int i=0; i<m_vertexAttribute.count(); i++)
    {
      float vmin = m_vertexAttribute[i].second[0];
      float vmax = m_vertexAttribute[i].second[0];
      for(int j=1; j<m_vertexAttribute[i].second.count(); j++)
	{
	  vmin = qMin((float)m_vertexAttribute[i].second[j], vmin);
	  vmax = qMax((float)m_vertexAttribute[i].second[j], vmax);
	}
      m_Vminmax.append(qMakePair(vmin, vmax));
      m_userVminmax.append(qMakePair((vmin+vmax)/2, vmax));
    }


  for(int i=0; i<m_edgeAttribute.count(); i++)
    {
      float emin = m_edgeAttribute[i].second[0];
      float emax = m_edgeAttribute[i].second[0];
      for(int j=1; j<m_edgeAttribute[i].second.count(); j++)
	{
	  emin = qMin((float)m_edgeAttribute[i].second[j], emin);
	  emax = qMax((float)m_edgeAttribute[i].second[j], emax);
	}
      m_Eminmax.append(qMakePair(emin, emax));
      m_userEminmax.append(qMakePair((emin+emax)/2, emax));
    }

  return true;
}
Exemplo n.º 3
0
void PolygonManager::init()
{
    string fileName;
    ConfigTools::read("parcel_polygon_file", fileName);
    NOTICE("PolygonManager::init", "Reading polygons from \""+fileName+"\" ...");
    NcFile file(fileName.c_str(), NcFile::ReadOnly);
    if (!file.is_valid()) {
        REPORT_ERROR(string("Failed to open file "+fileName+"."))
    }

    NcError ncError(NcError::silent_nonfatal);

    if (TimeManager::onLine()) {
        NcAtt *timeAtt = file.get_att("time");
        NcAtt *timeStepAtt = file.get_att("time_step");
        NcAtt *stepsAtt = file.get_att("steps");
        if (timeAtt != NULL && timeStepAtt != NULL && stepsAtt != NULL) {
            TimeManager::reset();
            double dt = timeStepAtt->as_double(0);
            double second = timeAtt->as_double(0);
            double steps = stepsAtt->as_int(0);
            TimeManager::setClock(dt, second, steps);
        }
    }
    NcDim *numVertexDim = file.get_dim("num_total_vertex");
    if (numVertexDim == NULL) {
        Message message;
        message << "Failed to find \"num_total_vertex\" dimension in file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    NcDim *numEdgeDim = file.get_dim("num_total_edge");
    if (numEdgeDim == NULL) {
        Message message;
        message << "Failed to find \"num_total_edge\" dimension in file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    NcDim *numPolygonDim = file.get_dim("num_total_polygon");
    if (numPolygonDim == NULL) {
        Message message;
        message << "Failed to find \"num_total_polygon\" dimension in file \"";
        message << fileName << "\"!";
        REPORT_ERROR(message.str());
    }
    int numVertex = static_cast<int>(numVertexDim->size());
    int numEdge = static_cast<int>(numEdgeDim->size());
    int numPolygon = static_cast<int>(numPolygonDim->size());
    // -------------------------------------------------------------------------
    // vertices part
    vertices.create(numVertex);
    double *oldVtxLon = new double[numVertex];
    double *oldVtxLat = new double[numVertex];
    double *oldVtxLev = new double[numVertex];
    double *newVtxLon = new double[numVertex];
    double *newVtxLat = new double[numVertex];
    double *newVtxLev = new double[numVertex];
    file.get_var("old_vertex_lon")->get(oldVtxLon, numVertex);
    file.get_var("old_vertex_lat")->get(oldVtxLat, numVertex);
    file.get_var("new_vertex_lon")->get(newVtxLon, numVertex);
    file.get_var("new_vertex_lat")->get(newVtxLat, numVertex);
    if (file.get_var("old_vertex_lev") != NULL) {
        file.get_var("old_vertex_lev")->get(oldVtxLev, numVertex);
        file.get_var("new_vertex_lev")->get(newVtxLev, numVertex);
    } else {
        for (int i = 0; i < vertices.size(); ++i) {
            oldVtxLev[i] = 0.0;
            newVtxLev[i] = 0.0;
        }
    }
    // change the units from degree to rad
    for (int i = 0; i < numVertex; ++i) {
        oldVtxLon[i] /= Rad2Deg;
        oldVtxLat[i] /= Rad2Deg;
        newVtxLon[i] /= Rad2Deg;
        newVtxLat[i] /= Rad2Deg;
    }
    Vertex *vertexMap[vertices.size()];
    Vertex *vertex = vertices.front();
    for (int i = 0; i < vertices.size(); ++i) {
        vertexMap[i] = vertex;
        vertex->setCoordinate(newVtxLon[i], newVtxLat[i], newVtxLev[i], NewTimeLevel);
        vertex->setCoordinate(oldVtxLon[i], oldVtxLat[i], oldVtxLev[i], OldTimeLevel);
        vertex = vertex->next;
    }
    delete [] newVtxLon;
    delete [] newVtxLat;
    delete [] newVtxLev;
    delete [] oldVtxLon;
    delete [] oldVtxLat;
    delete [] oldVtxLev;
    // -------------------------------------------------------------------------
    // edges part
    edges.create(numEdge);
    int *firstPoint = new int[numEdge];
    int *secondPoint = new int[numEdge];
    file.get_var("first_point_idx")->get(firstPoint, numEdge);
    file.get_var("second_point_idx")->get(secondPoint, numEdge);
    Edge *edgeMap[edges.size()];
    Edge *edge = edges.front();
    for (int i = 0; i < edges.size(); ++i) {
        edgeMap[i] = edge;
        edge->linkEndPoint(FirstPoint, vertexMap[firstPoint[i]-1]);
        edge->linkEndPoint(SecondPoint, vertexMap[secondPoint[i]-1]);
        edge->calcNormVector();
        edge = edge->next;
    }
    delete [] firstPoint;
    delete [] secondPoint;
    // test point
    double *oldTestLon = new double[numEdge];
    double *oldTestLat = new double[numEdge];
    double *newTestLon = new double[numEdge];
    double *newTestLat = new double[numEdge];
    file.get_var("old_testpoint_lon")->get(oldTestLon, numEdge);
    file.get_var("old_testpoint_lat")->get(oldTestLat, numEdge);
    file.get_var("new_testpoint_lon")->get(newTestLon, numEdge);
    file.get_var("new_testpoint_lat")->get(newTestLat, numEdge);
    for (int i = 0; i < numEdge; ++i) {
        oldTestLon[i] /= Rad2Deg;
        oldTestLat[i] /= Rad2Deg;
        newTestLon[i] /= Rad2Deg;
        newTestLat[i] /= Rad2Deg;
    }
    edge = edges.front();
    for (int i = 0; i < numEdge; ++i) {
        Vertex *testPoint = edge->getTestPoint();
        testPoint->setCoordinate(oldTestLon[i], oldTestLat[i], OldTimeLevel);
        testPoint->setCoordinate(newTestLon[i], newTestLat[i], NewTimeLevel);
        edge = edge->next;
    }
    delete [] oldTestLon;
    delete [] oldTestLat;
    delete [] newTestLon;
    delete [] newTestLat;
    // -------------------------------------------------------------------------
    // polygons part
    polygons.create(numPolygon);
    int edgeNum[numPolygon];
    file.get_var("edge_num")->get(edgeNum, numPolygon);
    int numEdgeIdx = static_cast<int>(file.get_dim("num_edge_idx")->size());
    int *edgeIdx = new int[numEdgeIdx];
    int *edgeOnt = new int[numEdgeIdx];
    file.get_var("edge_idx")->get(edgeIdx, numEdgeIdx);
    file.get_var("edge_ont")->get(edgeOnt, numEdgeIdx);
    Polygon *polygon = polygons.front();
    int counter = 0;
    for (int i = 0; i < polygons.size(); ++i) {
        for (int j = 0; j < edgeNum[i]; ++j) {
            OrientStatus orient;
            if (edgeOnt[counter] == 0) {
                orient = OrientLeft;
            } else if (edgeOnt[counter] == 1) {
                orient = OrientRight;
            } else {
                string message = "Invalid edge_ont in file "+fileName+".";
                REPORT_ERROR(message.c_str());
            }
            edgeMap[edgeIdx[counter]-1]->linkPolygon(orient, polygon);
            counter++;
        }
        polygon->edgePointers.ring();
        // calculate the angles
        EdgePointer *edgePointer = polygon->edgePointers.front();
        for (int j = 0; j < polygon->edgePointers.size(); ++j) {
            edgePointer->calcAngle();
            edgePointer = edgePointer->next;
        }
        polygon->calcArea();
        polygon = polygon->next;
    }
    delete [] edgeIdx;
    delete [] edgeOnt;
    // -------------------------------------------------------------------------
    file.close();
}
Exemplo n.º 4
0
bool
RemapWidget::getVolumeInfo(QString volfile,
			   int &skipheaderbytes,
			   uchar &voxelType,
			   int &voxelUnit,
			   float &vx, float &vy, float &vz,
			   QString &description,
			   QList<float> &rawMap,
			   QList<uchar> &pvlMap,
			   int &depth,
			   int &width,
			   int &height)
{
  NcError err(NcError::verbose_nonfatal);

  NcFile pvlFile(volfile.toAscii().data(), NcFile::ReadOnly);

  if (!pvlFile.is_valid())
    {
      QMessageBox::information(0, "Error",
       QString("%1 is not a valid preprocessed volume file").arg(volfile));
      return false;
    }

  int i;
  NcAtt *att;
  char *attval;
  QString pvalue;

  att = pvlFile.get_att("description");
  if (att)
    {
      attval = att->as_string(0);
      description = attval;
      delete [] attval;
    }

  att = pvlFile.get_att("voxeltype");
  if (att)
    {
      attval = att->as_string(0);
      pvalue = attval;
      if (pvalue == "unsigned char")
	voxelType = Raw2Pvl::_UChar;
      if (pvalue == "char")
	voxelType = Raw2Pvl::_Char;
      if (pvalue == "unsigned short")
	voxelType = Raw2Pvl::_UShort;
      if (pvalue == "short")
	voxelType = Raw2Pvl::_Short;
      if (pvalue == "int")
	voxelType = Raw2Pvl::_Int;
      if (pvalue == "float")
	voxelType = Raw2Pvl::_Float;
      delete [] attval;
    }


  att = pvlFile.get_att("voxelunit");
  if (att)
    { 
      attval = att->as_string(0);
      pvalue = attval;
      voxelUnit = Raw2Pvl::_Nounit;
      if (pvalue == "angstrom")
	voxelUnit = Raw2Pvl::_Angstrom;
      else if (pvalue == "nanometer")
	voxelUnit = Raw2Pvl::_Nanometer;
      else if (pvalue == "micron")
	voxelUnit = Raw2Pvl::_Micron;
      else if (pvalue == "millimeter")
	voxelUnit = Raw2Pvl::_Millimeter;
      else if (pvalue == "centimeter")
	voxelUnit = Raw2Pvl::_Centimeter;
      else if (pvalue == "meter")
	voxelUnit = Raw2Pvl::_Meter;
      else if (pvalue == "kilometer")
	voxelUnit = Raw2Pvl::_Kilometer;
      else if (pvalue == "parsec")
	voxelUnit = Raw2Pvl::_Parsec;
      else if (pvalue == "kiloparsec")
	voxelUnit = Raw2Pvl::_Kiloparsec;
      delete [] attval;
    }
  
  
  att = pvlFile.get_att("gridsize");
  if (att)
    {
      depth = att->as_int(0);
      width = att->as_int(1);
      height = att->as_int(2);
    }

  att = pvlFile.get_att("voxelsize");
  if (att)
    {
      vx = att->as_float(0);
      vy = att->as_float(1);
      vz = att->as_float(2);
    }

  att = pvlFile.get_att("skipheaderbytes");
  if (att)
    skipheaderbytes = att->as_int(0);
  
  att = pvlFile.get_att("mapraw");
  if (att)
    {
      for(i=0; i<att->num_vals(); i++)
	rawMap.append(att->as_float(i));
  
      att = pvlFile.get_att("mappvl");
      for(i=0; i<att->num_vals(); i++)
	pvlMap.append(att->as_ncbyte(i));
    }

  pvlFile.close();
  return true;
}