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

Added fix_inconsistent_variables #23

Merged
merged 4 commits into from
Dec 15, 2016

Conversation

braaannigan
Copy link
Contributor

I added the function fix_inconsistent_variables to add the extra row onto drC. This function is called after the data is read from the mds files but before the data is read into the xarray dataset. Trying to change the drC array once it is in the xarray dataset causes problems, because xarray generates an error because the shape is different to what it expects.

I also changed read_grid=False to True on Line 165, as this seems to override the choice made when the user calls the function and so the grid wasn't being read. I'm not sure what this should be in general.

@braaannigan
Copy link
Contributor Author

The indent on the new fix_inconsistent_variables was wrong in my first commit, I've modified this now

@codecov-io
Copy link

codecov-io commented Dec 15, 2016

Current coverage is 91.44% (diff: 100%)

Merging #23 into master will increase coverage by 0.14%

@@             master        #23   diff @@
==========================================
  Files             4          4          
  Lines           529        538     +9   
  Methods           0          0          
  Messages          0          0          
  Branches        118        120     +2   
==========================================
+ Hits            483        492     +9   
  Misses           31         31          
  Partials         15         15          

Powered by Codecov. Last update 21be463...2739dfe

@braaannigan
Copy link
Contributor Author

It's working now for both the test case and my own experiment now. It's great!

Copy link
Member

@rabernat rabernat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@braaannigan this is fantastic! Thanks so much for getting this started.

I have a few minor suggestions for how I would prefer this work.

Also, we need a test of this new feature. You will have to add a new function to test/test_mds_store.py which simulates the situation you have encountered with your old version of mitgcm. (The easiest way to do this would probably be just to copy DRF.meta|data to DRC.meta|data in order to mock a data file of the correct length).

@@ -153,7 +153,7 @@ def open_mdsdataset(data_dir, grid_dir=None,
nx=nx, ny=ny, nz=nz)
datasets = [open_mdsdataset(

data_dir, iters=iternum, read_grid=False, **kwargs)
data_dir, iters=iternum, read_grid=True, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this back? If you look more carefully at the code path, you will see that this is just part of the logic for how the dataset is put together. If read_grid is passed as True to open_mdsdataset, the function calls itself recursively. There are extensive tests (example to ensure that the options work properly. I am surprised this change didn't cause an error somewhere.

#In some older versions of mitgcm len(drC)=nr rather than nr+1
#Define a function to change it to the newer format if len(drC)=nr
#and apply it to the data before it is added to the xarray variable below
def fix_inconsistent_variables(data):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think you could make this function have the signature?

vname, dims, data, attrs = self._fix_inconsistent_variables(vname, dims, data, attrs)

Then you could do the check if vname=='drC' inside that function. (If false, you just return vname, dims, data, attrs with no modifications.) I would prefer this because it will keep the code path within __init__ much cleaner, and it will also generalize in case we have to do more such hacking on other variables.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also be moved outside of __init__ and become its own method of the _MDSDataStore class.

@braaannigan
Copy link
Contributor Author

Made those changes, it's still working for me

@rabernat
Copy link
Member

@braaannigan, just curious, do you know how to run the test suite yourself?

py.test -v xmitgcm

Can be useful for development.

Copy link
Member

@rabernat rabernat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks good, but it still needs a test.

@braaannigan
Copy link
Contributor Author

Hi @rabernat, this is all new to me!

In test/test_mds_store.py, what exactly needs to be added? Is it adding a new experiment to the dictionary on L60 with similar properties to global_oce_latlon?

Also, I'm going hiking for a few days, so I'll probably be offline from about 8am tomorrow until Tuesday. Hopefully we can get this sorted before then

@rabernat
Copy link
Member

Hi @rabernat, this is all new to me!

Yes, it was new to me a few years ago too. Once you start testing, you will never go back. 😉

Add a function to test_mds_store.py that goes something like this

def test_drc_length(all_mds_datadirs):
    dirname, _ = all_mds_datadirs
    copyfile(os.path.join(dirname, 'DRF.data'), os.path.join(dirname, 'DRC.data'))
    copyfile(os.path.join(dirname, 'DRF.meta'), os.path.join(dirname, 'DRC.meta'))
    ds = xmitgcm.open_mdsdataset(
                dirname, iters=None, read_grid=True,
                geometry=expected['geometry'])
    assert len(ds.drC)==(len(ds.drF)+1)

You will also have to add from shutil import copyfile at the top of test_mds_store.py.

@braaannigan
Copy link
Contributor Author

braaannigan commented Dec 15, 2016 via email

@rabernat
Copy link
Member

Awesome! Your test (along with all the others) was run by travis CI on this pull request, and it passed. It will continue to be run every time someone makes a new pull request. This is how we ensure we don't break things as the package evolves.

You can also see that the code coverage checks also passed. This means that the new code you added also came with sufficient tests.

@rabernat rabernat merged commit a5ee7df into MITgcm:master Dec 15, 2016
@braaannigan
Copy link
Contributor Author

Cheers Ryan, thanks for your help!

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.

3 participants