Using MATLAB to find multi-day 90th percentile temperature exceedences

834 Views Asked by At

I am working on a project where I'm trying to find multi-day events where temperature exceeds the 90th percentile.

I'm using 61 years of data and trying to calculate the percentiles based on each month between June-August. In other words, using the prctile function, I would like to calculate the 90th percentile for June, July, and August and compare each month's daily temperature values based on the 90th percentile value for that month.

Ultimately, I want the program to find strings of days (2-day, 3-day, 4-day, 5-day, etc.) where the temperature exceeds the 90th percentile value, based on that month's value.

Below is the code I have so far:

fprintf('Loading the data...\n')
    load file_name.txt
    year=file_name(:,1);
    month=file_name(:,2);
    day=file_name(:,3);
    temax=file_name(:,6);
    temin=file_name(:,9);

    molen=[30 31 31];

    for y=1:61
        for m=6:8
            for k=1:molen
                g1=find(year==y+1950&month==m&day==k&temax>-99);
                g2=find(year==y+1950&month==m&day==k&temin>-99);
            end
            temaxpct=prctile(temax(g1),90);
            teminpct=prctile(temin(g2),90);
        end
        clear g*
    end
    exceedence=find(year&month&day&temax>=temaxpct&temin>=teminpct); 

I don't know how to make the program calculate the 90th percentile for each month (instead of the whole 3-month range June-August).

Additionally, I don't know how to make the program find strings of days where the temperature exceeds the 90th percentile, especially, since 4-day events also include 3-day events and 2-day events.

I know there needs to be some if/else statements, but I'm not sure how to set it up.

1

There are 1 best solutions below

1
On

Just from reading your question (without digging through the code), here are two pointers:

  1. If you know how to get percentiles for a certain dataset, you can simply loop over the months each time giving your function only the data from that month. Then calculate and store the quantile for that month.
  2. If you know how to find n-day stretches where the temperature is above your treshold, here is a rather un-elegant way to find all stretches:
    • Find all stretches of maximum length, mark all found values as done
    • Find all stretches of maximum length - 1, only using unmarked values, mark all found values as done
    • Continue till you reach the minimum

Hope this can get you on track.