From: Priom Rahman on
Hi guys I had have a small problem,

I'm dealing with rainfall records, which i need to class (I've included the first 25 data sets)

data is in the form:
Year|month|day|rain

ObsRain = [
1962 7 6 0
1962 7 7 0
1962 7 8 0
1962 7 9 0
1962 7 10 0
1962 7 11 0
1962 7 12 0
1962 7 21 0
1962 7 22 0
1962 7 24 54
1962 7 25 0
1962 8 1 22.8000000000000
1962 8 4 5.80000000000000
1962 8 5 47.6000000000000
1962 8 6 0
1962 8 8 217.800000000000
1962 8 9 94.6000000000000
1962 8 10 4.50000000000000
1962 8 11 43.8000000000000
1962 8 12 128.200000000000
1962 8 13 241.700000000000
1962 8 14 115.700000000000
1962 8 15 96.1000000000000
1962 8 19 132.100000000000
1962 8 20 0]


----------
What i need to do is class them based on if its:

a random day with rain - (current day rained, day before didn't rain. day after didn't rain) = class 1
start of a wet spell - (current day rained, day before didn't rain., day after rained) = class 2
end of a wet spell - (current day rained, day before rained, day after didn't rain) = class 3
middle of a wet spell - (current day rained, day before rained, day after rained) = class 4

-----------

now before when I had a continuous sequence with no breaks in the record I was using the following function (and it worked fine):
-----------
for k =2:(size(ObsRain,1)-1)
% for rainfall days that are not equal to zero
if ObsRain(k,4)~=0

if (ObsRain((k-1),4)== 0) && (ObsRain((k+1),4)== 0)
classed_rain(k,:) = 1;
end

if(ObsRain((k-1),4)> 0) && (ObsRain((k+1),4)== 0)
classed_rain(k,:) = 2;
end

if(ObsRain((k-1),4)== 0) && (ObsRain((k+1),4)>0)
classed_rain(k,:) = 3;
end

if (ObsRain((k-1),4)> 0) && (ObsRain((k+1),4)>0)
classed_rain(k,:) = 4;
end

else
classed_rain(k,:) = NaN;

end


end







-------

how ever with missing dates, (as given above in the data example) i can no longer use a row - 1 function. I was trying to use datenum, but it was failing because it creates a blank value: here is my code:

--------
for k = 1: size(ObsRain,1)

% Current date
cdate = datenum(ObsRain(k,1), ObsRain(k,2), ObsRain(k,3));
%[Cyear Cmonth Cday] = datevec(Cdate);
[cyear cmonth cday] = datevec(cdate);
% Current rain
crain = ObsRain(datenum(ObsRain(:,1:3)) == (cdate),4);
% Rain previous day
crain_m_1 = ObsRain(datenum(ObsRain(:,1:3)) == (cdate-1),4);
% Rain following day
crain_p_1 = ObsRain(datenum(ObsRain(:,1:3)) == (cdate+1),4);

% for rainfall days that are not equal to zero
if ObsRain(k,4)~=0

if (crain_m_1 < 0) && (crain_p_1 < 0)
classed_rain(k,:) = 1;
end

if(crain_m_1 > 0) && (crain_p_1 == 0)
classed_rain(k,:) = 2;
end

if(crain_m_1 == 0) && (crain_p_1 > 0)
classed_rain(k,:) = 3;
end

if (crain_m_1 > 0) && (crain_p_1 >0)
classed_rain(k,:) = 4;
end

else
classed_rain(k,:) = NaN;

end


end




-----

can anyone suggest any ways to fix this ?

Thanks in advance for your help
From: Joseph on

Use datenum to develop a mapping of skipped days. Then handle accordingly




"Priom Rahman" <z3188254(a)unsw.edu.au> wrote in message <i3nl2a$fba$1(a)fred.mathworks.com>...
> Hi guys I had have a small problem,
>
> I'm dealing with rainfall records, which i need to class (I've included the first 25 data sets)
>
> data is in the form:
> Year|month|day|rain
>
> ObsRain = [
> 1962 7 6 0
> 1962 7 7 0
> 1962 7 8 0
> 1962 7 9 0
> 1962 7 10 0
> 1962 7 11 0
> 1962 7 12 0
> 1962 7 21 0
> 1962 7 22 0
> 1962 7 24 54
> 1962 7 25 0
> 1962 8 1 22.8000000000000
> 1962 8 4 5.80000000000000
> 1962 8 5 47.6000000000000
> 1962 8 6 0
> 1962 8 8 217.800000000000
> 1962 8 9 94.6000000000000
> 1962 8 10 4.50000000000000
> 1962 8 11 43.8000000000000
> 1962 8 12 128.200000000000
> 1962 8 13 241.700000000000
> 1962 8 14 115.700000000000
> 1962 8 15 96.1000000000000
> 1962 8 19 132.100000000000
> 1962 8 20 0]
>
>
> ----------
> What i need to do is class them based on if its:
>
> a random day with rain - (current day rained, day before didn't rain. day after didn't rain) = class 1
> start of a wet spell - (current day rained, day before didn't rain., day after rained) = class 2
> end of a wet spell - (current day rained, day before rained, day after didn't rain) = class 3
> middle of a wet spell - (current day rained, day before rained, day after rained) = class 4
>
> -----------
>
> now before when I had a continuous sequence with no breaks in the record I was using the following function (and it worked fine):
> -----------
> for k =2:(size(ObsRain,1)-1)
> % for rainfall days that are not equal to zero
> if ObsRain(k,4)~=0
>
> if (ObsRain((k-1),4)== 0) && (ObsRain((k+1),4)== 0)
> classed_rain(k,:) = 1;
> end
>
> if(ObsRain((k-1),4)> 0) && (ObsRain((k+1),4)== 0)
> classed_rain(k,:) = 2;
> end
>
> if(ObsRain((k-1),4)== 0) && (ObsRain((k+1),4)>0)
> classed_rain(k,:) = 3;
> end
>
> if (ObsRain((k-1),4)> 0) && (ObsRain((k+1),4)>0)
> classed_rain(k,:) = 4;
> end
>
> else
> classed_rain(k,:) = NaN;
>
> end
>
>
> end
>
>
>
>
>
>
>
> -------
>
> how ever with missing dates, (as given above in the data example) i can no longer use a row - 1 function. I was trying to use datenum, but it was failing because it creates a blank value: here is my code:
>
> --------
> for k = 1: size(ObsRain,1)
>
> % Current date
> cdate = datenum(ObsRain(k,1), ObsRain(k,2), ObsRain(k,3));
> %[Cyear Cmonth Cday] = datevec(Cdate);
> [cyear cmonth cday] = datevec(cdate);
> % Current rain
> crain = ObsRain(datenum(ObsRain(:,1:3)) == (cdate),4);
> % Rain previous day
> crain_m_1 = ObsRain(datenum(ObsRain(:,1:3)) == (cdate-1),4);
> % Rain following day
> crain_p_1 = ObsRain(datenum(ObsRain(:,1:3)) == (cdate+1),4);
>
> % for rainfall days that are not equal to zero
> if ObsRain(k,4)~=0
>
> if (crain_m_1 < 0) && (crain_p_1 < 0)
> classed_rain(k,:) = 1;
> end
>
> if(crain_m_1 > 0) && (crain_p_1 == 0)
> classed_rain(k,:) = 2;
> end
>
> if(crain_m_1 == 0) && (crain_p_1 > 0)
> classed_rain(k,:) = 3;
> end
>
> if (crain_m_1 > 0) && (crain_p_1 >0)
> classed_rain(k,:) = 4;
> end
>
> else
> classed_rain(k,:) = NaN;
>
> end
>
>
> end
>
>
>
>
> -----
>
> can anyone suggest any ways to fix this ?
>
> Thanks in advance for your help
From: Carl on
"Priom Rahman" <z3188254(a)unsw.edu.au> wrote in message <i3nl2a$fba$1(a)fred.mathworks.com>...
> Hi guys I had have a small problem,
>
> I'm dealing with rainfall records, which i need to class (I've included the first 25 data sets)
>
> data is in the form:
> Year|month|day|rain
>
> ObsRain = [
> 1962 7 6 0
> 1962 7 7 0
> 1962 7 8 0
> 1962 7 9 0
> 1962 7 10 0
> 1962 7 11 0
> 1962 7 12 0
> 1962 7 21 0
> 1962 7 22 0
> 1962 7 24 54
> 1962 7 25 0
> 1962 8 1 22.8000000000000
> 1962 8 4 5.80000000000000
> 1962 8 5 47.6000000000000
> 1962 8 6 0
> 1962 8 8 217.800000000000
> 1962 8 9 94.6000000000000
> 1962 8 10 4.50000000000000
> 1962 8 11 43.8000000000000
> 1962 8 12 128.200000000000
> 1962 8 13 241.700000000000
> 1962 8 14 115.700000000000
> 1962 8 15 96.1000000000000
> 1962 8 19 132.100000000000
> 1962 8 20 0]
>
>
> ----------
> What i need to do is class them based on if its:
>
> a random day with rain - (current day rained, day before didn't rain. day after didn't rain) = class 1
> start of a wet spell - (current day rained, day before didn't rain., day after rained) = class 2
> end of a wet spell - (current day rained, day before rained, day after didn't rain) = class 3
> middle of a wet spell - (current day rained, day before rained, day after rained) = class 4
>
> -----------
>
> now before when I had a continuous sequence with no breaks in the record I was using the following function (and it worked fine):
> -----------
> for k =2:(size(ObsRain,1)-1)
> % for rainfall days that are not equal to zero
> if ObsRain(k,4)~=0
>
> if (ObsRain((k-1),4)== 0) && (ObsRain((k+1),4)== 0)
> classed_rain(k,:) = 1;
> end
>
> if(ObsRain((k-1),4)> 0) && (ObsRain((k+1),4)== 0)
> classed_rain(k,:) = 2;
> end
>
> if(ObsRain((k-1),4)== 0) && (ObsRain((k+1),4)>0)
> classed_rain(k,:) = 3;
> end
>
> if (ObsRain((k-1),4)> 0) && (ObsRain((k+1),4)>0)
> classed_rain(k,:) = 4;
> end
>
> else
> classed_rain(k,:) = NaN;
>
> end
>
>
> end
>
>
>
>
>
>
>
> -------
>
> how ever with missing dates, (as given above in the data example) i can no longer use a row - 1 function. I was trying to use datenum, but it was failing because it creates a blank value: here is my code:
>
> --------
> for k = 1: size(ObsRain,1)
>
> % Current date
> cdate = datenum(ObsRain(k,1), ObsRain(k,2), ObsRain(k,3));
> %[Cyear Cmonth Cday] = datevec(Cdate);
> [cyear cmonth cday] = datevec(cdate);
> % Current rain
> crain = ObsRain(datenum(ObsRain(:,1:3)) == (cdate),4);
> % Rain previous day
> crain_m_1 = ObsRain(datenum(ObsRain(:,1:3)) == (cdate-1),4);
> % Rain following day
> crain_p_1 = ObsRain(datenum(ObsRain(:,1:3)) == (cdate+1),4);
>
> % for rainfall days that are not equal to zero
> if ObsRain(k,4)~=0
>
> if (crain_m_1 < 0) && (crain_p_1 < 0)
> classed_rain(k,:) = 1;
> end
>
> if(crain_m_1 > 0) && (crain_p_1 == 0)
> classed_rain(k,:) = 2;
> end
>
> if(crain_m_1 == 0) && (crain_p_1 > 0)
> classed_rain(k,:) = 3;
> end
>
> if (crain_m_1 > 0) && (crain_p_1 >0)
> classed_rain(k,:) = 4;
> end
>
> else
> classed_rain(k,:) = NaN;
>
> end
>
>
> end
>
>
>
>
> -----
>
> can anyone suggest any ways to fix this ?
>
> Thanks in advance for your help

Have a think about what you are asking Matlab to do and see if it is feasible.

Take for example the 1st August in your data set. You know it rained on that day but due to missing data you do not know if it rained on the 31st July or the 2nd August. You (and Matlab) therefore can not tell what class of day it was.

Looking at the 4th August you know it rained on the 5th but don't know if it rained on the 3rd, it could be a class 2 day or a class 4 day but not class 1 or 3.

First decide how do you want matlab to deal such unknowns, then think how to program it.
From: Priom Rahman on
I would like to teart the the missing data days as days with no rain (0 rain)

So in your example for 1st of August, I want it to be class 1 (because I want it to assume that there was no rain on 31st of July and 2nd of Ausgust) - this is so because often rainfall going back a few dacades were only reported when they occured.

so essentially -i want matlab to recognise that if there was no datenum value (i.e blank) it would treat the rain for that day as it as 0, and class any subsequent days accordingly.

I am not sure what you mean about mapping missing days ....

I'm sorry, I'm not yet very matlab savy.
From: Carl on
Sorrry to be so long getting back to you,

What Priom meant by mapping out missing days was to create a data set which contains every day. Have a look in the help for the details of the use of the following functions:

datenum
datevec

The follwoing code will fill in the missing days of your data with 0's enabling you to use the code yuo wrote.

% Construct an array of every day in from the from the forst observation you have to
% the last (assumes they are in order)
Datez=(datenum(ObsRain(1,1:3)):datenum(ObsRain(end,1:3)));
% Initally put 0 rain for every day
Rain=0.*Datez;
% For dates that have an observation enter the recorded rainfall
Rain(datenum(ObsRain(:,1:3))-Datez(1)+1)=ObsRain(:,4);
% get the year month and day of the complete list of dates
DatezVec =datevec(Datez);
FullObs=(DatezVec(:,1:3));
%Add the rainfall into the matrix of year month and day
FullObsRain=[DatezVec(:,1:3) Rain'];