Metadata-Version: 2.4
Name: access-config-utils
Version: 0.6
Summary: Utilities to work with configurations for the ACCESS climate models.
Author-email: Micael Oliveira <micael.oliveira@anu.edu.au>
Keywords: configurations,parser,access
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Utilities
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: lark
Requires-Dist: ruamel.yaml
Provides-Extra: devel
Requires-Dist: pre-commit; extra == "devel"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Dynamic: license-file

# access-config-utils

![CI](https://github.com/ACCESS-NRI/access-config-utils/actions/workflows/ci.yml/badge.svg) [![codecov](https://codecov.io/github/ACCESS-NRI/access-config-utils/graph/badge.svg?token=KtmrCtSyMv)](https://codecov.io/github/ACCESS-NRI/access-config-utils) [![License](https://img.shields.io/badge/license-Apache%202.0-blue?style=flat-square)](https://opensource.org/license/apache-2-0) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

## About

access-config-utils is a Python package for working with the configurations of the climate models developed at
[ACCESS-NRI](https://github.com/ACCESS-NRI). It has two parts, which you can use independently of each other: parsers
for configuration files, and a search for parallelisation layouts.

## Key Features

Parsers:
- Various parsers for configuration files used across ACCESS-NRI models:
  - Fortran namelists (`FortranNMLParser`)
  - MOM6 parameter files, `MOM_input` (`MOM6InputParser`)
  - NUOPC run configurations, `nuopc.runconfig` (`NUOPCParser`)
  - NUOPC run sequences, `nuopc.runseq` (`NUOPCRunSeqParser`)
  - FMS field tables in the legacy format, `field_table` (`FieldTableParser`)
  - Rose configurations, `rose-suite.conf` and `rose-app.conf` (`RoseConfParser`)
  - YAML (`YAMLParser`) and JSON (`JSONParser`)
- Support for round-trip parsing, preserving comments and formatting
- Simple API

Parallelisation layouts:
- Enumeration of the layouts that a model can run on a number of cores. A layout gives the cores, the MPI ranks, the
  OpenMP threads and the process grid of each component, and the search is filtered by allocation strategies and
  constraints (`iter_layouts`)

## Documentation

The documentation is at [access-config-utils.readthedocs.io](https://access-config-utils.readthedocs.io/). It has a user
guide, which explains how each format is read, edited and written, and how to describe a model and search for its
parallelisation layouts. It also has a developer guide and an API reference.

## Installation

### Using pip

You can install the latest release directly from PyPI:
```shell
pip install access-config-utils
```

### From source

If you prefer to install from source:
```shell
git clone https://github.com/ACCESS-NRI/access-config-utils.git
cd access-config-utils
pip install .
```

## Usage

### Configuration files parsers

Here is a simple example of how to parse a text and modify its contents.

To parse some text, call the `parse` method of the appropriate parser:
```python
from access.config import FortranNMLParser

text = '''&data_nml
parameterA = 1
parameterB = 'abc'
/'''

config = FortranNMLParser().parse(text)
print(config)
```

This prints:

```python
&data_nml
parameterA = 1
parameterB = 'abc'
/
```

You can then modify the parsed content like a Python dict. Everything that you do not touch is reproduced exactly,
including the comments, the blank lines and the spacing:
```python
config["data_nml"]["parameterA"] = 2
print(config)
```

This prints:

```python
&data_nml
parameterA = 2
parameterB = 'abc'
/
```

You can also add new keys. They are written like the entries around them, with the same spacing, delimiters, quotes and
line endings. Each value is written in the notation of the format, so a `bool` becomes a Fortran logical here:
```python
config["data_nml"]["parameterC"] = True
config["data_nml"]["parameterD"] = [1, 2, 3]
config["new_nml"] = {"parameterE": "def"}
print(config)
```

This prints:

```python
&data_nml
parameterA = 2
parameterB = 'abc'
parameterC = .true.
parameterD = 1, 2, 3
/
&new_nml
parameterE = "def"
/
```

A value that the format cannot express raises `UnsupportedEntryError`. The file would otherwise no longer mean what the
dict says. Examples are `None` in a format with no valueless assignment, and a string containing a space in a format
with no quoted strings.

A list value behaves like a list. You can change, add, remove and reorder its elements, by assignment or with `append`,
`extend`, `insert`, `pop`, `sort` and the rest. The elements that stay keep their text, comments included, and a new
element is written like its neighbours, on their line or on a line of its own:
```python
config = FortranNMLParser().parse('''&data_nml
parameterD = 1,
             2  ! two
/''')
config["data_nml"]["parameterD"].append(3)
print(config)
```

This prints:

```python
&data_nml
parameterD = 1,
             2,
             3  ! two
/
```

A few caveats on the dict and list behaviour:
- A list keeps at least two elements, since a single value is a scalar, and a scalar does not
  become a list or the other way round.
- A list whose elements are all of one type keeps them so: a new element of another type raises
  `TypeError`.
- A list written with repeat counts (`3*1.0`), array qualifiers (`x(2) = …`) or skipped
  positions, or assigned more than once, can have its elements changed but not its length:
  delete the key and assign it again instead. The elements a repeat count stands for can only
  be changed together, all to the same value (`6*9.0` to `6*9.5`): to change one of them,
  delete the key and assign it again.
- Replacing an existing block wholesale raises `SyntaxError` — a block can be created and edited,
  but not swapped out.

Every parser works in the same way, and only the structure of the dict depends on the format. An FMS field table, for
instance, is read with the structure of the FMS YAML field table. A header is three levels of keys. A method of three
strings is a record, which holds its name under `"value"`, and the records that the file writes for a method make a
list:
```python
from access.config import FieldTableParser

config = FieldTableParser().parse('''"TRACER", "atmos_mod", "sphum"
    "longname", "specific humidity"
    "profile_type", "fixed", "surface_value=3.e-6" /
''')
print(dict(config["tracer"]["atmos_mod"]["sphum"]))
```

This prints:

```python
{'longname': 'specific humidity', 'profile_type': [{'value': 'fixed', 'surface_value': 3e-06}]}
```
A record is read only. To change it, assign a dict to its place in the list. A new field is written as a whole entry,
after the last entry of its type and model:
```python
profile = config["tracer"]["atmos_mod"]["sphum"]["profile_type"]
profile[0] = {**profile[0], "surface_value": 1e-5}
config["tracer"]["atmos_mod"]["liq_wat"] = {"longname": "cloud liquid specific humidity"}
print(config)
```

This prints:

```python
"TRACER", "atmos_mod", "sphum"
    "longname", "specific humidity"
    "profile_type", "fixed", "surface_value=1e-05" /
"TRACER", "atmos_mod", "liq_wat"
    "longname", "cloud liquid specific humidity" /
```

A JSON file holds plain data, so you edit its dict directly, and the text is written again from it once it changes. The
formatting of the file is kept, down to which lists it wrote on one line:
```python
from access.config import JSONParser

config = JSONParser().parse('''{
    "name": "run",
    "levels": [1, 2, 3],
    "files": [
        "a.nc",
        "b.nc"
    ]
}
''')
config["dt"] = 900
config["levels"].append(4)
print(config)
```

This prints:

```python
{
    "name": "run",
    "levels": [1, 2, 3, 4],
    "files": [
        "a.nc",
        "b.nc"
    ],
    "dt": 900
}
```

## Development installation

If you intend to contribute or modify the package, it is recommended to work inside a virtual environment.

1. Create and activate a virtual environment
```shell
# Create a virtual environment
python3 -m venv .venv

# Activate the virtual environment
source .venv/bin/activate
```

2. Install in editable mode with development and test dependencies
```shell
pip install -e ".[devel,test]"
```
This will install the package in editable mode, meaning changes to the source code are reflected immediately without reinstallation. Development dependencies such as testing tools will also be installed.

3. Run the test suite
```shell
pytest
```

## Contributing

Contributions are welcome! Please open an issue or submit a pull request if you’d like to add features, fix bugs, or improve documentation.

For significant contributions, we recommend discussing proposed changes in an issue before opening a pull request.

## License

This project is licensed under the Apache 2.0 License.
