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

Should all grid attributes potentially found in files loaded by a Run object be required to be accessible to Model objects? #187

Closed
spencerkclark opened this issue Jun 2, 2017 · 2 comments
Milestone

Comments

@spencerkclark
Copy link
Collaborator

spencerkclark commented Jun 2, 2017

Currently if a grid attribute that can be found in a Run object cannot be found in a Model object, aospy crashes, because numpy.allclose cannot compare an array and None. (For example if zsurf is represented in a file loaded in by a Run object, but it does not exist in any of the Model grid files).

In [5]: import numpy as np

In [6]: np.allclose(np.array([1, 2, 3]), None)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-d64ad5a1c13a> in <module>()
----> 1 np.allclose(np.array([1, 2, 3]), None)

//anaconda/envs/xarray-dev/lib/python2.7/site-packages/numpy/core/numeric.pyc in allclose(a, b, rtol, atol, equal_nan)
   2370
   2371     """
-> 2372     res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
   2373     return bool(res)
   2374

//anaconda/envs/xarray-dev/lib/python2.7/site-packages/numpy/core/numeric.pyc in isclose(a, b, rtol, atol, equal_nan)
   2449
   2450     xfin = isfinite(x)
-> 2451     yfin = isfinite(y)
   2452     if all(xfin) and all(yfin):
   2453         return within_tol(x, y, atol, rtol)

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

I don't think this should be the desired behavior. I don't see any reason why one should not use a coordinate that is defined in data read in using a Run object that doesn't exist in the Model object.

A simple fix would be to just add an additional condition to line 278 in aospy.calc._add_grid_attributes to make sure that model_attr is not None:

def _add_grid_attributes(self, ds):
    """Add model grid attributes to a dataset"""
    for name_int, names_ext in self._grid_attrs.items():
        ds_coord_name = set(names_ext).intersection(set(ds.coords) |
                                                    set(ds.data_vars))
        model_attr = getattr(self.model, name_int, None)
        if ds_coord_name and (model_attr is not None):
...

@spencerahill what do you think? This came up in adapting aospy to use WRF output. I know historically we both have provided sample data files to each Model object we use (so this hasn't come up before), but now that we have enabled preprocessing functions (which do not get called on Model grid files, and I think this should remain this way) that often change or add coordinates, providing a sample data file to the Model object to fill out its grid attributes doesn't always make sense.

@spencerahill
Copy link
Owner

I don't see any reason why one should not use a coordinate that is defined in data read in using a Run object that doesn't exist in the Model object.

Totally agree. And your suggested fix looks good to me.

Now that we have enabled preprocessing functions (which do not get called on Model grid files, and I think this should remain this way) that often change or add coordinates, providing a sample data file to the Model object to fill out its grid attributes doesn't always make sense.

I agree. This raises the question of why have model grid files and attributes at all. But they are in fact useful, e.g. to remove miniscule coordinate value differences across runs.

@spencerkclark
Copy link
Collaborator Author

This raises the question of why have model grid files and attributes at all. But they are in fact useful, e.g. to remove miniscule coordinate value differences across runs.

I fully agree. It's also quite helpful to have in the case of grid attributes that cannot be found in output files that contain variables (like the atmos.static.nc output of GFDL models or a custom file containing surface area for the case of this WRF example).

@spencerahill spencerahill added this to the v0.2 milestone Aug 31, 2017
spencerkclark added a commit to spencerkclark/aospy that referenced this issue Sep 1, 2017
Fixes spencerahill#187

Previously an error would be raised if a grid attribute found in a Run
object did not exist in the Run's corresponding Model.  This allows
aospy to use grid attributes found only in Run objects.
spencerahill pushed a commit that referenced this issue Sep 22, 2017
…199)

(Fixes #187) Previously an error would be raised if a grid attribute found in a Run
object did not exist in the Run's corresponding Model.  This allows
aospy to use grid attributes found only in Run objects.
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

No branches or pull requests

2 participants