Exemple #1
0
def get_nightlight(ref_year, cntry_info, res_km=None, from_hr=None):
    """Obtain nightlight from different sources depending on reference year.
    Compute resolution factor used at resampling depending on source.

    Parameters:
        ref_year (int): reference year
        cntry_info (dict): key = ISO alpha_3 country, value = [country id,
            country name, country geometry]
        res_km (float): approx resolution in km.
        from_hr (bool, optional):
    Returns:
        nightlight (sparse.csr_matrix), coord_nl (np.array), fn_nl (str),
        res_fact (float)
    """
    if from_hr is None and ref_year > 2013:
        from_hr = True
    elif from_hr is None and ref_year <= 2013:
        from_hr = False

    if from_hr:
        if not res_km:
            res_km = 0.5
        nl_year = ref_year
        if ref_year > 2013:
            nl_year = 2016
        else:
            nl_year = 2012
        LOGGER.info("Nightlights from NASA's earth observatory for year %s.",
                    str(nl_year))
        res_fact = DEF_RES_NASA_KM / res_km
        geom = [info[2] for info in cntry_info.values()]
        geom = shapely.ops.cascaded_union(geom)
        req_files = nl_utils.check_required_nl_files(geom.bounds)
        files_exist, _ = nl_utils.check_nl_local_file_exists(
            req_files, SYSTEM_DIR, nl_year)
        nl_utils.download_nl_files(req_files, files_exist, SYSTEM_DIR, nl_year)
        # nightlight intensity with 15 arcsec resolution
        nightlight, coord_nl = nl_utils.load_nightlight_nasa(
            geom.bounds, req_files, nl_year)
        fn_nl = [
            file.replace('*', str(nl_year))
            for idx, file in enumerate(nl_utils.BM_FILENAMES) if req_files[idx]
        ]
        fn_nl = ' + '.join(fn_nl)
    else:
        if not res_km:
            res_km = 1.0
        nl_year = ref_year
        if ref_year < 1992:
            nl_year = 1992
        elif ref_year > 2013:
            nl_year = 2013
        LOGGER.info(
            "Nightlights from NOAA's earth observation group for year %s.",
            str(nl_year))
        res_fact = DEF_RES_NOAA_KM / res_km
        # nightlight intensity with 30 arcsec resolution
        nightlight, coord_nl, fn_nl = nl_utils.load_nightlight_noaa(nl_year)

    return nightlight, coord_nl, fn_nl, res_fact, res_km
Exemple #2
0
    def test_cut_nasa_esp_pass(self):
        """Test load_nightlight_nasa function."""
        shp_fn = shapereader.natural_earth(resolution='10m',
                                           category='cultural',
                                           name='admin_0_countries')
        shp_file = shapereader.Reader(shp_fn)
        list_records = list(shp_file.records())
        for info_idx, info in enumerate(list_records):
            if info.attributes['ADM0_A3'] == 'AIA':
                bounds = info.bounds

        req_files = nl_utils.check_required_nl_files(bounds)
        files_exist, _ = nl_utils.check_nl_local_file_exists(req_files)
        nl_utils.download_nl_files(req_files, files_exist)

        try:
            nightlight, coord_nl = load_nightlight_nasa(bounds, req_files, 2016)
        except TypeError:
            print('MemoryError caught')
            return

        self.assertTrue(coord_nl[0, 0] < bounds[1])
        self.assertTrue(coord_nl[1, 0] < bounds[0])
        self.assertTrue(coord_nl[0, 0] + (nightlight.shape[0] - 1) * coord_nl[0, 1] > bounds[3])
        self.assertTrue(coord_nl[1, 0] + (nightlight.shape[1] - 1) * coord_nl[1, 1] > bounds[2])
Exemple #3
0
    def test_required_files(self):
        """Test check_required_nl_files function with various countries."""
        # Switzerland
        bbox = [
            5.954809204000128, 45.82071848599999, 10.466626831000013,
            47.801166077000076
        ]
        min_lon, min_lat, max_lon, max_lat = bbox

        np.testing.assert_array_equal(nightlight.check_required_nl_files(bbox),
                                      [0., 0., 0., 0., 1., 0., 0., 0.])
        np.testing.assert_array_equal(
            nightlight.check_required_nl_files(min_lon, min_lat, max_lon,
                                               max_lat),
            [0., 0., 0., 0., 1., 0., 0., 0.])

        # UK
        bbox = [
            -13.69131425699993, 49.90961334800005, 1.7711694670000497,
            60.84788646000004
        ]
        min_lon, min_lat, max_lon, max_lat = bbox

        np.testing.assert_array_equal(nightlight.check_required_nl_files(bbox),
                                      [0., 0., 1., 0., 1., 0., 0., 0.])
        np.testing.assert_array_equal(
            nightlight.check_required_nl_files(min_lon, min_lat, max_lon,
                                               max_lat),
            [0., 0., 1., 0., 1., 0., 0., 0.])

        # entire world
        bbox = [-180, -90, 180, 90]
        min_lon, min_lat, max_lon, max_lat = bbox

        np.testing.assert_array_equal(nightlight.check_required_nl_files(bbox),
                                      [1., 1., 1., 1., 1., 1., 1., 1.])
        np.testing.assert_array_equal(
            nightlight.check_required_nl_files(min_lon, min_lat, max_lon,
                                               max_lat),
            [1., 1., 1., 1., 1., 1., 1., 1.])

        # Not enough coordinates
        bbox = [-180, -90, 180, 90]
        min_lon, min_lat, max_lon, max_lat = bbox

        self.assertRaises(ValueError, nightlight.check_required_nl_files,
                          min_lon, min_lat, max_lon)

        # Invalid coordinate order
        bbox = [-180, -90, 180, 90]
        min_lon, min_lat, max_lon, max_lat = bbox

        self.assertRaises(ValueError, nightlight.check_required_nl_files,
                          max_lon, min_lat, min_lon, max_lat)
        self.assertRaises(ValueError, nightlight.check_required_nl_files,
                          min_lon, max_lat, max_lon, min_lat)