Exemplo n.º 1
0
Geostat_grid* Csv_grid_infilter::read( std::ifstream& infile ) {

  int nx = dialog_->nx();
  int ny = dialog_->ny();
  int nz = dialog_->nz();
  float x_size = dialog_->x_size();
  float y_size = dialog_->y_size();
  float z_size = dialog_->z_size();
  float Ox = dialog_->Ox();
  float Oy = dialog_->Oy();
  float Oz = dialog_->Oz();

  bool use_no_data_value = dialog_->use_no_data_value();
  float no_data_value = GsTLGridProperty::no_data_value;
  QString no_data_value_str = QString().arg(no_data_value);
  if( dialog_->use_no_data_value() ) {
    no_data_value = dialog_->no_data_value();
    no_data_value_str = QString::number(no_data_value);

  }

  QByteArray tmp = dialog_->name().simplified().toLatin1();
  std::string name( tmp.constData() );

  // ask manager to get a new grid and initialize it
  SmartPtr<Named_interface> ni =
    Root::instance()->interface( gridModels_manager + "/" + name );

  if( ni.raw_ptr() != 0 ) {
    GsTLcerr << "object " << name << " already exists\n" << gstlIO::end;
    return 0;
  }

  appli_message( "creating new grid '" << name << "'" 
		             << " of dimensions: " << nx << "x" << ny << "x" << nz);

  ni = Root::instance()->new_interface( "cgrid", 
                                        gridModels_manager + "/" + name );
  Cartesian_grid* grid = dynamic_cast<Cartesian_grid*>( ni.raw_ptr() );
  appli_assert( grid != 0 );

  grid->set_dimensions( nx, ny, nz,
			x_size, y_size, z_size);
  grid->origin( GsTLPoint( Ox,Oy,Oz) );
  appli_message( "grid resized to " << nx << "x" << ny << "x" << nz
		<< "  total=: " << grid->size() );

  std::string buffer;
  
  //-------------------------
  //   now, read the file

  std::getline( infile, buffer, '\n');
  QStringList property_names = QString(buffer.c_str()).split(",");

//Read one column at a time
  std::streampos start_data = infile.tellg();
  for(unsigned int j = 0; j< property_names.size(); j++) {
    infile.clear();
    infile.seekg( start_data );
    // Check if property j is categorical
    bool is_categ = false;
    for(unsigned int i=0; i<30 ; i++ ) {
      bool ok;
      if( std::getline(infile, buffer) ) break;
      QString qstr(buffer.c_str());
      QStringList values_str = qstr.split(",");
      values_str[j].toFloat(&ok);
      if(!ok)  {
        is_categ = true;
        break;
      }
    }
    infile.clear();
    infile.seekg( start_data );

    if(is_categ) {
      GsTLGridCategoricalProperty* prop = 
        grid->add_categorical_property(property_names[j].toStdString());
      
      ni = Root::instance()->new_interface( categoricalDefinition_manager, name+"-"+property_names[j].toStdString());
      CategoricalPropertyDefinitionName* cat_def = 
            dynamic_cast<CategoricalPropertyDefinitionName*>(ni.raw_ptr());

      while( std::getline(infile, buffer) ) {
        QString qstr(buffer.c_str());
        QStringList values_qstr = qstr.split(",");
        cat_def->add_category(values_qstr[j].toStdString());
      }
      prop->set_category_definition(cat_def->name());
      infile.clear();
      infile.seekg( start_data );

      int node_id=0;
      while( std::getline(infile, buffer) ) {
        QString qstr(buffer.c_str());
        QStringList values_qstr = qstr.split(",");
        QString val = values_qstr[j];
        if(use_no_data_value && val == no_data_value_str)  {
          prop->set_value( GsTLGridProperty::no_data_value, node_id );
        }
        else {
          prop->set_value( val.toStdString(), node_id );
        }
        node_id++;
      }
    }
    else {
      GsTLGridProperty* prop = 
        grid->add_property(property_names[j].toStdString());
      int node_id=0;
      while( std::getline(infile, buffer) ) {
        QString qstr(buffer.c_str());
        QStringList values_qstr = qstr.split(",");
        if(!values_qstr[j].isEmpty()) {
          bool ok;
          float val = values_qstr[j].toFloat(&ok);
          if(ok  && val != no_data_value) prop->set_value(val,node_id);
        }
        node_id++;
      }
    }

  }
  return grid;
}
Exemplo n.º 2
0
Geostat_grid* Gslib_grid_infilter::read( std::ifstream& infile ) {

    int nx = dialog_->nx();
    int ny = dialog_->ny();
    int nz = dialog_->nz();
    float x_size = dialog_->x_size();
    float y_size = dialog_->y_size();
    float z_size = dialog_->z_size();
    float Ox = dialog_->Ox();
    float Oy = dialog_->Oy();
    float Oz = dialog_->Oz();

    QByteArray tmp = dialog_->name().simplified().toLatin1();
    std::string name( tmp.constData() );

    // ask manager to get a new grid and initialize it
    SmartPtr<Named_interface> ni =
        Root::instance()->interface( gridModels_manager + "/" + name );

    if( ni.raw_ptr() != 0 ) {
        GsTLcerr << "object " << name << " already exists\n" << gstlIO::end;
        return 0;
    }

    appli_message( "creating new grid '" << name << "'"
                   << " of dimensions: " << nx << "x" << ny << "x" << nz);

    ni = Root::instance()->new_interface( "cgrid",
                                          gridModels_manager + "/" + name );
    Cartesian_grid* grid = dynamic_cast<Cartesian_grid*>( ni.raw_ptr() );
    appli_assert( grid != 0 );

    grid->set_dimensions( nx, ny, nz,
                          x_size, y_size, z_size);
    grid->origin( GsTLPoint( Ox,Oy,Oz) );
    appli_message( "grid resized to " << nx << "x" << ny << "x" << nz
                   << "  total=: " << grid->size() );

    std::string buffer;

    //-------------------------
    //   now, read the file

    // read title
    std::getline( infile, buffer, '\n');

    // read nb of properties
    int property_count;
    infile >> property_count;
    std::getline( infile, buffer, '\n');


    // check whether the file contains multiple realizations
    bool has_multi_real = false;

    int lines_to_skip = grid->size() + property_count;
    for( int pos=0; pos < lines_to_skip ; pos++ )
        std::getline( infile, buffer, '\n');
    float val;
    if( infile >> val ) has_multi_real = true;

    // reposition the stream
    infile.clear();
    infile.seekg( 0 );
    std::getline( infile, buffer, '\n');
    std::getline( infile, buffer, '\n');

    if( has_multi_real ) {
        std::vector<MultiRealization_property*> properties;
        for( int i=0; i<property_count; i++ ) {
            std::getline( infile, buffer, '\n');
            QString prop_name( buffer.c_str() );
            MultiRealization_property* prop;
            QByteArray tmp = prop_name.simplified().toAscii();
            prop = grid->add_multi_realization_property( tmp.constData() );
            if( !prop ) {
                GsTLcerr << "Several properties share the same name " << gstlIO::end;
                return 0;
            }

            properties.push_back( prop );

        }


        while( infile ) {
            if( !infile ) break;
            char c = infile.peek();
            if( !std::isdigit(c) ) break;

            std::vector<GsTLGridProperty*> props;
            int index = 0;
            for( unsigned int ii=0; ii < property_count; ii++) {

                GsTLGridProperty* prop = properties[index]->new_realization();
                props.push_back( prop );
                ++index;

            }
            read_one_realization( infile, props, grid->size() );
        }
    }

    else {
        std::vector<GsTLGridProperty*> properties;
        for( int i=0; i<property_count; i++ ) {
            std::getline( infile, buffer, '\n');
            QString prop_name( buffer.c_str() );

            QByteArray tmp  =prop_name.simplified().toAscii();
            GsTLGridProperty* prop =
                grid->add_property( tmp.constData() );
            properties.push_back( prop );
        }
        read_one_realization( infile, properties, grid->size() );

    }

    return grid;
}