tsutterley/pyTMD

Issue with Iterating Over Model Files in pyTMD.io.ATLAS.extract_constants()

Closed this issue · 2 comments

Description:
I have encountered an issue in the extract_constants function in pyTMD.io.ATLAS, specifically when using the 'TPXO9-atlas-v2' model for currents. In previous versions, the function iterated over a list of model files directly, while in the latest version, it iterates over the keys ("u" and "v") of a dictionary containing the model files. This change causes the function to incorrectly loop over the dictionary keys instead of the actual files.

Affected Code:
The relevant code snippets in the latest version looks like this:
# number of constituents nc = len(model_files)

for i, model_file in enumerate(model_files):

Proposed Fix:
I have found that these corrections fix this issue, at least in my specific case.
The loop should iterate over the list of model files corresponding to the specified type. The modified snippets should be:
# number of constituents nc = len(model_files[kwargs['type']])

for i, model_file in enumerate(model_files[kwargs['type']]):

These changes ensure that the function correctly calculates the number of constituents and iterates over the actual files associated with the specified type.

Steps to Reproduce:

  1. Use a 'TPXO9-atlas-v2' currents model.
    model = pyTMD.io.model(tpxodirectory).current('TPXO9-atlas-v2')
  2. Call the extract_constants function, specifying 'u' or 'v' as the type.
    (amplitude, phase, D, constituents) = extract_constants(ilon, ilat, model.grid_file, model.model_file, type="u")

Expected Result:
The function should iterate over the list of model files for the specified type.

Actual Result:
The function currently iterates over the dictionary keys, causing a FileNotFoundError.

Additional Information:

  • pyTMD version: 2.0.8
  • Model: 'TPXO9-atlas-v2' currents
  • Type: 'u' or 'v'

Thank you for your attention to this matter.

Best regards,
Mirko.

Hi @mirkorupani,
I am reverting some changes I made in April for the TPXO9-atlas currents in #259. I think this fixes the issue in the case where the model files are netCDF4. I only have the OTIS-formatted versions of the atlas current files, so I'm not 100%. model.model_file being a dictionary is purposeful. I think the changes you'd have to make are to put the extraction function in a for loop.

for t in model.type:
    amplitude, phase, D, constituents = pyTMD.io.ATLAS.extract_constants(
        ilon, ilat, model.grid_file, model.model_file[t], type=t,
        scale=model.scale)

Let me know if this fixes the issue.

Changes are now merged into main. Feel free to reopen this issue if need be :)