1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#   This Python module is part of the PyRate software package.
#
#   Copyright 2020 Geoscience Australia
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
"""
This Python module contains fixtures for use in the PyRate test suite.
"""
import shutil
import random
import string
import tempfile
import pytest

import pyrate.constants as C
from pyrate.constants import PYRATEPATH
from pyrate.core import mpiops, shared
from pyrate.configuration import Configuration
from tests.common import TEST_CONF_ROIPAC, TEST_CONF_GAMMA, SML_TEST_DEM_TIF, MEXICO_CROPA_CONF
from tests.common import ROIPAC_SYSTEM_CONF, GAMMA_SYSTEM_CONF, GEOTIF_SYSTEM_CONF, SML_TEST_COH_LIST


@pytest.fixture
def tempdir():
    """
    tempdir for tests
    """
    def tmpdir():
        return tempfile.mkdtemp()
    return tmpdir


@pytest.fixture(params=[ROIPAC_SYSTEM_CONF, GEOTIF_SYSTEM_CONF, GAMMA_SYSTEM_CONF])
def system_conf(request):
    params = Configuration(request.param).__dict__
    yield request.param
    shutil.rmtree(params[C.OUT_DIR], ignore_errors=True)


@pytest.fixture
def random_filename(tmp_path_factory):
    def make_random_filename(ext=''):
        fname = ''.join(random.choice(string.ascii_lowercase) for _ in range(10))
        return tmp_path_factory.mktemp(basename='pyrate-').joinpath(fname + ext)
    return make_random_filename


# Make sure all MPI tests use this fixure
@pytest.fixture()
def mpisync(request):
    mpiops.comm.barrier()

    def fin():
        mpiops.comm.barrier()

    request.addfinalizer(fin)
    return mpiops.comm


@pytest.fixture(params=[0, 1])
def coh_mask(request):
    return request.param


@pytest.fixture(params=[1, 2])
def ref_est_method(request):
    return request.param


@pytest.fixture(params=[(-1, -1), (150.941666654, -34.218333314)])
def ref_pixel(request):
    return request.param


@pytest.fixture(params=[1, 3])
def orbfit_lks(request):
    return request.param


@pytest.fixture(params=C.ORB_METHOD_NAMES.keys())
def orbfit_method(request):
    return request.param


@pytest.fixture(params=C.ORB_DEGREE_NAMES.keys())
def orbfit_degrees(request):
    return request.param


@pytest.fixture(params=[1, 3])
def get_lks(request):
    return request.param


@pytest.fixture(params=[1, 4])
def get_crop(request):
    return request.param


@pytest.fixture()
def get_config():
    def params(conf_file):
        params_ = Configuration(conf_file).__dict__
        return params_
    return params


@pytest.fixture
def gamma_params():
    params = Configuration(TEST_CONF_GAMMA).__dict__
    shared.mkdir_p(params[C.OUT_DIR])
    yield params
    shutil.rmtree(params[C.OUT_DIR], ignore_errors=True)


@pytest.fixture
def roipac_params():
    params = Configuration(TEST_CONF_ROIPAC).__dict__
    yield params
    shutil.rmtree(params[C.OUT_DIR], ignore_errors=True)


@pytest.fixture
def mexico_cropa_params():
    params = Configuration(MEXICO_CROPA_CONF).__dict__
    yield params
    shutil.rmtree(params[C.OUT_DIR], ignore_errors=True)


@pytest.fixture(params=[TEST_CONF_GAMMA, TEST_CONF_ROIPAC])
def roipac_or_gamma_conf(request):
    return request.param


@pytest.fixture(params=[TEST_CONF_GAMMA], scope='session')
def gamma_conf(request):
    params = Configuration(TEST_CONF_GAMMA).__dict__
    yield request.param
    shutil.rmtree(params[C.OUT_DIR], ignore_errors=True)


@pytest.fixture
def coh_list_file():
    return SML_TEST_COH_LIST


@pytest.fixture
def dem():
    d = shared.dem_or_ifg(SML_TEST_DEM_TIF)
    d.open()
    return d


@pytest.fixture(params=[TEST_CONF_GAMMA, MEXICO_CROPA_CONF], scope='session')
def gamma_or_mexicoa_conf(request):
    params = Configuration(request.param).__dict__
    yield request.param
    shutil.rmtree(params[C.OUT_DIR], ignore_errors=True)


@pytest.fixture(params=range(5))
def run_number(request):
    return request.param


GEOTIFF = PYRATEPATH.joinpath('tests', 'test_data', 'geotiffs')


@pytest.fixture
def geotiffs():
    tifs = [u.as_posix() for u in GEOTIFF.glob('*_unw.tif')]
    tifs.sort()
    return tifs


@pytest.fixture
def ten_geotiffs():
    tifs = [u.as_posix() for u in GEOTIFF.glob('*_unw.tif')]
    tifs.sort()
    return tifs[:10]


@pytest.fixture
def cropa_geotifs(mexico_cropa_params):
    m_paths = mexico_cropa_params[C.INTERFEROGRAM_FILES]
    return [m.converted_path for m in m_paths]