def setUp(self):
        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)
        self.log.logger.handlers.append(logging.StreamHandler())
        self.log.logger.info(self.id())

        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2014, 1, 2, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        output_dir = "/data/lm/tests/output"
        test_dir = self.id().split('.')[-1]
        self.output_path = os.path.join(output_dir, test_dir)
        if not os.path.isdir(self.output_path):
            os.makedirs(self.output_path)
        self.output_formats = [ex.H5Trackline]

        cache_dir = "/data/lm/tests/cache"
        self.cache_path = os.path.join(cache_dir, test_dir, 'cache.nc')

        self.bathy_file = "/data/lm/bathy/global/ETOPO1_Bed_g_gmt4.grd"

        self.shoreline_path = "/data/lm/shore"
    def setUp(self):
        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)
        self.log.logger.handlers.append(logging.StreamHandler())
        self.log.logger.info(self.id())

        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2014, 1, 2, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        output_dir = "/data/lm/tests/output"
        test_dir = self.id().split('.')[-1]
        self.output_path = os.path.join(output_dir, test_dir)
        if not os.path.isdir(self.output_path):
            os.makedirs(self.output_path)
        self.output_formats = [ex.H5GDALShapefile, ex.H5Trackline]

        cache_dir = "/data/lm/tests/cache"
        self.cache_path = os.path.join(cache_dir, test_dir, 'cache.nc')

        self.bathy_file = "/data/lm/bathy/global/ETOPO1_Bed_g_gmt4.grd"

        self.shoreline_path = "/data/lm/shore"
    def setUp(self):
        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2012, 8, 1, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)

        self.output_path = "/data/lm/output"
        self.cache_path = "/data/lm/cache"
        self.bathy_file = "/data/lm/bathy/ETOPO1_Bed_g_gmt4.grd"
        self.shoreline_path = "/data/lm/shore"
    def setUp(self):
        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2012, 8, 1, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)

        self.output_path = "/data/lm/output"
        self.cache_path = "/data/lm/cache"
        self.bathy_file = "/data/lm/bathy/ETOPO1_Bed_g_gmt4.grd"
        self.shoreline_path = "/data/lm/shore"
    def setUp(self):
        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)
        self.log.logger.info("**************************************")
        self.log.logger.info(self.id())

        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2014, 1, 2, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        output_dir = "/data/lm/tests/output"
        self.output_path = os.path.join(output_dir, self.id())

        cache_dir = "/data/lm/tests/cache"
        self.cache_path = os.path.join(cache_dir, self.id())

        self.bathy_file = "/data/lm/bathy/ETOPO1_Bed_g_gmt4.grd"

        self.shoreline_path = "/data/lm/shore"
class LarvalBehaviourTest(unittest.TestCase):

    def setUp(self):
        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)
        self.log.logger.handlers.append(logging.StreamHandler())
        self.log.logger.info(self.id())

        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2014, 1, 2, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        output_dir = "/data/lm/tests/output"
        test_dir = self.id().split('.')[-1]
        self.output_path = os.path.join(output_dir, test_dir)
        if not os.path.isdir(self.output_path):
            os.makedirs(self.output_path)
        self.output_formats = [ex.H5GDALShapefile, ex.H5Trackline]

        cache_dir = "/data/lm/tests/cache"
        self.cache_path = os.path.join(cache_dir, test_dir, 'cache.nc')

        self.bathy_file = "/data/lm/bathy/global/ETOPO1_Bed_g_gmt4.grd"

        self.shoreline_path = "/data/lm/shore"

    def tearDown(self):
        self.log.close()

    def draw_trackline(self, geojson_file):
        with fiona.open(geojson_file) as src:
            self.log.logger.info((gj2ascii.render(src, 100, fill='.', char='o', bbox=shape(src.next()['geometry']).buffer(0.1).envelope.bounds)))

    def test_behavior_growth_and_settlement(self):
        # 6 days
        num_steps = 144
        num_particles = 2

        # Behavior
        behavior_config = open(os.path.normpath(os.path.join(os.path.dirname(__file__), "./resources/files/behavior_for_run_testing.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = CachingModelController(
            latitude=self.start_lat,
            longitude=self.start_lon,
            depth=self.start_depth,
            start=self.start_time,
            step=self.time_step,
            nstep=num_steps,
            npart=num_particles,
            models=models,
            use_bathymetry=True,
            use_shoreline=True,
            time_chunk=24,
            horiz_chunk=4,
            time_method='nearest',
            bathy_path=self.bathy_file)

        model.setup_run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=self.cache_path)
        model.run(output_formats=self.output_formats, output_path=self.output_path)
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

    def test_quick_settlement(self):
        num_steps = 24
        num_particles = 4

        # Behavior
        behavior_config = open(os.path.normpath(os.path.join(os.path.dirname(__file__), "./resources/files/behavior_quick_settle.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = CachingModelController(
            latitude=self.start_lat,
            longitude=self.start_lon,
            depth=self.start_depth,
            start=self.start_time,
            step=self.time_step,
            nstep=num_steps,
            npart=num_particles,
            models=models,
            use_bathymetry=True,
            use_shoreline=True,
            time_chunk=12,
            horiz_chunk=2,
            time_method='nearest',
            bathy_path=self.bathy_file)

        model.setup_run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_DAS.nc", cache=self.cache_path)
        model.run(output_path=self.output_path, output_formats=self.output_formats)
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

    @unittest.skip("Lost behavior file")
    def test_kayak_island(self):
        # 6 days
        num_steps = 1632
        num_particles = 100
        time_step = 3600

        behavior_config = json.loads(urlopen("http://behaviors.larvamap.asascience.com/library/50ef1bb1cc7b61000700001d.json").read())
        lb = LarvaBehavior(data=behavior_config[u'results'][0])

        models = [Transport(horizDisp=0.01, vertDisp=0.001)]
        models.append(lb)

        start_time = datetime(2011, 5, 2, 00, tzinfo=pytz.utc)
        start_lat = 59.93517413488866
        start_lon = -144.496213677788
        depth = -1

        shoreline_path = os.path.join(self.shoreline_path, "westcoast", "New_Land_Clean.shp")

        model = CachingModelController(
            latitude=start_lat,
            longitude=start_lon,
            depth=depth,
            start=start_time,
            step=time_step,
            nstep=num_steps,
            npart=num_particles,
            models=models,
            use_bathymetry=True,
            use_shoreline=True,
            time_chunk=24,
            horiz_chunk=5,
            time_method='interp',
            shoreline_path=shoreline_path,
            bathy_path=self.bathy_file)

        model.setup_run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L1_FCST.nc", cache=self.cache_path)
        model.run(output_path=self.output_path, output_formats=self.output_formats)
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

    @unittest.skip("Lost behavior file")
    def test_sheep_bay(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: Sheep Bay")

        # 6 days
        num_steps = 1632

        num_particles = 100

        time_step = 3600

        behavior_config = json.loads(urlopen("http://behaviors.larvamap.asascience.com/library/50ef1bb1cc7b61000700001d.json").read())
        lb = LarvaBehavior(data=behavior_config[u'results'][0])

        models = [Transport(horizDisp=0.01, vertDisp=0.001)]
        models.append(lb)

        start_time = datetime(2011, 5, 2, 00, tzinfo=pytz.utc)

        start_lat = 60.60899655733162
        start_lon = -145.97402533055956
        depth = -1

        shoreline_path = os.path.join(self.shoreline_path, "westcoast", "New_Land_Clean.shp")

        model = CachingModelController(
            latitude=start_lat,
            longitude=start_lon,
            depth=depth,
            start=start_time,
            step=time_step,
            nstep=num_steps,
            npart=num_particles,
            models=models,
            use_bathymetry=True,
            use_shoreline=True,
            time_chunk=24,
            horiz_chunk=5,
            time_method='interp',
            shoreline_path=shoreline_path,
            bathy_path=self.bathy_file)

        model.setup_run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=self.cache_path)
        model.run(output_path=self.output_path, output_formats=self.output_formats)
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

    def test_diel_migration(self):
        num_steps = 168
        num_particles = 4
        start_time = datetime(2013, 4, 1, 0, tzinfo=pytz.utc)

        # Behavior
        behavior_config = open(os.path.normpath(os.path.join(os.path.dirname(__file__), "./resources/files/diel_suncycles.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = CachingModelController(
            latitude=60.68,
            longitude=-146.42,
            depth=self.start_depth,
            start=start_time,
            step=self.time_step,
            nstep=num_steps,
            npart=num_particles,
            models=models,
            use_bathymetry=True,
            use_shoreline=True,
            time_chunk=24,
            horiz_chunk=2,
            time_method='nearest',
            bathy_path=self.bathy_file)

        model.setup_run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_DAS.nc", cache=self.cache_path)
        model.run(output_path=self.output_path, output_formats=self.output_formats)
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))
class CachingModelControllerTest(unittest.TestCase):

    def setUp(self):
        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)
        self.log.logger.info("**************************************")
        self.log.logger.info(self.id())

        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2014, 1, 2, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        output_dir = "/data/lm/tests/output"
        self.output_path = os.path.join(output_dir, self.id())

        cache_dir = "/data/lm/tests/cache"
        self.cache_path = os.path.join(cache_dir, self.id())

        self.bathy_file = "/data/lm/bathy/ETOPO1_Bed_g_gmt4.grd"

        self.shoreline_path = "/data/lm/shore"

    def tearDown(self):
        self.log.logger.info("**************************************")
        self.log.close()

    def test_run_from_multiple_files_with_cache(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = CachingModelController(geometry=p,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       se_shoreline=False,
                                       time_chunk=10,
                                       horiz_chunk=4,
                                       bathy_path=self.bathy_file)

        particles = model.run("/data/lm/tests/pws_das_2014*.nc", output_formats = ['NetCDF'], output_path=self.output_path, cache_path=self.cache_path, remove_cache=False)
        self.assertEquals(len(particles), self.num_particles)
        self.assertTrue(os.path.exists(self.cache_path))
        os.remove(self.cache_path)
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_run_from_multiple_files_without_cache_on_ipy_cluster(self):
        try:
            from IPython.parallel import Client
            client = Client()

            pool = client.load_balanced_view()
        except:
            raise unittest.SkipTest("Cluster connection failed")

        self.test_run_from_multiple_files_without_cache(pool=pool)

    def test_run_from_multiple_files_without_cache(self, pool=None):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    pool=pool)

        particles = model.run("/data/lm/tests/pws_das_2014*.nc", output_formats = ['NetCDF', 'trackline'], output_path=self.output_path)
        self.assertEquals(len(particles), self.num_particles)
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_run_from_dap_with_cache(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = CachingModelController(geometry=p,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=200,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False,
                                       time_chunk=24,
                                       horiz_chunk=4)

        particles = model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", output_formats = ['NetCDF'], output_path=self.output_path, cache_path=self.cache_path, remove_cache=False)
        self.assertEquals(len(particles), self.num_particles)
        self.assertTrue(os.path.exists(self.cache_path))
        os.remove(self.cache_path)
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_run_from_dap_without_cache(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = CachingModelController(geometry=p,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False)

        particles = model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", output_formats = ['NetCDF'], output_path=self.output_path)
        self.assertEquals(len(particles), self.num_particles)
        # We didn't pass remove_cache=False, so it should have been removed by the CachingModelController.
        self.assertFalse(os.path.exists(self.cache_path))
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_run_from_polygon(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat).buffer(0.001)

        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=datetime(2014, 1, 2, 0),
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    time_chunk=10,
                                    horiz_chunk=4)

        particles = model.run("/data/lm/tests/pws_das_2014*.nc", output_formats=['NetCDF'], output_path=self.output_path)
        self.assertEquals(len(particles), self.num_particles)
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_run_from_point(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        particles = model.run("/data/lm/tests/pws_das_2014*.nc", output_formats = ['NetCDF'], output_path=self.output_path)
        self.assertEquals(len(particles), self.num_particles)
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_run_from_point_with_wfs_shoreline(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True,
                                    shoreline_path='http://geo.asascience.com/geoserver/shorelines/ows',
                                    shoreline_feature='shorelines:10m_land_polygons')

        particles = model.run("/data/lm/tests/pws_das_2014*.nc", output_formats = ['NetCDF'], output_path=self.output_path)
        self.assertEquals(len(particles), self.num_particles)
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_time_method_interp(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    time_method="interp")

        particles = model.run("/data/lm/tests/pws_das_2014*.nc", output_formats = ['NetCDF'], output_path=self.output_path)
        self.assertEquals(len(particles), self.num_particles)
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_time_method_nearest(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    time_method="nearest")

        particles = model.run("/data/lm/tests/pws_das_2014*.nc", output_formats = ['NetCDF'], output_path=self.output_path)
        self.assertEquals(len(particles), self.num_particles)
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_time_method_bad(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        with self.assertRaises(TypeError):
            BaseModelController(geometry=p,
                                depth=self.start_depth,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=self.num_steps,
                                npart=self.num_particles,
                                models=models,
                                use_bathymetry=False,
                                use_shoreline=False,
                                time_method="umm_what_am_i")

    def test_start_on_land_from_lat_lon(self):
        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019

        models = [self.transport]

        model = BaseModelController(latitude=start_lat,
                                    longitude=start_lon,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True)

        with raises(ModelError):
            model.run("/data/lm/tests/pws_das_2014*.nc")

    def test_start_on_land_from_point_no_depth(self):
        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019

        p = Point(start_lon, start_lat)

        models = [self.transport]

        model = BaseModelController(geometry=p,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True)

        with raises(ModelError):
            model.run("/data/lm/tests/pws_das_2014*.nc")

    def test_start_on_land_from_point_with_depth(self):
        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019
        depth     = -10

        p = Point(start_lon, start_lat)

        models = [self.transport]

        model = BaseModelController(geometry=p,
                                    depth=depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True)

        with raises(ModelError):
            model.run("/data/lm/tests/pws_das_2014*.nc")

    def test_bad_dataset(self):
        models = [self.transport]

        model = BaseModelController(latitude=self.start_lat,
                                    longitude=self.start_lon,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        with raises(BaseDataControllerError):
            model.run("http://example.com/thisisnotadataset.nc")

    def test_timechunk_greater_than_timestep(self):
        models = [self.transport]

        model = CachingModelController(latitude=self.start_lat,
                                       longitude=self.start_lon,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False,
                                       time_chunk=48,
                                       horiz_chunk=2)

        particles = model.run("/data/lm/tests/pws_das_2014*.nc", output_formats = ['NetCDF'], output_path=self.output_path, cache_path=self.cache_path, remove_cache=False)
        self.assertEquals(len(particles), self.num_particles)
        self.assertTrue(os.path.exists(self.cache_path))
        os.remove(self.cache_path)
        self.assertTrue(os.path.exists(os.path.join(self.output_path, "trajectories.nc")))

    def test_no_local_data_for_requested_run(self):
        models = [self.transport]

        # Start is after available time
        model = BaseModelController(latitude=self.start_lat,
                                    longitude=self.start_lon,
                                    depth=self.start_depth,
                                    start=datetime.utcnow() + timedelta(days=30),
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.run("/data/lm/tests/pws_das_2014*.nc", output_formats = ['NetCDF'], output_path=self.output_path, cache_path=self.cache_path, remove_cache=False)

        # Start is OK but Ending is after available time
        model = BaseModelController(latitude=self.start_lat,
                                    longitude=self.start_lon,
                                    depth=self.start_depth,
                                    start=datetime(2014, 1, 1, 0),
                                    step=self.time_step,
                                    nstep=500,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.run("/data/lm/tests/pws_das_2014*.nc", output_formats = ['NetCDF'], output_path=self.output_path, cache_path=self.cache_path, remove_cache=False)

    def test_no_dap_data_for_requested_run(self):
        models = [self.transport]

        # Start is after available time
        model = CachingModelController(latitude=self.start_lat,
                                       longitude=self.start_lon,
                                       depth=self.start_depth,
                                       start=datetime.utcnow() + timedelta(days=30),
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", output_formats = ['NetCDF'], output_path=self.output_path, cache_path=self.cache_path, remove_cache=False)

        # Start is OK but Ending is after available time
        model = CachingModelController(latitude=self.start_lat,
                                       longitude=self.start_lon,
                                       depth=self.start_depth,
                                       start=datetime.utcnow() - timedelta(days=2),
                                       step=self.time_step,
                                       nstep=500,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", output_formats = ['NetCDF'], output_path=self.output_path, cache_path=self.cache_path, remove_cache=False)

    def test_run_10m_shoreline(self):
        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=300,
                                    npart=2,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True,
                                    shoreline_index_buffer=0.05)

        particles = model.run("/data/lm/tests/pws_das_2014*.nc")
        self.assertEquals(len(particles), 2)
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_run_west_coast_shoreline(self):

        models = [self.transport]

        p = Point(self.start_lon, self.start_lat)

        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=300,
                                    npart=2,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True,
                                    shoreline_path='/data/lm/shore/westcoast/New_Land_Clean.shp',
                                    shoreline_index_buffer=0.05)

        particles = model.run("/data/lm/tests/pws_das_2014*.nc")
        self.assertEquals(len(particles), 2)
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))
class ModelControllerTest(unittest.TestCase):

    def setUp(self):
        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)
        self.log.logger.handlers.append(logging.StreamHandler())
        self.log.logger.info(self.id())

        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2014, 1, 2, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        output_dir = "/data/lm/tests/output"
        test_dir = self.id().split('.')[-1]
        self.output_path = os.path.join(output_dir, test_dir)
        if not os.path.isdir(self.output_path):
            os.makedirs(self.output_path)
        self.output_formats = [ex.H5Trackline]

        cache_dir = "/data/lm/tests/cache"
        self.cache_path = os.path.join(cache_dir, test_dir, 'cache.nc')

        self.bathy_file = "/data/lm/bathy/global/ETOPO1_Bed_g_gmt4.grd"

        self.shoreline_path = "/data/lm/shore"

    def tearDown(self):
        self.log.close()

    def draw_trackline(self, geojson_file):
        with fiona.open(geojson_file) as src:
            self.log.logger.info((gj2ascii.render(src, 100, fill='.', char='o', bbox=shape(src.next()['geometry']).buffer(0.1).envelope.bounds)))

    def test_run_from_multiple_files_with_cache(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = CachingModelController(geometry=p,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       se_shoreline=False,
                                       time_chunk=10,
                                       horiz_chunk=4)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc", cache_path=self.cache_path, remove_cache=False)
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))
        self.assertTrue(os.path.exists(self.cache_path))
        os.remove(self.cache_path)

    def test_run_from_multiple_files_without_cache_on_ipy_cluster(self):
        try:
            from IPython.parallel import Client
            client = Client()

            pool = client.load_balanced_view()
        except:
            raise unittest.SkipTest("Cluster connection failed")

        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = IPythonClusterModelController(geometry=p,
                                              depth=self.start_depth,
                                              start=self.start_time,
                                              step=self.time_step,
                                              nstep=self.num_steps,
                                              npart=self.num_particles,
                                              models=models,
                                              use_bathymetry=False,
                                              use_shoreline=False,
                                              pool=pool)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_run_from_multiple_files_without_cache(self, pool=None):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    pool=pool)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_run_from_dap_with_cache(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = CachingModelController(geometry=p,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=200,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False,
                                       time_chunk=24,
                                       horiz_chunk=4)

        model.setup_run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache_path=self.cache_path, remove_cache=False)
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

        self.assertTrue(os.path.exists(self.cache_path))
        os.remove(self.cache_path)

    def test_run_from_dap_without_cache(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = CachingModelController(geometry=p,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False)

        model.setup_run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc")
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

        # We didn't pass remove_cache=False, so it should have been removed by the CachingModelController.
        self.assertFalse(os.path.exists(self.cache_path))

    def test_run_from_polygon(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat).buffer(0.001)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=datetime(2014, 1, 2, 0),
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    time_chunk=10,
                                    horiz_chunk=4)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_distributed_from_polygon(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat).buffer(0.001)
        model = DistributedModelController(geometry=p,
                                           depth=self.start_depth,
                                           start=datetime(2014, 1, 2, 0),
                                           step=self.time_step,
                                           nstep=self.num_steps,
                                           npart=self.num_particles,
                                           models=models,
                                           use_bathymetry=False,
                                           use_shoreline=False,
                                           time_chunk=10,
                                           horiz_chunk=4)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc", redis_url='redis://127.0.0.1:6379/150', redis_results_channel='test_distributed_from_polygon:results')
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    @unittest.skip("Need to find a new WFS server to test against")
    def test_run_from_point_with_wfs_shoreline(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True,
                                    shoreline_path='http://geo.asascience.com/geoserver/shorelines/ows',
                                    shoreline_feature='shorelines:10m_land_polygons')

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_time_method_interp(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    time_method="interp")

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_time_method_nearest(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    time_method="nearest")

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_time_method_bad(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        with self.assertRaises(TypeError):
            BaseModelController(geometry=p,
                                depth=self.start_depth,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=self.num_steps,
                                npart=self.num_particles,
                                models=models,
                                use_bathymetry=False,
                                use_shoreline=False,
                                time_method="umm_what_am_i")

    def test_start_on_land_from_lat_lon(self):
        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019
        models = [self.transport]
        model = BaseModelController(latitude=start_lat,
                                    longitude=start_lon,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True)

        with raises(ModelError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")
            model.run()

    def test_start_on_land_from_point_no_depth(self):
        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019

        p = Point(start_lon, start_lat)
        models = [self.transport]
        model = BaseModelController(geometry=p,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True)

        with raises(ModelError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")
            model.run()

    def test_start_on_land_from_point_with_depth(self):
        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019
        depth     = -10

        p = Point(start_lon, start_lat)
        models = [self.transport]
        model = BaseModelController(geometry=p,
                                    depth=depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True)

        with raises(ModelError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")
            model.run()

    def test_bad_dataset(self):
        models = [self.transport]
        model = BaseModelController(latitude=self.start_lat,
                                    longitude=self.start_lon,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run("http://example.com/thisisnotadataset.nc")

    def test_timechunk_greater_than_timestep(self):
        models = [self.transport]
        model = CachingModelController(latitude=self.start_lat,
                                       longitude=self.start_lon,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False,
                                       time_chunk=48,
                                       horiz_chunk=2)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc", cache_path=self.cache_path, remove_cache=False)
        model.run(output_formats=self.output_formats, output_path=self.output_path)

        self.assertTrue(os.path.exists(os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(os.path.join(self.output_path, "simple_trackline.geojson"))

        self.assertTrue(os.path.exists(self.cache_path))
        os.remove(self.cache_path)

    def test_no_local_data_for_requested_run(self):
        models = [self.transport]
        # Start is after available time
        model = BaseModelController(latitude=self.start_lat,
                                    longitude=self.start_lon,
                                    depth=self.start_depth,
                                    start=datetime.utcnow() + timedelta(days=30),
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")

        # Start is OK but Ending is after available time
        model = BaseModelController(latitude=self.start_lat,
                                    longitude=self.start_lon,
                                    depth=self.start_depth,
                                    start=datetime(2014, 1, 1, 0),
                                    step=self.time_step,
                                    nstep=500,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")

    def test_no_dap_data_for_requested_run(self):
        models = [self.transport]
        # Start is after available time
        model = CachingModelController(latitude=self.start_lat,
                                       longitude=self.start_lon,
                                       depth=self.start_depth,
                                       start=datetime.utcnow() + timedelta(days=30),
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc")

        # Start is OK but Ending is after available time
        model = CachingModelController(latitude=self.start_lat,
                                       longitude=self.start_lon,
                                       depth=self.start_depth,
                                       start=datetime.utcnow() - timedelta(days=2),
                                       step=self.time_step,
                                       nstep=500,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc")

    def test_run_10m_shoreline(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=300,
                                    npart=2,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True,
                                    shoreline_index_buffer=0.05)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run()

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_run_west_coast_shoreline(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=300,
                                    npart=2,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True,
                                    shoreline_path='/data/lm/shore/westcoast/New_Land_Clean.shp',
                                    shoreline_index_buffer=0.05)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run()

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))
class ModelControllerTest(unittest.TestCase):
    def setUp(self):
        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)
        self.log.logger.handlers.append(logging.StreamHandler())
        self.log.logger.info(self.id())

        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2014, 1, 2, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        output_dir = "/data/lm/tests/output"
        test_dir = self.id().split('.')[-1]
        self.output_path = os.path.join(output_dir, test_dir)
        if not os.path.isdir(self.output_path):
            os.makedirs(self.output_path)
        self.output_formats = [ex.H5Trackline]

        cache_dir = "/data/lm/tests/cache"
        self.cache_path = os.path.join(cache_dir, test_dir, 'cache.nc')

        self.bathy_file = "/data/lm/bathy/global/ETOPO1_Bed_g_gmt4.grd"

        self.shoreline_path = "/data/lm/shore"

    def tearDown(self):
        self.log.close()

    def draw_trackline(self, geojson_file):
        with fiona.open(geojson_file) as src:
            self.log.logger.info((gj2ascii.render(
                src,
                100,
                fill='.',
                char='o',
                bbox=shape(
                    src.next()['geometry']).buffer(0.1).envelope.bounds)))

    def test_run_from_multiple_files_with_cache(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = CachingModelController(geometry=p,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       se_shoreline=False,
                                       time_chunk=10,
                                       horiz_chunk=4)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc",
                        cache_path=self.cache_path,
                        remove_cache=False)
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))
        self.assertTrue(os.path.exists(self.cache_path))
        os.remove(self.cache_path)

    def test_run_from_multiple_files_without_cache_on_ipy_cluster(self):
        try:
            from IPython.parallel import Client
            client = Client()

            pool = client.load_balanced_view()
        except:
            raise unittest.SkipTest("Cluster connection failed")

        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = IPythonClusterModelController(geometry=p,
                                              depth=self.start_depth,
                                              start=self.start_time,
                                              step=self.time_step,
                                              nstep=self.num_steps,
                                              npart=self.num_particles,
                                              models=models,
                                              use_bathymetry=False,
                                              use_shoreline=False,
                                              pool=pool)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))
        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_run_from_multiple_files_without_cache(self, pool=None):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    pool=pool)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_run_from_dap_with_cache(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = CachingModelController(geometry=p,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=200,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False,
                                       time_chunk=24,
                                       horiz_chunk=4)

        model.setup_run(
            "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc",
            cache_path=self.cache_path,
            remove_cache=False)
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))

        self.assertTrue(os.path.exists(self.cache_path))
        os.remove(self.cache_path)

    def test_run_from_dap_without_cache(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = CachingModelController(geometry=p,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False)

        model.setup_run(
            "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc")
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))

        # We didn't pass remove_cache=False, so it should have been removed by the CachingModelController.
        self.assertFalse(os.path.exists(self.cache_path))

    def test_run_from_polygon(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat).buffer(0.001)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=datetime(2014, 1, 2, 0),
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    time_chunk=10,
                                    horiz_chunk=4)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_distributed_from_polygon(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat).buffer(0.001)
        model = DistributedModelController(geometry=p,
                                           depth=self.start_depth,
                                           start=datetime(2014, 1, 2, 0),
                                           step=self.time_step,
                                           nstep=self.num_steps,
                                           npart=self.num_particles,
                                           models=models,
                                           use_bathymetry=False,
                                           use_shoreline=False,
                                           time_chunk=10,
                                           horiz_chunk=4)

        model.setup_run(
            "/data/lm/tests/pws_das_2014*.nc",
            redis_url='redis://127.0.0.1:6379/150',
            redis_results_channel='test_distributed_from_polygon:results')
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    @unittest.skip("Need to find a new WFS server to test against")
    def test_run_from_point_with_wfs_shoreline(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(
            geometry=p,
            depth=self.start_depth,
            start=self.start_time,
            step=self.time_step,
            nstep=self.num_steps,
            npart=self.num_particles,
            models=models,
            use_bathymetry=False,
            use_shoreline=True,
            shoreline_path='http://geo.asascience.com/geoserver/shorelines/ows',
            shoreline_feature='shorelines:10m_land_polygons')

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_time_method_interp(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    time_method="interp")

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_time_method_nearest(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False,
                                    time_method="nearest")

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_time_method_bad(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        with self.assertRaises(TypeError):
            BaseModelController(geometry=p,
                                depth=self.start_depth,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=self.num_steps,
                                npart=self.num_particles,
                                models=models,
                                use_bathymetry=False,
                                use_shoreline=False,
                                time_method="umm_what_am_i")

    def test_start_on_land_from_lat_lon(self):
        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019
        models = [self.transport]
        model = BaseModelController(latitude=start_lat,
                                    longitude=start_lon,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True)

        with raises(ModelError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")
            model.run()

    def test_start_on_land_from_point_no_depth(self):
        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019

        p = Point(start_lon, start_lat)
        models = [self.transport]
        model = BaseModelController(geometry=p,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True)

        with raises(ModelError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")
            model.run()

    def test_start_on_land_from_point_with_depth(self):
        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019
        depth = -10

        p = Point(start_lon, start_lat)
        models = [self.transport]
        model = BaseModelController(geometry=p,
                                    depth=depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True)

        with raises(ModelError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")
            model.run()

    def test_bad_dataset(self):
        models = [self.transport]
        model = BaseModelController(latitude=self.start_lat,
                                    longitude=self.start_lon,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run("http://example.com/thisisnotadataset.nc")

    def test_timechunk_greater_than_timestep(self):
        models = [self.transport]
        model = CachingModelController(latitude=self.start_lat,
                                       longitude=self.start_lon,
                                       depth=self.start_depth,
                                       start=self.start_time,
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False,
                                       time_chunk=48,
                                       horiz_chunk=2)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc",
                        cache_path=self.cache_path,
                        remove_cache=False)
        model.run(output_formats=self.output_formats,
                  output_path=self.output_path)

        self.assertTrue(
            os.path.exists(
                os.path.join(self.output_path, "simple_trackline.geojson")))
        self.draw_trackline(
            os.path.join(self.output_path, "simple_trackline.geojson"))

        self.assertTrue(os.path.exists(self.cache_path))
        os.remove(self.cache_path)

    def test_no_local_data_for_requested_run(self):
        models = [self.transport]
        # Start is after available time
        model = BaseModelController(latitude=self.start_lat,
                                    longitude=self.start_lon,
                                    depth=self.start_depth,
                                    start=datetime.utcnow() +
                                    timedelta(days=30),
                                    step=self.time_step,
                                    nstep=self.num_steps,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")

        # Start is OK but Ending is after available time
        model = BaseModelController(latitude=self.start_lat,
                                    longitude=self.start_lon,
                                    depth=self.start_depth,
                                    start=datetime(2014, 1, 1, 0),
                                    step=self.time_step,
                                    nstep=500,
                                    npart=self.num_particles,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run("/data/lm/tests/pws_das_2014*.nc")

    def test_no_dap_data_for_requested_run(self):
        models = [self.transport]
        # Start is after available time
        model = CachingModelController(latitude=self.start_lat,
                                       longitude=self.start_lon,
                                       depth=self.start_depth,
                                       start=datetime.utcnow() +
                                       timedelta(days=30),
                                       step=self.time_step,
                                       nstep=self.num_steps,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run(
                "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc")

        # Start is OK but Ending is after available time
        model = CachingModelController(latitude=self.start_lat,
                                       longitude=self.start_lon,
                                       depth=self.start_depth,
                                       start=datetime.utcnow() -
                                       timedelta(days=2),
                                       step=self.time_step,
                                       nstep=500,
                                       npart=self.num_particles,
                                       models=models,
                                       use_bathymetry=False,
                                       use_shoreline=False)

        with self.assertRaises(BaseDataControllerError):
            model.setup_run(
                "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc")

    def test_run_10m_shoreline(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(geometry=p,
                                    depth=self.start_depth,
                                    start=self.start_time,
                                    step=self.time_step,
                                    nstep=300,
                                    npart=2,
                                    models=models,
                                    use_bathymetry=False,
                                    use_shoreline=True,
                                    shoreline_index_buffer=0.05)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run()

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))

    def test_run_west_coast_shoreline(self):
        models = [self.transport]
        p = Point(self.start_lon, self.start_lat)
        model = BaseModelController(
            geometry=p,
            depth=self.start_depth,
            start=self.start_time,
            step=self.time_step,
            nstep=300,
            npart=2,
            models=models,
            use_bathymetry=False,
            use_shoreline=True,
            shoreline_path='/data/lm/shore/westcoast/New_Land_Clean.shp',
            shoreline_index_buffer=0.05)

        model.setup_run("/data/lm/tests/pws_das_2014*.nc")
        model.run()

        # Not a caching controller, no cache path should exist
        self.assertFalse(os.path.exists(self.cache_path))
class ModelControllerTest(unittest.TestCase):

    def setUp(self):
        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2012, 8, 1, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)

        self.output_path = "/data/lm/output"
        self.cache_path = "/data/lm/cache"
        self.bathy_file = "/data/lm/bathy/ETOPO1_Bed_g_gmt4.grd"
        self.shoreline_path = "/data/lm/shore"

    def tearDown(self):
        self.log.close()

    def test_run_from_point(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_run_from_point")

        models = [self.transport]

        p = Point(self.start_lon, self.start_lat, self.start_depth)

        model = ModelController(geometry=p, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4)

        cache_path = os.path.join(self.cache_path, "test_run_from_point.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=cache_path)

    def test_run_from_point_with_wfs(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_run_from_point_with_wfs")

        models = [self.transport]

        p = Point(self.start_lon, self.start_lat, self.start_depth)

        model = ModelController(geometry=p, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4, shoreline_path='http://geo.asascience.com/geoserver/shorelines/ows', shoreline_feature='shorelines:10m_land_polygons')

        cache_path = os.path.join(self.cache_path, "test_run_from_point.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=cache_path)

    def test_run_from_polygon(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_run_from_polygon")

        models = [self.transport]

        poly = Point(self.start_lon, self.start_lat, self.start_depth).buffer(0.001)

        model = ModelController(geometry=poly, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4)

        cache_path = os.path.join(os.path.dirname(__file__), "..", "paegan/transport/_cache/test_run_from_polygon.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=cache_path)

    def test_interp(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_interp")

        models = [self.transport]

        num_steps = 100

        output_path = os.path.join(self.output_path, "test_interp")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline']

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=num_steps, npart=self.num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=10, horiz_chunk=4)

        cache_path = os.path.join(self.cache_path, "test_interp.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)

    def test_nearest(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_nearest")

        models = [self.transport]
        
        num_steps = 100

        output_path = os.path.join(self.output_path, "test_nearest")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline']

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=10, horiz_chunk=4, time_method='nearest')

        cache_path = os.path.join(self.cache_path, "test_nearest.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)

    def test_start_on_land(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_start_on_land")

        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019

        models = [self.transport]

        model = ModelController(latitude=start_lat, longitude=start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4, time_method='nearest')

        cache_path = os.path.join(self.cache_path, "test_start_on_land.nc")

        with raises(ModelError):
            model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=cache_path)

    def test_bad_dataset(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_bad_dataset")

        models = [self.transport]

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4, time_method='nearest')

        cache_path = os.path.join(self.cache_path, "test_bad_dataset.nc")
        
        with raises(DataControllerError):
            model.run("http://asascience.com/thisisnotadataset.nc", cache=cache_path)

    """
    def test_behavior_growth_and_settlement(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_behavior_growth_and_settlement")

        # 6 days
        num_steps = 144

        num_particles = 2

        # Behavior
        behavior_config = open(os.path.normpath(os.path.join(os.path.dirname(__file__),"./resources/files/behavior_for_run_testing.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=4, time_method='nearest')

        output_path = os.path.join(self.output_path, "test_behavior_growth_and_settlement")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline','Pickle']

        cache_path = os.path.join(self.cache_path, "test_behavior_growth_and_settlement.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)
    """

    def test_quick_settlement(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_quick_settlement")

        num_steps = 24

        num_particles = 4

        # Behavior
        behavior_config = open(os.path.normpath(os.path.join(os.path.dirname(__file__),"./resources/files/behavior_quick_settle.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=12, horiz_chunk=2, time_method='nearest')

        output_path = os.path.join(self.output_path, "test_quick_settlement")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline']

        cache_path = os.path.join(self.cache_path, "test_quick_settlement.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_DAS.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)

    def test_timechunk_greater_than_timestep(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_timechunk_greater_than_timestep")

        # 6 days
        num_steps = 10

        num_particles = 2

        models = [self.transport]

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=2)

        cache_path = os.path.join(self.cache_path, "test_timechunk_greater_than_timestep.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path)

    """
    def test_kayak_island(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: Kayak Island")

        # 6 days
        num_steps = 1632

        num_particles = 100

        time_step = 3600

        behavior_config = json.loads(urllib.urlopen("http://behaviors.larvamap.asascience.com/library/50ef1bb1cc7b61000700001d.json").read())
        lb = LarvaBehavior(data=behavior_config[u'results'][0])

        #behavior_config = json.loads(open(os.path.normpath(os.path.join(os.path.dirname(__file__),"./resources/files/nick.json"))).read())
        #lb = LarvaBehavior(data=behavior_config[u'results'][0])

        models = [Transport(horizDisp=0.01, vertDisp=0.001)]
        models.append(lb)

        start_time = datetime(2011, 5, 2, 00, tzinfo=pytz.utc)

        start_lat = 59.93517413488866
        start_lon = -144.496213677788
        depth = -1

        shoreline_path = os.path.join(self.shoreline_path, "westcoast", "New_Land_Clean.shp")

        model = ModelController(latitude=start_lat, longitude=start_lon, depth=depth, start=start_time, step=time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=5, time_method='interp', shoreline_path=shoreline_path)

        output_path = os.path.join(self.output_path, "kayak_island")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline']
        
        cache_path = os.path.join(self.cache_path, "kayak_island.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L1_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)
        
    def test_sheep_bay(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: Sheep Bay")

        # 6 days
        num_steps = 1632

        num_particles = 100

        time_step = 3600

        behavior_config = json.loads(urllib.urlopen("http://behaviors.larvamap.asascience.com/library/50ef1bb1cc7b61000700001d.json").read())
        lb = LarvaBehavior(data=behavior_config[u'results'][0])

        models = [Transport(horizDisp=0.01, vertDisp=0.001)]
        models.append(lb)

        start_time = datetime(2011, 5, 2, 00, tzinfo=pytz.utc)

        start_lat = 60.60899655733162
        start_lon = -145.97402533055956
        depth = -1

        shoreline_path = os.path.join(self.shoreline_path, "westcoast", "New_Land_Clean.shp")

        model = ModelController(latitude=start_lat, longitude=start_lon, depth=depth, start=start_time, step=time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=5, time_method='interp', shoreline_path=shoreline_path)

        output_path = os.path.join(self.output_path, "sheep_bay")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline']
        
        cache_path = os.path.join(self.cache_path, "sheep_bay.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)
    """

    def test_diel_migration(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_diel_migration")

        num_steps = 168

        num_particles = 4

        start_time = datetime(2013,4,1,0, tzinfo=pytz.utc)

        # Behavior
        behavior_config = open(os.path.normpath(os.path.join(os.path.dirname(__file__),"./resources/files/diel_suncycles.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = ModelController(latitude=60.68, longitude=-146.42, depth=self.start_depth, start=start_time, step=self.time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=2, time_method='nearest')

        output_path = os.path.join(self.output_path, "test_diel_migration")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline']

        cache_path = os.path.join(self.cache_path, "test_diel_migration.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_DAS.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)
class ModelControllerTest(unittest.TestCase):
    def setUp(self):
        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2012, 8, 1, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)

        self.output_path = "/data/lm/output"
        self.cache_path = "/data/lm/cache"
        self.bathy_file = "/data/lm/bathy/ETOPO1_Bed_g_gmt4.grd"
        self.shoreline_path = "/data/lm/shore"

    def tearDown(self):
        self.log.close()

    def test_run_from_point(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_run_from_point")

        models = [self.transport]

        p = Point(self.start_lon, self.start_lat, self.start_depth)

        model = ModelController(geometry=p,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=self.num_steps,
                                npart=self.num_particles,
                                models=models,
                                use_bathymetry=False,
                                use_shoreline=True,
                                time_chunk=10,
                                horiz_chunk=4)

        cache_path = os.path.join(self.cache_path, "test_run_from_point.nc")
        model.run(
            "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc",
            cache=cache_path)

    def test_run_from_polygon(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_run_from_polygon")

        models = [self.transport]

        poly = Point(self.start_lon, self.start_lat,
                     self.start_depth).buffer(0.001)

        model = ModelController(geometry=poly,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=self.num_steps,
                                npart=self.num_particles,
                                models=models,
                                use_bathymetry=False,
                                use_shoreline=True,
                                time_chunk=10,
                                horiz_chunk=4)

        cache_path = os.path.join(
            os.path.dirname(__file__), "..",
            "paegan/transport/_cache/test_run_from_polygon.nc")
        model.run(
            "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc",
            cache=cache_path)

    def test_interp(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_interp")

        models = [self.transport]

        num_steps = 100

        output_path = os.path.join(self.output_path, "test_interp")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile', 'NetCDF', 'Trackline']

        model = ModelController(latitude=self.start_lat,
                                longitude=self.start_lon,
                                depth=self.start_depth,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=num_steps,
                                npart=self.num_particles,
                                models=models,
                                use_bathymetry=True,
                                use_shoreline=True,
                                time_chunk=10,
                                horiz_chunk=4)

        cache_path = os.path.join(self.cache_path, "test_interp.nc")
        model.run(
            "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc",
            bathy=self.bathy_file,
            cache=cache_path,
            output_path=output_path,
            output_formats=output_formats)

    def test_nearest(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_nearest")

        models = [self.transport]

        num_steps = 100

        output_path = os.path.join(self.output_path, "test_nearest")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile', 'NetCDF', 'Trackline']

        model = ModelController(latitude=self.start_lat,
                                longitude=self.start_lon,
                                depth=self.start_depth,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=self.num_steps,
                                npart=self.num_particles,
                                models=models,
                                use_bathymetry=True,
                                use_shoreline=True,
                                time_chunk=10,
                                horiz_chunk=4,
                                time_method='nearest')

        cache_path = os.path.join(self.cache_path, "test_nearest.nc")
        model.run(
            "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc",
            bathy=self.bathy_file,
            cache=cache_path,
            output_path=output_path,
            output_formats=output_formats)

    def test_start_on_land(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_start_on_land")

        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019

        models = [self.transport]

        model = ModelController(latitude=start_lat,
                                longitude=start_lon,
                                depth=self.start_depth,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=self.num_steps,
                                npart=self.num_particles,
                                models=models,
                                use_bathymetry=False,
                                use_shoreline=True,
                                time_chunk=10,
                                horiz_chunk=4,
                                time_method='nearest')

        cache_path = os.path.join(self.cache_path, "test_start_on_land.nc")

        with raises(ModelError):
            model.run(
                "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc",
                cache=cache_path)

    def test_bad_dataset(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_bad_dataset")

        models = [self.transport]

        model = ModelController(latitude=self.start_lat,
                                longitude=self.start_lon,
                                depth=self.start_depth,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=self.num_steps,
                                npart=self.num_particles,
                                models=models,
                                use_bathymetry=False,
                                use_shoreline=True,
                                time_chunk=10,
                                horiz_chunk=4,
                                time_method='nearest')

        cache_path = os.path.join(self.cache_path, "test_bad_dataset.nc")

        with raises(DataControllerError):
            model.run("http://asascience.com/thisisnotadataset.nc",
                      cache=cache_path)

    """
    def test_behavior_growth_and_settlement(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_behavior_growth_and_settlement")

        # 6 days
        num_steps = 144

        num_particles = 2

        # Behavior
        behavior_config = open(os.path.normpath(os.path.join(os.path.dirname(__file__),"./resources/files/behavior_for_run_testing.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=4, time_method='nearest')

        output_path = os.path.join(self.output_path, "test_behavior_growth_and_settlement")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline','Pickle']

        cache_path = os.path.join(self.cache_path, "test_behavior_growth_and_settlement.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)
    """

    def test_quick_settlement(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_quick_settlement")

        num_steps = 24

        num_particles = 4

        # Behavior
        behavior_config = open(
            os.path.normpath(
                os.path.join(
                    os.path.dirname(__file__),
                    "./resources/files/behavior_quick_settle.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = ModelController(latitude=self.start_lat,
                                longitude=self.start_lon,
                                depth=self.start_depth,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=num_steps,
                                npart=num_particles,
                                models=models,
                                use_bathymetry=True,
                                use_shoreline=True,
                                time_chunk=12,
                                horiz_chunk=2,
                                time_method='nearest')

        output_path = os.path.join(self.output_path, "test_quick_settlement")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile', 'NetCDF', 'Trackline']

        cache_path = os.path.join(self.cache_path, "test_quick_settlement.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_DAS.nc",
                  bathy=self.bathy_file,
                  cache=cache_path,
                  output_path=output_path,
                  output_formats=output_formats)

    def test_timechunk_greater_than_timestep(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_timechunk_greater_than_timestep")

        # 6 days
        num_steps = 10

        num_particles = 2

        models = [self.transport]

        model = ModelController(latitude=self.start_lat,
                                longitude=self.start_lon,
                                depth=self.start_depth,
                                start=self.start_time,
                                step=self.time_step,
                                nstep=num_steps,
                                npart=num_particles,
                                models=models,
                                use_bathymetry=True,
                                use_shoreline=True,
                                time_chunk=24,
                                horiz_chunk=2)

        cache_path = os.path.join(self.cache_path,
                                  "test_timechunk_greater_than_timestep.nc")
        model.run(
            "http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc",
            bathy=self.bathy_file,
            cache=cache_path)

    """
    def test_kayak_island(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: Kayak Island")

        # 6 days
        num_steps = 1632

        num_particles = 100

        time_step = 3600

        behavior_config = json.loads(urllib.urlopen("http://behaviors.larvamap.asascience.com/library/50ef1bb1cc7b61000700001d.json").read())
        lb = LarvaBehavior(data=behavior_config[u'results'][0])

        #behavior_config = json.loads(open(os.path.normpath(os.path.join(os.path.dirname(__file__),"./resources/files/nick.json"))).read())
        #lb = LarvaBehavior(data=behavior_config[u'results'][0])

        models = [Transport(horizDisp=0.01, vertDisp=0.001)]
        models.append(lb)

        start_time = datetime(2011, 5, 2, 00, tzinfo=pytz.utc)

        start_lat = 59.93517413488866
        start_lon = -144.496213677788
        depth = -1

        shoreline_path = os.path.join(self.shoreline_path, "westcoast", "New_Land_Clean.shp")

        model = ModelController(latitude=start_lat, longitude=start_lon, depth=depth, start=start_time, step=time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=5, time_method='interp', shoreline_path=shoreline_path)

        output_path = os.path.join(self.output_path, "kayak_island")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline']
        
        cache_path = os.path.join(self.cache_path, "kayak_island.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L1_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)
        
    def test_sheep_bay(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: Sheep Bay")

        # 6 days
        num_steps = 1632

        num_particles = 100

        time_step = 3600

        behavior_config = json.loads(urllib.urlopen("http://behaviors.larvamap.asascience.com/library/50ef1bb1cc7b61000700001d.json").read())
        lb = LarvaBehavior(data=behavior_config[u'results'][0])

        models = [Transport(horizDisp=0.01, vertDisp=0.001)]
        models.append(lb)

        start_time = datetime(2011, 5, 2, 00, tzinfo=pytz.utc)

        start_lat = 60.60899655733162
        start_lon = -145.97402533055956
        depth = -1

        shoreline_path = os.path.join(self.shoreline_path, "westcoast", "New_Land_Clean.shp")

        model = ModelController(latitude=start_lat, longitude=start_lon, depth=depth, start=start_time, step=time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=5, time_method='interp', shoreline_path=shoreline_path)

        output_path = os.path.join(self.output_path, "sheep_bay")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline']
        
        cache_path = os.path.join(self.cache_path, "sheep_bay.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)
    """

    def test_diel_migration(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_diel_migration")

        num_steps = 168

        num_particles = 4

        start_time = datetime(2013, 4, 1, 0, tzinfo=pytz.utc)

        # Behavior
        behavior_config = open(
            os.path.normpath(
                os.path.join(os.path.dirname(__file__),
                             "./resources/files/diel_suncycles.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = ModelController(latitude=60.68,
                                longitude=-146.42,
                                depth=self.start_depth,
                                start=start_time,
                                step=self.time_step,
                                nstep=num_steps,
                                npart=num_particles,
                                models=models,
                                use_bathymetry=True,
                                use_shoreline=True,
                                time_chunk=24,
                                horiz_chunk=2,
                                time_method='nearest')

        output_path = os.path.join(self.output_path, "test_diel_migration")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile', 'NetCDF', 'Trackline']

        cache_path = os.path.join(self.cache_path, "test_diel_migration.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_DAS.nc",
                  bathy=self.bathy_file,
                  cache=cache_path,
                  output_path=output_path,
                  output_formats=output_formats)
Beispiel #12
0
class ModelControllerTest(unittest.TestCase):

    def setUp(self):
        self.start_lat = 60.75
        self.start_lon = -147
        self.start_depth = 0
        self.num_particles = 4
        self.time_step = 3600
        self.num_steps = 10
        self.start_time = datetime(2012, 8, 1, 00)
        self.transport = Transport(horizDisp=0.05, vertDisp=0.0003)

        self.log = EasyLogger('testlog.txt', level=logging.PROGRESS)

        self.output_path = "/data/lm/output"
        self.cache_path = "/data/lm/cache"
        self.bathy_file = "/data/lm/bathy/ETOPO1_Bed_g_gmt4.grd"
        self.shoreline_path = "/data/lm/shore"

    def tearDown(self):
        self.log.close()

    def test_run_from_point(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_run_from_point")

        models = [self.transport]

        p = Point(self.start_lon, self.start_lat, self.start_depth)

        model = ModelController(geometry=p, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4)

        cache_path = os.path.join(self.cache_path, "test_run_from_point.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=cache_path)

    def test_run_from_polygon(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_run_from_polygon")

        models = [self.transport]

        poly = Point(self.start_lon, self.start_lat, self.start_depth).buffer(0.001)

        model = ModelController(geometry=poly, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4)

        cache_path = os.path.join(os.path.dirname(__file__), "..", "paegan/transport/_cache/test_run_from_polygon.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=cache_path)

    def test_interp(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_interp")

        models = [self.transport]

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4)

        cache_path = os.path.join(self.cache_path, "test_interp.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=cache_path)

    def test_nearest(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_nearest")

        models = [self.transport]
        
        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4, time_method='nearest')

        cache_path = os.path.join(self.cache_path, "test_nearest.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=cache_path)

    def test_start_on_land(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_start_on_land")

        # Set the start position and time for the models
        start_lat = 60.15551950079041
        start_lon = -148.1999130249019

        models = [self.transport]

        model = ModelController(latitude=start_lat, longitude=start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4, time_method='nearest')

        cache_path = os.path.join(self.cache_path, "test_start_on_land.nc")

        with raises(ModelError):
            model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", cache=cache_path)

    def test_bad_dataset(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_bad_dataset")

        models = [self.transport]

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=self.num_steps, npart=self.num_particles, models=models, use_bathymetry=False, use_shoreline=True,
            time_chunk=10, horiz_chunk=4, time_method='nearest')

        cache_path = os.path.join(self.cache_path, "test_bad_dataset.nc")
        
        with raises(DataControllerError):
            model.run("http://asascience.com/thisisnotadataset.nc", cache=cache_path)

    """
    def test_behavior_growth_and_settlement(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_behavior_growth_and_settlement")

        # 6 days
        num_steps = 144

        num_particles = 2

        # Behavior
        behavior_config = open(os.path.normpath(os.path.join(os.path.dirname(__file__),"./resources/files/behavior_for_run_testing.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=4, time_method='nearest')

        output_path = os.path.join(self.output_path, "test_behavior_growth_and_settlement")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline','Pickle']

        cache_path = os.path.join(self.cache_path, "test_behavior_growth_and_settlement.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)
    """

    def test_quick_settlement(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_quick_settlement")

        num_steps = 24

        num_particles = 4

        # Behavior
        behavior_config = open(os.path.normpath(os.path.join(os.path.dirname(__file__),"./resources/files/behavior_quick_settle.json"))).read()
        lb = LarvaBehavior(json=behavior_config)

        models = [self.transport]
        models.append(lb)

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=12, horiz_chunk=2, time_method='nearest')

        output_path = os.path.join(self.output_path, "test_quick_settlement")
        shutil.rmtree(output_path, ignore_errors=True)
        os.makedirs(output_path)
        output_formats = ['Shapefile','NetCDF','Trackline','Pickle']

        cache_path = os.path.join(self.cache_path, "test_quick_settlement.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path, output_path=output_path, output_formats=output_formats)

    def test_timechunk_greater_than_timestep(self):
        self.log.logger.info("**************************************")
        self.log.logger.info("Running: test_timechunk_greater_than_timestep")

        # 6 days
        num_steps = 10

        num_particles = 2

        models = [self.transport]

        model = ModelController(latitude=self.start_lat, longitude=self.start_lon, depth=self.start_depth, start=self.start_time, step=self.time_step, nstep=num_steps, npart=num_particles, models=models, use_bathymetry=True, use_shoreline=True,
            time_chunk=24, horiz_chunk=2)

        cache_path = os.path.join(self.cache_path, "test_timechunk_greater_than_timestep.nc")
        model.run("http://thredds.axiomalaska.com/thredds/dodsC/PWS_L2_FCST.nc", bathy=self.bathy_file, cache=cache_path)

    """