Skip to content

Commit

Permalink
removed prints from geo_json, and cleaned up some other outputter tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBarker-NOAA committed Jan 21, 2025
1 parent a4c744b commit 04b8a4c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 47 deletions.
7 changes: 2 additions & 5 deletions py_gnome/gnome/outputters/geo_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ def output_to_file(self, json_content, step_num):
filename = os.path.join(self.output_dir,
file_format.format(step_num))

print("output to file")
print(type(json_content))
with open(filename, 'w+', encoding='utf-8') as outfile:
dump(json_content, outfile, indent=4)

Expand Down Expand Up @@ -215,12 +213,11 @@ def _dataarray_p_types(self, data_array):
# self.clean_output_files()

def clean_output_files(self):
print("in clean_output_files")
if self.output_dir:
files = glob(os.path.join(self.output_dir, 'geojson_*.geojson'))

print("files are:")
print(files)
self.logger.debug("Cleaning output files: ")
self.logger.debug(files)

for f in files:
os.remove(f)
Expand Down
16 changes: 5 additions & 11 deletions py_gnome/gnome/outputters/kmz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
kmz outputter
"""

import os
# import os
from pathlib import Path
from datetime import timedelta, datetime
import zipfile
import base64
Expand Down Expand Up @@ -43,16 +44,9 @@ def __init__(self, filename, **kwargs):
uses super to pass optional ``**kwargs`` to base class ``__init__`` method
'''
# a little check:
# move check to prepare_for_model_run so don't get error loading save files
#self._check_filename(filename)

# strip off the .kml or .kmz
filename = filename[:-4] if filename.endswith(".kml") else filename
filename = filename[:-4] if filename.endswith(".kmz") else filename
filename += ".kmz"

self.kml_name = os.path.split(filename)[-1] + ".kml"
filename = Path(filename) # make sure it's a Path object
filename = filename.with_suffix(".kmz")
self.kml_name = filename.with_suffix(".kml").parts[-1]

super(KMZOutput, self).__init__(filename=filename,
**kwargs)
Expand Down
39 changes: 17 additions & 22 deletions py_gnome/tests/unit_tests/test_outputters/test_kmz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
'''

import os
from glob import glob
# from glob import glob
from datetime import datetime, timedelta
from pathlib import Path

import numpy as np
import pytest
from pytest import raises

from gnome.outputters import KMZOutput
from gnome.outputters import kmz_templates
Expand All @@ -24,13 +23,9 @@
# this is used by the output_filename fixture in conftest:
FILE_EXTENSION = ".kmz"


def local_dirname():
dirname = os.path.split(__file__)[0]
dirname = os.path.join(dirname, "output_kmz")
if not os.path.exists(dirname):
os.mkdir(dirname)
return dirname
HERE = Path(__file__).parent
OUTPUT_DIR = HERE / "output_kmz"
OUTPUT_DIR.mkdir(exist_ok=True)


@pytest.fixture(scope='function')
Expand All @@ -43,9 +38,9 @@ def model(sample_model, output_filename):
model.uncertain = True

model.spills += point_line_spill(2,
start_position=rel_start_pos,
release_time=model.start_time,
end_position=rel_end_pos)
start_position=rel_start_pos,
release_time=model.start_time,
end_position=rel_end_pos)

model.time_step = 3600
model.duration = timedelta(hours=1)
Expand All @@ -56,7 +51,7 @@ def model(sample_model, output_filename):

def test_init(output_dir):
'simple initialization passes'
kmz = KMZOutput(os.path.join(output_dir, 'test.kmz'))
kmz = KMZOutput(output_dir / 'test.kmz')

# check_filename now happens in prepare_for_model_run
# def test_init_exceptions():
Expand All @@ -78,29 +73,29 @@ def test_exceptions(output_filename):
kmz.rewind() # delete temporary files

# this test is now moot since kmz extension is added to the filename on init
# with raises(ValueError):
# with pytest.raises(ValueError):
# # must be filename, not dir name
# file_path = os.path.abspath(os.path.dirname(__file__))
# KMZOutput(file_path).prepare_for_model_run(datetime.now(), spill_pair)

with raises(ValueError):
with pytest.raises(ValueError):
file_path = 'invalid_path_to_file/file.kmz'
KMZOutput(file_path).prepare_for_model_run(datetime.now(), spill_pair)

with raises(TypeError):
with pytest.raises(TypeError):
# need to pass in model start time
kmz.prepare_for_model_run(num_time_steps=4)

with raises(TypeError):
with pytest.raises(TypeError):
# need to pass in model start time and spills
kmz.prepare_for_model_run()

with raises(ValueError):
with pytest.raises(ValueError):
# need a cache object
kmz.write_output(0)

# Maybe add ability to specify which data later on..
# with raises(ValueError):
# with pytest.raises(ValueError):
# kmz.which_data = 'some random string'

# changed renderer and netcdf ouputter to delete old files in
Expand All @@ -116,12 +111,12 @@ def test_exceptions(output_filename):
num_time_steps=4)


# with raises(AttributeError):
# with pytest.raises(AttributeError):
# 'cannot change after prepare_for_model_run has been called'
# kmz.which_data = 'most'

def test_timesteps(model):
filename = os.path.join(local_dirname(), "multi_timesteps.kml")
filename = OUTPUT_DIR / "multi_timesteps.kml"

kmz = KMZOutput(filename)
model.outputters += kmz
Expand Down
24 changes: 15 additions & 9 deletions py_gnome/tests/unit_tests/test_outputters/test_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import dateutil
import geopandas as gpd
import os
import pathlib
from pathlib import Path
import tempfile
import zipfile

Expand All @@ -22,6 +22,11 @@
# this is used by the output_filename fixture in conftest:
FILE_EXTENSION = ""

# fixme: there's also a output_dir fixture that should to the trick
HERE = Path(__file__).parent
OUTPUT_DIR = HERE / "output_shape"
OUTPUT_DIR.mkdir(exist_ok=True)


@pytest.fixture(scope='function')
def model(sample_model_fcn, output_filename):
Expand All @@ -47,7 +52,9 @@ def model(sample_model_fcn, output_filename):

def test_init(output_dir):
'simple initialization passes'
shp = ShapeOutput(os.path.join(output_dir, 'test'))
shp = ShapeOutput(output_dir / 'test')

assert isinstance(shp, ShapeOutput)


def test_init_filename_exceptions():
Expand Down Expand Up @@ -137,7 +144,7 @@ def get_shape_file_stats(filename):


def test_multi_timesteps(model, output_dir):
filename = os.path.join(output_dir, "multi_timesteps.zip")
filename = output_dir / "multi_timesteps.zip"

shp = ShapeOutput(filename, include_uncertain_boundary=True,
include_certain_boundary=True)
Expand Down Expand Up @@ -215,7 +222,7 @@ def test_singlestep_model_start(model, output_dir):
# set to False. Should just output single set of points for the model
# start time.
"""
filename = os.path.join(output_dir, "single_step_model_start.zip")
filename = output_dir / "single_step_model_start.zip"
# Just look at certain for this test...
model.uncertain = False
# model_step = timedelta(seconds=model.time_step)
Expand All @@ -241,7 +248,7 @@ def test_singlestep_model_start(model, output_dir):


def test_singlestep(model, output_dir):
filename = os.path.join(output_dir, "single_step.zip")
filename = output_dir / "single_step.zip"
# Just look at certain for this test...
model.uncertain = False
model_step = timedelta(seconds=model.time_step)
Expand Down Expand Up @@ -350,7 +357,7 @@ def test_singlestep(model, output_dir):


def test_nozip(model, output_dir):
filename = pathlib.Path(output_dir, "shapefile_no_zip.shp")
filename = output_dir / "shapefile_no_zip.shp"

shp = ShapeOutput(filename, zip_output=False)
model.outputters += shp
Expand Down Expand Up @@ -407,11 +414,10 @@ def test_timesteps2(model, output_dir):


@pytest.mark.xfail
# NOTE: This currently fails because the model isn't
# calling post_model_run after a failure -- it's just crashing out.
def test_model_stops_in_middle(model):
"""
NOTE: This currently fails because the model isn't
calling post_model_run after a failure -- it's just crashing out.
If the model stops in the middle of a run:
e.g. runs out of data, it should still output results.
"""
Expand Down

0 comments on commit 04b8a4c

Please sign in to comment.