compare_oras4_ersst.m 10.4 KB
Newer Older
Claas Faber's avatar
Claas Faber committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                      %
%   COMPARE ORAS4 AND ERSST ATL3 SST   %
%   SETTINGS                           %
%                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all;

compare_oras4_ersst_settings;
disp(sprintf('%s-%s, %d-%d',index_name,var_name,start_base,stop_base));

% --------- %
% LOAD DATA %
% --------- %

    data = struct();

    for dataset_name = {all_data{:}}

        dataset_name = char(dataset_name);

        infile = strrep(file_dummy,'@DATASET_NAME@',upper(dataset_name));
        data.(dataset_name) = load(infile);

    end

% ------------------------------------------ %
% CALCULATIONS: SEASONAL CYCLE AND ANOMALIES %
% ------------------------------------------ %

    % calculate seasonal cycles
    seasonal_cycle    = struct(char(all_data{1}),[],char(all_data{2}),[]);
    seasonal_variance = struct(char(all_data{1}),[],char(all_data{2}),[]);
    anomalies         = struct(char(all_data{1}),[],char(all_data{2}),[]);
    cropped           = struct(char(all_data{1}),[],char(all_data{2}),[]);

    for dataset_name = {all_data{:}}

        dataset_name  = char(dataset_name);
        date_vector   = data.(dataset_name).date_vector;

        is_year  = ( ( date_vector(:,1) >= start_base ) & ...
                     ( date_vector(:,1) <= stop_base  ) );

        cropped.(dataset_name) = is_year;

        for month = 1:12

            is_month = ( date_vector(:,2) == month );

            seasonal_cycle.(dataset_name)(month) = ...
                    nanmean(data.(dataset_name).index(is_year & is_month));

            seasonal_variance.(dataset_name)(month) = ...
                    std(data.(dataset_name).index(is_year & is_month));

            anomalies.(dataset_name)(is_month) = ...
                    data.(dataset_name).index(is_month)-...
                    seasonal_cycle.(dataset_name)(month);

        end

    end

    % calculate seasonal correlations
    dataset_1 = char(all_data{1});
    dataset_2 = char(all_data{2});
    dv_1      = data.(dataset_1).date_vector;
    dv_2      = data.(dataset_2).date_vector;
    seasonal_accs = [];

    for month = 1:12

        is_month_1 = ( dv_1(:,2) == month );
        is_month_2 = ( dv_2(:,2) == month );

        series_1 = anomalies.(dataset_1)(cropped.(dataset_1) & is_month_1);
        series_2 = anomalies.(dataset_2)(cropped.(dataset_2) & is_month_2);

        rho = corrcoef(series_1,series_2);

        seasonal_accs(month) = rho(1,2);

    end

    % correlation between the anomalies for all months
    time_series1 = anomalies.(char(all_data{1}))(cropped.(char(all_data{1})));
    time_series2 = anomalies.(char(all_data{2}))(cropped.(char(all_data{2})));

    rho = corrcoef(time_series1,time_series2);
    rho = rho(1,2);

 % ----- %
 % PLOTS %
 % ----- %

%      % SEASONAL CYCLE
%      % ++++++++++++++
%
%          hf = figure;
%          hax_sc = axes('XLim',[0.5 12.5],'XTick',1:12,'XTickLabel',{month_names{:}});
%          hold(hax_sc,'on');
%          grid(hax_sc,'on');
%
%          legend_handles = [];
%          legend_entries = {};
%
%          for dataset_name = {all_data{:}}
%
%              dataset_name = char(dataset_name);
%
%              % background
%              plot(seasonal_cycle.(dataset_name),'-w','LineWidth',4);
%
%              legend_handles(end+1) = plot(seasonal_cycle.(dataset_name),'-',...
%                      'LineWidth',2,'Color',all_colours.(dataset_name));
%
%              legend_entries{end+1} = dataset_name;
%
%          end
%
%          hold(hax_sc,'off');
%
%          h_legend = legend(legend_handles,legend_entries,'Location','SouthOutside');
%          set(h_legend,'Box','off','Orientation','horizontal');
%
%          ylabel(['SC of ',var_name]);
%
%          fig_title = strrep(title_dummy,'@PLOT_TYPE@','Seasonal Cycle');
%          title(fig_title);
%
%          fig_name = strrep(name_dummy,'@PLOT_TYPE@','sc');
%
%          print(res_flag,hf,fig_name,'-dpng');
%          close(hf);
%
     % SEASONAL VARIANCE
     % +++++++++++++++++

        % set up figure
        hf = figure;
        hf.Position = hf.Position.*scale_figure;

        % axes set up
        hax_sc = axes('XLim',[0.5 12.5],'XTick',1:12,'XTickLabel',{month_names{:}},...
                   'YLim',y_limits_stddev.(var_name));

        hold(hax_sc,'on');
        grid(hax_sc,'on');

        % font size
        new_size = floor(hax_sc.FontSize*scale_font_size);
        set(hax_sc,'FontSize',new_size);

        % legend initialization
        legend_handles = [];
        legend_entries = {};

         for c_data = 1:n_data

             dataset_name = char(all_data{c_data});

             % background
             plot(seasonal_variance.(dataset_name),'-w','LineWidth',4);

             legend_handles(end+1) = plot(seasonal_variance.(dataset_name),'-',...
                     'LineWidth',2,'Color',all_colours.(dataset_name));

             legend_entries{end+1} = char(data_names{c_data});

         end

         hold(hax_sc,'off');

         h_legend = legend(legend_handles,legend_entries,'Location','NorthEastOutside');
         set(h_legend,'Box','off','Orientation','vertical');

         % common x-position of the y labels
         h_ylabel_std = ylabel('Std-dev');
         set(h_ylabel_std,...
                 'Units','normalized',...
                 'Position',y_label_position);

         fig_title = strrep(title_dummy,'@PLOT_TYPE@','Seasonal Std-dev');
         title(fig_title);

         % set common axes position
         hax_sc.Position = axes_position;

         fig_name = strrep(name_dummy,'@PLOT_TYPE@','std');

         print(res_flag,hf,fig_name,'-dpng');
         % close(hf);

     % ANOMALIES
     % +++++++++

         hf = figure;
         hf.Position = hf.Position.*scale_figure;

         x_start = datenum([start_anomalies 1 1 0 0 0]);
         x_end   = datenum([stop_anomalies 12 31 23 59 59]);

         hax = axes('XLim',[x_start x_end],'YLim',y_limits_anomalies.(var_name),...
              'Layer','top');

         hold(hax,'on');
         grid(hax,'on');

         % font size
         new_size = floor(hax.FontSize*scale_font_size);
         set(hax,'FontSize',new_size);

         % 0-line and grey shading
         % shading
         % need to shade more than the axes limits
         artificial_start = datenum([start_anomalies-5 1 1 0 0 0]);
         artificial_stop  = datenum([stop_anomalies+5 1 1 0 0 0]);
         x_values = [artificial_start artificial_stop ...
                 artificial_stop artificial_start artificial_start];
         y_values = [hax.YLim(1) hax.YLim(1) 0 0 hax.YLim(1)];

         hpatch = patch(x_values,y_values,my_gray);
         set(hpatch,'EdgeColor','none');

         plot(hax,[artificial_start artificial_stop],[0 0],'Color',hax.XColor);

         legend_handles = [];
         legend_entries = {};

         for c_data = 1:n_data

             dataset_name = char(all_data{c_data});

             is_period = ( ( data.(dataset_name).date_vector(:,1) >= start_anomalies ) & ...
                           ( data.(dataset_name).date_vector(:,1) <= stop_anomalies ));

             x_data = datenum(data.(dataset_name).date_vector(is_period,:));
             y_data = anomalies.(dataset_name)(is_period);

             % background
             plot(hax,x_data,y_data,'-w','LineWidth',4);

             legend_handles(end+1) = plot(hax,x_data,y_data,'-',...
                     'LineWidth',2,'Color',all_colours.(dataset_name));

             legend_entries{end+1} = char(data_names{c_data});

         end

         hold off;

         h_legend = legend(legend_handles,legend_entries,'Location','NorthEastOutside');
         set(h_legend,'Box','off','Orientation','vertical');

         datetick;

         % common x-position of the y-label
         h_ylabel_anomalies = ylabel(['Anomalies']);
         set(h_ylabel_anomalies,...
                 'Units','normalized',...
                 'Position',y_label_position);

         % reset axis limits since datetick kills them
         set(hax,'XLim',[x_start x_end]);

         title_string = ['Anomalies, base: ',num2str(start_base),'-',num2str(stop_base),...
                 ', rho=',num2str(rho,'%1.2f')];

         fig_title = strrep(title_dummy,'@PLOT_TYPE@',title_string);
         title(fig_title);

         % set common axes position
         hax.Position = axes_position;

         fig_name = strrep(name_dummy,'@PLOT_TYPE@','anomalies');

         print(res_flag,hf,fig_name,'-dpng');
         % close(hf);

     % % CROPPED ANOMALIES
     % % +++++++++++++++++
     %
     %     hf = figure;
     %     hold on;
     %     grid on;
     %
     %     legend_handles = [];
     %     legend_entries = {};
     %
     %     for dataset_name = {all_data{:}}
     %
     %         dataset_name = char(dataset_name);
     %         x_data = datenum(data.(dataset_name).date_vector(cropped.(dataset_name),:));
     %         y_data = anomalies.(dataset_name)(cropped.(dataset_name));
     %
     %         % background
     %         plot(x_data,y_data,'-w','LineWidth',4);
     %
     %         legend_handles(end+1) = plot(x_data,y_data,'-',...
     %                 'LineWidth',2,'Color',all_colours.(dataset_name));
     %
     %         legend_entries{end+1} = dataset_name;
     %
     %     end
     %
     %     hold off;
     %
     %     h_legend = legend(legend_handles,legend_entries,'Location','SouthOutside');
     %     set(h_legend,'Box','off','Orientation','horizontal');
     %
     %     datetick;
     %     ylabel([var_name,' anomalies']);
     %
     %     title_string = ['Anomalies,',num2str(start_base),'-',num2str(stop_base),...
     %             ', rho=',num2str(rho,'%1.2f')];
     %
     %     fig_title = strrep(title_dummy,'@PLOT_TYPE@',title_string);
     %     title(fig_title);
     %
     %     fig_name = strrep(name_dummy,'@PLOT_TYPE@','anomalies');
     %
     %     print(res_flag,hf,fig_name,'-dpng');
     %     close(hf);

%      % SEASONAL ACCs
%      % +++++++++++++
%
%          hf = figure;
%          axes(hf,'XLim',[0.5 12.5],'XTick',1:12,'XTickLabel',{month_names{:}},...
%               'YLim',[0 1]);
%          hold on;
%          grid on;
%
%          legend_handles = [];
%          legend_entries = {};
%
%          plot(1:12,seasonal_accs,'-k','LineWidth',2);
%          hold off;
%
%          ylabel([var_name,' ACCs']);
%
%          fig_title = strrep(title_dummy,'@PLOT_TYPE@','Monthly ACCs');
%          title(fig_title);
%
%          fig_name = strrep(name_dummy,'@PLOT_TYPE@','accs');
%
%          print(res_flag,hf,fig_name,'-dpng');
%          close(hf);