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

Read mitgrid files #114

Merged
merged 21 commits into from
Jan 15, 2019
Merged

Read mitgrid files #114

merged 21 commits into from
Jan 15, 2019

Conversation

raphaeldussin
Copy link
Contributor

fixes #14

tested with llc90 and aste. there is room to add cube sphere support once #91 is resolved
and it would be also easy to make it work with simpler geometries, although I have not seen
yet the case of users using input grid files in those.

gridfile must contain <NFACET> as wildcard (e.g. tile<NFACET>.mitgrid)
nx: int
size of the face in the x direction
ny: int
Copy link
Contributor

Choose a reason for hiding this comment

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

E501 line too long (81 > 79 characters)

xmitgcm/utils.py Outdated
# create placeholders for data
gridfields= {}
for field in file_metadata['fldList']:
gridfields.update({field: np.zeros(shape_out)})
Copy link
Contributor

Choose a reason for hiding this comment

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

F841 local variable 'shape' is assigned to but never used

nygrid = file_metadata['nx'] + 1

grid_metadata.update({'nx': nxgrid, 'ny': nygrid,
'has_faces': False})
Copy link
Contributor

Choose a reason for hiding this comment

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

E501 line too long (80 > 79 characters)

gridfile: str
gridfile must contain <NFACET> as wildcard (e.g. tile<NFACET>.mitgrid)
nx: int
size of the face in the x direction
Copy link
Contributor

Choose a reason for hiding this comment

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

E501 line too long (81 > 79 characters)


# create placeholders for data
gridfields = {}
for field in file_metadata['fldList']:
Copy link
Contributor

Choose a reason for hiding this comment

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

F841 local variable 'shape' is assigned to but never used

nxgrid = file_metadata['ny_facets'][kfacet] + 1
nygrid = file_metadata['nx'] + 1

grid_metadata.update({'nx': nxgrid, 'ny': nygrid,
Copy link
Contributor

Choose a reason for hiding this comment

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

E501 line too long (80 > 79 characters)

@codecov-io
Copy link

codecov-io commented Jan 6, 2019

Codecov Report

Merging #114 into master will increase coverage by 1.3%.
The diff coverage is 98.33%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #114     +/-   ##
=========================================
+ Coverage   92.28%   93.59%   +1.3%     
=========================================
  Files           4        4             
  Lines         830      890     +60     
  Branches      190      203     +13     
=========================================
+ Hits          766      833     +67     
+ Misses         38       32      -6     
+ Partials       26       25      -1
Impacted Files Coverage Δ
xmitgcm/utils.py 94.85% <98.33%> (+2.6%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 421c4c5...9be3655. Read the comment docs.

gridfile: str
gridfile must contain <NFACET> as wildcard (e.g. tile<NFACET>.mitgrid)
nx: int
size of the face in the x direction
Copy link
Contributor

Choose a reason for hiding this comment

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

W291 trailing whitespace

@rabernat
Copy link
Member

rabernat commented Jan 8, 2019

This looks very useful!

Tests and documentation are necessary.

xmitgcm/utils.py Outdated
'RAS': (['face', 'j_g', 'i'], gridfields['RAS']),
'DXG': (['face', 'j_g', 'i'], gridfields['DXG']),
'DYG': (['face', 'j', 'i_g'], gridfields['DYG'])
})
Copy link
Member

Choose a reason for hiding this comment

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

This dataset is missing the coordinates for j and i. In a regular xmitgcm dataset, these variables have explicit coordinates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


for field in file_metadata['fldList']:
# symetrize
tmp = rawfields[field][:, :, :-1, :-1].squeeze()
Copy link
Member

Choose a reason for hiding this comment

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

It occurs to me that we are potentially throwing away some useful information here. One thing that I dislike about MDS output is that it throws away the outer cell face point. Might be worth having an option to keep those outer elements for variables on the u and v points.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's all zeros so no worries.

import numpy as np
all = np.fromfile('tile003.mitgrid','>d')
XC = all[:91*91]
XC = np.reshape(XC,(91,91))

In [14]: XC[-1,:]
Out[14]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.])

In [15]: XC[:,-1]
Out[15]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.])

xmitgcm/utils.py Outdated
if file_metadata['transpose_face'][face]:
dataface = dataface.transpose()
# assign values
gridfields[field][face, :, :] = dataface
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this same logic already live elsewhere in xmitgcm? If so, it's probably better to re-call that function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't seen anywhere in xmitgcm functions working on multi-files but I can have missed it.

Copy link
Member

Choose a reason for hiding this comment

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

Does this work with dask?

xmitgcm/utils.py Outdated
grid_metadata.update({'nx': nxgrid, 'ny': nygrid,
'has_faces': False})

raw = read_all_variables(grid_metadata['vars'], grid_metadata)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a dask option here?

I'm thinking about what will happen if I run this on the LLC4320 mitgrid files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

use_dask=False,
extra_metadata=md)
assert type(ds) == xarray.Dataset
assert ds['XC'].values.shape == expected['shape']
Copy link
Contributor

Choose a reason for hiding this comment

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

F405 'xarray' may be undefined, or defined from star imports: xmitgcm.test.test_xmitgcm_common

Copy link
Member

Choose a reason for hiding this comment

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

maybe add a check here that all the expected variables are in ds?

Copy link
Member

Choose a reason for hiding this comment

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

Also need to check that use_dask works as expected and doesn't trigger eagerreading of the data.

xmitgcm/test/test_xmitgcm_common.py Outdated Show resolved Hide resolved
precision='double', endian='>',
use_dask=False,
extra_metadata=md)
assert type(ds) == xarray.Dataset
Copy link
Contributor

Choose a reason for hiding this comment

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

F405 'xarray' may be undefined, or defined from star imports: xmitgcm.test.test_xmitgcm_common

@raphaeldussin
Copy link
Contributor Author

archive for grid tests are on figshare. Build will work when #115 is merged


# passing llc without metadata should fail
if expected['geometry'] == 'llc':
with pytest.raises(ValueError):
Copy link
Contributor

Choose a reason for hiding this comment

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

F405 'pytest' may be undefined, or defined from star imports: xmitgcm.test.test_xmitgcm_common

xmitgcm/test/test_utils.py Outdated Show resolved Hide resolved
xmitgcm/test/test_utils.py Outdated Show resolved Hide resolved
xmitgcm/test/test_utils.py Outdated Show resolved Hide resolved
xmitgcm/test/test_utils.py Outdated Show resolved Hide resolved
xmitgcm/test/test_utils.py Outdated Show resolved Hide resolved
xmitgcm/test/test_utils.py Outdated Show resolved Hide resolved
xmitgcm/test/test_utils.py Outdated Show resolved Hide resolved
xmitgcm/test/test_utils.py Outdated Show resolved Hide resolved
xmitgcm/test/test_utils.py Show resolved Hide resolved
@raphaeldussin
Copy link
Contributor Author

@rabernat strickler-ci is really nitpicking, can we merge this anyway? I will fill in the blanks with the cube sphere grid in the cs branch. Not very orthogonal here I'm sorry!

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.

Looking good. Just want to verify that the use_dask option actually works as described. So that needs a test.

use_dask=False,
extra_metadata=md)
assert type(ds) == xarray.Dataset
assert ds['XC'].values.shape == expected['shape']
Copy link
Member

Choose a reason for hiding this comment

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

maybe add a check here that all the expected variables are in ds?

xmitgcm/utils.py Outdated
gridfields.update({field: np.zeros(shape)})

if geometry == 'llc':
for kfacet in np.arange(nfacets):
Copy link
Member

Choose a reason for hiding this comment

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

range(nfacets) would be better here

xmitgcm/utils.py Outdated
if file_metadata['transpose_face'][face]:
dataface = dataface.transpose()
# assign values
gridfields[field][face, :, :] = dataface
Copy link
Member

Choose a reason for hiding this comment

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

Does this work with dask?

use_dask=False,
extra_metadata=md)
assert type(ds) == xarray.Dataset
assert ds['XC'].values.shape == expected['shape']
Copy link
Member

Choose a reason for hiding this comment

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

Also need to check that use_dask works as expected and doesn't trigger eagerreading of the data.

@@ -843,3 +844,39 @@ def test_get_extra_metadata(domain, nx):

with pytest.raises(ValueError):
em = get_extra_metadata(domain='notinlist', nx=nx)


@pytest.mark.parametrize("usedask", [True, False])
Copy link
Contributor

Choose a reason for hiding this comment

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

F405 'pytest' may be undefined, or defined from star imports: xmitgcm.test.test_xmitgcm_common

# create placeholders for data
gridfields = {}
for field in file_metadata['fldList']:
gridfields.update({field: None})
Copy link
Contributor

Choose a reason for hiding this comment

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

F841 local variable 'shape' is assigned to but never used

gridfields = {}
for field in file_metadata['fldList']:
gridfields.update({field: None})

Copy link
Contributor

Choose a reason for hiding this comment

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

F841 local variable 'chunks' is assigned to but never used

@raphaeldussin
Copy link
Contributor Author

@rabernat dask should work with global llc grids (not regional because of the padding), the dsa.concatenate on l1260 in utils.py makes the results being a dask array wether the input is a dask or numpy array so I can't test for the type in the output.

@rabernat
Copy link
Member

dsa.concatenate on l1260 in utils.py makes the results being a dask array wether the input is a dask or numpy array so I can't test for the type in the output.

Ok, then maybe we can come back to this in a future PR. I would prefer to use a version of concatenate that returns numpy arrays for numpy inputs. We have something like that in xgcm:
https://github.com/xgcm/xgcm/blob/master/xgcm/duck_array_ops.py#L37

@rabernat rabernat merged commit 8a6cf12 into MITgcm:master Jan 15, 2019
@raphaeldussin raphaeldussin deleted the read_grid branch January 15, 2019 16:11
fraserwg pushed a commit to fraserwg/xmitgcm that referenced this pull request Nov 23, 2021
* start working on reading grid

* read ext grid

* works for llc

* works with aste+docstring

* Fixing style errors.

* fix cosmetics

* fix cosmetics

* fixes

* Fixing style errors.

* testing

* Fixing style errors.

* fix strickler

* add link/md5

* fix strickler

* more testing

* Fixing style errors.

* fix strickler

* fix strickler

* make dask lazy

* Fixing style errors.

* fix from strickler
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.

read mitgrid files
4 participants