Example #1
0
bool Simulacre_output_filter::write( std::string outfile, const Geostat_grid* grid, 
                                     std::string* errors ) {
 Reduced_grid rg;
  QFile file( outfile.c_str() );
  if( !file.open( IO_WriteOnly ) ) {
    if( errors ) 
      errors->append( "can't write to file: " + outfile );
    return false;
  }

  QDataStream stream( &file );

  // Write a header with a "magic number" and the grid type
  stream << (Q_UINT32)0xB211175D;
  stream << grid->classname().c_str();
  
  // write the name of the object
  SmartPtr<Named_interface> ni = 
    Root::instance()->interface( gridModels_manager );
  Manager* grid_manager = dynamic_cast<Manager*>( ni.raw_ptr() );
  appli_assert( grid_manager );
  std::string name = grid_manager->name( (Named_interface*) grid ).c_str();
  stream << name.c_str();

  // TL modified
  if (grid->classname() == rg.classname()) 
	return write_reduced_grid(stream, grid);

  if( dynamic_cast< const Point_set* >( grid ) )
    return write_pointset( stream, grid );
  if( dynamic_cast< const Cartesian_grid* >( grid ) )
    return write_cartesian_grid( stream, grid );

  return false;
}
Example #2
0
Geostat_grid* 
Simulacre_input_filter::read_reduced_grid( QDataStream& stream, std::string* errors ) 
{
	char* name;
	Q_INT32 numActive;
	stream >> name;
	std::string grid_name( name );

	Q_INT32 version;
	stream >> version;
	if( version < 100 ) {
		errors->append( "file too old" );
		return 0;
	}

	stream >> numActive;  //# of active cells

	Q_UINT32 nx, ny, nz;
	stream >> nx >> ny >> nz;
	float xsize, ysize, zsize;
	float ox,oy,oz;
	stream >> xsize >> ysize >> zsize >> ox >> oy >> oz;

	std::ostringstream ostr;
	ostr << numActive;
	std::string final_grid_name;
	SmartPtr<Named_interface> ni = 
		Root::instance()->new_interface( "reduced_grid://" + ostr.str(),
		gridModels_manager + "/" + grid_name,
		&final_grid_name );
	Reduced_grid* grid = dynamic_cast<Reduced_grid*>( ni.raw_ptr() );

	if (!grid) return false;

	grid->set_dimensions( nx, ny, nz, xsize, ysize, zsize );
	grid->origin( Cartesian_grid::location_type( ox,oy,oz ) );

	Q_UINT32 properties_count;
	stream >> properties_count;

	std::vector< char* > prop_names( properties_count );
	for( unsigned int i = 0; i < properties_count; i++ ) 
		stream >> prop_names[i];

	if (!grid->populate(stream, prop_names))
		return false;

	// clean up
	for( unsigned int k = 0; k < properties_count; k++ ) {
		delete [] prop_names[k];
	}
	delete [] name;

	return grid;

}
bool Create_mgrid_from_cgrid::exec(){
  // Get the grid from the manager
  SmartPtr<Named_interface> ni =
    Root::instance()->interface( gridModels_manager + "/" + cgrid_name_ );
  if( ni.raw_ptr() == 0 ) {
    errors_->report( "Object " + cgrid_name_ + " does not exist." );
    return false;
  }

  Cartesian_grid* cgrid = dynamic_cast<Cartesian_grid*>( ni.raw_ptr() );
  if( cgrid == 0) {
    errors_->report( "Object " + cgrid_name_ + " is not a cartesian grid." );
    return false;
  }    
 // Get the region
  std::vector<Grid_region*> regions;
  std::vector<std::string>::iterator it =  region_names_.begin();
  for( ; it!= region_names_.end(); ++it) {
    Grid_region* region = cgrid->region( *it );
    if(!region) {
      errors_->report( "Region " + (*it) + " does not exist."  );
      return false;
    }
    regions.push_back(region);
  }
  
// Create the new masked_grid

  ni = 
      Root::instance()->new_interface("reduced_grid://"+mgrid_name_,
      gridModels_manager + "/" + mgrid_name_);
 
  if( ni.raw_ptr() == 0 ) {
    errors_->report( "Object " + mgrid_name_ + " already exists. Use a different name." );
    return false;
  }
  
  Reduced_grid* grid = dynamic_cast<Reduced_grid*>( ni.raw_ptr() );

  //Create the grid
  if( regions.size() == 1 ) {  //avoid a copy of the region array
    grid->set_dimensions( 
      cgrid->geometry()->dim(0), 
      cgrid->geometry()->dim(1), 
      cgrid->geometry()->dim(2),
      cgrid->cell_dimensions()[0], 
      cgrid->cell_dimensions()[1], 
      cgrid->cell_dimensions()[2], 
      regions[0]->data(),
      cgrid->rotation_z());
  } else {
    int mask_size = regions[0]->size();
    std::vector<bool> mask( mask_size );
    for( int i=0; i< mask_size; ++i ) {
      bool ok = false;
      for( int j = 0; j<regions.size(); j++ ) {
        ok == ok || regions[j]->is_inside_region(i);
      }
      mask[i] = ok;
    }
    grid->set_dimensions( 
      cgrid->geometry()->dim(0), 
      cgrid->geometry()->dim(1), 
      cgrid->geometry()->dim(2),
      cgrid->cell_dimensions()[0], 
      cgrid->cell_dimensions()[1], 
      cgrid->cell_dimensions()[2], 
      mask,
      cgrid->rotation_z());
  }

  grid->origin( cgrid->origin() );

  proj_->new_object( mgrid_name_ );
  return true;
}
Geostat_grid* Masked_grid_geometry_xml_io::
  read_grid_geometry(QDir dir,const QDomElement& elem, std::string* errors) const {


	QString grid_name = elem.attribute("name");

	QDomElement elemGeom = elem.firstChildElement("Geometry");
	QString grid_size = elemGeom.attribute("nActiveCells");


	bool ok_size;
	grid_size.toInt(&ok_size);
	if(!ok_size) {
		errors->append("Failed to get the size of the active grid");
		return 0;
	}

  int nx = elemGeom.attribute("nx").toInt();
  int ny = elemGeom.attribute("ny").toInt();
  int nz = elemGeom.attribute("nz").toInt();

  double xsize = elemGeom.attribute("dx").toDouble();
  double ysize = elemGeom.attribute("dy").toDouble();
  double zsize = elemGeom.attribute("dz").toDouble();

  double ox = elemGeom.attribute("ox").toDouble();
  double oy = elemGeom.attribute("oy").toDouble();
  double oz = elemGeom.attribute("oz").toDouble();

  float rotation_z_angle = elemGeom.attribute("rotation_z_angle").toFloat();


// Read the mask
 // std::string maskfile = dir.absolutePath().toStdString()+"/gridmask.sgems";
 // std::fstream stream(maskfile.c_str());

  QFile file( dir.absolutePath()+"/gridmask.sgems" );
  if( !file.open( QIODevice::ReadOnly ) ) {
  	errors->append("Could not open the mask gridmask.sgems ");
  	return 0;
  }
	QDataStream stream( &file );
#if QT_VERSION >= 0x040600
	stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
#endif

	std::vector<bool> mask;
	mask.reserve(nx*ny*nz);
	int mask_size = 0;
	for( GsTLInt k = 0; k < nx*ny*nz ; k++ ) {
		bool val;
		stream >> val;
		mask.push_back( val );
		if(val) mask_size++;
	}

  std::string final_grid_name;
	SmartPtr<Named_interface> ni =
		Root::instance()->new_interface( "reduced_grid://" + grid_name.toStdString()+"::"+grid_size.toStdString(),
		gridModels_manager + "/" + grid_name.toStdString());
	Reduced_grid* grid = dynamic_cast<Reduced_grid*>( ni.raw_ptr() );
	if(grid==0) return 0;

	if (!grid) {
		errors->append("could not create the grid");
		return 0;
	}

  grid->set_dimensions( nx, ny, nz, xsize, ysize, zsize, mask, rotation_z_angle );
	grid->origin( Cartesian_grid::location_type( ox,oy,oz ) );

	return grid;

}