decadal_sst_event_variations.m 8.98 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                            %
%   DECADAL VARIATIONS OF EVENT PARAMETERS   %
%   MAIN                                     %
%                                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all;

decadal_variations_settings;

disp(sprintf('\n%s: %s-%s',basin_name,dataset_name,var_name));

if load_data
% --------- %
% LOAD DATA %
% --------- %

    disp('loading ...');

    % load data
    % grids
    grids = load(grid_file);

    % data
    data = load(data_file);

    % time constraint and masking
    % time crop
    is_time = ( data.date_vector(:,1) >= start_year & data.date_vector(:,1) <= stop_year );

    data.monthly_mean = data.monthly_mean(:,:,is_time);
    data.date_vector  = data.date_vector(is_time,:);

    % SORT LONGITUDE DATA
    % Swap 0E away from the matrix edge
    % +++++++++++++++++++++++++++++++++

        if sort_longitude_data

            lon_vector = grids.(dataset_name).lon_grid(:,1);

            if ( strcmp(dataset_name,'ersst') | strcmp(dataset_name,'oras4') )

                disp(['swapping ',dataset_name,' longitudes']);

                % SORT LONGITUDES
                % +++++++++++++++

                    % assumption: Data is ordered ascending or descending
                    % within the positive or negative parts
                    % identify negative parts
                    is_negative = (lon_vector < 0);
                    n_negative  = sum(is_negative);

                    new_lon_vector = zeros(size(lon_vector));

                    % sort longitudes
                    new_lon_vector(1:n_negative) = lon_vector(is_negative);
                    new_lon_vector(n_negative+1:end) = lon_vector(~is_negative);

                % SORT DATA
                % +++++++++

                    old_data = data.monthly_mean;

                    new_data = zeros(size(old_data));
                    new_data(1:n_negative,:,:)     = old_data( is_negative,:,:);
                    new_data(n_negative+1:end,:,:) = old_data(~is_negative,:,:);

                    data.monthly_mean = new_data;

                % SORT GRID DATA
                % ++++++++++++++

                    for swap_name = {'lon_grid','lat_grid','grid_box_area'}

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

                        old_data = grids.(dataset_name).(swap_name);

                        new_data = zeros(size(old_data));
                        new_data(1:n_negative,:,:)     = old_data( is_negative,:,:);
                        new_data(n_negative+1:end,:,:) = old_data(~is_negative,:,:);

                        grids.(dataset_name).(swap_name) = new_data;

                    end

            % if: swap longitudes for ERSST and ORAS4
            end

        % if: swap around longitude data
        end

% if: load data
end

if perform_calculations
% -------------------- %
% PERFORM CALCULATIONS %
% -------------------- %

    % time axis for chunked analysis
    time_axis = start_year:(stop_year-window_width+1);

    % storage
    strength_data = struct();

    textprogressbar('calculating event characteristics: ');
%      c_lon = 111;
    for c_lon = 1:length(central_longitudes)

        textprogressbar(c_lon/(length(central_longitudes))*100);

        % ANALYSIS REGION
        % +++++++++++++++

            % for this short period: don't detrend the data,
            % just use the local seasonal cycle

            % specify the box
            use_lon = central_longitudes(c_lon);
            use_lat = central_latitudes(c_lon);

            lon_bounds = [(use_lon-lon_extent) (use_lon)+lon_extent];
            lat_bounds = [(use_lat-lat_extent) (use_lat+lat_extent)];

            % index
            index = calcIndexAveraged(data.monthly_mean,...
                    grids.(dataset_name).lat_grid,grids.(dataset_name).lon_grid,...
                    grids.(dataset_name).grid_box_area,...
                    lat_bounds,lon_bounds);

        % loop over all time windows
        for c_start = 1:length(time_axis);

            % correct time
            start_window = time_axis(c_start);
            stop_window  = start_window+window_width-1;

            % anomalies: include linear detrending
            [anomalies,cropped_dates] = calcAnomalies(index,data.date_vector,start_window,stop_window);

            % IDENTIFY EVENTS
            % +++++++++++++++

                for event_type = [1 -1]

                    % naming
                    if ( event_type == 1 )
                        type_string = 'positive';
                    else
                        type_string = 'negative';
                    end

                    if use_anomalies_instead_of_events

                        is_data        = ( sign(anomalies) == event_type );
                        peak_strengths = anomalies(is_data);
                        event_dates    = cropped_dates(is_data,:);

                    % base strength on events
                    else

                        [lengths,growths,decays,event_dates,peak_strengths,mean_strengths] = ...
                                eventLengths(anomalies,cropped_dates,t_id,event_type);

                    end

                    % swap strength data to have ninos and ninas of comparable strengths
                    peak_strengths = abs(peak_strengths);

                    % identify events
                    is_events = ismember(event_dates(:,2),season_def);

                    % RECORD DATA
                    % +++++++++++

                        n_events = sum(is_events);

                        strength_data.(type_string)(c_start,c_lon,1:n_events) = peak_strengths(is_events);

                % for: identify positive and negative events
                end

        % for: identify events for all time chunks
        end

    % for: identify characteristics for all central longitudes
    end
    textprogressbar('');

    % CLEAN UP MISSING VALUES FOR SIGNIFICANCE TESTING
    % ++++++++++++++++++++++++++++++++++++++++++++++++

        for event_type = [-1 1]

            % naming
            if ( event_type == 1 )
                type_string = 'positive';
            else
                type_string = 'negative';
            end

            is_missing = ( strength_data.(type_string) == 0);
            strength_data.(type_string)(is_missing) = NaN;


        % for: clean up missing data
        end

    % SIGNIFICANCE TEST
    % +++++++++++++++++

        disp('testing significance ...');

        is_significant = [];

        % test difference in means for the two parameters
        for c_lon = 1:length(central_longitudes)

            for c_start = 1:length(time_axis);

                use_positive = squeeze(strength_data.positive(c_start,c_lon,:));
                use_negative = squeeze(strength_data.negative(c_start,c_lon,:));

                % exclude NaNs
                use_positive(isnan(use_positive)) = [];
                use_negative(isnan(use_negative)) = [];

                % perform the test
                reject_h0 = ttest2(use_positive,use_negative,...
                        'Alpha',significance_level,'Vartype','unequal');

                % save the decision
                is_significant(c_start,c_lon) = reject_h0;

            % for: significant diffs for both seasons
            end

        % for: signifiat differences between positive and negative events
        end

% if: perform calculations
end

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

    disp('saving data');

    README = sprintf(['\nVARIABLE LIST\n\n',...
                      'var_name\n',...
                      'dataset_name\n',...
                      'start_year:        Start of the overall analysis period\n',...
                      'stop_year:         End of the overakk analysis period\n',...
                      'window_width:      Over how many years does the running mean average (for each month)?\n',...
                      'central_longitudes\n',...
                      'time_axis:         All start years of the sub-periods analysed via the running mean\n',...
                      'season_def:        Which calendar months have been analysed?\n',...
                      'use_anomalies_instead_of_events: Diagnose anomalie strength or event strength?\n',...
                      't_id:              Identification threshold in terms of the local variance\n',...
                      'significance_level\n\n',...
                      'strength_data:     For positive and negative events\n',...
                      'is_significant:    Corresponding significance']);

    save(out_file,...
         'var_name','dataset_name','start_year','stop_year','window_width',...
         'central_longitudes','time_axis','season_def','use_anomalies_instead_of_events',...
         't_id','significance_level',...
         'strength_data','is_significant',...
         'README');

end

if load_previous_data
    load(out_file);
end

if produce_plots
% ----- %
% PLOTS %
% ----- %

    decadal_variations_plots;

end