Skip to content

Commit

Permalink
handle to/from_spec cases where there is no focus
Browse files Browse the repository at this point in the history
  • Loading branch information
phobson committed Nov 14, 2018
1 parent 4717e43 commit 8e1cf58
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 9 additions & 2 deletions pygridgen/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,18 @@ def __call__(self, x, y):
return x, y

def to_spec(self):
""" Export to the defining properties of the focus to a JSON-like
structure
"""
output_spec = []
for focuspoint in self._focuspoints:
output_spec.append(focuspoint.to_dict())
return output_spec

@classmethod
def from_spec(cls, foci):
""" Create a new focus object from a JSON-like structure
"""
f = cls()
for focuspoint in foci:
f.add_focus(**focuspoint)
Expand Down Expand Up @@ -986,9 +991,10 @@ def generate_grid(self):
super(Gridgen, self).__init__(x, y)

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,
'focus': self.focus.to_spec(),
'focus': self.focus.to_spec() if self.focus else None,
'ul_idx': self.ul_idx, 'proj': self.proj,
'nnodes': self.nnodes, 'precision': self.precision,
'nppe': self.nppe, 'newton': self.newton,
Expand All @@ -998,8 +1004,9 @@ def to_spec(self):

@classmethod
def from_spec(cls, attributes):
""" Create a new grid from a JSON-like data structure """
focus_spec = attributes.pop('focus', None)
focus = Focus.from_spec(focus_spec)
focus = Focus.from_spec(focus_spec) if focus_spec else None
return cls(focus=focus, **attributes)


Expand Down
11 changes: 7 additions & 4 deletions pygridgen/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,14 +591,17 @@ def test_mask_poylgon(grid_and_knowns):
nptest.assert_array_almost_equal(grid.mask_rho, known_mask_rho)


def test_gridgen_to_from_spec():
@pytest.mark.parametrize('use_focus', [True, False])
def test_gridgen_to_from_spec(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])

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)
focus = None
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)

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

0 comments on commit 8e1cf58

Please sign in to comment.