Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions applications/resdata/rd_pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ int main(int argc, char **argv) {
filelist.at(i - 1).c_str(), filelist.at(i).c_str());

prev_report_step = report_step;
rd_file_ptr src_file(rd_file_open(filelist.at(i).c_str(), 0),
&rd_file_close);
rd_file_ptr src_file = open_rd_file(filelist.at(i));
if (target_type == RD_UNIFIED_RESTART_FILE) {
/* Must insert the SEQNUM keyword first. */
rd_kw_iset_int(seqnum_kw.get(), 0, report_step);
Expand Down
19 changes: 14 additions & 5 deletions applications/resdata/rd_unpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
for more details.
*/

#include <cstddef>
#include <cstdio>

#include <ios>
#include <memory>
#include <filesystem>
#include <string>

#include <ert/util/util.hpp>

#include <resdata/rd_file.hpp>
#include <resdata/rd_util.hpp>
#include <resdata/rd_kw.hpp>
#include <resdata/rd_endian_flip.hpp>
#include <resdata/rd_kw_magic.hpp>
#include <filesystem>
#include <resdata/FortIO.hpp>
#include <resdata/rd_file_view.hpp>

namespace fs = std::filesystem;

Expand All @@ -44,7 +53,7 @@ static void unpack_file(const fs::path &filepath) {
printf("** Warning: when unpacking unified summary files it as "
"ambigous - starting with 0001 -> \n");
}
rd_file_ptr src_file(rd_file_open(filename.c_str(), 0), &rd_file_close);
rd_file_ptr src_file = open_rd_file(filename);
int size;
int offset;
int report_step = 0;
Expand All @@ -55,7 +64,7 @@ static void unpack_file(const fs::path &filepath) {
size = rd_file_get_num_named_kw(src_file.get(), "SEQNUM");

for (int block_index = 0; block_index < size; block_index++) {
rd_file_view_type *active_view;
std::shared_ptr<rd::FileView> active_view;

if (target_type == RD_SUMMARY_FILE) {
active_view = rd_file_get_global_blockview(src_file.get(),
Expand All @@ -66,7 +75,7 @@ static void unpack_file(const fs::path &filepath) {
rd_kw_type *seqnum_kw;
active_view = rd_file_get_global_blockview(src_file.get(),
SEQNUM_KW, block_index);
seqnum_kw = rd_file_view_iget_named_kw(active_view, SEQNUM_KW, 0);
seqnum_kw = active_view->get_kw(SEQNUM_KW, 0);
report_step = rd_kw_iget_int(seqnum_kw, 0);
offset = 1;
}
Expand All @@ -81,7 +90,7 @@ static void unpack_file(const fs::path &filepath) {
fs::path target_file =
rd::filename(filepath.stem(), target_type, fmt_file, report_step);
ERT::FortIO fortio_target(target_file, std::ios_base::out, fmt_file);
rd_file_view_fwrite(active_view, fortio_target, offset);
active_view->write(fortio_target, offset);
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,15 @@ add_resdata_pybind_module(_grid rd_grid_pybind.cpp resdata/grid)
add_resdata_pybind_module(_kw rd_kw_pybind.cpp resdata/resfile)
add_resdata_pybind_module(_rsthead rsthead_pybind.cpp resdata/resfile)
add_resdata_pybind_module(_file rd_file_pybind.cpp resdata/resfile)
add_resdata_pybind_module(rd_file_view rd_file_view_pybind.cpp resdata/resfile)
add_resdata_pybind_module(fortio fortio_pybind.cpp resdata/resfile)
add_resdata_pybind_module(well well_pybind.cpp resdata)
add_resdata_pybind_module(_subsidence rd_subsidence_pybind.cpp
resdata/gravimetry)
add_resdata_pybind_module(_grav rd_grav_pybind.cpp resdata/gravimetry)
add_resdata_pybind_module(_rd_sum rd_sum_pybind.cpp resdata/summary)
add_resdata_pybind_module(_type rd_type_pybind.cpp resdata/types)
add_resdata_pybind_module(_file_mode file_mode_pybind.cpp resdata)

if(NOT BUILD_TESTS)
return()
Expand Down
1 change: 1 addition & 0 deletions lib/include/resdata/FortIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class FortIO {
void fflush() const;
void rewind() const;
[[nodiscard]] const char *filename_ref() const;
[[nodiscard]] const std::string &filename() const { return m_filename; };
[[nodiscard]] bool fmt_file() const;
[[nodiscard]] offset_type ftell() const;
bool fseek(offset_type offset, int whence);
Expand Down
62 changes: 36 additions & 26 deletions lib/include/resdata/rd_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,42 @@
#include <string>

#include <resdata/rd_kw.hpp>
#include <resdata/rd_file_kw.hpp>
#include <resdata/rd_file_view.hpp>
#include <resdata/FortIO.hpp>
#include <resdata/rd_util.hpp>
#include <resdata/rd_type.hpp>
#include <resdata/rd_file_flag.hpp>

#include "ert/util/type_macros.hpp"

#define RD_FILE_FLAGS_ENUM_DEFS \
{.value = 1, .name = "RD_FILE_CLOSE_STREAM"}, \
{.value = 2, .name = "RD_FILE_WRITABLE"}
#define RD_FILE_FLAGS_ENUM_SIZE 2
namespace rd {

/* The resources borrowed by every FileView derived from an rd_file. They are
held jointly through a std::shared_ptr so that a FileView (e.g. a restart
view held from Python) can safely outlive the rd_file it originated from
without dangling. */
struct FileContext {
ERT::FortIO fortio;
FileMode flags;
inv_map_type inv_map;

FileContext(ERT::FortIO fortio, FileMode flags)
: fortio(std::move(fortio)), flags(flags) {}
};

} // namespace rd

typedef struct rd_file_struct rd_file_type;
bool rd_file_load_all(rd_file_type *rd_file);
rd_file_type *rd_file_open(const char *filename, int flags);
rd_file_type *rd_file_open(const char *filename,
FileMode flags = FileMode::DEFAULT);
rd_file_type *rd_file_fast_open(const char *filename,
const char *index_filename, int flags);
const char *index_filename,
FileMode flags = FileMode::DEFAULT);
bool rd_file_write_index(const rd_file_type *rd_file,
const char *index_filename);
bool rd_file_index_valid(const char *file_name, const char *index_file_name);
void rd_file_close(rd_file_type *rd_file);
void rd_file_free(rd_file_type *rd_file);
rd_kw_type *rd_file_icopy_kw(const rd_file_type *rd_file, int index);
bool rd_file_has_kw(const rd_file_type *rd_file, const char *kw);
int rd_file_get_num_named_kw(const rd_file_type *rd_file, const char *kw);
Expand All @@ -40,18 +54,15 @@ void rd_file_fwrite_fortio(const rd_file_type *ec_file, ERT::FortIO &fortio,
int rd_file_get_phases(const rd_file_type *init_file);

bool rd_file_writable(const rd_file_type *rd_file);
bool rd_file_flags_set(const rd_file_type *rd_file, int flags);

rd_kw_type *rd_file_iget_kw(const rd_file_type *file, int global_index);
rd_kw_type *rd_file_iget_named_kw(const rd_file_type *file, const char *kw,
int ith);
rd_file_view_type *rd_file_get_global_blockview(rd_file_type *rd_file,
const char *kw, int occurence);
rd_file_view_type *rd_file_alloc_global_blockview(rd_file_type *rd_file,
const char *kw,
int occurence);
rd_file_view_type *rd_file_get_global_view(rd_file_type *rd_file);
rd_file_view_type *rd_file_get_active_view(rd_file_type *rd_file);
std::shared_ptr<rd::FileView>
rd_file_get_global_blockview(rd_file_type *rd_file, const char *kw,
int occurence);
std::shared_ptr<rd::FileView> rd_file_get_global_view(rd_file_type *rd_file);
std::shared_ptr<rd::FileView> rd_file_get_active_view(rd_file_type *rd_file);
bool rd_file_save_kw(const rd_file_type *rd_file, const rd_kw_type *rd_kw);

double rd_file_iget_restart_sim_days(const rd_file_type *restart_file,
Expand All @@ -63,22 +74,21 @@ int rd_file_get_restart_index(const rd_file_type *restart_file,
bool rd_file_has_report_step(const rd_file_type *rd_file, int report_step);
bool rd_file_has_sim_time(const rd_file_type *rd_file, time_t sim_time);

rd_file_view_type *rd_file_get_restart_view(rd_file_type *rd_file,
int input_index, int report_step,
time_t sim_time, double sim_days);
rd_file_view_type *rd_file_get_summary_view(rd_file_type *rd_file,
int report_step);
std::shared_ptr<rd::FileView> rd_file_get_summary_view(rd_file_type *rd_file,
int report_step);

UTIL_IS_INSTANCE_HEADER(rd_file);

bool rd_file_subselect_block(rd_file_type *rd_file, const char *kw,
int occurence);

using rd_file_ptr = std::unique_ptr<rd_file_type, decltype(&rd_file_close)>;
using rd_file_ptr = std::unique_ptr<rd_file_type, decltype(&rd_file_free)>;

inline rd_file_ptr open_rd_file(const std::string &path, int flags) {
return {rd_file_open(path.c_str(), flags), &rd_file_close};
inline rd_file_ptr open_rd_file(const std::string &path,
FileMode flags = FileMode::DEFAULT) {
return {rd_file_open(path.c_str(), flags), &rd_file_free};
}
inline rd_file_ptr open_rd_file(const std::filesystem::path &path, int flags) {
return {rd_file_open(path.string().c_str(), flags), &rd_file_close};
inline rd_file_ptr open_rd_file(const std::filesystem::path &path,
FileMode flags = FileMode::DEFAULT) {
return {rd_file_open(path.string().c_str(), flags), &rd_file_free};
}
51 changes: 51 additions & 0 deletions lib/include/resdata/rd_file_flag.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <type_traits>

enum class FileMode : int {
DEFAULT = 0,
CLOSE_STREAM =
1, /* This flag will close the underlying FILE object between each access;
this is mainly to save filedescriptors in cases where many rd_file
instances are open at the same time. */
WRITABLE =
2 /* This flag opens the file in a mode where it can be updated and modified,
but it must still exist and be readable. I.e. this should not compared
with the normal: fopen(filename , "w") where an existing file is
truncated to zero upon successfull open. */
};

constexpr FileMode operator|(FileMode lhs, FileMode rhs) {
using T = std::underlying_type_t<FileMode>;
return static_cast<FileMode>(static_cast<T>(lhs) | static_cast<T>(rhs));
}

constexpr FileMode operator&(FileMode lhs, FileMode rhs) {
using T = std::underlying_type_t<FileMode>;
return static_cast<FileMode>(static_cast<T>(lhs) & static_cast<T>(rhs));
}

constexpr FileMode operator^(FileMode lhs, FileMode rhs) {
using T = std::underlying_type_t<FileMode>;
return static_cast<FileMode>(static_cast<T>(lhs) ^ static_cast<T>(rhs));
}

constexpr FileMode operator~(FileMode value) {
using T = std::underlying_type_t<FileMode>;
return static_cast<FileMode>(~static_cast<T>(value));
}

constexpr FileMode &operator|=(FileMode &lhs, FileMode rhs) {
lhs = lhs | rhs;
return lhs;
}

constexpr FileMode &operator&=(FileMode &lhs, FileMode rhs) {
lhs = lhs & rhs;
return lhs;
}

constexpr FileMode &operator^=(FileMode &lhs, FileMode rhs) {
lhs = lhs ^ rhs;
return lhs;
}
Loading
Loading