%% =====================================================================
%  Author      :  Mahmut S. Ipek (Bilkent University)
%  E-mail      :  mahmuts@bilkent.edu.tr
%  Webpage     :  http://www.bilkent.edu.tr/~mahmuts
%  Date        :  03-Jul-2025, Ankara
%  Description :  This script downloads data from Eurostat's JSON API, 
%                   processes it, and plots it as a time series.
%                   Real GDP and HICP inflation are only examples.
% =====================================================================

% Housecleaning
clc;                  % Clear the command window
clear;                % Remove all variables from the workspace

% Define the JSON API endpoint from Eurostat (https://jsonstat.com/eurostat/)
url = "https://ec.europa.eu/eurostat/api/dissemination/statistics/1.0/data/namq_10_gdp?lang=en&lastTimePeriod=201&unit=CLV_I20&s_adj=SCA&na_item=B1GQ&geo=EA20";
% url = "https://ec.europa.eu/eurostat/api/dissemination/statistics/1.0/data/prc_hicp_mv12r?lang=en&lastTimePeriod=330&coicop=CP00&geo=EA20";

% Name of the variable to be used in the output timetable and plot
variableName = "Real GDP";
% variableName = "HICP Inflation";

% Fetch the data using the custom function `fetchEurostat`
tt = fetchEurostat(url, variableName);

% Plot the time series
plot(tt.time_, tt.(variableName), 'LineWidth', 1.5);                        % Plot the data
title([char(variableName) ' in the euro area'], 'Interpreter', 'none');     % Title of the plot
ylabel('Index 2020=100');                                                   % Y-axis label
% ylabel('Percent');                                                        % Y-axis label
xlabel('Time');                                                             % X-axis label

% Notify user that the script has completed successfully
disp('Done!');
