Error

There will be errors when there are no irrigated crop as in this case ir_crop_gdx in combine_inputs will result in NA values that are not allowed in the GAMS file.

Error

harmonize_inputs - split_harmonized_inputs - select_grid_cells - rank_cl In case for a certain adm2, pa is very low - even lower than the available cropland in the first grid, there will be an error because no grid cell is selected, while the first grid cell should be selected. An extra rule is needed that adds a at least the grid cell with the highest rank.

Error

When there is no irrigation in a country (or adm1 if model_solve = 1), crop_area_gdx and/or crop_ir_gdx will be empty in combine_inputs_adm_level, resulting in an error => added if else statements in combine_inputs_adm_level. However GAMS needs positive values for ir_crop so this gives an error, need to update the GAMS code for these cases.

make ffunction visible

run_gams_adm_level() can be very useful to run a single adm1 model for large countries => rename into run_model_adm_level, document and export so it can be accessed as function.

Package protection

gdxrrd

Artificial adm calculations

all cropping systems irrigated gives warnings as some priors assume values.

Loading files, also when gdalwarp is called

Import of packages

Only ggplot now but need to check what is really necessary

Doi badge to paper and sticker

https://github.com/GuangchuangYu/badger https://github.com/GuangchuangYu/hexSticker

create spam folders

check if input is consistently zero (eg when there are no subsistence systems, rps is zero) in combine_inputs script.

If data is zero, it cannot be written to a GAMS file. Added rough code to filter out set and data related to subsistence but this check should be done for all. Most likely GAMS will not be able to read the file in case of fitness model as required data (e.g when subsistence is fully missing) is not present.

Joe used adm_level = 1 but with adm2 data, resulting duplicated grid ID in adm_map_r because of joing on adm1 instead of 2. We can add a warning that if adm_level2 is selected, adm2 data cannot be present in the map when rasterizing!

Small adms

Error messages

create_statistics_template("ha", param) gives error if adm_list does not exist => need to improve message.

KEY!! Overlay of grid and acc shows that the extent is the same BUT actual selected cells are different on the edges (5 arcmin grid)!!! This means that allign rasters in combination with cutline does not select the same cells> Probably easiest solution is to use gdal to create the raster....perhaps create (1) create raster of ALL cellS touched by polygon and then run gdal...

Finish documentation and functions

Subnational statistics

TODO FABLE

To do

update 04_synergy_cropland for now using SASAM product compare_processed statistics compare raw statistics remove cl maps and df in harmonization file when they are redundant to save memory space Create separated functions to calculate priors for each system so they can be easily adjusted. Check this part on using .data$x to make sure data is in the local environment: https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html Check what happens when CHECK: SPAM uses additional constraint on artificial adms, for which no slack is allowed. Not applied at the moment

To do for running over adm.

Functions to add

Process raw subnational statistics file.

Code to rebalance ADM totals does not account for cases when, say, all ADM2 units for maize have a value except for one (or more) and the sum of the ADMs where values are not NA are nearly the same as the total of ADM2 but not identical due to rounding. In these cases ADM1 will not be updated as there are missing values. The result is that when the artificial ADMs are constructed the artificial ADM that represents all maize will have a very small value which creates problems in GAMS. Write a function to checks for these cases and presents a warning. Perhaps introduce a threshold. If |sum(ADM2)-ADM1| < 10 Replace and set missing ADM2 to 0. This also means that no artificial adms will be created with pa = 0 as is the case for tea in the present data set.

Wrap rebalancing code in function.

Balancing code is not correct as it does not replace, say ADM1 when there is missing information at ADM2. This is ONLY correct if ADM1 > sum(ADM2). If not there is an inconsistency. In previous code. We also had an extra rule:

Also replace cases when sum(ADM2) > ADM1 and is.na(ADM2) Set is.na(ADM2) to 0.

Also check if it has adm0, adm1 and adm2 (if specified) data. Sometimes adm0 data is not added by mistake, causing errors.

Check if adm0, adm1, adm adm2 are consistent cross ha, ci and fs!

Introduce class subnat stat?!

Perhaps introduce a class subnat_stat, which always consists of subnatstat in wide format.

And has passed a number of checks so it is always consistent.

CHECK that need to be added: When running solve_level = 1, if data is complete at that level, i.e. if data is available for all crops at ADM1 level!

Scaling to FAOSTAT.

We do not scale if there are missing values and sum(ADM1)<faostat. Here the same rule should be applied. If the sum is very close to FAOSTAT, missing values should be set to 0.

Compare adm

Build check function that compares adm_map_list, which should be leading, with adm stat

production systems

Add in 02_agricultural statistics a script that creates overview of export shares of crops to determine system. Also add script that creates template for setting shares. Including pre-processing share of irrigated.

Running at ADM0

Need to start at beginning and add code to make this work, for example in Combine statistics. Best is to use MWI as test case and split into three parts, one for each ADM1.

Irrigated area

unclear how to deal with resolution higher than 30arcsec. At the moment gia is the preferred map but if we warp this to 30 arcsec. share of irrigated area will be very low so unclear how ranking will result. Also in case there is not ir area (CL > IR) SPAM uses the rule that CL = IR), which can be implemented at 5min arcsec but probably not at 30 arcsec

Maybe have to use same rule as used for gia to run GMIA as it it might be that warping a 5min map to 5min changes the results.

CHECK GMIA raster is smaller than GIA (although extent etc is the same). It does not seem to include all rasters that are located on the border. Probably is related with the fact that GMIA is already at 5 arcmin. CHECK if this has impact, probably so as this excludes border cells.

Load_inputs funtion

Should read file list from an internal file with names, not hardcode. Perhaps also create function that loads data as there is a lot of repetition.

Similarly there is a need to make the other paths variable. At the moment, load spatial has a hard coded folder, this should become a variable so it is taken from the param. Maybe better to add these to the parameters file

Check script

In MOZ lc appeared to be negative in GDX file, giving error that log <0. Need to create a check file that also checks if all inputs are positive. Add to GAMS code as well like in original scripts

A the moment we are only using replacement crops for same system (e.g. teas_I for coff_I, which both might have 0). Perhaps use coff_H as replacement for coff_I)

Add check if files exist before they are loaded!

Help information in prepare model functions

Functions to fix

NB: run all commands in backward order to see if errors are produced that show input data is missing. Add @title on top, then @decription and then @details (see mapspamc_par for example)

create_folders: add res subfolder to all maps and other processed folders where needed. aggregate_to_adm: ensure that param and alt_param are for the same model apart from adm_level align_raster: validate input format create_all_tif: check if results file exist. create_mapspamc_folders: check if all (too many) folders are created load_data: reprogram and check if data is available harmonize_inputs and split_harmonized_inputs: add much more help info, check all and in particular generation of log. Set maximim to slackp and slackn. Check why slack for ia= max and for cl = min prepara_physical_area: problem with cat which somehow seems to be surpressed. prepare_cropland_tp1": Move to development branch prepare_priors_and_scores: add documentation reaggregate_statistics: add documentation

Chick if all mapping files are relevant and complete

Calculation of priors

In the present case it is possible that a warning shows up that something is divided by zero when calculating the priors/max score: Problem while computing prior = 100 * (prior - min(prior, na.rm = T)) / .... i no non-missing arguments to min; returning Inf.

This is produced by split_priors, the function where the priors are calculated. The calculation of the prior uses the minimum and maximum of the function to create the prior/score ranking. In case all values are zero (because GAEZ does not have values for this crop/region), there is a divide by zero resulting in NAN values, resulting in a warning. The NAN values are replaced by 0 so there is no real error but it does mean that prior/scores are zero for all crops of the system for which the prior/score is calculated, in turn, resulting in a somewhat random distribution, or at least one that does not take the prior/score into account. The error has the most chance of occuring for irrigated crops and with model_solution 1 as it is possible that suitability is 0 for a certain region. Also suitability for irrigated crops is much often zero than for non-irrigated crops. An example when this happens is ooil_I in ETH6. We might need to add a warning for this in the log file so the user is aware. A solution for this would be the possibility to add more replace crops and also allow for example ooil_H to replace ooil_I. Having zero suitability for all grid cells for a crop that needs to be allocated in that area is a problem in any case so best would be the ensure that always suitability > 0 for a crop in a certain area.

Probably it would be better to create separate functions for each prior/score calculation so they can be easily updated.

Checking statistics

In check and calibrate statistics, FAO is compared to subnat stat and if FAO has data for a crop, it is added add the national level. NOTE that now adm1 level data might have become incomplete resulting in errors when running soolution_model = 1. We NEED to add a function that checks if the data is complete at adm1 level (and gives a warning when model is run at ADM1!!

Need to add a function that checks if adm_list from polygon is identical to subnat stat! Again, I had a case in which adm0_name was ET in the polygon and ETH in the statistics resulting in problems.

Also need a function that compares if adm_list is consistent between ci, fs and ha

Naming if files

Names of intermediate files is not completely consistent with model type (misses adm and solve level. Perhaps add to file name in some sort of short form?)

Log file

Solve level 1

zero biophysical suitability

Gives problems in max score model as zero bs implies zero rps for that crop. If subsistence crop needs to be allocated this will not work because the Constraint 6 - Subsistence allocation should be similar to rural population share in sample will be infeasible.



michielvandijk/mapspamc documentation built on April 17, 2025, 7:31 p.m.