123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- function prepare_FAO2022TOT_data(source_name, last_hist_year)
- global CONSTANTS;
- errorID = 'CreateDatabase:DataPreparation:FAO:PrepareFAO2022Data:';
- narginchk(0, 2)
- if nargin == 2
- source = source_name;
- elseif nargin == 1
- if ischar(source_name)
- source = source_name;
- last_hist_year = 0;
- else
- source = 'FAO2022P2';
- end
- else
- source = 'FAO2022P2';
- last_hist_year = 0;
- end
- sourceSuffix = 'I';
- procSource = [source sourceSuffix];
- scenario = 'HISTORY';
- scenarioProjection = 'PROJECTION';
- nYearKey = 5;
- allProcTablenames = get_table_sheetcodes_for({CONSTANTS.nameOfSheetSource}, {{procSource}});
- remove_tables_from_database(allProcTablenames);
- copy_source_scenario_tables_from_to(source, scenario, procSource, scenario);
- allTablenames = get_table_sheetcodes_for({CONSTANTS.nameOfSheetSource}, ...
- {{procSource}});
- warning off Tools:AssertValidcategoryforSourceScenario:ValidCategory
- theseTables = struct;
- for iTable = 1:length(allTablenames)
-
- newTable = adjust_countries(allTablenames{iTable}, nYearKey, false);
- if isempty(fieldnames(newTable))
- primap_log([errorID 'EmptyTable'], CONSTANTS.ERRuserErrorWarning, ...
- ['Table ' allTablenames{iTable} ' empty after country adjustment and removal of NaN and zero data']);
- else
- theseTables.(newTable.(CONSTANTS.nameOfSheetCode)) = newTable;
- end
- end
- remove_tables_from_database(allTablenames);
- success = overwrite_or_add_tables_in_database(theseTables, true, false);
- if ~all(success)
- primap_log([errorID 'NotAllSplitTablesAdded'], CONSTANTS.ERRprogramError, ...
- 'Some downscaled AGRI tables not added to DB. Check preceeding messages for reason');
- end
- if last_hist_year > 0
-
- newTables = struct;
- allTablenames = get_table_sheetcodes_for({CONSTANTS.nameOfSheetSource}, {{procSource}});
- for iTable = 1 : length(allTablenames)
-
- currentTable = get_table_from_database(allTablenames{iTable});
-
- if any(currentTable.(CONSTANTS.nameOfYearVector) > last_hist_year)
-
- currentTableProj = currentTable;
- currentTableProj.(CONSTANTS.nameOfDataField) = currentTableProj.(CONSTANTS.nameOfDataField)(:, ...
- currentTableProj.(CONSTANTS.nameOfYearVector) > last_hist_year);
- currentTableProj.(CONSTANTS.nameOfYearVector) = ...
- currentTableProj.(CONSTANTS.nameOfYearVector)(currentTableProj.(CONSTANTS.nameOfYearVector) > last_hist_year);
- currentTableProj.(CONSTANTS.nameOfSheetScenario) = scenarioProjection;
- currentTableProj.(CONSTANTS.nameOfSheetCode) = concatenate_structure_field_values(currentTableProj, ...
- CONSTANTS.tableNameConstructionFromSheets);
- currentTableProj = delete_only_nan_containing_countries(currentTableProj);
- if ~isempty(fieldnames(currentTableProj))
- newTables.(currentTableProj.(CONSTANTS.nameOfSheetCode)) = currentTableProj;
- end
-
- currentTableHist = currentTable;
- currentTableHist.(CONSTANTS.nameOfDataField) = currentTableHist.(CONSTANTS.nameOfDataField)(:, ...
- currentTableHist.(CONSTANTS.nameOfYearVector) <= last_hist_year);
- currentTableHist.(CONSTANTS.nameOfYearVector) = ...
- currentTableHist.(CONSTANTS.nameOfYearVector)(currentTableHist.(CONSTANTS.nameOfYearVector) <= last_hist_year);
- currentTableHist = delete_only_nan_containing_countries(currentTableHist);
- newTables.(currentTableHist.(CONSTANTS.nameOfSheetCode)) = currentTableHist;
- end
- end
- success = overwrite_or_add_tables_in_database(newTables, true, false);
- if ~all(success)
- primap_log([errorID 'NotAllSplitTablesAdded'], CONSTANTS.ERRprogramError, ...
- 'Some split tables not added to DB. Check preceeding messages for reason');
- end
- end
- defaultParams=struct;
- defaultParams.future.type='none';
- defaultParams.future.fityears=15;
- defaultParams.past.type='none';
- defaultParams.past.fityears=20;
- defaultParams.past.fallback='none';
- FAOEntities = {'CO2', 'CH4', 'N2O'};
- categories = struct;
- categories.IPC3A = struct;
- categories.IPC3A.targetCategory = 'IPC3A';
- categories.IPC3A.subcategories = {'IPC3A1', 'IPC3A2'};
- categories.IPC3A.entities = FAOEntities;
- categories.IPC3A.params = defaultParams;
- categories.IPCM3C1AG = struct;
- categories.IPCM3C1AG.targetCategory = 'IPCM3C1AG';
- categories.IPCM3C1AG.subcategories = {'IPC3C1C', 'IPC3C1B'};
- categories.IPCM3C1AG.entities = FAOEntities;
- categories.IPCM3C1AG.params = defaultParams;
- categories.IPC3C1 = struct;
- categories.IPC3C1.targetCategory = 'IPC3C1';
- categories.IPC3C1.subcategories = {'IPCM3C1AG', 'IPCM3C1LU'};
- categories.IPC3C1.entities = FAOEntities;
- categories.IPC3C1.params = defaultParams;
- categories.IPC3C4 = struct;
- categories.IPC3C4.targetCategory = 'IPC3C4';
- categories.IPC3C4.subcategories = {'IPCM3C4AG'};
- categories.IPC3C4.entities = FAOEntities;
- categories.IPC3C4.params = defaultParams;
- categories.IPC3C = struct;
- categories.IPC3C.targetCategory = 'IPC3C';
- categories.IPC3C.subcategories = {'IPC3C1', 'IPCM3C45AG', 'IPC3C7'};
- categories.IPC3C.entities = FAOEntities;
- categories.IPC3C.params = defaultParams;
- categories.IPCM3CAG = struct;
- categories.IPCM3CAG.targetCategory = 'IPCM3CAG';
- categories.IPCM3CAG.subcategories = {'IPCM3C1AG', 'IPCM3C45AG', 'IPC3C7'};
- categories.IPCM3CAG.entities = FAOEntities;
- categories.IPCM3CAG.params = defaultParams;
- categories.IPCMAG = struct;
- categories.IPCMAG.targetCategory = 'IPCMAG';
- categories.IPCMAG.subcategories = {'IPC3A', 'IPCM3CAG'};
- categories.IPCMAG.entities = FAOEntities;
- categories.IPCMAG.params = defaultParams;
- categories.IPCMAGELV = struct;
- categories.IPCMAGELV.targetCategory = 'IPCMAGELV';
- categories.IPCMAGELV.subcategories = {'IPCM3CAG'};
- categories.IPCMAGELV.entities = FAOEntities;
- categories.IPCMAGELV.params = defaultParams;
- entities = struct;
- entities.KYOTOGHG = struct;
- entities.KYOTOGHG.targetEntity = 'KYOTOGHG';
- entities.KYOTOGHG.subEntities = {'CO2', 'CH4', 'N2O'};
- entities.KYOTOGHG.categories = {'IPC3', 'IPC3A', 'IPC3C', 'IPCMAG', 'IPCMAGELV', 'IPCMLULUCF'};
- entities.KYOTOGHG.params = defaultParams;
- entities.KYOTOGHG.GWP = CONSTANTS.gwpSARSpecification;
- entities.KYOTOGHGAR4 = struct;
- entities.KYOTOGHGAR4.targetEntity = 'KYOTOGHGAR4';
- entities.KYOTOGHGAR4.subEntities = {'CO2', 'CH4', 'N2O'};
- entities.KYOTOGHGAR4.categories = {'IPC3', 'IPC3A', 'IPC3C', 'IPCMAG', 'IPCMAGELV', 'IPCMLULUCF'};
- entities.KYOTOGHGAR4.params = defaultParams;
- entities.KYOTOGHGAR4.GWP = CONSTANTS.gwpAR4Specification;
- entities.KYOTOGHGAR5 = struct;
- entities.KYOTOGHGAR5.targetEntity = 'KYOTOGHGAR5';
- entities.KYOTOGHGAR5.subEntities = {'CO2', 'CH4', 'N2O'};
- entities.KYOTOGHGAR5.categories = {'IPC3', 'IPC3A', 'IPC3C', 'IPCMAG', 'IPCMAGELV', 'IPCMLULUCF'};
- entities.KYOTOGHGAR5.params = defaultParams;
- entities.KYOTOGHGAR5.GWP = CONSTANTS.gwpAR5Specification;
- entities.KYOTOGHGAR6 = struct;
- entities.KYOTOGHGAR6.targetEntity = 'KYOTOGHGAR6';
- entities.KYOTOGHGAR6.subEntities = {'CO2', 'CH4', 'N2O'};
- entities.KYOTOGHGAR6.categories = {'IPC3', 'IPC3A', 'IPC3C', 'IPCMAG', 'IPCMAGELV', 'IPCMLULUCF'};
- entities.KYOTOGHGAR6.params = defaultParams;
- entities.KYOTOGHGAR6.GWP = CONSTANTS.gwpAR6Specification;
- aggregate_and_extrapolate_source(procSource, scenario, categories, entities);
- aggregate_and_extrapolate_source(procSource, scenarioProjection, categories, entities);
- remove_desired_regions_from_source_scenario({'EARTH', 'POLYNESIA'}, procSource, scenario);
- remove_desired_regions_from_source_scenario({'EARTH', 'POLYNESIA'}, procSource, scenarioProjection);
- sum_countries_primap(procSource, false);
- warning off Tools:AssertValidcategoryforSourceScenario:ValidCategory
- end
- function newTable = adjust_countries(tablename, nYearKey, negativeToZero)
- global CONSTANTS;
- errorID = 'CreateDatabase:DataPreparation:FAO:PrepareFAO2016Data:AdjustCountries';
- thisTable = get_table_from_database(tablename);
-
-
- thisTable.(CONSTANTS.nameOfSheetDescr) = [thisTable.(CONSTANTS.nameOfSheetDescr) ...
- ', regions summed and downscaled to country time series'];
- source = thisTable.(CONSTANTS.nameOfSheetSource);
- if negativeToZero
-
- isneg = thisTable.data < 0;
- thisTable.data(isneg) = 0;
- end
-
- thisTable = delete_only_nan_containing_countries(thisTable);
-
- thisTable = delete_only_zero_containing_countries(thisTable);
- if ~isempty(fieldnames(thisTable))
-
-
-
-
-
-
-
-
- regions2Delete = {'FSU'};
- theseCountries = get_members_of(regions2Delete{1});
- yearKey = 1992;
- thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
-
- regions2Delete = {'SRBMNE'};
- theseCountries = get_members_of(regions2Delete{1});
- yearKey = 2006;
- thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
-
- regions2Delete = {'YUG'};
- theseCountries = get_members_of(regions2Delete{1});
- yearKey = 1992;
- thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
-
- regions2Delete = {'CZESVK'};
- theseCountries = get_members_of(regions2Delete{1});
- yearKey = 1992;
- thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
-
-
- regions2Delete = {'TTPI'};
- theseCountries = get_members_of(regions2Delete{1});
- yearKey = 1995;
- thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
-
- regions2Delete = {'BELLUX'};
- theseCountries = get_members_of(regions2Delete{1});
- yearKey = 2000;
- thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
-
- regions2Delete = {'ETHERI'};
- theseCountries = get_members_of(regions2Delete{1});
- yearKey = 1993;
- thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
-
- if any(strcmp(source, {'FAO2016P', 'FAO2018P', 'FAO2019AP', 'FAO2019BP', 'FAO2020AP', 'FAO2020BP', ...
- 'FAO2020CP', 'FAO2021AP'}))
- regions2Delete = {'SDN'};
- theseCountries = {'SDN', 'SSD'};
- yearKey = 2012;
- thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
- end
-
- if any(strcmp(source, {'FAO2023AI', 'FAO2024AI'}))
- regions2Delete = {'SDNSSD'};
- theseCountries = {'SDN', 'SSD'};
- yearKey = 2012;
- thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
- end
-
- dataFSM = get_cydata_from_independent_table(thisTable, {'FSM'}, 'all');
- if ~all(isnan(dataFSM.(CONSTANTS.nameOfDataField)))
- gaps = get_gaps(dataFSM.(CONSTANTS.nameOfDataField));
- if ~isempty(gaps)
- dataFSM.(CONSTANTS.nameOfDataField) = interpolate_in_single_dv(gaps, dataFSM.(CONSTANTS.nameOfDataField));
- thisTable = add_cydata_to_independent_table(thisTable, dataFSM);
- end
- end
-
- thisTable = delete_only_nan_containing_countries(thisTable);
-
- newTable = delete_only_zero_containing_countries(thisTable);
-
- else
- newTable = struct;
- end
- end
- function thisTable = downscale_existing(thisTable,regions2Delete,theseCountries, yearKey, nYearKey, remove)
-
-
- global CONSTANTS
- errorID = 'CreateDatabase:DataPreparation:FAO:PrepareFAO2016Data:DownscaleExisting';
- if ~exist('remove', 'var')
- remove = true;
- end
- cyRegion = get_cydata_from_independent_table(thisTable,regions2Delete,'all');
- if isempty(cyRegion.(CONSTANTS.nameOfCountryVector))
- return;
- end
- cyKey = get_cydata_from_independent_table(thisTable,theseCountries,'all');
- cyDownscaled = cyRegion;
- cyDownscaled.(CONSTANTS.nameOfCountryVector) = cyKey.(CONSTANTS.nameOfCountryVector);
-
- if ~all(isnan(cyRegion.(CONSTANTS.nameOfDataField)) | cyRegion.(CONSTANTS.nameOfDataField) == 0)
-
- iYearKey = find(cyKey.(CONSTANTS.nameOfYearVector) == yearKey);
- cyKey.(CONSTANTS.nameOfYearVector) = cyKey.(CONSTANTS.nameOfYearVector)(iYearKey:end);
- cyKey.(CONSTANTS.nameOfDataField) = cyKey.(CONSTANTS.nameOfDataField)(:,iYearKey:end);
-
- iYearStart = find(~isnan(nansum_primap(cyKey.(CONSTANTS.nameOfDataField),1)) & ...
- (nansum_primap(cyKey.(CONSTANTS.nameOfDataField),1) ~= 0));
- if isempty(iYearStart)
-
- primap_log([errorID 'ProgramError'], CONSTANTS.ERRuserErrorWarning, ...
- ['Downscaling not possible for region ' regions2Delete{1} ...
- ', year(s) for downscaling key have only zeros or NaN. Table: ' ...
- thisTable.(CONSTANTS.nameOfSheetCode)]);
- if remove
- thisTable = remove_countries_from_independent_table(thisTable,regions2Delete);
- end
- return
- end
-
- yearStart = cyKey.(CONSTANTS.nameOfYearVector)(iYearStart(1));
- iYearStartRegion = find(cyDownscaled.(CONSTANTS.nameOfYearVector) == yearStart);
- cyDownscaled.(CONSTANTS.nameOfDataField) = cyDownscaled.(CONSTANTS.nameOfDataField)(:, 1 : iYearStartRegion - 1);
- cyDownscaled.(CONSTANTS.nameOfYearVector) = cyDownscaled.(CONSTANTS.nameOfYearVector)(1 : iYearStartRegion - 1);
-
- cyKey.(CONSTANTS.nameOfYearVector) = cyKey.(CONSTANTS.nameOfYearVector)(iYearStart(1):end);
- cyKey.(CONSTANTS.nameOfDataField) = cyKey.(CONSTANTS.nameOfDataField)(:,iYearStart(1):end);
-
-
- if length(cyKey.(CONSTANTS.nameOfYearVector)) > nYearKey
- cyKey.(CONSTANTS.nameOfYearVector) = cyKey.(CONSTANTS.nameOfYearVector)(1:nYearKey);
- cyKey.(CONSTANTS.nameOfDataField) = cyKey.(CONSTANTS.nameOfDataField)(:,1:nYearKey);
- end
- key = nansum_primap(cyKey.(CONSTANTS.nameOfDataField),2);
- arrRatio = repmat(key/nansum_primap(key), ...
- 1,length(cyDownscaled.(CONSTANTS.nameOfYearVector)));
- cyDownscaled.(CONSTANTS.nameOfDataField) = arrRatio .* ...
- repmat(cyDownscaled.(CONSTANTS.nameOfDataField),length(cyKey.(CONSTANTS.nameOfCountryVector)),1);
- cyDownscaled = delete_only_nan_containing_countries(cyDownscaled);
-
- thisCydata = get_cydata_from_independent_table(thisTable,theseCountries,'all');
-
- warning off Tools:AssertContentOfCydata:FailedValidation
- thisNewCydata = add_cydata_to_cydata(thisCydata, cyDownscaled);
-
- warning on Tools:AssertContentOfCydata:FailedValidation
- if remove
- thisTable = remove_countries_from_independent_table(thisTable,regions2Delete);
- end
- thisTable = add_cydata_to_independent_table(thisTable,thisNewCydata);
- else
- if remove
- thisTable = remove_countries_from_independent_table(thisTable,regions2Delete);
- end
- end
- end
|