Skip to content

Fix io bug with MAS model#112

Open
MohamedNedal wants to merge 1 commit into
predsci:mainfrom
MohamedNedal:main
Open

Fix io bug with MAS model#112
MohamedNedal wants to merge 1 commit into
predsci:mainfrom
MohamedNedal:main

Conversation

@MohamedNedal

Copy link
Copy Markdown

In the recent version, there was a problem with importing MAS data files:

---> 14 mas_output = MASOutput(filename)
     15 rho = mas_output['rho']

File ~/.conda/envs/psp/lib/python3.9/site-packages/psipy/model/base.py:46, in ModelOutput.__init__(self, path)
     43 # Leave data empty for now, as we want to load on demand
     44 # This is a mapping from variable name to xr.DataSet
     45 self._data = {}
---> 46 self._variables = self.get_variables()
     47 self._variables.sort()

File ~/.conda/envs/psp/lib/python3.9/site-packages/psipy/model/mas.py:74, in MASOutput.get_variables(self)
     73 def get_variables(self):
---> 74     return get_mas_variables(self.path)

File ~/.conda/envs/psp/lib/python3.9/site-packages/psipy/io/mas.py:125, in get_mas_variables(path)
    123 var_names = [Path(f).stem.split(".")[0][:-3] for f in files]
    124 if not len(var_names):
--> 125     raise FileNotFoundError(f"No variable files found in {path}")
    126 # Use list(set()) to get unique values
    127 return list(set(var_names))

FileNotFoundError: No variable files found in rho002.hdf

I've modified the get_mas_variables function to be:

def get_mas_variables(path):
    """
    Return a list of variables present in a given directory.

    Parameters
    ----------
    path :
        Path to the folder containing the MAS data files.

    Returns
    -------
    var_names : list
        List of variable names present in the given directory.
    """
    path = Path(path)  # Convert path to a Path object
    files = glob.glob(str(path / "*[0-9][0-9][0-9].*"))
    # Get the variable name from the filename
    # Here we take the filename before .hdf, and remove the last three
    # characters which give the timestep
    var_names = [Path(f).stem.split(".")[0][:-3] for f in files]
    if not len(var_names):
        raise FileNotFoundError(f"No variable files found in {path}")
    # Use list(set()) to get unique values
    return list(set(var_names))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant