ingest.py 3.46 KB
Newer Older
1
import json
2
3
4
from pathlib import Path
from ntpath import basename
from knack.log import get_logger
5
from lasloader.file_loader import LasParser, LocalFileLoader, FileUtilities
6
from lasloader.record_mapper import LasToRecordMapper
Niall McDaid's avatar
Niall McDaid committed
7
from lasloader.osdu_client import OsduClient
8
from lasloader.configuration import Configuration
9
from lasloader.well_service import WellBoreService, WellLogService, LasLoaderConflictError
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43


logger = get_logger(__name__)


def wellbore(
        input_path: str,
        url: str,
        token: str,
        config_path: str,
        no_recognize: bool = False):
    """
    Ingest a LAS file (single) or directory of LAS files (bulk) into OSDU
    :param str input_path: Path and filename of a LAS file OR path to directory containing LAS and config files.
    :param str url: The base URL of the OSDU instance to which to upload the file.
    :param str token: A valid bearer token that is used to authenticate the ingest request.
    :param str config_path: Path to the LAS metadata file.
    :param bool no_recognize: If true don't attempt to recognize the curves, otherwise recognize the curves
    """
    config = Configuration(LocalFileLoader(), config_path)
    las_parser = LasParser(LocalFileLoader())
    client = OsduClient(url, token, config.data_partition_id)
    service = WellBoreService(client, WellLogService(client))

    failed_ingests = []

    for file_path in FileUtilities.get_filenames(Path(input_path), '.las'):
        logger.warning(f"Begining ingest of LAS file: {file_path}")

        try:
            las_data = las_parser.load_las_file(file_path)
            las_mapper = LasToRecordMapper(las_data, config)
            service.file_ingest(las_mapper, config.data_partition_id, no_recognize)
            logger.warning(f"Ingest complete: {basename(file_path)}")
44
45
46
47
48
        except LasLoaderConflictError as e:
            logger.error(f"Ingest failed: {basename(file_path)} (see summary for details)")
            ids = json.dumps(e.ids, indent=4, sort_keys=True)
            logger.debug(f"{basename(file_path)} ({str(e)} Matching wellbore ids {ids})")
            failed_ingests.append(f"{basename(file_path)} ({str(e)} Perform a search to list the conflicted wellbore ids.)")
49
50
51
52
53
        except Exception as e:
            failed_ingests.append(f"{basename(file_path)} ({str(e)})")
            logger.error(f"Ingest failed: {basename(file_path)} (see summary for details)")

    if failed_ingests != []:
54
55
56
        logger.error("SUMMARY - files not ingested: ")
        for message in failed_ingests:
            logger.error(f"     {message}")
57
58
59
60
61
62
63
64
65
66


def welllog_data(
        welllog_id: str,
        input_path: str,
        url: str,
        token: str,
        config_path: str):
    """
    Write data from a LAS file to an existing Well Log.
67
68
    :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.
69
70
71
72
73
74
    :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)
    client = OsduClient(url, token, config.data_partition_id)
Niall McDaid's avatar
Fix bug    
Niall McDaid committed
75
    service = WellLogService(client)
76

77
    service.ingest_welllog_data(input_path, welllog_id)
78
79

    logger.warning(f"Ingest complete: Data written from {basename(input_path)} to Well Log ({welllog_id}).")