예제 #1
0
 def test_check_files_exist(self):
     """ Test check_nightlight_local_file_exists"""
     # If invalid path is supplied it has to fall back to DATA_DIR
     np.testing.assert_array_equal(nightlight.check_nl_local_file_exists(np.ones\
                    (np.count_nonzero(BM_FILENAMES)), 'Invalid/path')[0],\
                     nightlight.check_nl_local_file_exists(np.ones\
                   (np.count_nonzero(BM_FILENAMES)), SYSTEM_DIR)[0])
예제 #2
0
 def test_check_files_exist(self):
     """Test check_nightlight_local_file_exists"""
     # If invalid directory is supplied it has to fail
     try:
         _ = nightlight.check_nl_local_file_exists(
             np.ones(np.count_nonzero(BM_FILENAMES)), 'Invalid/path')[0]
         raise Exception(
             "if the path is not valid, check_nl_local_file_exists should fail"
         )
     except ValueError:
         pass
     files_exist, _check_path = nightlight.check_nl_local_file_exists(
         np.ones(np.count_nonzero(BM_FILENAMES)), SYSTEM_DIR)
     self.assertTrue(files_exist.sum() > 0, f'{files_exist} {BM_FILENAMES}')
예제 #3
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])
예제 #4
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