Commit 27a374f3 authored by Niall McDaid's avatar Niall McDaid
Browse files

Extract logic into service level

parent 877d2712
Pipeline #71867 passed with stage
in 41 seconds
from pathlib import Path
from ntpath import basename
from knack.log import get_logger
from lasloader.file_loader import FileValidationError, LasParser, LocalFileLoader, FileUtilities
from lasloader.file_loader import LasParser, LocalFileLoader, FileUtilities
from lasloader.record_mapper import LasToRecordMapper
from lasloader.osdu_client import OsduClient
from lasloader.configuration import Configuration
......@@ -58,31 +58,16 @@ def welllog_data(
config_path: str):
"""
Write data from a LAS file to an existing Well Log.
:param str welllog_id: ID of Well Log to be updated.
:param str input_path: Path and filename of a LAS file containing data to be written to existing Well Log.
:param str welllog_id: ID of well log to be updated.
:param str input_path: Path and filename of a LAS file containing data to be written to existing well log.
:param str url: The base URL of the OSDU instance to which to find the existing Well Log.
:param str token: A valid bearer token that is used to authenticate the update request.
:param str config_path: Path to the LAS metadata file.
"""
config = Configuration(LocalFileLoader(), config_path)
las_parser = LasParser(LocalFileLoader())
client = OsduClient(url, token, config.data_partition_id)
service = WellBoreService(client, WellLogService(client))
las_data = las_parser.load_las_file(input_path)
logger.warning("Beginning LAS file validation.")
try:
service.validate_welllog_data_ingest_file(las_data, welllog_id)
except FileValidationError as e:
logger.error(f"Data not ingested - LAS file validation failed: {str(e)}")
return
except Exception as e:
logger.error(f"Data not ingested: {str(e)}")
return
logger.warning("LAS file validation completed.")
logger.warning("Extracting data from LAS file and writing to existing Well Log.")
service.write_data_to_welllog_record(las_data, welllog_id)
service.ingest_welllog_data(input_path, welllog_id)
logger.warning(f"Ingest complete: Data written from {basename(input_path)} to Well Log ({welllog_id}).")
......@@ -4,7 +4,7 @@ import urllib
from lasio.las import LASFile
from knack.log import get_logger
from lasloader.file_loader import LasParser, LocalFileLoader
from lasloader.file_loader import FileValidationError, LasParser, LocalFileLoader
from lasloader.osdu_client import OsduClient, LasLoaderWebResponseError
from lasloader.record_mapper import LasToRecordMapper, Record
......@@ -79,27 +79,29 @@ class WellLogService:
return f"{data_partition_id}:reference-data--LogCurveFamily:{family_id}:"
def validate_welllog_data_ingest_file(self, las_data: LASFile, welllog_id: str) -> None:
def ingest_welllog_data(self, input_path: str, welllog_id: str) -> None:
"""
Validate a LAS File against an existing well log record.
This should be done before attempting to write data from a LAS file to an existing record.
Write data from a LAS file to an existing well log. First the LAS file is validated against the
existing well log record, then the data is extracted and written to the existing well log record.
:param LASFile las_data: LASFile object to be validated.
:param str welllog_id: ID of the well log record to write data to.
:param str input_path: Path and filename of a LAS file containing data to be written to existing well log.
:param str welllog_id: ID of well log to be updated.
"""
logger.info("Retrieving well log record and associated information.")
welllog_well_name, welllog_curve_ids = self._get_data_ingest_validation_variables(welllog_id)
logger.info("Validating LAS file well name and curves against existing well log record.")
las_parser = LasParser(LocalFileLoader())
las_parser.validate_las_file_against_record(las_data, welllog_well_name, welllog_curve_ids)
las_data = las_parser.load_las_file(input_path)
def write_data_to_welllog_record(self, las_data: LASFile, welllog_id: str) -> None:
"""
Extract data from LAS file and write to existing well log.
:param LASFile las_data: LASFile object containing data to be written to existing well log record.
:param str welllog_id: ID of the well log record to write data to.
"""
logger.info("Beginning LAS file validation.")
try:
self._validate_welllog_data_ingest_file(las_data, welllog_id)
except FileValidationError as e:
logger.error(f"Data not ingested - LAS file validation failed: {str(e)}")
return
except Exception as e:
logger.error(f"Data not ingested: {str(e)}")
return
logger.info("LAS file validation completed.")
logger.info("Extracting data from LAS file and writing to existing Well Log.")
data = las_data.df().reset_index()
self._client.add_welllog_data(data, welllog_id)
......@@ -120,6 +122,21 @@ class WellLogService:
return urllib.parse.unquote(split_units[2])
def _validate_welllog_data_ingest_file(self, las_data: LASFile, welllog_id: str) -> None:
"""
Validate a LAS File against an existing well log record.
This should be done before attempting to write data from a LAS file to an existing record.
:param LASFile las_data: LASFile object to be validated.
:param str welllog_id: ID of the well log record to write data to.
"""
logger.info("Retrieving well log record and associated information.")
welllog_well_name, welllog_curve_ids = self._get_data_ingest_validation_variables(welllog_id)
logger.info("Validating LAS file well name and curves against existing well log record.")
las_parser = LasParser(LocalFileLoader())
las_parser.validate_las_file_against_record(las_data, welllog_well_name, welllog_curve_ids)
def _get_data_ingest_validation_variables(self, welllog_id: str) -> Tuple[str, list[str]]:
"""
Get the associated well name and curve IDs of the existing well log for validating against LAS file.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment