prepare_FAO2022TOT_data.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. function prepare_FAO2022TOT_data(source_name, last_hist_year)
  2. % Prepares FAO data into country time series. Countries which were
  3. % split up during the course of history are downscaled to their current
  4. % members using the shares that were present in the first year in which all
  5. % members had emission values in the CDIAC source. countries which are now
  6. % merged over time get their previous members added up to the country with
  7. % the current political boundaries.
  8. % The resulting table is: CO2_CATM0EL_TOTAL_NET_HISTORY_CDIAC2010PROC
  9. %
  10. % author : CC
  11. % version : 20120315
  12. % version : 20120403 JG - substitute negative data by zero, fixed
  13. % version : 20131024 JG - CDIAC2013 based on the CDIAC2011 file
  14. % combine_subregions
  15. % version : 20150416 JG - delete time series consisting only of zeros
  16. % version : 20150511 JG - fixed wrong region downscaling
  17. % version : 20150715 JG - summing to primap country definitions
  18. % version : 20170120 JG - 2016 data
  19. % version : 20170825 JG - IPCC 2006 categories
  20. % version : 20190708 JG - convert to Gg from GgCO2eq for all single gas tables
  21. % version : 20201009 JG - 2020A data which needs extrpolation as not all sectors have been updated
  22. % version : 20201207 JG - 2020B data
  23. %% issues FAO
  24. % gap in Micronesia data (sometimes filled with zero data)
  25. % gap in Maldives data (sometimes filled with zero data)
  26. % Saint kitts and Newis. very low data in last years (CH4, IPC3A, IPC3C)
  27. % Afghanistan: very low data in last year (CH4, IPC3C)
  28. % many countries: very low data before 1990 and sometimes also in last year (CH4, IPC3C)
  29. % this is probably due to mixing LU and AGRI data, where LU data only covers 1990-2015
  30. % while AGRI data covers 1960 - 2016 (same for N2O)
  31. % saint Lucia: data only until 1980 (CH4, IPC3C)
  32. %%%% drained organic soils current omitted
  33. global CONSTANTS;
  34. errorID = 'CreateDatabase:DataPreparation:FAO:PrepareFAO2022Data:';
  35. narginchk(0, 2)
  36. if nargin == 2
  37. source = source_name;
  38. elseif nargin == 1
  39. if ischar(source_name)
  40. source = source_name;
  41. last_hist_year = 0;
  42. else
  43. source = 'FAO2022P2';
  44. end
  45. else
  46. source = 'FAO2022P2';
  47. last_hist_year = 0;
  48. end
  49. sourceSuffix = 'I';
  50. %tempSuffix = 'P';
  51. procSource = [source sourceSuffix];
  52. %tempSource = [source tempSuffix];
  53. scenario = 'HISTORY';
  54. scenarioProjection = 'PROJECTION';
  55. %GWPspec = CONSTANTS.gwpSARSpecification; % make source dependent if necessary
  56. nYearKey = 5; % number of years to consider for downscaling key
  57. %% TODO check if extrapolation of subsectors needed
  58. %% remove existing proc source
  59. allProcTablenames = get_table_sheetcodes_for({CONSTANTS.nameOfSheetSource}, {{procSource}});%, tempSource}});
  60. remove_tables_from_database(allProcTablenames);
  61. %% copy to temp source
  62. copy_source_scenario_tables_from_to(source, scenario, procSource, scenario);
  63. %% adjust countries
  64. % first aggregate what are now parts of countries into current countries,
  65. % by adding parts + country.
  66. allTablenames = get_table_sheetcodes_for({CONSTANTS.nameOfSheetSource}, ...
  67. {{procSource}});
  68. warning off Tools:AssertValidcategoryforSourceScenario:ValidCategory
  69. theseTables = struct;
  70. % for each emissions table, make these group manipulations
  71. for iTable = 1:length(allTablenames)
  72. % adjust countries
  73. newTable = adjust_countries(allTablenames{iTable}, nYearKey, false);
  74. if isempty(fieldnames(newTable))
  75. primap_log([errorID 'EmptyTable'], CONSTANTS.ERRuserErrorWarning, ...
  76. ['Table ' allTablenames{iTable} ' empty after country adjustment and removal of NaN and zero data']);
  77. else
  78. theseTables.(newTable.(CONSTANTS.nameOfSheetCode)) = newTable;
  79. end
  80. end
  81. %%% add tables to database
  82. remove_tables_from_database(allTablenames);
  83. success = overwrite_or_add_tables_in_database(theseTables, true, false);
  84. if ~all(success)
  85. primap_log([errorID 'NotAllSplitTablesAdded'], CONSTANTS.ERRprogramError, ...
  86. 'Some downscaled AGRI tables not added to DB. Check preceeding messages for reason');
  87. end
  88. %% split into historical data and projection in case projections are available and last_hist_year is given
  89. if last_hist_year > 0
  90. % get all tablenames
  91. newTables = struct;
  92. allTablenames = get_table_sheetcodes_for({CONSTANTS.nameOfSheetSource}, {{procSource}});
  93. for iTable = 1 : length(allTablenames)
  94. % get table from DB
  95. currentTable = get_table_from_database(allTablenames{iTable});
  96. % check if table contains projection values
  97. if any(currentTable.(CONSTANTS.nameOfYearVector) > last_hist_year)
  98. % create projection table
  99. currentTableProj = currentTable;
  100. currentTableProj.(CONSTANTS.nameOfDataField) = currentTableProj.(CONSTANTS.nameOfDataField)(:, ...
  101. currentTableProj.(CONSTANTS.nameOfYearVector) > last_hist_year);
  102. currentTableProj.(CONSTANTS.nameOfYearVector) = ...
  103. currentTableProj.(CONSTANTS.nameOfYearVector)(currentTableProj.(CONSTANTS.nameOfYearVector) > last_hist_year);
  104. currentTableProj.(CONSTANTS.nameOfSheetScenario) = scenarioProjection;
  105. currentTableProj.(CONSTANTS.nameOfSheetCode) = concatenate_structure_field_values(currentTableProj, ...
  106. CONSTANTS.tableNameConstructionFromSheets);
  107. currentTableProj = delete_only_nan_containing_countries(currentTableProj);
  108. if ~isempty(fieldnames(currentTableProj))
  109. newTables.(currentTableProj.(CONSTANTS.nameOfSheetCode)) = currentTableProj;
  110. end
  111. % create historical table
  112. currentTableHist = currentTable;
  113. currentTableHist.(CONSTANTS.nameOfDataField) = currentTableHist.(CONSTANTS.nameOfDataField)(:, ...
  114. currentTableHist.(CONSTANTS.nameOfYearVector) <= last_hist_year);
  115. currentTableHist.(CONSTANTS.nameOfYearVector) = ...
  116. currentTableHist.(CONSTANTS.nameOfYearVector)(currentTableHist.(CONSTANTS.nameOfYearVector) <= last_hist_year);
  117. currentTableHist = delete_only_nan_containing_countries(currentTableHist);
  118. newTables.(currentTableHist.(CONSTANTS.nameOfSheetCode)) = currentTableHist;
  119. end
  120. end
  121. success = overwrite_or_add_tables_in_database(newTables, true, false);
  122. if ~all(success)
  123. primap_log([errorID 'NotAllSplitTablesAdded'], CONSTANTS.ERRprogramError, ...
  124. 'Some split tables not added to DB. Check preceeding messages for reason');
  125. end
  126. end
  127. %% construct higher level IPC categories
  128. % general parameters
  129. defaultParams=struct;
  130. %defaultParams.future.type='linear';
  131. defaultParams.future.type='none';
  132. defaultParams.future.fityears=15;
  133. %defaultParams.past.type='linear';
  134. defaultParams.past.type='none';
  135. defaultParams.past.fityears=20;
  136. defaultParams.past.fallback='none';
  137. FAOEntities = {'CO2', 'CH4', 'N2O'};
  138. categories = struct;
  139. %%% IPC3A = IPC3A1 + IPC3A2
  140. categories.IPC3A = struct;
  141. categories.IPC3A.targetCategory = 'IPC3A';
  142. categories.IPC3A.subcategories = {'IPC3A1', 'IPC3A2'};
  143. categories.IPC3A.entities = FAOEntities;
  144. categories.IPC3A.params = defaultParams;
  145. %%% IPCM3C1AG = IPC3C1B + IPC3C1C
  146. categories.IPCM3C1AG = struct;
  147. categories.IPCM3C1AG.targetCategory = 'IPCM3C1AG';
  148. categories.IPCM3C1AG.subcategories = {'IPC3C1C', 'IPC3C1B'};
  149. categories.IPCM3C1AG.entities = FAOEntities;
  150. categories.IPCM3C1AG.params = defaultParams;
  151. %%% IPC3C1 = IPCM3C1AG + IPCM3C1LU
  152. %%%%%%%%%% LULUCF currently missing
  153. categories.IPC3C1 = struct;
  154. categories.IPC3C1.targetCategory = 'IPC3C1';
  155. categories.IPC3C1.subcategories = {'IPCM3C1AG', 'IPCM3C1LU'};
  156. categories.IPC3C1.entities = FAOEntities;
  157. categories.IPC3C1.params = defaultParams;
  158. %%% IPCM3C4AG = 'IPC3C4B', 'IPC3C4C', 'IPC3C4D',
  159. % omit currently as drained organic soils is missing but present in M3C45AG
  160. % categories.IPCM3C4AG = struct;
  161. % categories.IPCM3C4AG.targetCategory = 'IPCM3C4AG';
  162. % categories.IPCM3C4AG.subcategories = {'IPC3C4A', 'IPC3C4B', 'IPC3C4C', 'IPC3C4D', };
  163. % categories.IPCM3C4AG.entities = FAOEntities;
  164. % categories.IPCM3C4AG.params = defaultParams;
  165. %%% IPC3C4 = IPCM3C4AG
  166. %%%%%%%%%% LULUCF currently missing
  167. categories.IPC3C4 = struct;
  168. categories.IPC3C4.targetCategory = 'IPC3C4';
  169. categories.IPC3C4.subcategories = {'IPCM3C4AG'};
  170. categories.IPC3C4.entities = FAOEntities;
  171. categories.IPC3C4.params = defaultParams;
  172. %%% IPC3C = IPC3C1 + IPC3C4 + IPC3C5 + IPC3C6 + IPC3C7
  173. %%%%%%%%%% LULUCF currently missing
  174. categories.IPC3C = struct;
  175. categories.IPC3C.targetCategory = 'IPC3C';
  176. categories.IPC3C.subcategories = {'IPC3C1', 'IPCM3C45AG', 'IPC3C7'}; % 'IPC3C5', 'IPC3C6',
  177. categories.IPC3C.entities = FAOEntities;
  178. categories.IPC3C.params = defaultParams;
  179. %%% IPCM3CAG = IPCM3C1AG + IPCM3C4AG + IPC3C5 + IPC3C6 + IPC3C7
  180. categories.IPCM3CAG = struct;
  181. categories.IPCM3CAG.targetCategory = 'IPCM3CAG';
  182. categories.IPCM3CAG.subcategories = {'IPCM3C1AG', 'IPCM3C45AG', 'IPC3C7'}; %'IPC3C5', 'IPC3C6',
  183. categories.IPCM3CAG.entities = FAOEntities;
  184. categories.IPCM3CAG.params = defaultParams;
  185. % %%% IPCM3CLU = IPCM3C1LU
  186. % categories.IPCM3CLU = struct;
  187. % categories.IPCM3CLU.targetCategory = 'IPCM3CLU';
  188. % categories.IPCM3CLU.subcategories = {'IPCM3C1LU'};
  189. % categories.IPCM3CLU.entities = FAOEntities;
  190. % categories.IPCM3CLU.params = defaultParams;
  191. %
  192. % %%% IPC3 = IPC3A + IPC3B + IPC3C
  193. % if any(strcmp(source, {'FAO2021A'}))
  194. % categories.IPC3 = struct;
  195. % categories.IPC3.targetCategory = 'IPC3';
  196. % categories.IPC3.subcategories = {'IPC3A', 'IPCMLULUCF', 'IPCM3CAG'};
  197. % categories.IPC3.entities = FAOEntities;
  198. % categories.IPC3.params = defaultParams;
  199. % else
  200. % categories.IPC3 = struct;
  201. % categories.IPC3.targetCategory = 'IPC3';
  202. % categories.IPC3.subcategories = {'IPC3A', 'IPC3B', 'IPC3C'};
  203. % categories.IPC3.entities = FAOEntities;
  204. % categories.IPC3.params = defaultParams;
  205. %
  206. % %%% IPCMLULUCF = IPC3B + IPC3CLU
  207. % categories.IPCMLULUCF = struct;
  208. % categories.IPCMLULUCF.targetCategory = 'IPCMLULUCF';
  209. % categories.IPCMLULUCF.subcategories = {'IPC3B', 'IPCM3CLU'};
  210. % categories.IPCMLULUCF.entities = FAOEntities;
  211. % categories.IPCMLULUCF.params = defaultParams;
  212. % end
  213. %%% IPCMAG = IPC3A + IPCM3CA
  214. categories.IPCMAG = struct;
  215. categories.IPCMAG.targetCategory = 'IPCMAG';
  216. categories.IPCMAG.subcategories = {'IPC3A', 'IPCM3CAG'};
  217. categories.IPCMAG.entities = FAOEntities;
  218. categories.IPCMAG.params = defaultParams;
  219. %%% IPCMAGELV = IPCM3CA
  220. categories.IPCMAGELV = struct;
  221. categories.IPCMAGELV.targetCategory = 'IPCMAGELV';
  222. categories.IPCMAGELV.subcategories = {'IPCM3CAG'};
  223. categories.IPCMAGELV.entities = FAOEntities;
  224. categories.IPCMAGELV.params = defaultParams;
  225. entities = struct;
  226. entities.KYOTOGHG = struct;
  227. entities.KYOTOGHG.targetEntity = 'KYOTOGHG';
  228. entities.KYOTOGHG.subEntities = {'CO2', 'CH4', 'N2O'};
  229. entities.KYOTOGHG.categories = {'IPC3', 'IPC3A', 'IPC3C', 'IPCMAG', 'IPCMAGELV', 'IPCMLULUCF'}; %'IPC3B',
  230. entities.KYOTOGHG.params = defaultParams;
  231. entities.KYOTOGHG.GWP = CONSTANTS.gwpSARSpecification;
  232. entities.KYOTOGHGAR4 = struct;
  233. entities.KYOTOGHGAR4.targetEntity = 'KYOTOGHGAR4';
  234. entities.KYOTOGHGAR4.subEntities = {'CO2', 'CH4', 'N2O'};
  235. entities.KYOTOGHGAR4.categories = {'IPC3', 'IPC3A', 'IPC3C', 'IPCMAG', 'IPCMAGELV', 'IPCMLULUCF'}; %'IPC3B',
  236. entities.KYOTOGHGAR4.params = defaultParams;
  237. entities.KYOTOGHGAR4.GWP = CONSTANTS.gwpAR4Specification;
  238. entities.KYOTOGHGAR5 = struct;
  239. entities.KYOTOGHGAR5.targetEntity = 'KYOTOGHGAR5';
  240. entities.KYOTOGHGAR5.subEntities = {'CO2', 'CH4', 'N2O'};
  241. entities.KYOTOGHGAR5.categories = {'IPC3', 'IPC3A', 'IPC3C', 'IPCMAG', 'IPCMAGELV', 'IPCMLULUCF'}; %'IPC3B',
  242. entities.KYOTOGHGAR5.params = defaultParams;
  243. entities.KYOTOGHGAR5.GWP = CONSTANTS.gwpAR5Specification;
  244. entities.KYOTOGHGAR6 = struct;
  245. entities.KYOTOGHGAR6.targetEntity = 'KYOTOGHGAR6';
  246. entities.KYOTOGHGAR6.subEntities = {'CO2', 'CH4', 'N2O'};
  247. entities.KYOTOGHGAR6.categories = {'IPC3', 'IPC3A', 'IPC3C', 'IPCMAG', 'IPCMAGELV', 'IPCMLULUCF'}; %'IPC3B',
  248. entities.KYOTOGHGAR6.params = defaultParams;
  249. entities.KYOTOGHGAR6.GWP = CONSTANTS.gwpAR6Specification;
  250. aggregate_and_extrapolate_source(procSource, scenario, categories, entities);
  251. aggregate_and_extrapolate_source(procSource, scenarioProjection, categories, entities);
  252. remove_desired_regions_from_source_scenario({'EARTH', 'POLYNESIA'}, procSource, scenario);
  253. remove_desired_regions_from_source_scenario({'EARTH', 'POLYNESIA'}, procSource, scenarioProjection);
  254. % TODO
  255. % remove years with partial data from aggregate timeseries
  256. % FAO2020B
  257. % IPC3C1, IPCM3CAG, IPC3C, IPC3, IPCMAG, IPCMAGELV, IPC3B, IPCMLULUCF (remove 2019, 2020)
  258. % sum countries to CRF / UNFCCC reporting level
  259. sum_countries_primap(procSource, false);
  260. %% remove the FAO terminology tables
  261. % tempTables = get_table_sheetcodes_for({CONSTANTS.nameOfSheetSource}, {{tempSource}});
  262. % %success = remove_tables_from_database(tempTables);
  263. % if ~all(success)
  264. % disp('some tales could not be removed from the DB');
  265. % end
  266. % keep them as sometimes country downscaled tables in FAO eminology are needed
  267. warning off Tools:AssertValidcategoryforSourceScenario:ValidCategory
  268. end %prepareFAO2015data
  269. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  270. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  271. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  272. function newTable = adjust_countries(tablename, nYearKey, negativeToZero)
  273. global CONSTANTS;
  274. errorID = 'CreateDatabase:DataPreparation:FAO:PrepareFAO2016Data:AdjustCountries';
  275. thisTable = get_table_from_database(tablename);
  276. %thisTable.(CONSTANTS.nameOfSheetSource) = tempSource;
  277. %thisTable.(CONSTANTS.nameOfSheetCode) = concatenate_structure_field_values(thisTable,'default');
  278. thisTable.(CONSTANTS.nameOfSheetDescr) = [thisTable.(CONSTANTS.nameOfSheetDescr) ...
  279. ', regions summed and downscaled to country time series'];
  280. source = thisTable.(CONSTANTS.nameOfSheetSource);
  281. if negativeToZero
  282. % change negative values to zero for FAO1
  283. isneg = thisTable.data < 0;
  284. thisTable.data(isneg) = 0;
  285. end
  286. % remove countries which contain only NaN data
  287. thisTable = delete_only_nan_containing_countries(thisTable);
  288. % remove countries which contain only zero data
  289. thisTable = delete_only_zero_containing_countries(thisTable);
  290. if ~isempty(fieldnames(thisTable))
  291. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  292. %% REGIONS DOWNSCALED TO COUNTRIES, REGIONS DELETED
  293. % FIRST, REGIONS THAT SPLIT INTO COUNTRIES THAT EXIST IN FAO
  294. % split most recent first. Some regions split into other regions that
  295. % must be split first.
  296. % USSR = RUSSIA + ARMENIA + AZERBAIJAN + BELARUS + BULGARIA + ESTONIA (<= 1991)
  297. % + GEORGIA + KAZAKHSTAN + KYRGYZSTAN + LATVIA + LITHUANIA +
  298. % MOLDOVA + TAJIKISTAN + TURKMENISTAN + UKRAINE + UZBEKISTAN
  299. regions2Delete = {'FSU'}; % doesn't work for FAO1K
  300. theseCountries = get_members_of(regions2Delete{1});
  301. yearKey = 1992;
  302. thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
  303. % SERBIA/MONTENEGRO = SERBIA + MONTENEGRO (<= 2005)
  304. regions2Delete = {'SRBMNE'};
  305. theseCountries = get_members_of(regions2Delete{1});
  306. yearKey = 2006;
  307. thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
  308. % YUGOSLAVIA = BOSNIA/HERZEGOVINA + CROATIA + MACEDONIA + SLOVENIA + SERBIA + MONTENEGRO (<= 1991)
  309. regions2Delete = {'YUG'};
  310. theseCountries = get_members_of(regions2Delete{1});
  311. yearKey = 1992;
  312. thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
  313. % CZECHOSLOVAKIA = CZECH REPUBLIC + SLOVAKIA (<= 1991)
  314. regions2Delete = {'CZESVK'};
  315. theseCountries = get_members_of(regions2Delete{1}); %{'CZE','SVK'};
  316. yearKey = 1992;
  317. thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
  318. % Pacific Islands Trust Territory = Federated States of Micronesia + Palau
  319. % + Northern Mariana Islands + Marshall Islands (<= 1994)
  320. regions2Delete = {'TTPI'};
  321. theseCountries = get_members_of(regions2Delete{1}); %{'FSM' 'MHL' 'MNP' 'PLW'};
  322. yearKey = 1995;
  323. thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
  324. % BELLUX = Belgium + Luxembourg (<= 1999)
  325. regions2Delete = {'BELLUX'};
  326. theseCountries = get_members_of(regions2Delete{1});
  327. yearKey = 2000;
  328. thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
  329. % ETHERI = Ethiopia + Eritrea (<= 1992)
  330. regions2Delete = {'ETHERI'};
  331. theseCountries = get_members_of(regions2Delete{1});
  332. yearKey = 1993;
  333. thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
  334. % Sudan and South Sudan (South Sudan has individual data for 2012 onwards)
  335. if any(strcmp(source, {'FAO2016P', 'FAO2018P', 'FAO2019AP', 'FAO2019BP', 'FAO2020AP', 'FAO2020BP', ...
  336. 'FAO2020CP', 'FAO2021AP'}))
  337. regions2Delete = {'SDN'};
  338. theseCountries = {'SDN', 'SSD'};
  339. yearKey = 2012;
  340. thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
  341. end
  342. % Sudan and South Sudan (South Sudan has individual data for 2012 onwards)
  343. if any(strcmp(source, {'FAO2023AI', 'FAO2024AI'}))
  344. regions2Delete = {'SDNSSD'};
  345. theseCountries = {'SDN', 'SSD'};
  346. yearKey = 2012;
  347. thisTable = downscale_existing(thisTable,regions2Delete,theseCountries,yearKey, nYearKey, true);
  348. end
  349. % interpolate gap in FSM data
  350. dataFSM = get_cydata_from_independent_table(thisTable, {'FSM'}, 'all');
  351. if ~all(isnan(dataFSM.(CONSTANTS.nameOfDataField)))
  352. gaps = get_gaps(dataFSM.(CONSTANTS.nameOfDataField));
  353. if ~isempty(gaps)
  354. dataFSM.(CONSTANTS.nameOfDataField) = interpolate_in_single_dv(gaps, dataFSM.(CONSTANTS.nameOfDataField));
  355. thisTable = add_cydata_to_independent_table(thisTable, dataFSM);
  356. end
  357. end
  358. % remove countries which contain only NaN data
  359. thisTable = delete_only_nan_containing_countries(thisTable);
  360. % remove countries which contain only zero data
  361. newTable = delete_only_zero_containing_countries(thisTable);
  362. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  363. else
  364. newTable = struct;
  365. end
  366. end
  367. function thisTable = downscale_existing(thisTable,regions2Delete,theseCountries, yearKey, nYearKey, remove)
  368. % split regional data proportionally, based on ratios in first year
  369. % after yearKey where there is at least one nonzero value
  370. global CONSTANTS
  371. errorID = 'CreateDatabase:DataPreparation:FAO:PrepareFAO2016Data:DownscaleExisting';
  372. if ~exist('remove', 'var')
  373. remove = true;
  374. end
  375. cyRegion = get_cydata_from_independent_table(thisTable,regions2Delete,'all');
  376. if isempty(cyRegion.(CONSTANTS.nameOfCountryVector))
  377. return;
  378. end
  379. cyKey = get_cydata_from_independent_table(thisTable,theseCountries,'all');
  380. cyDownscaled = cyRegion;
  381. cyDownscaled.(CONSTANTS.nameOfCountryVector) = cyKey.(CONSTANTS.nameOfCountryVector);
  382. %split only if region is not all NaN or zeros
  383. if ~all(isnan(cyRegion.(CONSTANTS.nameOfDataField)) | cyRegion.(CONSTANTS.nameOfDataField) == 0)
  384. % remove all years before yearKey
  385. iYearKey = find(cyKey.(CONSTANTS.nameOfYearVector) == yearKey);
  386. cyKey.(CONSTANTS.nameOfYearVector) = cyKey.(CONSTANTS.nameOfYearVector)(iYearKey:end);
  387. cyKey.(CONSTANTS.nameOfDataField) = cyKey.(CONSTANTS.nameOfDataField)(:,iYearKey:end);
  388. % check which year the first nonzero value arises
  389. iYearStart = find(~isnan(nansum_primap(cyKey.(CONSTANTS.nameOfDataField),1)) & ...
  390. (nansum_primap(cyKey.(CONSTANTS.nameOfDataField),1) ~= 0));
  391. if isempty(iYearStart)
  392. % cannot divide regional data
  393. primap_log([errorID 'ProgramError'], CONSTANTS.ERRuserErrorWarning, ...
  394. ['Downscaling not possible for region ' regions2Delete{1} ...
  395. ', year(s) for downscaling key have only zeros or NaN. Table: ' ...
  396. thisTable.(CONSTANTS.nameOfSheetCode)]);
  397. if remove
  398. thisTable = remove_countries_from_independent_table(thisTable,regions2Delete);
  399. end
  400. return
  401. end
  402. % for the data to be downscaled remove all data beginning with the startYear
  403. yearStart = cyKey.(CONSTANTS.nameOfYearVector)(iYearStart(1));
  404. iYearStartRegion = find(cyDownscaled.(CONSTANTS.nameOfYearVector) == yearStart);
  405. cyDownscaled.(CONSTANTS.nameOfDataField) = cyDownscaled.(CONSTANTS.nameOfDataField)(:, 1 : iYearStartRegion - 1);
  406. cyDownscaled.(CONSTANTS.nameOfYearVector) = cyDownscaled.(CONSTANTS.nameOfYearVector)(1 : iYearStartRegion - 1);
  407. % for the key remove all years before yearStart
  408. cyKey.(CONSTANTS.nameOfYearVector) = cyKey.(CONSTANTS.nameOfYearVector)(iYearStart(1):end);
  409. cyKey.(CONSTANTS.nameOfDataField) = cyKey.(CONSTANTS.nameOfDataField)(:,iYearStart(1):end);
  410. % key is constructed from nYearKey years or available years,
  411. % whatever is less
  412. if length(cyKey.(CONSTANTS.nameOfYearVector)) > nYearKey
  413. cyKey.(CONSTANTS.nameOfYearVector) = cyKey.(CONSTANTS.nameOfYearVector)(1:nYearKey);
  414. cyKey.(CONSTANTS.nameOfDataField) = cyKey.(CONSTANTS.nameOfDataField)(:,1:nYearKey);
  415. end
  416. key = nansum_primap(cyKey.(CONSTANTS.nameOfDataField),2);
  417. arrRatio = repmat(key/nansum_primap(key), ...
  418. 1,length(cyDownscaled.(CONSTANTS.nameOfYearVector)));
  419. cyDownscaled.(CONSTANTS.nameOfDataField) = arrRatio .* ...
  420. repmat(cyDownscaled.(CONSTANTS.nameOfDataField),length(cyKey.(CONSTANTS.nameOfCountryVector)),1);
  421. cyDownscaled = delete_only_nan_containing_countries(cyDownscaled);
  422. % add downscaled numbers to existing data
  423. thisCydata = get_cydata_from_independent_table(thisTable,theseCountries,'all');
  424. %thisCydata = delete_only_nan_containing_countries(thisCydata);
  425. warning off Tools:AssertContentOfCydata:FailedValidation
  426. thisNewCydata = add_cydata_to_cydata(thisCydata, cyDownscaled);
  427. %thisNewCydata = summation_of_cydata(thisCydata, cyDownscaled,false,true);
  428. warning on Tools:AssertContentOfCydata:FailedValidation
  429. if remove
  430. thisTable = remove_countries_from_independent_table(thisTable,regions2Delete);
  431. end
  432. thisTable = add_cydata_to_independent_table(thisTable,thisNewCydata);
  433. else
  434. if remove
  435. thisTable = remove_countries_from_independent_table(thisTable,regions2Delete);
  436. end
  437. end
  438. end
  439. % countries in the source which are not UNFCCC
  440. % 'ARUBA'
  441. % 'ANGUILLA'
  442. % 'NETHERLANDS ANTILLES'
  443. % 'AMERICAN SAMOA'
  444. % 'BERMUDA'
  445. % 'CHANNEL ISLANDS'
  446. % 'CAYMAN ISLANDS'
  447. % 'WESTERN SAHARA'
  448. % 'FALKLAND ISLANDS (MALVINAS)'
  449. % 'FAROE ISLANDS'
  450. % 'GIBRALTAR'
  451. % 'GUADELOUPE'
  452. % 'GREENLAND'
  453. % 'FRENCH GUIANA'
  454. % 'GUAM'
  455. % 'HONG KONG'
  456. % 'ISLE OF MAN'
  457. % 'MACAO'
  458. % 'NORTHERN MARIANA ISLANDS'
  459. % 'MONTSERRAT'
  460. % 'MARTINIQUE'
  461. % 'MAYOTTE'
  462. % 'NEW CALEDONIA'
  463. % 'NORFOLK ISLAND'
  464. % 'PITCAIRN, HENDERSON, DUICE AN...'
  465. % 'PUERTO RICO'
  466. % 'PALESTINIAN TERRITORY, Occupied'
  467. % 'FRENCH POLYNESIA'
  468. % 'REUNION'
  469. % 'SAINT HELENA, ASCENSION AND T...'
  470. % 'SVALBARD AND JAN MAYEN ISLANDS'
  471. % 'SAINT PIERRE AND MIQUELON'
  472. % 'TURKS AND CAICOS ISLANDS'
  473. % 'TOKELAU'
  474. % 'TAIWAN'
  475. % 'VIRGIN ISLANDS (BRITISH)'
  476. % 'VIRGIN ISLANDS (U.S.)'
  477. % 'WALLIS AND FUTUNA ISLANDS'