decadal_feedback_variations.m 8.04 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                           %
%    DIAGNOSE DECADAL FEEDBACK VARIATIONS   %
%    MAIN                                   %
%    Version in which I calculate the       %
%    anomalies for each analysis period     %
%    separately.                            %
%                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

decadal_feedback_variations_settings;

disp(sprintf('\n\n%s: %s-%s, %d months',basin_name,...
        upper(ref_name),upper(lag_name),window_width));

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

    data = struct();

    for var_name = {ref_name,lag_name}

        var_name     = char(var_name);
        dataset_name = all_data.(var_name);
        index_name   = all_indices.(basin_name).(var_name);

        infile = strrep(infile_dummy,'@DATASET_NAME@',upper(dataset_name));
        infile = strrep(infile,'@VAR_NAME@',var_name);
        infile = strrep(infile,'@INDEX_NAME@',index_name);

        data.(var_name) = load(infile);

    % for: load all variables
    end

% -------------------- %
% PERFORM CALCULATIONS %
% -------------------- %

    % switch off warning
    warning('off',warn_id);

    all_starts = [start_year:(stop_year-window_width)];
    n_starts   = length(all_starts);

    feedback_strengths = struct('positive',zeros(12,n_starts),'negative',zeros(12,n_starts));
    significances      = struct('positive',zeros(12,n_starts,1),'negative',zeros(12,n_starts));
    lag_variations     = zeros(12,n_starts);

    % set up loop
    textprogressbar('calculating feedback strengths: ');
    c_step = 1;

    for c_start = 1:n_starts

        use_start = all_starts(c_start);
        use_stop  = use_start+window_width;

        % CALCULATE ANOMALIES FOR THIS PERIOD
        % +++++++++++++++++++++++++++++++++++

            anomalies  = struct();
            cropped_dv = struct();

            for var_name = {ref_name,lag_name}

                var_name     = char(var_name);
                dataset_name = all_data.(var_name);

                % calculate anomalies
                if detrend_data
                    [anomalies.(var_name),cropped_dv.(var_name)] = calcAnomalies(...
                            data.(var_name).index,data.(var_name).date_vector,...
                            use_start,use_stop);
                else

                    % do not detrend the data
                    % specify a dummy trend that consists only of zeros

                    dummy_trend = zeros(size(data.(var_name).index));

                    [anomalies.(var_name),cropped_dv.(var_name)] = calcAnomalies(...
                            data.(var_name).index,data.(var_name).date_vector,...
                            dummy_trend,...
                            use_start,use_stop);

                end

            % for: calculate anomalies
            end

        % DIAGNOSE FEEDBACK STRENGTHS
        % +++++++++++++++++++++++++++

        for analysis_month = 1:12

            textprogressbar(c_step/(n_starts*12)*100);

            % --> cropped time series
            [all_strengths,lag_value,cropped_ref,cropped_lag] = diagnoseLag(...
                    anomalies.(ref_name),cropped_dv.(ref_name),...
                    anomalies.(lag_name),cropped_dv.(lag_name),...
                    analysis_type,...
                    analysis_month,maximum_lag,use_start,use_stop);

            % ignore NaNs: need to do this to make sure that
            % the random bootstrapping does not draw NaNs
            ref_nans = isnan(cropped_ref);
            lag_nans = isnan(cropped_lag);
            all_nans = ( ref_nans | lag_nans );
            cropped_ref(all_nans) = [];
            cropped_lag(all_nans) = [];

            lag_variations(analysis_month,c_start) = lag_value;

            % calculate positive and negative feedback strengths
            for anomaly_type = [-1 1]

                % assign correct anomaly type
                switch anomaly_type
                    case -1
                        type_string = 'negative';
                    case 1
                        type_string = 'positive';
                end

                % select correct piece of data
                is_anomaly  = ( sign(cropped_ref) == anomaly_type );
                sample_size = length(find(is_anomaly));

                % ignore cases where the sample size does not allow even robust regression
                % i.e. when there's only two samples
                if sample_size < 3

                    current_strength = NaN;
                    is_significant   = 0;

                else

                    % calculate actual feedback strength
                    current_strength =  calculateFeedback(...
                            cropped_ref(is_anomaly),cropped_lag(is_anomaly),analysis_type);

                    % bootstrap additional strengths
                    bootstrapped_strengths = bootstrapFeedbackStrength(...
                                    cropped_ref,cropped_lag,...
                                    sample_size,n_bootstraps,analysis_type);

                    % perform significance test
                    is_significant = testValue(bootstrapped_strengths,current_strength,alpha);

                end

                % assign data
                feedback_strengths.(type_string)(analysis_month,c_start) = current_strength;
                significances.(type_string)(analysis_month,c_start)      = is_significant;

            % for: calculate feedback strengths and perform bootstrapping for anomaly types
            end

            % keep track
            c_step = c_step+1;

        % for: calculate feedback strengths for all months
        end

    % for: calculate feedback strengths for all periods
    end
    textprogressbar('');

    % switch warning back on
    warning('on',warn_id);

% if: perform calculations
end

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

    disp('saving data');

    README = sprintf(['\nVARIABLE LIST\n\n',...
                      'ref_name:      Forcing variable\n',...
                      'ref_dataset:   Dataset of the forcing variable\n',...
                      'ref_index:     Region over which the forcing variable has been averaged\n',...
                      'lag_name:      Response variable\n',...
                      'lag_dataset:   Dataset of the response variable\n',...
                      'lag_index:     Region over which the response variable has been averaged\n',...
                      'start_year:    Start of the entire analysis period\n',...
                      'stop_year:     End of the entire analysis period\n',...
                      'all_starts:    Starts of all sub-periods\n',...
                      'window_width:  Length of the individual sub-periods\n',...
                      'analysis_type: Method to estimate the feedback strength\n',...
                      'n_bootstraps:  Number of bootstraps to estimate the significance\n',...
                      'alpha:         Significance level to estimate the bootstrapped significance\n\n',...
                      'feedback_strengths: Positive and negative feedback strength composites\n',...
                      'significances:      Corresponding significance relative to the expected feedback strength\n',...
                      'lag_variations:     Variations of the diagnosed lag']);

    ref_dataset = all_data.(ref_name);
    ref_index   = all_indices.(basin_name).(ref_name);
    lag_dataset = all_data.(lag_name);
    lag_index   = all_indices.(basin_name).(lag_name);

    save(out_file,'ref_name','ref_dataset','ref_index','lag_name','lag_dataset','lag_index',...
                  'start_year','stop_year','all_starts','window_width',...
                  'analysis_type','n_bootstraps','alpha',...
                  'feedback_strengths','significances','lag_variations');

end

if load_previous_data

    load(out_file);

end

% ------------ %
% PLOT RESULTS %
% ------------ %

    if plot_composites
        decadal_feedback_variations_plots;
    end

    if plot_differences
        decadal_feedback_variations_plot_differences;
    end