|
| 1 | +import contextlib |
| 2 | +import filecmp |
| 3 | +import shutil |
| 4 | +from os.path import isdir |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +with contextlib.suppress(ImportError): |
| 11 | + import rmsapi |
| 12 | + |
| 13 | +import xtgeo |
| 14 | + |
| 15 | +from fmu.tools.rms.get_rms_simbox_params import get_simbox_param, write_simbox |
| 16 | + |
| 17 | +# ====================================================================================== |
| 18 | +# settings to create RMS project! |
| 19 | + |
| 20 | +REMOVE_RMS_PROJECT_AFTER_TEST = False |
| 21 | + |
| 22 | +TMPD = Path("TMP") |
| 23 | +TMPD.mkdir(parents=True, exist_ok=True) |
| 24 | + |
| 25 | + |
| 26 | +PROJNAME = "tmp_project_get_rms_simbox_params.rmsxxx" |
| 27 | +PRJ = str(TMPD / PROJNAME) |
| 28 | +RESULTDIR = TMPD / "rms_simbox" |
| 29 | +RESULTDIR.mkdir(parents=True, exist_ok=True) |
| 30 | +REFERENCE_DIR = Path("tests/rms/rms_simbox") |
| 31 | + |
| 32 | +NX = 50 |
| 33 | +NY = 70 |
| 34 | +NZ_ZONEA = 10 |
| 35 | +NZ_ZONEB = 35 |
| 36 | +XINC = 50.0 |
| 37 | +YINC = 50.0 |
| 38 | +ZINC = 5.0 |
| 39 | +ORIGIN = (10000.0, 20000.0, 2000.0) |
| 40 | +ROTATION = 50.0 |
| 41 | +ZONEA = "ZoneA" |
| 42 | +ZONEB = "ZoneB" |
| 43 | + |
| 44 | + |
| 45 | +ZONEA_NUMBER = 1 |
| 46 | +ZONEB_NUMBER = 2 |
| 47 | + |
| 48 | + |
| 49 | +GRID_MODEL_NAME = "Geogrid" |
| 50 | +OUTPUT_FILE_PREFIX = "simbox" |
| 51 | +OUTPUT_REF_FILE_PREFIX = "ref_simbox" |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.skipunlessroxar |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "rotation, flip, zone_index_list", |
| 57 | + [ |
| 58 | + ( |
| 59 | + 0.0, |
| 60 | + 1, |
| 61 | + [0, 1], |
| 62 | + ), |
| 63 | + ], |
| 64 | +) |
| 65 | +def test_get_rms_simbox_params( |
| 66 | + rotation: float, flip: int, zone_index_list: list[int] |
| 67 | +) -> None: |
| 68 | + """Create a tmp RMS project for testing, populate with basic data.""" |
| 69 | + project = create_project() |
| 70 | + |
| 71 | + create_grids(project, rotation, flip) |
| 72 | + for zone_index in zone_index_list: |
| 73 | + simbox_output_file_name = Path(RESULTDIR) / Path( |
| 74 | + OUTPUT_FILE_PREFIX |
| 75 | + + "_angle" |
| 76 | + + str(int(rotation)) |
| 77 | + + "_zone" |
| 78 | + + str(zone_index) |
| 79 | + + ".txt" |
| 80 | + ) |
| 81 | + simbox_dict = get_simbox_param(project, GRID_MODEL_NAME, zone_index) |
| 82 | + write_simbox(simbox_output_file_name, simbox_dict) |
| 83 | + |
| 84 | + # Verify that original is equal to the new params |
| 85 | + reference_filename = Path(REFERENCE_DIR) / Path( |
| 86 | + OUTPUT_REF_FILE_PREFIX |
| 87 | + + "_angle" |
| 88 | + + str(int(rotation)) |
| 89 | + + "_zone" |
| 90 | + + str(zone_index) |
| 91 | + + ".txt" |
| 92 | + ) |
| 93 | + compare_results_with_reference(simbox_output_file_name, reference_filename) |
| 94 | + |
| 95 | + project.close() |
| 96 | + |
| 97 | + if REMOVE_RMS_PROJECT_AFTER_TEST: |
| 98 | + print("\n******* Teardown RMS project!\n") |
| 99 | + if isdir(PRJ): |
| 100 | + print("Remove existing project!") |
| 101 | + shutil.rmtree(PRJ) |
| 102 | + if isdir(RESULTDIR): |
| 103 | + print("Remove temporary files") |
| 104 | + shutil.rmtree(RESULTDIR) |
| 105 | + |
| 106 | + |
| 107 | +def create_project(): |
| 108 | + """Create a tmp RMS project for testing, populate with basic data.""" |
| 109 | + |
| 110 | + prj1 = str(PRJ) |
| 111 | + |
| 112 | + print("\n******** Setup RMS project!\n") |
| 113 | + if isdir(prj1): |
| 114 | + print("Remove existing project! (1)") |
| 115 | + shutil.rmtree(prj1) |
| 116 | + |
| 117 | + project = rmsapi.Project.create() |
| 118 | + |
| 119 | + rox = xtgeo.RoxUtils(project) |
| 120 | + print("rmsapi version is", rox.roxversion) |
| 121 | + print("RMS version is", rox.rmsversion(rox.roxversion)) |
| 122 | + assert "1." in rox.roxversion |
| 123 | + |
| 124 | + project.save_as(prj1) |
| 125 | + return project |
| 126 | + |
| 127 | + |
| 128 | +def create_grids(project: Any, rotation: float, flip: int = 1): |
| 129 | + nx = NX |
| 130 | + ny = NY |
| 131 | + nz_zoneA = NZ_ZONEA |
| 132 | + nz_zoneB = NZ_ZONEB |
| 133 | + nz = nz_zoneA + nz_zoneB |
| 134 | + dimension = (nx, ny, nz) |
| 135 | + increment = (XINC, YINC, ZINC) |
| 136 | + origin = ORIGIN |
| 137 | + |
| 138 | + subgrid_dict = { |
| 139 | + ZONEA: nz_zoneA, |
| 140 | + ZONEB: nz_zoneB, |
| 141 | + } |
| 142 | + geogrid = xtgeo.create_box_grid( |
| 143 | + dimension, |
| 144 | + origin=origin, |
| 145 | + increment=increment, |
| 146 | + rotation=rotation, |
| 147 | + flip=flip, |
| 148 | + ) |
| 149 | + geogrid.set_subgrids(subgrid_dict) |
| 150 | + geogrid.to_roxar(project, GRID_MODEL_NAME) |
| 151 | + |
| 152 | + |
| 153 | +def compare_results_with_reference(filename, reference_filename): |
| 154 | + # Compare with reference |
| 155 | + check = filecmp.cmp(filename, reference_filename) |
| 156 | + if check: |
| 157 | + print("Check OK for multi zone grid") |
| 158 | + assert check |
0 commit comments