Skip to content

bgc_data_processing.core.loaders.abfile_loaders

ABfiles Loaders.

ABFileLoader(provider_name, category, exclude, variables, grid_basename)

Bases: BaseLoader

Loader class to use with ABFiles.

Parameters:

Name Type Description Default
provider_name str

Data provider name.

required
category str

Category provider belongs to.

required
exclude list[str]

Filenames to exclude from loading.

required
variables SourceVariableSet

Storer object containing all variables to consider for this data, both the one in the data file but and the one not represented in the file.

required
grid_basename str

Basename of the ab grid grid file for the loader. => files are considered to be loaded over the same grid.

required
Source code in src/bgc_data_processing/core/loaders/abfile_loaders.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def __init__(
    self,
    provider_name: str,
    category: str,
    exclude: list[str],
    variables: "SourceVariableSet",
    grid_basename: str,
) -> None:
    super().__init__(
        provider_name=provider_name,
        category=category,
        exclude=exclude,
        variables=variables,
    )
    self.grid_basename = grid_basename
    self.grid_file = ABFileGrid(basename=grid_basename, action="r")
    self._index = None

level_column: str = 'LEVEL' class-attribute instance-attribute

level_key_bfile: str = 'k' class-attribute instance-attribute

field_key_bfile: str = 'field' class-attribute instance-attribute

pascal_by_seawater_meter: int = 9806 class-attribute instance-attribute

grid_basename = grid_basename instance-attribute

grid_file = ABFileGrid(basename=grid_basename, action='r') instance-attribute

convert_filepath_to_basename(filepath) staticmethod

Convert a filepath to the basename.

Parameters:

Name Type Description Default
filepath Path | str

Filepath ot convert.

required

Returns:

Type Description
Path

Basename

Source code in src/bgc_data_processing/core/loaders/abfile_loaders.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
@staticmethod
def convert_filepath_to_basename(filepath: Path | str) -> Path:
    """Convert a filepath to the basename.

    Parameters
    ----------
    filepath : Path | str
        Filepath ot convert.

    Returns
    -------
    Path
        Basename
    """
    path = Path(filepath)
    return path.parent.joinpath(path.stem)

load(filepath, constraints=None)

Load a abfiles from basename.

Parameters:

Name Type Description Default
filepath Path | str

Path to the basename of the file to load.

required
constraints Constraints | None

Constraints slicer., by default None

None

Returns:

Type Description
DataFrame

DataFrame corresponding to the file.

Source code in src/bgc_data_processing/core/loaders/abfile_loaders.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def load(
    self,
    filepath: Path | str,
    constraints: Constraints | None = None,
) -> pd.DataFrame:
    """Load a abfiles from basename.

    Parameters
    ----------
    filepath: Path | str
        Path to the basename of the file to load.
    constraints : Constraints| None, optional
        Constraints slicer., by default None

    Returns
    -------
    pd.DataFrame
        DataFrame corresponding to the file.
    """
    if constraints is None:
        constraints = Constraints()
    basename = self.convert_filepath_to_basename(filepath)
    raw_data = self._read(basename=str(basename))
    # transform thickness in depth
    with_depth = self._create_depth_column(raw_data)
    # create date columns
    with_dates = self._set_date_related_columns(with_depth, Path(basename))
    # converts types
    typed = self._convert_types(with_dates)
    # apply corrections
    corrected = self._correct(typed)
    # apply constraints
    constrained = constraints.apply_constraints_to_dataframe(corrected)
    return self.remove_nan_rows(constrained)

is_file_valid(filepath)

Check whether a file is valid or not.

Parameters:

Name Type Description Default
filepath Path | str

File filepath.

required

Returns:

Type Description
bool

True if the file can be loaded.

Raises:

Type Description
FileNotFoundError

If the afile doesn't not exist.

FileNotFoundError

If the bfile doesn't not exist.

Source code in src/bgc_data_processing/core/loaders/abfile_loaders.py
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
def is_file_valid(self, filepath: Path | str) -> bool:
    """Check whether a file is valid or not.

    Parameters
    ----------
    filepath : Path | str
        File filepath.

    Returns
    -------
    bool
        True if the file can be loaded.

    Raises
    ------
    FileNotFoundError
        If the afile doesn't not exist.
    FileNotFoundError
        If the bfile doesn't not exist.
    """
    path = Path(filepath)
    basepath = path.parent / path.name[:-2]
    keep_filepath = str(path) not in self.excluded_filenames
    keep_filename = path.name not in self.excluded_filenames
    keep_file = keep_filename and keep_filepath
    keep_basepath = str(basepath) not in self.excluded_filenames
    keep_basename = basepath.name not in self.excluded_filenames
    keep_base = keep_basename and keep_basepath
    afile_path = Path(f"{basepath}.a")
    bfile_path = Path(f"{basepath}.b")
    if not afile_path.is_file():
        error_msg = f"{afile_path} does not exist."
        raise FileNotFoundError(error_msg)
    if not bfile_path.is_file():
        error_msg = f"{bfile_path} does not exist."
        raise FileNotFoundError(error_msg)
    return keep_base and keep_file