def test_write_output_post_run(model, output_ts_factor):
    """
    Create netcdf file post run from the cache. Under the hood, it is simply
    calling write_output so no need to check the data is correctly written
    test_write_output_standard already checks data is correctly written.

    Instead, make sure if output_timestep is not same as model.time_step,
    then data is output at correct time stamps
    """

    model.rewind()

    o_put = [model.outputters[outputter.id] for outputter in
             model.outputters if isinstance(outputter, NetCDFOutput)][0]
    o_put.all_data = False
    o_put.output_timestep = timedelta(seconds=model.time_step *
                                      output_ts_factor)

    del model.outputters[o_put.id]  # remove from list of outputters

    _run_model(model)

    # now write netcdf output
    o_put.write_output_post_run(model.start_time,
                                model.num_time_steps,
                                model._cache,
                                model.uncertain)

    assert os.path.exists(o_put.netcdf_filename)
    if model.uncertain:
        assert os.path.exists(o_put._u_netcdf_filename)

    uncertain = False
    for file_ in (o_put.netcdf_filename, o_put._u_netcdf_filename):
        for step in range(model.num_time_steps, int(output_ts_factor)):
            print "step: {0}".format(step)
            scp = model._cache.load_timestep(step)
            curr_time = scp.LE('current_time_stamp', uncertain)
            nc_data = NetCDFOutput.read_data(file_, curr_time)
            assert curr_time == nc_data['current_time_stamp'].item()

        if o_put.output_last_step:
            scp = model._cache.load_timestep(model.num_time_steps - 1)
            curr_time = scp.LE('current_time_stamp', uncertain)
            nc_data = NetCDFOutput.read_data(file_, curr_time)
            assert curr_time == nc_data['current_time_stamp'].item()

        """ at least one matching time found """
        print ('\nAll expected timestamps are written out for'
                ' output_ts_factor: {1}'.format(file_, output_ts_factor))

        # 2nd time around, look at uncertain filename so toggle uncertain flag
        uncertain = True

    # add this back in so cleanup script deletes the generated *.nc files
    model.outputters += o_put
def test_read_data_exception(model):
    """
    tests the exception is raised by read_data when file contains more than one
    output time and read_data is not given the output time to read
    """

    model.rewind()

    # check contents of netcdf File at multiple time steps (should only be 1!)
    o_put = [model.outputters[outputter.id] for outputter in
             model.outputters if isinstance(outputter, NetCDFOutput)][0]

    _run_model(model)

    with pytest.raises(ValueError):
        NetCDFOutput.read_data(o_put.netcdf_filename)
def test_read_standard_arrays(model, output_ts_factor):
    """
    tests the data returned by read_data is correct when `all_data` flag is
    False. It is only reading the standard_data_arrays

    Test will only verify the data when time_stamp of model matches the
    time_stamp of data written out. output_ts_factor means not all data is
    written out.
    """

    model.rewind()

    # check contents of netcdf File at multiple time steps (should only be 1!)
    o_put = [model.outputters[outputter.id] for outputter in
             model.outputters if isinstance(outputter, NetCDFOutput)][0]
    o_put.output_timestep = timedelta(seconds=model.time_step *
                                      output_ts_factor)
    _run_model(model)

    atol = 1e-5
    rtol = 0

    uncertain = False
    for file_ in (o_put.netcdf_filename, o_put._u_netcdf_filename):
        _found_a_matching_time = False
        for step in range(0, model.num_time_steps, int(output_ts_factor)):
            scp = model._cache.load_timestep(step)
            curr_time = scp.LE('current_time_stamp', uncertain)
            nc_data = NetCDFOutput.read_data(file_, curr_time)

            # check time
            if curr_time == nc_data['current_time_stamp'].item():
                _found_a_matching_time = True
                # check standard variables
                assert np.allclose(scp.LE('positions', uncertain),
                                   nc_data['positions'], rtol, atol)
                assert np.all(scp.LE('spill_num', uncertain)[:]
                              == nc_data['spill_num'])
                assert np.all(scp.LE('status_codes', uncertain)[:]
                              == nc_data['status_codes'])

                # flag variable is not currently set or checked

                if 'mass' in scp.LE_data:
                    assert np.all(scp.LE('mass', uncertain)[:]
                                  == nc_data['mass'])

                if 'age' in scp.LE_data:
                    assert np.all(scp.LE('age', uncertain)[:]
                                  == nc_data['age'])

        if _found_a_matching_time:
            """ at least one matching time found """
            print ('\ndata in model matches for output in \n{0} \nand'
                   ' output_ts_factor: {1}'.format(file_, output_ts_factor))

        # 2nd time around, look at uncertain filename so toggle uncertain flag

        uncertain = True
Example #4
0
    def _load_spill_data(self):
        """ load NetCDF file and add spill data back in """

        if not os.path.exists(self._certainspill_data):
            return

        array_types = {}

        for mover in self.model.movers:
            array_types.update(mover.array_types)

        for sc in self.model.spills.items():
            if sc.uncertain:
                data = NetCDFOutput.read_data(self._uncertainspill_data,
                                              time=None,
                                              all_data=True)
            else:
                data = NetCDFOutput.read_data(self._certainspill_data,
                                              time=None,
                                              all_data=True)

            sc.current_time_stamp = data.pop('current_time_stamp').item()
            sc._data_arrays = data
            sc._array_types.update(array_types)
def test_read_all_arrays(model):
    """
    tests the data returned by read_data is correct when `all_data` flag is
    True. It is only reading the standard_data_arrays
    """

    model.rewind()
    o_put = [model.outputters[outputter.id] for outputter in
             model.outputters if isinstance(outputter, NetCDFOutput)][0]
    o_put.all_data = True

    _run_model(model)

    atol = 1e-5
    rtol = 0

    uncertain = False
    for file_ in (o_put.netcdf_filename, o_put._u_netcdf_filename):
        _found_a_matching_time = False
        for step in range(model.num_time_steps):
            scp = model._cache.load_timestep(step)
            curr_time = scp.LE('current_time_stamp', uncertain)
            nc_data = NetCDFOutput.read_data(file_, curr_time, all_data=True)

            if curr_time == nc_data['current_time_stamp'].item():
                _found_a_matching_time = True
                for key in scp.LE_data:
                    if key == 'current_time_stamp':
                        """ already matched """
                        continue
                    elif key == 'positions':
                        assert np.allclose(scp.LE('positions', uncertain),
                                nc_data['positions'], rtol, atol)
                    else:
                        assert np.all(scp.LE(key, uncertain)[:]
                                      == nc_data[key])

        if _found_a_matching_time:
            print ('\ndata in model matches for output in \n{0}'.format(file_))

        # 2nd time around, look at uncertain filename so toggle uncertain flag

        uncertain = True