Skip to content

Access colormaps folder in scripts realpath (not symlink path) #19

@mefrediani

Description

@mefrediani

When we link the plot script to the simulation folder, it can't find the colormaps folder with the reflectivity colorscale.

Add this line at the start:
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))

Replace the statement in line 262 (elif os.path.exists(colorfile:=f"colormaps/{cmapname}.yaml"):) with

    elif (colorfile_info := resolve_custom_colormap_file(cmapname))[0]:
        colorfile, _ = colorfile_info

You can add more information to the raise statement (line 279):

        _, searched = resolve_custom_colormap_file(cmapname)
        raise ValueError(
            f"Requested color map {cmapname} is not valid. "
            f"Checked built-in matplotlib colormaps and custom files: {searched}"
        )

And add this function:

def resolve_custom_colormap_file(cmapname: str):
    """
    Resolve custom colormap YAML by checking:
    1) explicit user-provided path (with/without .yaml),
    2) script-local colormaps directory,
    3) current-working-directory colormaps directory.
    """
    searched = []

    # Allow explicit path-like values in config (absolute or relative)
    if os.path.isabs(cmapname) or os.path.sep in cmapname:
        explicit = cmapname if cmapname.endswith(".yaml") else f"{cmapname}.yaml"
        searched.append(explicit)
        if os.path.exists(explicit):
            return explicit, searched

    candidates = [
        os.path.join(SCRIPT_DIR, "colormaps", f"{cmapname}.yaml"),
        os.path.join(os.getcwd(), "colormaps", f"{cmapname}.yaml"),
    ]
    # Remove duplicates while preserving order.
    candidates = list(dict.fromkeys(candidates))
    for candidate in candidates:
        searched.append(candidate)
        if os.path.exists(candidate):
            return candidate, searched

    return None, searched

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions