Skip to content

Commit

Permalink
convert x, y, beta arrays to lists before serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
phobson committed Nov 15, 2018
1 parent 540f21e commit 5f6b772
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pygridgen/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,8 @@ def generate_grid(self):

def to_spec(self):
""" Export the grid-defining parameters into a JSON-like structure """
output_dict = {'xbry': self.xbry, 'ybry': self.ybry,
'beta': self.beta, 'shape': self.shape,
output_dict = {'xbry': self.xbry.tolist(), 'ybry': self.ybry.tolist(),
'beta': self.beta.tolist(), 'shape': self.shape,
'focus': self.focus.to_spec() if self.focus else None,
'ul_idx': self.ul_idx, 'proj': self.proj,
'nnodes': self.nnodes, 'precision': self.precision,
Expand Down
40 changes: 34 additions & 6 deletions pygridgen/tests/test_grid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from copy import deepcopy
import tempfile
import json
import os

import numpy

Expand Down Expand Up @@ -591,21 +594,46 @@ def test_mask_poylgon(grid_and_knowns):
nptest.assert_array_almost_equal(grid.mask_rho, known_mask_rho)


@pytest.mark.parametrize('use_focus', [True, False])
def test_gridgen_to_from_spec(use_focus):
@pytest.fixture
def simple_grid():
x = numpy.array([0.50, 2.00, 2.00, 3.50, 3.50, 2.00, 2.00, 0.50, 0.50])
y = numpy.array([0.50, 0.50, 1.75, 1.75, 2.25, 2.25, 3.50, 3.50, 0.50])
beta = numpy.array([1, 1, -1, 1, 1, -1, 1, 1, 0])

focus = None
return pygridgen.Gridgen(x, y, beta, shape=(20, 10), focus=focus)


@pytest.mark.parametrize('use_focus', [True, False])
def test_gridgen_to_from_spec(simple_grid, use_focus):
x = numpy.array([0.50, 2.00, 2.00, 3.50, 3.50, 2.00, 2.00, 0.50, 0.50])
y = numpy.array([0.50, 0.50, 1.75, 1.75, 2.25, 2.25, 3.50, 3.50, 0.50])
beta = numpy.array([1, 1, -1, 1, 1, -1, 1, 1, 0])

if use_focus:
focus = pygridgen.Focus()
focus.add_focus(0.50, 'y', factor=5, extent=0.25)
focus.add_focus(0.50, 'x', factor=5, extent=0.25)
simple_grid.focus = focus
simple_grid.generate_grid()

grid2 = pygridgen.grid.Gridgen.from_spec((simple_grid.to_spec()))

# testing - using almost equal due to rounding issues with floats
numpy.testing.assert_array_almost_equal(simple_grid.x, grid2.x)
numpy.testing.assert_array_almost_equal(simple_grid.y, grid2.y)


def test_gridgen_spec_valid_json(simple_grid):
with tempfile.TemporaryDirectory() as folder:
with open(os.path.join(folder, 'test.json'), 'w') as gridjson:
json.dump(simple_grid.to_spec(), gridjson)

with open(os.path.join(folder, 'test.json'), 'r') as gridjson:
spec = json.load(gridjson)

grid1 = pygridgen.Gridgen(x, y, beta, shape=(20, 10), focus=focus)
grid2 = pygridgen.grid.Gridgen.from_spec((grid1.to_spec()))
grid2 = pygridgen.grid.Gridgen.from_spec(spec)

# testing - using almost equal due to rounding issues with floats
numpy.testing.assert_array_almost_equal(grid1.x, grid2.x)
numpy.testing.assert_array_almost_equal(grid1.y, grid2.y)
numpy.testing.assert_array_almost_equal(simple_grid.x, grid2.x)
numpy.testing.assert_array_almost_equal(simple_grid.y, grid2.y)

0 comments on commit 5f6b772

Please sign in to comment.