Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a global method to set the tidal data directory #1109

Closed
omad opened this issue Aug 24, 2023 · 5 comments · Fixed by #1112
Closed

Provide a global method to set the tidal data directory #1109

omad opened this issue Aug 24, 2023 · 5 comments · Fixed by #1112
Assignees
Labels
bug Something isn't working

Comments

@omad
Copy link
Contributor

omad commented Aug 24, 2023

The dea_tools.coastal.model_tides() function has a directory argument which defaults to /var/share/tide_models. This works fine when running or testing dea_tools in DEA Sandbox or inside a Docker container, but is awkward anywhere else.

I don't want to overcomplicate things, but would like to do something like allowing changing this default with an Environment variable, to allow a bit more flexibility.

For example, I could create a ~/tide_models directory in my home directory, and set DEA_TOOLS_TIDE_MODELS to point to it.

@omad omad added the bug Something isn't working label Aug 24, 2023
@robbibt
Copy link
Member

robbibt commented Aug 24, 2023

Hey @omad, I've wondered about this myself... could we do both? Have it use the global environment param if it exists, but revert to the path if it doesn't (or vice versa)? Just thinking it may be easier for some users to specify the path directly rather than set up a global environmental variable.

Do you have example code of loading a global var into Python?

@robbibt
Copy link
Member

robbibt commented Aug 24, 2023

Perhaps something like this actually:

Use directory path if provided. If directory is None, try global variable

For backwards compatibility, we might also want to at least temporarily fall back to the global variable if a custom path is provided by directory but doesn't exist in the file system. That way the current default path should still work, but give us more flexibility outside of the DEA Sandbox or Docker.

Eventually, we can set the default path to None, so the global var becomes the default option.

@omad
Copy link
Contributor Author

omad commented Aug 24, 2023

I'm thinking something roughly like:

def model_tides(directory=None):
    """
    :directory: defaults to environment variable DEA_TOOLS_TIDE_MODELS if set, or /var/share/tide_models
    """
    if directory is None:
        if 'DEA_TOOLS_TIDE_MODELS' in os.environ:
            directory = os.environ['DEA_TOOLS_TIDE_MODELS']
        else:
            directory = '/var/share/tide_models'
      ...

@robbibt robbibt self-assigned this Aug 28, 2023
@robbibt
Copy link
Member

robbibt commented Aug 28, 2023

@omad That looks great, I have some planned updates to these funcs in progress, so I can easily make that change.

What would we need to do to get that environment variable set in the Sandbox?

@robbibt
Copy link
Member

robbibt commented Aug 28, 2023

I think this is what I'm going with:

    directory : string, optional
        The directory containing tide model data files. If no path is
        provided, this will default to the environment variable 
        "DEA_TOOLS_TIDE_MODELS" if set, otherwise "/var/share/tide_models".
        Tide modelling files should be stored in sub-folders for each
        model that match the structure provided by `pyTMD`:
        https://pytmd.readthedocs.io/en/latest/getting_started/Getting-Started.html#directories
        For example:
        - {directory}/fes2014/ocean_tide/
          {directory}/fes2014/load_tide/
        - {directory}/tpxo8_atlas/
        - {directory}/TPXO9_atlas_v5/
    # Set tide modelling files directory. If no custom path is provided, 
    # first try global environmental var, then "/var/share/tide_models"
    if directory is None:
        if "DEA_TOOLS_TIDE_MODELS" in os.environ:
            directory = os.environ["DEA_TOOLS_TIDE_MODELS"]
        else:
            directory = "/var/share/tide_models"

    # Verify path exists
    directory = pathlib.Path(directory).expanduser()
    if not directory.exists():
        raise FileNotFoundError("Invalid tide directory")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants