def force_geotiff_to_match_projection_ndv_and_datatype(input_path, match_path, output_path, output_datatype=None, output_ndv=None):
    """Rather than actually projecting, just change the metadata so it matches exactly. This only will be useful
    if there was a data error and something got a projection defined when the underlying data wasnt actually transofmred
    into that shape.

    NOTE that the output will keep the same geotransform as input, and only the projection, no data and datatype will change.
    """

    if not output_datatype:

        output_datatype = hb.get_datatype_from_uri(match_path)

    if not output_ndv:
        output_ndv = hb.get_ndv_from_path(match_path)
    match_wkt = hb.get_dataset_projection_wkt_uri(match_path)
    input_geotransform  = hb.get_geotransform_uri(input_path)

    # Load the array, but use numpy to convert it to the new datatype
    input_array = hb.as_array(input_path).astype(hb.gdal_number_to_numpy_type[output_datatype])

    if not output_ndv:
        output_ndv = -9999

    hb.save_array_as_geotiff(input_array, output_path,
                             data_type=output_datatype,
                             ndv=output_ndv,
                             geotransform_override=input_geotransform,
                             projection_override=match_wkt)
def force_global_angular_data_to_equal_area_earth_grid(input_path, output_path):

    output_datatype = hb.get_datatype_from_uri(input_path)
    output_ndv = hb.get_ndv_from_path(input_path)
    match_wkt = hb.get_dataset_projection_wkt_uri(input_path)
    match_wkt = hb.get_wkt_from_epsg_code(6933)

    input_geotransform  = hb.get_geotransform_uri(input_path)

    output_geotransform = list(hb.common_geotransforms['wec_30s'])

    output_geotransform[1] = input_geotransform[1] * hb.size_of_one_arcdegree_at_equator_in_meters
    output_geotransform[5] = input_geotransform[5] * hb.size_of_one_arcdegree_at_equator_in_meters


    # Load the array, but use numpy to convert it to the new datatype
    input_array = hb.as_array(input_path).astype(hb.gdal_number_to_numpy_type[output_datatype])

    if not output_ndv:
        output_ndv = -9999

    hb.save_array_as_geotiff(input_array, output_path,
                             data_type=output_datatype,
                             ndv=output_ndv,
                             geotransform_override=output_geotransform,
                             projection_override=match_wkt)
def get_linear_unit_from_other_projection(input_uri, projected_uri, also_return_size=False):
    input_ds = gdal.Open(input_uri)
    projected_wkt = hb.get_dataset_projection_wkt_uri(projected_uri)

    # Create a virtual raster that is projected based on the output WKT. This
    # vrt does not save to disk and is used to get the proper projected
    # bounding box and size.
    vrt = gdal.AutoCreateWarpedVRT(
        input_ds, None, projected_wkt, gdal.GRA_Bilinear)

    geo_t = vrt.GetGeoTransform()
    x_size = vrt.RasterXSize  # Raster xsize
    y_size = vrt.RasterYSize  # Raster ysize

    resolution = geo_t[1]

    if also_return_size:
        return resolution, x_size, y_size
    else:
        return resolution
Esempio n. 4
0
def calc_caloric_production_from_lulc_uri(input_lulc_uri, ui=None, **kw):
    # First check that the required files exist, creating them if not.
    # Data from Johnson et al 2016.

    aoi_uri = kw['aoi_uri']
    output_dir = kw['workspace_dir']
    working_dir = os.path.split(os.path.split(input_lulc_uri)[0])[0]
    baseline_dir = os.path.join(working_dir, 'Baseline')
    scenario_dir = os.path.split(input_lulc_uri)[0]

    # intermediate files
    global_calories_per_cell_uri = os.path.join(kw['model_base_data_dir'],
                                                'calories_per_cell.tif')
    clipped_calories_per_5m_cell_uri = os.path.join(
        output_dir, 'clipped_calories_per_5m_cell.tif')
    calories_resampled_uri = os.path.join(output_dir,
                                          'calories_per_ha_2000.tif')

    # Get global 5m calories map from base data and project the full global map to projection of lulc, but keeping the global resolution
    global_calories_per_cell_projected_uri = os.path.join(
        output_dir, 'global_calories_per_cell_projected.tif')
    output_wkt = hb.get_dataset_projection_wkt_uri(input_lulc_uri)

    # Set cell size based on size at equator. Need to fully think this through.
    # cell_size = 111319.49079327358 * hb.get_cell_size_from_uri(global_calories_per_cell_uri)
    cell_size = hb.get_cell_size_from_uri(global_calories_per_cell_uri)

    # Reproject the global calorie data into lulc projection
    hb.reproject_dataset_uri(global_calories_per_cell_uri, cell_size,
                             output_wkt, 'bilinear',
                             global_calories_per_cell_projected_uri)

    # Clip the global data to the project aoi, but keep at 5 min for math summation reasons later
    hb.clip_dataset_uri(global_calories_per_cell_projected_uri,
                        aoi_uri,
                        clipped_calories_per_5m_cell_uri,
                        assert_projections=True,
                        process_pool=None)

    # Now that it's clipped, it's small enough to resample to lulc resolution
    hb.resize_and_resample_dataset_uri(
        clipped_calories_per_5m_cell_uri, hb.get_bounding_box(input_lulc_uri),
        hb.get_cell_size_from_uri(input_lulc_uri), calories_resampled_uri,
        'bilinear')

    # Load both baseline lulc and scenario lulc. Even if only 1 scenario is being included, still must have the baseline calculated
    # to calibrate how the baseline data extrapolates to the scenario.
    baseline_lulc_uri = os.path.join(
        baseline_dir, 'lulc.tif')  # TODO NOTE manual  edit used for Tanzania
    # baseline_resampled_uri = baseline_lulc_uri.replace('.tif', '_resampled.tif')
    baseline_resampled_uri = os.path.join(output_dir, 'lulc_resampled.tif')
    hb.resize_and_resample_dataset_uri(
        baseline_lulc_uri, hb.get_bounding_box(input_lulc_uri),
        hb.get_cell_size_from_uri(input_lulc_uri), baseline_resampled_uri,
        'nearest')

    # Base on teh assumption that full ag is twice as contianing of calroies as mosaic, allocate the
    # caloric presence to these two ag locations. Note that these are still not scaled, but they are
    # correct relative to each other.
    # This simplification means we are doing the equivilent to the invest crop model beacause
    # the cells to allocate are lower res than the target.
    baseline_resampled_array = utilities.as_array(baseline_resampled_uri)
    calories_resampled_array = utilities.as_array(calories_resampled_uri)
    unscaled_calories_baseline = np.where(baseline_resampled_array == 12,
                                          calories_resampled_array, 0)
    unscaled_calories_baseline = np.where(baseline_resampled_array == 14,
                                          0.5 * calories_resampled_array,
                                          unscaled_calories_baseline)

    # Do the same replacement for the scenario lulc
    input_lulc_array = utilities.as_array(input_lulc_uri)
    unscaled_calories_input_lulc = np.where(input_lulc_array == 12,
                                            calories_resampled_array, 0)
    unscaled_calories_input_lulc = np.where(input_lulc_array == 14,
                                            0.5 * calories_resampled_array,
                                            unscaled_calories_input_lulc)

    note = """In ilm 
        for both ghana and honduras,
        
        55% from most intensive
        35% from mosaic
        10% agro
        
        honduras FOOD PRODUCTION grew by 16%
        ghana 45%
        
        
        IDEA Use monfreda, subset out perrenial trees, to get yield of agroforestry
        """

    if 'Honduras' in ui.root_app.project_folder or 'Ghana' in ui.root_app.project_folder:

        unscaled_calories_input_lulc = np.where(input_lulc_array == 12,
                                                calories_resampled_array, 0)
        unscaled_calories_input_lulc = np.where(
            input_lulc_array == 14, 0.35 * calories_resampled_array,
            unscaled_calories_input_lulc)

        # CUSTOM calc in agroforestry  value following Johan's suggested %
        unscaled_calories_input_lulc = np.where(input_lulc_array == 17,
                                                0.6 * calories_resampled_array,
                                                unscaled_calories_input_lulc)
        if 'ilm' in scenario_dir.lower():
            avg_palm_cal_per_reg_cal = (8516 / 1295) * (
                3000 / 1800
            )  # oil palm cal per kg/ avg crop calkg * (ilm ratio improvement over trend, which is assumed to be same as in earthstat.
            avg_palm_cal_per_reg_cal = 0.1
            unscaled_calories_input_lulc = np.where(
                input_lulc_array == 18,
                avg_palm_cal_per_reg_cal * calories_resampled_array,
                unscaled_calories_input_lulc)

    if 'Tanzania' in ui.root_app.project_folder or 'Ghana' in ui.root_app.project_folder:

        unscaled_calories_input_lulc = np.where(input_lulc_array == 12,
                                                calories_resampled_array, 0)
        unscaled_calories_input_lulc = np.where(
            input_lulc_array == 14, 0.35 * calories_resampled_array,
            unscaled_calories_input_lulc)

        # CUSTOM calc in agroforestry  value following Johan's suggested %
        unscaled_calories_input_lulc = np.where(input_lulc_array == 17,
                                                0.6 * calories_resampled_array,
                                                unscaled_calories_input_lulc)
        if 'ilm' in scenario_dir.lower():
            avg_palm_cal_per_reg_cal = (8516 / 1295) * (
                3000 / 1800
            )  # oil palm cal per kg/ avg crop calkg * (ilm ratio improvement over trend, which is assumed to be same as in earthstat.
            avg_palm_cal_per_reg_cal = 0.1
            unscaled_calories_input_lulc = np.where(
                input_lulc_array == 18,
                avg_palm_cal_per_reg_cal * calories_resampled_array,
                unscaled_calories_input_lulc)

    # Multiply the unscaled calories by this adjustment factor, which is the ratio between the actual calories present
    # calculated from the 5 min resolution data, and the unscaled.
    clipped_calories_per_5m_cell_array = utilities.as_array(
        clipped_calories_per_5m_cell_uri)
    n_calories_present = np.nansum(clipped_calories_per_5m_cell_array)
    n_unscaled_calories_in_baseline = np.nansum(unscaled_calories_baseline)
    adjustment_factor = n_calories_present / n_unscaled_calories_in_baseline

    # Scale the scenario calories by the baseline adjustment factor
    output_calories = unscaled_calories_input_lulc * adjustment_factor
    output_calories_uri = os.path.join(output_dir, 'caloric_production.tif')

    ui.update_run_log('Total calories: ' + str(np.nansum(output_calories)))
    ui.update_run_log('Creating caloric_production.tif')
    utilities.save_array_as_geotiff(output_calories,
                                    output_calories_uri,
                                    input_lulc_uri,
                                    data_type_override=7)
Esempio n. 5
0
def execute(kw, ui):
    if not kw:
        kw = create_default_kw(ui)
    kw['output_folder'] = kw['workspace_dir']

    ui.update_run_log('Calculating crop-specific production')

    baseline_lulc_uri = ui.root_app.project_key_raster

    if not kw.get('model_base_data_dir'):
        kw['model_base_data_dir'] = os.path.join(ui.root_app.base_data_folder,
                                                 'models',
                                                 'nutritional_adequacy')

    bounding_box = hb.get_datasource_bounding_box(ui.root_app.project_aoi)
    ui.update_run_log('Loading input maps. Bounding box set to: ' +
                      str(bounding_box))

    # TODO Unimplemented switch here.
    run_full_nutritional_model = False  # OTW just cal calories
    if run_full_nutritional_model:
        try:
            os.remove(
                os.path.join(kw['output_folder'],
                             'crop_proportion_baseline_500m.tif'))
            os.remove(
                os.path.join(kw['output_folder'],
                             'crop_proportion_baseline_1km.tif'))
            os.remove(
                os.path.join(kw['output_folder'], 'crop_proportion_500m.tif'))
            os.remove(
                os.path.join(kw['output_folder'], 'crop_proportion_1km.tif'))
        except:
            'no'

        match_uri = kw['lulc_uri']

        # Load the LULC array as the baseline
        lulc_baseline = hb.as_array(baseline_lulc_uri)
        no_data_value = hb.get_nodata_from_uri(baseline_lulc_uri)

        # Extract the nan_mask for later
        lulc_nan_mask = np.where(lulc_baseline == no_data_value, 1,
                                 0).astype(np.int8)

        # Calculate the proportion of the grid-cell that is in cultivation as a function of the LULC.
        # For MODIS, this means 12 and 14 are 1.0 and 0.5 respectively.
        crop_proportion_baseline = np.where(lulc_baseline == 12, 1.0, 0.0)
        # TODO START HERE, i missed a nan mask and now the results have near infinite value. create a robust solution
        # crop_proportion_baseline[lulc_nan_mask] = np.nan
        crop_proportion_baseline = np.where(lulc_baseline == 14, .5,
                                            crop_proportion_baseline)

        # BUG If the files are not in the normal folder and onl linked to, it fails to find them.
        try:
            os.mkdir(os.path.join(kw['output_folder'], 'YieldTonsPerCell'))
        except:
            'Dir already exists.'

        clipped_dir = os.path.join(kw['output_folder'],
                                   'crop_production_and_harvested_area')
        try:
            os.mkdir(clipped_dir)
        except:
            'Dir already exists.'
        try:
            os.mkdir(os.path.join(kw['output_folder'], 'nutrient_production'))
        except:
            'Dir already exists.'

        crop_proportion_baseline_500m_uri = os.path.join(
            kw['output_folder'], 'YieldTonsPerCell',
            'crop_proportion_baseline_500m.tif')
        utilities.save_array_as_geotiff(crop_proportion_baseline,
                                        crop_proportion_baseline_500m_uri,
                                        kw['lulc_uri'])
        crop_proportion_baseline_1km_uri = os.path.join(
            kw['output_folder'], 'YieldTonsPerCell',
            'crop_proportion_baseline_1km.tif')

        population_bounding_box = utilities.get_bounding_box(
            kw['population_uri'])
        cell_size = utilities.get_cell_size_from_uri(kw['population_uri'])
        hb.resize_and_resample_dataset_uri(crop_proportion_baseline_500m_uri,
                                           population_bounding_box, cell_size,
                                           crop_proportion_baseline_1km_uri,
                                           'bilinear')

        if kw['lulc_uri'] != baseline_lulc_uri:
            lulc_scenario = utilities.as_array(kw['lulc_uri'])

            crop_proportion = np.where(lulc_scenario == 12, 1.0, 0.0)
            crop_proportion = np.where(lulc_scenario == 14, .5,
                                       crop_proportion)
            crop_proportion_500m_uri = os.path.join(
                kw['output_folder'], 'YieldTonsPerCell',
                'crop_proportion_500m.tif')
            utilities.save_array_as_geotiff(crop_proportion,
                                            crop_proportion_500m_uri,
                                            kw['lulc_uri'])
            crop_proportion_1km_uri = os.path.join(kw['output_folder'],
                                                   'YieldTonsPerCell',
                                                   'crop_proportion_1km.tif')
            # original_dataset_uri, bounding_box, out_pixel_size, output_uri, resample_method
            hb.resize_and_resample_dataset_uri(crop_proportion_500m_uri,
                                               population_bounding_box,
                                               cell_size,
                                               crop_proportion_1km_uri,
                                               'bilinear')

            crop_proportion_baseline_1km = utilities.as_array(
                crop_proportion_baseline_1km_uri)
            crop_proportion_1km = utilities.as_array(crop_proportion_1km_uri)

            change_ratio = np.where(
                crop_proportion_baseline_1km > 0,
                crop_proportion_1km / crop_proportion_baseline_1km, 1.0)

            change_ratio_mean = np.mean(change_ratio)
        else:
            change_ratio_mean = 1.0

        ui.update_run_log('Loading input maps')
        crop_maps_folder = kw['crop_maps_folder']
        nutritional_content_odict = utilities.file_to_python_object(
            kw['nutritional_content_table_uri'], declare_type='2d_odict'
        )  # outputs as OrderedDict([('almond', OrderedDict([('fraction_refuse', '0.6'), ('Protein', '212.2'), ('Lipid', '494.2'), etc
        nutritional_requirements_odict = utilities.file_to_python_object(
            kw['nutritional_requirements_table_uri'],
            declare_type='2d_indexed_odict')

        population = utilities.as_array(kw['population_uri'])
        demographic_groups_list = kw['demographic_groups_list']
        demographics_folder = kw['demographics_folder']

        ui.update_run_log('Calculating crop-specific production')
        lulc_array = utilities.as_array(kw['lulc_uri'])
        lulc_wkt = hb.get_dataset_projection_wkt_uri(kw['lulc_uri'])
        harvested_area_ha_filenames = []
        harvested_area_fraction_filenames = []
        yield_tons_per_ha_filenames = []
        yield_tons_per_cell_filenames = []
        ha_array = 0
        yield_per_ha_array = 0

        # Calculate ha per cell
        cell_size = hb.get_cell_size_from_uri(kw['lulc_uri'])
        ha_per_cell = np.ones(lulc_array.shape) * (cell_size**2 / 10000)
        ha_per_cell_uri = os.path.join(kw['output_folder'], 'ha_per_cell.tif')
        utilities.save_array_as_geotiff(ha_per_cell, ha_per_cell_uri,
                                        kw['lulc_uri'])

        force_recalculation = False
        for folder_name in os.listdir(crop_maps_folder):
            current_folder = os.path.join(crop_maps_folder, folder_name)
            if os.path.isdir(current_folder):
                print('current_folder', current_folder)
                current_crop_name = folder_name.split('_', 1)[0]
                input_harvested_area_fraction_uri = os.path.join(
                    current_folder,
                    current_crop_name + '_HarvestedAreaFraction.tif')
                clipped_harvested_area_fraction_uri = os.path.join(
                    clipped_dir,
                    current_crop_name + '_HarvestedAreaFraction.tif')
                input_yield_tons_per_ha_uri = os.path.join(
                    current_folder, current_crop_name + '_YieldPerHectare.tif')
                clipped_yield_tons_per_ha_uri = os.path.join(
                    clipped_dir, current_crop_name + '_YieldPerHectare.tif')
                yield_tons_per_cell_uri = os.path.join(
                    clipped_dir, current_crop_name + '_YieldTonsPerCell.tif')

                if not os.path.exists(
                        clipped_harvested_area_fraction_uri
                ) or not os.path.exists(
                        clipped_yield_tons_per_ha_uri) or not os.path.exists(
                            yield_tons_per_cell_uri) or force_recalculation:
                    # hb.clip_dataset_uri(input_harvested_area_fraction_uri, kw['aoi_uri'], clipped_harvested_area_fraction_uri)
                    utilities.clip_by_shape_with_buffered_intermediate_uri(
                        input_harvested_area_fraction_uri,
                        kw['aoi_uri'],
                        clipped_harvested_area_fraction_uri,
                        match_uri,
                        resampling_method='bilinear')
                    harvested_area_fraction_array = utilities.as_array(
                        clipped_harvested_area_fraction_uri)

                    # hb.clip_dataset_uri(input_yield_tons_per_ha_uri, kw['aoi_uri'], clipped_yield_tons_per_ha_uri)
                    utilities.clip_by_shape_with_buffered_intermediate_uri(
                        input_yield_tons_per_ha_uri,
                        kw['aoi_uri'],
                        clipped_yield_tons_per_ha_uri,
                        match_uri,
                        resampling_method='bilinear')
                    yield_tons_per_ha_array = utilities.as_array(
                        clipped_yield_tons_per_ha_uri)

                    nan1 = utilities.get_nodata_from_uri(
                        input_harvested_area_fraction_uri)
                    nan2 = utilities.get_nodata_from_uri(
                        input_yield_tons_per_ha_uri)

                    nan_mask = np.where((yield_tons_per_ha_array == nan1) & (
                        harvested_area_fraction_array == nan2))

                    yield_tons_per_cell_array = yield_tons_per_ha_array * harvested_area_fraction_array * ha_per_cell

                    yield_tons_per_cell_array[nan_mask] == nan1

                    # NOTE forcing ndv to zero for calcualtions
                    utilities.save_array_as_geotiff(yield_tons_per_cell_array,
                                                    yield_tons_per_cell_uri,
                                                    kw['lulc_uri'],
                                                    data_type_override=7,
                                                    no_data_value_override=0)

                harvested_area_fraction_filenames.append(
                    clipped_harvested_area_fraction_uri)
                yield_tons_per_ha_filenames.append(
                    clipped_yield_tons_per_ha_uri)
                yield_tons_per_cell_filenames.append(yield_tons_per_cell_uri)

                ui.update_run_log('Creating yield (tons) map for ' +
                                  folder_name)

        match_5min_uri = os.path.join(kw['output_folder'],
                                      'crop_production_and_harvested_area',
                                      'maize_HarvestedAreaFraction.tif')
        # match_5min_uri = os.path.join(ui.root_app.base_data_folder, 'models/crop_production/global_dataset/observed_yield/rice_yield_map.tif')
        match_array = utilities.as_array(match_5min_uri)

        # TODO Figure out if nans all right
        nan3 = utilities.get_nodata_from_uri(
            clipped_harvested_area_fraction_uri)
        array = utilities.as_array(clipped_harvested_area_fraction_uri)
        nan_mask = np.where(array == nan3, True, False).astype(np.bool)

        # TODO Why default to run always?
        # Fat = np.zeros(match_array.shape).astype(np.float64)
        #[Fatnan_mask] = nan3
        Energy = np.zeros(match_array.shape).astype(np.float64)
        Energy[nan_mask] = nan3
        Protein = np.zeros(match_array.shape).astype(np.float64)
        Protein[nan_mask] = nan3
        VitA = np.zeros(match_array.shape).astype(np.float64)
        VitA[nan_mask] = nan3
        VitC = np.zeros(match_array.shape).astype(np.float64)
        VitC[nan_mask] = nan3
        VitE = np.zeros(match_array.shape).astype(np.float64)
        VitE[nan_mask] = nan3
        Thiamin = np.zeros(match_array.shape).astype(np.float64)
        Thiamin[nan_mask] = nan3
        Riboflavin = np.zeros(match_array.shape).astype(np.float64)
        Riboflavin[nan_mask] = nan3
        Niacin = np.zeros(match_array.shape).astype(np.float64)
        Niacin[nan_mask] = nan3
        VitB6 = np.zeros(match_array.shape).astype(np.float64)
        VitB6[nan_mask] = nan3
        Folate = np.zeros(match_array.shape).astype(np.float64)
        Folate[nan_mask] = nan3
        VitB12 = np.zeros(match_array.shape).astype(np.float64)
        VitB12[nan_mask] = nan3
        Ca = np.zeros(match_array.shape).astype(np.float64)
        Ca[nan_mask] = nan3
        Ph = np.zeros(match_array.shape).astype(np.float64)
        Ph[nan_mask] = nan3
        Mg = np.zeros(match_array.shape).astype(np.float64)
        Mg[nan_mask] = nan3
        K = np.zeros(match_array.shape).astype(np.float64)
        K[nan_mask] = nan3
        Na = np.zeros(match_array.shape).astype(np.float64)
        Na[nan_mask] = nan3
        Fe = np.zeros(match_array.shape).astype(np.float64)
        Fe[nan_mask] = nan3
        Zn = np.zeros(match_array.shape).astype(np.float64)
        Zn[nan_mask] = nan3
        Cu = np.zeros(match_array.shape).astype(np.float64)
        Cu[nan_mask] = nan3

        for i in range(len(yield_tons_per_cell_filenames)):
            current_crop_name = os.path.splitext(
                os.path.split(
                    harvested_area_fraction_filenames[i])[1])[0].split('_',
                                                                       1)[0]
            ui.update_run_log('Calculating nutritional contribution of ' +
                              current_crop_name)
            if current_crop_name in nutritional_content_odict.keys():
                print('adding Nutritional content of ' + current_crop_name)
                # Fat += utilities.as_array(yield_tons_per_cell_filenames[i]) * 1000.0 * float(nutritional_content_odict[current_crop_name]['Fat'])
                Energy += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Energy'])
                Protein += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]
                        ['Protein'])
                VitA += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['VitA'])
                VitC += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['VitC'])
                VitE += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['VitE'])
                Thiamin += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]
                        ['Thiamin'])
                Riboflavin += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]
                        ['Riboflavin'])
                Niacin += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Niacin'])
                VitB6 += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['VitB6'])
                Folate += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Folate'])
                VitB12 += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['VitB12'])
                Ca += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Ca'])
                Ph += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Ph'])
                Mg += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Energy'])
                K += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Mg'])
                Na += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['K'])
                Fe += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Fe'])
                Zn += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Energy'])
                Cu += utilities.as_array(
                    yield_tons_per_cell_filenames[i]) * 1000.0 * float(
                        nutritional_content_odict[current_crop_name]['Zn'])

        # TODO make this happen earlier in calcs
        # Fat *= change_ratio_mean
        Energy *= change_ratio_mean
        Protein *= change_ratio_mean
        VitA *= change_ratio_mean
        VitC *= change_ratio_mean
        VitE *= change_ratio_mean
        Thiamin *= change_ratio_mean
        Riboflavin *= change_ratio_mean
        Niacin *= change_ratio_mean
        VitB6 *= change_ratio_mean
        Folate *= change_ratio_mean
        VitB12 *= change_ratio_mean
        Ca *= change_ratio_mean
        Ph *= change_ratio_mean
        Mg *= change_ratio_mean
        K *= change_ratio_mean
        Na *= change_ratio_mean
        Fe *= change_ratio_mean
        Zn *= change_ratio_mean
        Cu *= change_ratio_mean

        # Fat *= change_ratio_mean
        Energy[nan_mask] = 0
        Protein[nan_mask] = 0
        VitA[nan_mask] = 0
        VitC[nan_mask] = 0
        VitE[nan_mask] = 0
        Thiamin[nan_mask] = 0
        Riboflavin[nan_mask] = 0
        Niacin[nan_mask] = 0
        VitB6[nan_mask] = 0
        Folate[nan_mask] = 0
        VitB12[nan_mask] = 0
        Ca[nan_mask] = 0
        Ph[nan_mask] = 0
        Mg[nan_mask] = 0
        K[nan_mask] = 0
        Na[nan_mask] = 0
        Fe[nan_mask] = 0
        Zn[nan_mask] = 0
        Cu[nan_mask] = 0

        match_1km_uri = os.path.join(kw['output_folder'], 'YieldTonsPerCell',
                                     'crop_proportion_baseline_1km.tif')

        utilities.save_array_as_geotiff(
            Energy,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Energy_per_cell_5min.tif'),
            match_5min_uri,
            data_type_override=7,
            no_data_value_override=nan3)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Energy_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Energy_per_cell_1km.tif'), match_1km_uri)
        # utilities.save_array_as_geotiff(Fat, os.path.join(kw['output_folder'], 'nutrient_production', 'Fat_per_cell_5min.tif'), match_5min_uri)
        # utilities.resample_preserve_sum(os.path.join(kw['output_folder'], 'nutrient_production', 'Fat_per_cell_5min.tif'), os.path.join(kw['output_folder'], 'nutrient_production', 'Fat_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Protein,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Protein_per_cell_5min.tif'),
            match_5min_uri,
            no_data_value_override=nan3)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Protein_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Protein_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            VitA,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitA_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitA_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitA_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            VitC,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitC_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitC_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitC_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            VitE,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitE_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitE_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitE_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Thiamin,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Thiamin_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Thiamin_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Thiamin_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Riboflavin,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Riboflavin_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Riboflavin_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Riboflavin_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Niacin,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Niacin_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Niacin_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Niacin_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            VitB6,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitB6_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitB6_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitB6_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Folate,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Folate_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Folate_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Folate_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            VitB12,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitB12_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitB12_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'VitB12_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Ca,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Ca_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Ca_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Ca_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Ph,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Ph_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Ph_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Ph_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Mg,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Mg_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Mg_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Mg_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            K,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'K_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'K_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'K_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Na,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Na_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Na_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Na_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Fe,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Fe_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Fe_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Fe_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Zn,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Zn_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Zn_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Zn_per_cell_1km.tif'), match_1km_uri)
        utilities.save_array_as_geotiff(
            Cu,
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Cu_per_cell_5min.tif'), match_5min_uri)
        utilities.resample_preserve_sum(
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Cu_per_cell_5min.tif'),
            os.path.join(kw['output_folder'], 'nutrient_production',
                         'Cu_per_cell_1km.tif'), match_1km_uri)

        # calculate demand
        calculate_demand = True
        if calculate_demand:
            # TODO FOR HONDURAS need to switch to a non-population-map derived resolution. Incorporate decision making unit map to set the desired resolution
            ui.update_run_log('Calculating total nutrient demand')
            overall_nutrient_sum = 0
            overall_nutrient_requirement_sum = 0
            overall_ratio_array = np.zeros(population.shape)
            overall_ratio = 0
            population_zero_normalized = np.where(population < 0, 0,
                                                  population)
            for nutrient in nutritional_requirements_odict:
                nutrient_uri = os.path.join(kw['output_folder'],
                                            'nutrient_production',
                                            nutrient + '_per_cell_1km.tif')
                nutrient_array = utilities.as_array(nutrient_uri)

                nutrient_requirement_array = population_zero_normalized * float(
                    nutritional_requirements_odict[nutrient]
                    ['recommended_daily_allowance']) * 365.0
                # nutrient_requirement_array[nan_mask] = np.nan
                # nutrient_requirement_array[nutrient_requirement_array<=0] = np.nan

                nutrient_provision_ratio = np.where(
                    (nutrient_array / nutrient_requirement_array > 0) &
                    (nutrient_array / nutrient_requirement_array <
                     999999999999999999999999999999),
                    nutrient_array / nutrient_requirement_array, 0)

                overall_ratio_array += nutrient_provision_ratio
                nutrient_sum = np.nansum(nutrient_array)
                overall_nutrient_sum += nutrient_sum
                nutrient_requirement_sum = np.nansum(
                    nutrient_requirement_array)
                overall_nutrient_requirement_sum += nutrient_requirement_sum
                output_string = 'Full landscape produced ' + str(
                    nutrient_sum
                ) + ' of ' + nutrient + ' compared to a national requirement of ' + str(
                    nutrient_requirement_sum
                ) + ', yielding nutritional adequacy ratio of ' + str(
                    nutrient_sum / nutrient_requirement_sum) + '.'
                ui.update_run_log(output_string)
                overall_ratio += nutrient_provision_ratio
                utilities.save_array_as_geotiff(
                    nutrient_provision_ratio,
                    nutrient_uri.replace('_per_cell_1km.tif',
                                         '_adequacy_ratio.tif'), nutrient_uri)

            overall_ratio_array *= 1.0 / 19.0
            overall_ratio = (1.0 / 19.0) * (overall_nutrient_sum /
                                            overall_nutrient_requirement_sum)

            output_string = 'Overall nutrion adequacy ratio: ' + str(
                overall_ratio) + '.'
            ui.update_run_log(output_string)
            overall_ratio_uri = os.path.join(kw['output_folder'],
                                             'overall_adequacy_ratio.tif')
            utilities.save_array_as_geotiff(overall_ratio_array,
                                            overall_ratio_uri, nutrient_uri)

    run_calories_only_model = True
    if run_calories_only_model:
        calc_caloric_production_from_lulc_uri(kw['lulc_uri'], ui=ui, **kw)

    return