Example #1
0
def test_contains():
    bb0 = extents.from_boundary_offset(0, 0, 399, 399)
    bb1 = extents.from_boundary_offset(50, 50, 100, 100)

    assert bb0.contains(bb1)
    assert not bb1.contains(bb0)

    assert bb0.contains(extents.from_cell_offset(250, 250))

    assert extents.default().contains(bb0)
Example #2
0
def test_translate_subdata():
    mock_array = build_mock_array()

    bb = extents.from_boundary_offset(100, 100, 399, 399)

    sub_data = mock_array[bb.x_index, bb.y_index]

    assert (sub_data == mock_array[100:400, 100:400]).all()

    bb1 = extents.from_boundary_offset(20, 20, 29, 29, bb.parent_ref)

    print(bb1)

    assert (sub_data[bb1.x_index, bb1.y_index] == mock_array[120:130,
                                                             120:130]).all()
Example #3
0
def test_contains():
    e = extents.default()

    b1 = extents.from_boundary_offset(200, 200, 210, 210)
    assert e.contains(b1)

    b2 = extents.from_boundary_offset(675, 835, 685, 845)
    assert not e.contains(b2)

    b3 = extents.from_boundary_offset(700, 900, 710, 910)
    assert not e.contains(b3)

    # translate parent_ref to local (from global)
    b1t = b1.translate_localise_origin()
    assert e.contains(b1t)
Example #4
0
def test_output_graph_processing_splitfm_B():
    from awrams.utils import extents
    from awrams.utils import datetools as dt

    import awrams.models.awral.description
    awrams.models.awral.description.CLIMATE_DATA = os.path.join(
        os.path.dirname(__file__), '..', '..', 'test_data', 'simulation')

    from awrams.utils.nodegraph import nodes, graph
    from awrams.simulation.ondemand import OnDemandSimulator
    from awrams.models import awral

    input_map = awral.get_default_mapping()
    output_map = {
        's0_save':
        nodes.write_to_annual_ncfile(os.path.dirname(__file__), 's0', mode='w')
    }
    # outputs = graph.OutputGraph(output_map)
    runner = OnDemandSimulator(awral, input_map.mapping, omapping=output_map)

    print("RUNNER NEW (FILES EXISTING): multiple cells, multiple years")
    period = dt.dates('2010-2011')
    extent = extents.from_boundary_offset(200, 200, 201, 201)
    r = runner.run(period, extent)

    print("RUNNER OLD (FILES EXISTING): single cell, single year")
    period = dt.dates('2015')
    extent = extents.from_cell_offset(202, 202)
    r = runner.run(period, extent)
Example #5
0
def test_bb_translate_mask():

    bb = extents.from_boundary_offset(100, 100, 399, 399)

    bb_t = bb.translate_localise_origin()

    assert (bb.mask == bb_t.mask).all()
Example #6
0
def test_area_with_osr():
    try:
        import osr

        c = extents.from_cell_offset(300, 200)
        c.compute_areas()
        assert_almost_equal(c.area, 27956330.541281, places=6)
        print("300,200", c.area)

        c = extents.from_cell_offset(300, 400)
        c.compute_areas()
        assert_almost_equal(c.area, 27956330.541269, places=6)
        print("300,400", c.area)

    except ImportError:
        pass

    try:
        import osr

        c = extents.from_boundary_offset(300, 200, 302, 201)
        c.compute_areas()
        assert_almost_equal(c.area, 167671123.116, places=3)
        print(c.areas.shape, c.areas)

        for cell in c:
            print(cell)
            cc = extents.from_cell_offset(*cell)
            cc.compute_areas()
            lcell = c.localise_cell(cell)
            print(cc.area, c.areas[lcell[0], lcell[1]])
            assert cc.area == c.areas[lcell[0], lcell[1]]

    except ImportError:
        pass
Example #7
0
def test_output_filewrite():
    import awrams.models.awral.settings as settings
    settings.CLIMATE_DATA = '/mnt/awramsi_test_data/AWAP/'
    from awrams.utils import extents
    from awrams.utils import datetools as dt
    outputs = build_output_graph()
    test_simple_outputs(dt.dates('2008-2009'),
                        extents.from_boundary_offset(200,200,250,250),
                        output_mapping=outputs)
Example #8
0
def test_area_without_osr():
    import awrams.utils.extents
    awrams.utils.extents._LONGLAT_TO_AEA = None

    c = extents.from_cell_offset(300, 200)
    c.compute_areas()
    # assert_almost_equal(c.area,27956330.541281,places=6) # with gdal
    assert_almost_equal(c.area, 28044093.890163, places=6)

    c = extents.from_boundary_offset(300, 200, 301, 201)
    c.compute_areas()
    # assert_almost_equal(c.area,111803049.532,places=3) # with gdal
    assert_almost_equal(c.area, 112153279.927, places=3)
Example #9
0
def test_get_padded_by_coords():
    from awrams.utils.io.data_mapping import SplitFileManager
    from awrams.utils.extents import from_boundary_offset
    from awrams.utils.mapping_types import gen_coordset
    import awrams.utils.datetools as dt

    path = os.path.join(os.path.dirname(__file__),'..','..','test_data','simulation')

    sfm = SplitFileManager.open_existing(path,'temp_min_day_*.nc','temp_min_day')
    # return sfm
    extent = from_boundary_offset(200,200,231,231)
    period = dt.dates('2011')
    coords = gen_coordset(period,extent)

    data = sfm.get_padded_by_coords(coords)
    print(data.shape)
    print(coords.shape)
    assert data.shape == coords.shape
Example #10
0
def test_multiextent():
    m = dict(a=extents.from_cell_offset(250, 250),
             b=extents.from_cell_offset(275, 275),
             c=extents.from_cell_offset(300, 300),
             d=extents.from_boundary_offset(200, 290, 202, 292))
    e = extents.from_multiple(m)
    # print(e.mask.shape)
    # print(e.mask[49:52,:2])
    # print(e.mask[:4,40:44])

    for k, cell in m.items():
        # print(k,cell)
        assert (e.contains(cell))

    # for cell in e:
    #     print("LLL",cell)
    #
    assert e.x_min == 250
    assert e.x_max == 300
    assert e.y_min == 200
    assert e.y_max == 300

    assert e.cell_count == 12
Example #11
0
def test_initial_states_region():
    import numpy as np

    from awrams.utils import extents
    from awrams.utils import datetools as dt

    from awrams.utils.nodegraph import nodes, graph
    from awrams.simulation.ondemand import OnDemandSimulator
    from awrams.models import awral

    period = dt.dates('dec 2010')
    ### test a region
    extent = extents.from_boundary_offset(400, 170, 407, 177)
    print(extent)

    ### simulation with default initial states
    sim = OnDemandSimulator(awral, input_map.mapping)
    r, i = sim.run(period, extent, return_inputs=True)
    outputs_default = r['final_states']

    ### simulation with initial states read from nc files
    get_initial_states(input_map)
    sim = OnDemandSimulator(awral, input_map.mapping)
    r, i = sim.run(period, extent, return_inputs=True)
    outputs_init = r['final_states']

    ### compare final states with default states simulation
    ### should be different
    for k, o in outputs_init.items():
        assert not (o == outputs_default[k]).any()

    ### save initial states to compare
    ini_states = {}
    for k in i:
        try:
            if k.startswith('init'):
                ini_states[k] = i[k]
        except:
            pass

    ### simulation with initial states read from dict
    get_initial_states_dict(input_map, period, extent)
    sim = OnDemandSimulator(awral, input_map.mapping)
    r, i = sim.run(period, extent, return_inputs=True)
    outputs_init_dict = r['final_states']

    ### compare final states with other ini states simulation
    ### should be same
    for k, o in outputs_init_dict.items():
        assert (o == outputs_init[k]).any()

    ### compare initial states from both methods
    ### should be same
    for k in i:
        try:
            if k.startswith('init'):
                assert ini_states[k] == i[k]
        except:
            pass

    extent = extents.from_boundary_coords(-30, 120.5, -30.35, 120.85)
    print(extent)

    ### simulation with initial states read from nc files
    get_initial_states(input_map)
    sim = OnDemandSimulator(awral, input_map.mapping)
    r, i = sim.run(period, extent, return_inputs=True)
    outputs_init = r['final_states']

    ### compare final states with other ini states simulation
    ### should be same
    for k, o in outputs_init_dict.items():
        assert (o == outputs_init[k]).any()

    ### compare initial states from both methods
    ### should be same
    for k in i:
        try:
            if k.startswith('init'):
                assert ini_states[k] == i[k]
        except:
            pass