void RegularSurfaceRotated<A>::ReadFromFile(const std::string& filename,
                                            SurfaceFileFormat  format)
{
  if (format == SURF_UNKNOWN) {
    format = FindSurfaceFileType(filename);
    if (format == SURF_UNKNOWN) {
      throw FileFormatError("Failed to determine file format for surface file: " + filename);
    }
  }

  switch (format) {
    case SURF_IRAP_CLASSIC_ASCII:
      ReadIrapClassicAsciiSurf(filename, surface_, angle_);
      break;
    case SURF_STORM_BINARY:
      ReadStormBinarySurf(filename, surface_);
      angle_ = 0.0;
      break;
    case SURF_SGRI:
      ReadSgriSurf(filename, surface_, angle_);
      break;
    default:
      throw FileFormatError("Reading of file " + filename + " on format " + ToString(format)
                             + " as a rotated grid is currently not supported.");
  }

  x_ref_ = surface_.GetXMin();
  y_ref_ = surface_.GetYMin();

  CalculateMinMaxXY();
}
Exemple #2
0
void WriteMesh::write_mesh( const Mesh& mesh, const URI& file, const std::vector<URI>& fields)
{
  update_list_of_available_writers();

  /// @todo this should be improved to allow http(s) which would then upload the mesh
  ///       to a remote location after writing to a temporary file
  ///       uploading can be achieved using the curl library (which we already search for in the build system)

  URI filepath = file;

  if( filepath.scheme() != URI::Scheme::FILE )
    filepath.scheme( URI::Scheme::FILE );


  const std::string extension = filepath.extension();

  if ( m_extensions_to_writers.count(extension) == 0 )
    throw FileFormatError (FromHere(), "No meshwriter exists for files with extension " + extension);

  if (m_extensions_to_writers[extension].size()>1)
  {
     std::string msg;
     msg = filepath.string() + " has ambiguous extension " + extension + "\n"
       +  "Possible writers for this extension are: \n";
     boost_foreach(const Handle< MeshWriter > writer , m_extensions_to_writers[extension])
       msg += " - " + writer->name() + "\n";
     throw FileFormatError( FromHere(), msg);
   }
Exemple #3
0
TabularFile::TabularFile(const std::string& filename)
{
  size_t first_data_line, n_columns;
  bool read_last_line;
  std::string last_line;
  if (!CheckFile(filename, first_data_line, n_columns, read_last_line, last_line))
    throw FileFormatError("The format of " + filename + " is not supported.");

  ReadFromFile(filename, first_data_line, n_columns, read_last_line);
}
Exemple #4
0
void NRLib::ReadStormBinarySurf(const std::string & filename,
                                RegularSurface<A> & surface)
{
    std::ifstream file;
    OpenRead(file, filename.c_str(), std::ios::in | std::ios::binary);

    int line = 0;

    // Header
    try {
        std::string token = ReadNext<std::string>(file, line);
        if (token != "STORMGRID_BINARY") {
            throw FileFormatError("Error reading " + filename + ", file is not "
                                  "in STORM binary format.");
        }

        int ni       = ReadNext<int>(file, line);
        int nj       = ReadNext<int>(file, line);
        double dx    = ReadNext<double>(file, line);
        double dy    = ReadNext<double>(file, line);
        double x_min = ReadNext<double>(file, line);
        double x_max = ReadNext<double>(file, line);
        double y_min = ReadNext<double>(file, line);
        double y_max = ReadNext<double>(file, line);

        double lx = x_max - x_min;
        double ly = y_max - y_min;

        if (!NRLibPrivate::Equal(lx/(ni-1), dx)) {
            throw FileFormatError("Inconsistent data in file. dx != lx/(nx-1).");
        }
        if (!NRLibPrivate::Equal(ly/(nj-1), dy)) {
            throw FileFormatError("Inconsistent data in file. dy != ly/(ny-1).");
        }

        surface.Resize(ni, nj);
        surface.SetDimensions(x_min, y_min, lx, ly);

        DiscardRestOfLine(file, line, true);
        ReadBinaryDoubleArray(file, surface.begin(), surface.GetN());

        surface.SetMissingValue(static_cast<A>(STORM_MISSING));

        if (!CheckEndOfFile(file)) {
            throw FileFormatError("File too long.");
        }

        surface.SetName(GetStem(filename));
    }
    catch (EndOfFile& ) {
        throw FileFormatError("Unexcpected end of file found while parsing "
                              " \"" + filename + "\"");
    }
    catch (Exception& e) {
        throw FileFormatError("Error parsing \"" + filename + "\" as a "
                              "STORM surface file at line " + ToString(line) + ":" + e.what() + "\n");
    }
}
Exemple #5
0
Wavelet::Wavelet(const std::string& filename,
                 WaveletFileType    file_type,
                 double             scaling_factor)
{
  if (file_type == UnknownWavelet) {
    file_type = FindWaveletFileType(filename);
  }
  if (file_type == UnknownWavelet) {
    throw FileFormatError(filename + " has unknown wavelet format.");
  }
  ReadFromFile(filename, file_type);

  if (scaling_factor != 1.0)
    Scale(scaling_factor);
}
void RegularSurfaceRotated<A>::WriteToFile(const std::string& filename,
                                           SurfaceFileFormat  format) const
{
  switch (format) {
    case SURF_IRAP_CLASSIC_ASCII:
      WriteIrapClassicAsciiSurf(surface_, angle_, filename);
      break;
    case SURF_STORM_BINARY:
      {
        if (angle_ != 0.0) {
          RegularSurface<A> resampled_surf = ResampleSurface();
          WriteStormBinarySurf(resampled_surf, filename);
        }
        else {
          WriteStormBinarySurf(surface_, filename);
        }
      }
      break;
    default:
      throw FileFormatError("Writing of surface to file " + filename + " on format "
                            + ToString(format) + " is currently not supported.");
  }
}
Exemple #7
0
void  NRLib::ReadSgriSurf(const std::string & filename,
                          RegularSurface<A> & surface,
                          double            & angle)
{
    std::ifstream header_file;
    OpenRead(header_file, filename.c_str(), std::ios::in | std::ios::binary);
    int i;
    std::string tmp_str;
    int dim;
    try {
        //Reading record 1: Version header
        getline(header_file, tmp_str);
        //Reading record 2: Grid dimension
        header_file >> dim;
        if(dim!=2)
            throw Exception("Wrong dimension of Sgri file. We expect a surface, dimension should be 2.\n");

        getline(header_file, tmp_str);
        //Reading record 3 ... 3+dim: Axis labels + grid value label
        std::vector<std::string> axis_labels(dim);
        for (i=0; i<dim; i++)
            getline(header_file, axis_labels[i]);
        if (((axis_labels[0].find("X") == std::string::npos) && (axis_labels[0].find("x") == std::string::npos)) ||
                ((axis_labels[1].find("Y") == std::string::npos) && (axis_labels[1].find("y") == std::string::npos)))
            throw Exception("Wrong axis labels. First axis should be x-axis, second axis should be y-axis.\n");
        // if((axis_labels[0]!="X" && axis_labels[0] !="x") || (axis_labels[1]!="Y" && axis_labels[1]!="y"))
        //  throw Exception("Wrong axis labels. First axis should be x-axis, second axis should be y-axis.\n");
        getline(header_file, tmp_str);
        //int config = IMISSING;

        //Reading record 4+dim: Number of grids
        int n_grid;
        header_file >> n_grid;
        if (n_grid < 1) {
            throw Exception("Error: Number of grids read from sgri file must be >0");
        }
        getline(header_file, tmp_str);
        //Reading record 5+dim ... 5+dim+ngrid-1: Grid labels

        for (i=0; i<n_grid; i++)
            getline(header_file, tmp_str);

        std::vector<float> d_values1(dim);
        std::vector<float> d_values2(dim);
        std::vector<int>   i_values(dim);
        //Reading record 5+dim+ngrid: Scaling factor of grid values
        for (i=0; i<dim; i++)
            header_file >> d_values1[i];
        getline(header_file,tmp_str);
        //Reading record 6+dim+ngrid: Number of samples in each dir.
        for (i=0; i<dim; i++)
            header_file >> i_values[i];
        getline(header_file,tmp_str);
        //Reading record 7+dim+ngrid: Grid sampling in each dir.
        for (i=0; i<dim; i++) {
            header_file >> d_values2[i];
        }
        getline(header_file,tmp_str);
        //Reading record 8+dim+ngrid: First point coord.
        std::vector<float> min_values(dim);
        for (i=0; i<dim; i++)
        {
            header_file >> min_values[i];
        }
        int nx = 1;
        int ny = 1;

        double dx, dy;
        nx      = i_values[0];
        dx     = d_values2[0];
        ny      = i_values[1];
        dy     = d_values2[1];

        if (nx < 1) {
            throw Exception("Error: Number of samples in X-dir must be >= 1.\n");
        }
        if (ny < 1) {
            throw Exception("Error: Number of samples in Y-dir must be >= 1.\n");
        }

        if (dx <= 0.0) {
            throw Exception("Error: Grid sampling in X-dir must be > 0.0.\n");

        }
        if (dy <= 0.0) {
            throw Exception("Error: Grid sampling in Y-dir must be > 0.0.\n");
        }

        double lx = nx*dx;
        double ly = ny*dy;

        double x_min = min_values[0]-0.5*dx; //In regular grid, these are at value;
        double y_min = min_values[1]-0.5*dy; //in sgri, at corner of cell, hence move.

        header_file >> angle;

        surface.Resize(nx, ny, 0.0);
        surface.SetDimensions(x_min, y_min, lx, ly);

        getline(header_file, tmp_str);
        //Reading record 10+dim+ngrid: Undef value
        float missing_code;
        header_file >> missing_code;
        surface.SetMissingValue(missing_code);
        getline(header_file, tmp_str);
        //Reading record 11+dim+ngrid: Filename of binary file
        std::string bin_file_name;
        getline(header_file, tmp_str);
        if (!tmp_str.empty()) {
            std::locale loc;
            int i = 0;
            char c = tmp_str[i];
            while (!std::isspace(c,loc)) {
                i++;
                c = tmp_str[i];
            }
            tmp_str.erase(tmp_str.begin()+i, tmp_str.end());
        }
        if (tmp_str.empty())
            bin_file_name = NRLib::ReplaceExtension(filename, "Sgri");
        else {
            std::string path = GetPath(filename);
            bin_file_name = path + "/" + tmp_str;
        }
        //Reading record 12+dim+ngrid: Complex values
        bool has_complex;
        header_file >> has_complex;
        if (has_complex != 0 ) {
            throw Exception("Error: Can not read Sgri binary file. Complex values?");
        }

        surface.SetName(GetStem(bin_file_name));

        std::ifstream bin_file;
        OpenRead(bin_file, bin_file_name, std::ios::in | std::ios::binary);
        ReadBinaryFloatArray(bin_file, surface.begin(), surface.GetN());
    }
    catch (Exception& e) {
        throw FileFormatError("Error parsing \"" + filename + "\" as a "
                              "Sgri surface file " + e.what() + "\n");
    }
}
Exemple #8
0
void NRLib::ReadIrapClassicAsciiSurf(const std::string & filename,
                                     RegularSurface<A> & surface,
                                     double            & angle)
{
    std::ifstream file;
    OpenRead(file, filename);

    int line = 0;
    // Header
    try {
        ReadNext<int>(file, line);   // -996
        int nj       = ReadNext<int>(file, line);
        double dx    = ReadNext<double>(file, line);
        double dy    = ReadNext<double>(file, line);
        // ----------- line shift --------------
        double x_min = ReadNext<double>(file, line);
        double x_max = ReadNext<double>(file, line);
        double y_min = ReadNext<double>(file, line);
        double y_max = ReadNext<double>(file, line);
        // ----------- line shift --------------
        int ni       = ReadNext<int>(file, line);
        angle        = ReadNext<double>(file, line);
        angle        = NRLib::Degree*angle;
        ReadNext<double>(file, line); // rotation origin - x
        ReadNext<double>(file, line); // rotation origin - y
        // ----------- line shift --------------
        ReadNext<int>(file, line);
        ReadNext<int>(file, line);
        ReadNext<int>(file, line);
        ReadNext<int>(file, line);
        ReadNext<int>(file, line);
        ReadNext<int>(file, line);
        ReadNext<int>(file, line);
        //double lx = (ni-1)*dx;
        //double ly = (nj-1)*dy;
        double lx = (x_max - x_min)*cos(angle);
        double ly = (y_max - y_min)*cos(angle);


        if (!NRLibPrivate::Equal(lx/(ni-1), dx)) {
            std::string text = "Inconsistent data in file. dx != lx/(nx-1).\n";
            text += "dx        = "+NRLib::ToString(dx,2)+"\n";
            text += "lx        = "+NRLib::ToString(lx,2)+"\n";
            text += "nx        = "+NRLib::ToString(ni,0)+"\n";
            text += "lx/(nx-1) = "+NRLib::ToString(lx/(ni - 1),2);
            throw FileFormatError(text);
        }
        if (!NRLibPrivate::Equal(ly/(nj-1), dy)) {
            std::string text = "Inconsistent data in file. dy != ly/(ny-1).\n";
            text += "dy        = "+NRLib::ToString(dy,2)+"\n";
            text += "ly        = "+NRLib::ToString(ly,2)+"\n";
            text += "ny        = "+NRLib::ToString(nj,0)+"\n";
            text += "ly/(ny-1) = "+NRLib::ToString(ly/(nj - 1),2);
            throw FileFormatError(text);
        }


        surface.Resize(ni, nj);
        surface.SetDimensions(x_min, y_min, lx, ly);

        ReadAsciiArrayFast(file, surface.begin(), surface.GetN());

        surface.SetMissingValue(static_cast<A>(IRAP_MISSING));

        surface.SetName(GetStem(filename));

        if (!CheckEndOfFile(file)) {
            throw FileFormatError("File too long.");
        }
    }
    catch (EndOfFile& ) {
        throw FileFormatError("Unexcpected end of file found while parsing "
                              " \"" + filename + "\"");
    }
    catch (Exception& e) {
        throw FileFormatError("Error parsing \"" + filename + "\" as a "
                              "IRAP ASCII surface file at line " + ToString(line) + ":" + e.what() + "\n");
    }
}
void RegularSurfaceRotated<A>::ReadFromFile(std::string       filename,
                                            SurfaceFileFormat format,
                                            double            segy_angle,
                                            double            x_ref,
                                            double            y_ref,
                                            double            lx,
                                            double            ly,
                                            int             * ilxl_area,
                                            int               il0,
                                            int               xl0,
                                            bool              first_axis_il,
                                            double            in_line0,
                                            double            cross_line0,
                                            double            il_step_x,
                                            double            il_step_y,
                                            double            xl_step_x,
                                            double            xl_step_y)
{
  if (format == SURF_UNKNOWN) {
    format = FindSurfaceFileType(filename);
    if (format == SURF_UNKNOWN) {
      throw FileFormatError("Failed to determine file format for surface file: " + filename
                            + "Allowed formats are Irap Classic, Storm binary, Sgri, Multicolumn Ascii (X,Y,Z,IL,XL) and XYZ ascii.");
    }
  }

  switch (format) {
    case SURF_IRAP_CLASSIC_ASCII:
      ReadIrapClassicAsciiSurf(filename, surface_, angle_);
      break;
    case SURF_STORM_BINARY:
      ReadStormBinarySurf(filename, surface_);
      angle_ = 0.0;
      break;
    case SURF_SGRI:
      ReadSgriSurf(filename, surface_, angle_);
      break;
    case SURF_XYZ_ASCII:
      ReadXYZAsciiSurf(filename,
                       surface_,
                       x_ref,
                       y_ref,
                       lx,
                       ly,
                       ilxl_area,
                       il0,
                       xl0,
                       first_axis_il,
                       in_line0, //The last 6 variables are used to create an IL/XL map
                       cross_line0,
                       il_step_x,
                       il_step_y,
                       xl_step_x,
                       xl_step_y);
      angle_ = segy_angle;
      break;
    case SURF_MULT_ASCII:
      ReadMulticolumnAsciiSurf(filename,
                               surface_,
                               x_ref,
                               y_ref,
                               lx,
                               ly,
                               ilxl_area,
                               il0,
                               xl0,
                               first_axis_il);
      angle_ = segy_angle;
      break;
    default:
      throw FileFormatError("Reading of file " + filename + " on format " + GetSurfFormatString(format)
                             + " as a rotated grid is currently not supported.");
  }

  x_ref_ = surface_.GetXMin();
  y_ref_ = surface_.GetYMin();

  CalculateMinMaxXY();
}