calculate_instantaneous_feedback.m 7.45 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                      %
%   CALCULATE INSTANTANEOUS FEEDBACK   %
%   MAIN                               %
%                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all;

calculate_instantaneous_feedback_settings;
disp(sprintf('\nInstantaneous %s %s-%s - %s-%s, %d-%d\n',...
        basin_name,...
        dataset_names.(reference_name),reference_name,...
        dataset_names.(response_name),response_name,...
        start_year,stop_year));

if perform_calculations
% --------- %
% LOAD DATA %
% --------- %

    disp('loading');

    data = struct();


    for var_name = {reference_name,response_name}

        var_name = char(var_name);
        disp(sprintf('\t%s',var_name));

        in_file = strrep(in_dummy,'@DATASET_NAME@',dataset_names.(var_name));
        in_file = strrep(in_file,'@VAR_NAME@',var_name);

        data.(var_name) = load(in_file);

    % for: load data
    end

    % easier access
    central_longitudes = data.(var_name).central_longitudes;

% ------------------ %
% CALCULATE FEEDBACK %
% ------------------ %

    % mute warning from robust regression
    % query the last warning, including it's ID with
    % w = warning('query','last').
    warn_id = 'stats:statrobustfit:IterationLimit';

    warning('off',warn_id);

    % storage
    n_lon              = length(central_longitudes);
    feedback_strengths = struct();

    textprogressbar('Calculating feedbacks: ');
    for c_lon = 1:n_lon
    textprogressbar(c_lon/n_lon*100);

        for month = 1:12

            % SELECT DATA
            % +++++++++++

                use_data = struct();

                for var_name = {reference_name,response_name}

                    var_name = char(var_name);

                    % correct time
                    is_month = ( data.(var_name).date_vector(:,2) == month );

                    % select data
                    use_data.(var_name) = squeeze(data.(var_name).indices(c_lon,is_month));

                % for: select data for feedback calculation
                end

                % check that data are of the same size
                same_size = ( length(use_data.(reference_name)) == ...
                              length(use_data.(response_name)) );
                if ~same_size
                    error(['Date error: Reference and response variables ',...
                           'are not croped to the same periods.']);
                end

            % CALCULATE FEEDBACK
            % ++++++++++++++++++

                for anomaly_type = [-1 1]

                    % naming
                    if anomaly_type < 0
                        type_string = 'negative';
                    else
                        type_string = 'positive';
                    end

                    % select the correct anomalies
                    is_anomaly = ( sign(use_data.(reference_name)) == anomaly_type );

                    feedback_strengths.(type_string)(month,c_lon) = ...
                            calculateFeedback(use_data.(reference_name)(is_anomaly),...
                                    use_data.(response_name)(is_anomaly),...
                                    analysis_type);

                % for: calculate feedback for positive and negative types
                end

        % for: calculate feedback, monhs
        end

    % for: calculate feedback, longitudes
    end
    textprogressbar('');

    warning('on',warn_id);

% if: perform calculations
end

if save_data
% --------- %
% SAVE DATA %
% --------- %

    disp('saving data');

    README = sprintf(['\nVARIABLE LIST:\n\n',...
                      'central_longitudes:\n',...
                      'start_year\n',...
                      'stop_year\n',...
                      'reference_name:     Forcing variable\n',...
                      'response_name:      Response variable\n',...
                      'dataset_names:      Dataset each variable is associated with\n',...
                      'analysis_type:      ACC, linear regression (reg), robust regression (rreg)\n',...
                      'feedback_strengths: month x lon, positive and negative composites']);

    save(out_file,'central_longitudes','start_year','stop_year',...
                  'reference_name','response_name','dataset_names',...
                  'analysis_type',...
                  'feedback_strengths',...
                  'README');

% if: save data
end

if plot_results
% ------------ %
% PLOT RESULTS %
% ------------ %

    disp('plotting');

    for type_string = {'positive','negative'}

        type_string = char(type_string);
        disp(sprintf('\t%s',type_string));

        % SET UP FIGURE
        % +++++++++++++

            hf  = figure;
            hf.Position(3) = scale_figure_width*hf.Position(3);
            hf.Position(4) = scale_figure_height*hf.Position(4);

            % axes
            hax = axes('XLim',lon_cover,'YLim',[0.5 12.5],...
                    'YTick',[],'YTickLabel','',...
                    'XGrid','on','Layer','top');

            font_size    = floor(hax.FontSize*font_size_scale);
            hax.FontSize = font_size;

            hold(hax,'on');

            % naming the regions
            x_ticks        = [];
            x_tick_labels  = {};

        % PLOT DATA
        % +++++++++

            % checkerboard, as in the dynamics analysis
            image(central_longitudes,[1:12],feedback_strengths.(type_string),...
                    'CDataMapping','scaled');
            colorbar(hax);

        % NINO REGIONS
        % ++++++++++++

            for c_region = 1:length(region_names)

                x_values = [highlight_regions(c_region,1) highlight_regions(c_region,2) ...
                            highlight_regions(c_region,2) highlight_regions(c_region,1) ...
                            highlight_regions(c_region,1)];
                y_values = [hax.YLim(1) hax.YLim(1) hax.YLim(2) hax.YLim(2) hax.YLim(1)];

                plot(x_values,y_values,'LineWidth',4,'Color',my_white);
                plot(x_values,y_values,'LineWidth',2,'Color',region_colours(c_region,:));

                x_ticks(end+1)       = mean(highlight_regions(c_region,:));
                x_tick_labels{end+1} = region_names{c_region};

            % plot in highlight regions
            end

            % label boxes in a different axis
            hax_labels = axes('XLim',lon_cover,'YLim',[0.5 12.5],...
                    'YTick',[1:12],'YTickLabel',month_ids,...
                    'FontSize',font_size);
            set(hax_labels,'Color','none','XAxisLocation','top');

        % POLISHING AND PRINTING
        % ++++++++++++++++++++++

            xlabel(hax,'\circ lon');
            ylabel(hax_labels,'Month');

            % title
            fig_title = strrep(title_dummy,'@ANOMALYTYPE@',type_string);
            title(hax_labels,fig_title);

            % align axes
            common_axes_position = hax_labels.Position+axes_offset;
            set(hax_labels,'Position',common_axes_position);
            set(hax,'Position',common_axes_position);

            % colormap and number of displayed colours
            hax.CLim = c_limits;
            colormap(use_cmap);

            % display region names
            set(hax_labels,'XTick',x_ticks,'XTickLabel',x_tick_labels);

            % print
            fig_name = strrep(name_dummy,'@ANOMALYTYPE@',type_string);
            print(hf,res_flag,fig_name,'-dpng');
            close(hf);

    % for: plot positive and negative
    end

% if: plot results
end