Identify Storms from rain data

1 view (last 30 days)
E O
E O on 16 Jan 2024
Answered: Hassaan on 16 Jan 2024
Hi everyone. I hope you can help me. I have 5-min rain data:
Station ID yyyy mm dd hh mm rain
APWXI 2014 09 12 14 25 0
APWXI 2014 09 12 14 30 0
APWXI 2014 09 12 14 35 0
APWXI 2014 09 12 14 40 0.2
APWXI 2014 09 12 14 45 0.2
I would like to accumulate every rain value restarting the sum every time the station had at least 6 hours without rain, in order to produce a matrix with the start and end dates of each rain event, as well as the total amount of rain accumulated over the period:
start_rain end_rain h_ accum_rain
2014/09/12 14:40 2014/09/12 18:20 12.2
How can I do this on MatLab?
Thank you

Accepted Answer

Hassaan
Hassaan on 16 Jan 2024
  1. Read the Data: Use MATLAB's data import functions (readtable, readmatrix, or similar) to load your data into MATLAB.
  2. Convert to Datetime: Convert the date and time columns to MATLAB datetime objects for easier manipulation.
  3. Initialize Variables: Create variables to keep track of the start and end times of rain events and the accumulated rain.
  4. Process the Data: Loop through the dataset, checking for periods without rain and summing the rainfall during rain events.
  5. Store Results: For each rain event, store the start time, end time, and accumulated rainfall in an array or table.
% Assuming data is already loaded into a table 'data_table'
% with variables: 'StationID', 'yyyy', 'mm', 'dd', 'hh', 'mm', 'rain'
% Convert to datetime
data_table.datetime = datetime(data_table.yyyy, data_table.mm, data_table.dd, data_table.hh, data_table.mm, zeros(size(data_table.rain)));
% Sort data just in case it's not in chronological order
data_table = sortrows(data_table, 'datetime');
% Initialize variables
rain_events = table(datetime, datetime, 'VariableNames', {'start_rain', 'end_rain'});
accum_rain = 0;
event_in_progress = false;
hours_without_rain = 0;
last_rain_time = NaT;
% Loop over each row in the table
for i = 1:height(data_table)
if data_table.rain(i) > 0
% If this is the first rain in an event, record the start time
if ~event_in_progress
start_time = data_table.datetime(i);
event_in_progress = true;
accum_rain = 0; % Reset the accumulated rain
end
% Accumulate rain
accum_rain = accum_rain + data_table.rain(i);
% Update last rain time
last_rain_time = data_table.datetime(i);
elseif event_in_progress
% Calculate the time since it last rained
hours_since_last_rain = hours(data_table.datetime(i) - last_rain_time);
if hours_since_last_rain >= 6
% If it has been 6 or more hours without rain, record the event
rain_events = [rain_events; {start_time, last_rain_time}];
rain_events.h_accum_rain(end+1) = accum_rain;
event_in_progress = false;
end
end
end
% If an event was in progress at the end of the data, record it
if event_in_progress
rain_events = [rain_events; {start_time, last_rain_time}];
rain_events.h_accum_rain(end+1) = accum_rain;
end
% Display the results
disp(rain_events)
This script assumes that you have a table named data_table with the appropriate columns as you described. You will need to adjust the column names and table name according to your actual data.
Make sure to handle any potential edge cases according to your specific needs. The code might need to be adapted if there are particular nuances in your dataset not covered in the general case here.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!