How to convert hourly rainfall data into daily rainfall - matlab

I have hourly rainfall and other data for long period. I would like to get daily values from these hourly data. My daily values should start from hour 1 to hour 24.
Year Month Day Hour Rain RH Temp
1976 1 1 1 3.4 60 16
1976 1 1 2 0 80 18
1976 1 1 3 NaN 50 18

First, get rid of NaN's, because e.g. 1+NaN=NaN, so it destroys any summation, average etc.
Then you can do for example simply this:
% M_hourly is a matrix with rows [Year Month Day Hour Rain RH Temp]
[H,W]=size(M_hourly);
for ii=1:24:H
Rain=M_hourly(ii:ii+23,5);
Rain_daily(ii)=sum(Rain);
end
You should prealocate the Rain_daily, cause it grows inside a loop.
I could be more helful, if you provide us with a more specific questions. I am just guessing what is the problem.

Related

Calculating monthly averages of daily temperature data including negative and positive values

I am trying to calculate monthly average temperatures for a dataset with daily temperature values that spans three years. With the data.frame appearing like this example"
Date Month Temperature
12-2-2016 December -10
12-3-2016 December -12
01-2-2017 January -15
01-3-2017 January -14
02-3-2017 February 3
02-4-2017 February 7
03-2-2017 March 8
03-3-2017 March 9
I tried running the following code in order to create a new dataframe with the Month and the average temperature:
group_by(df$month) %>%
summarise(mean_airtemp = mean(Temperature))
However, when running this code I get NA for certain months which I believe is attributed to negative values. I have tried to figure it out but have only found solutions that seem to separate the values based on whether they are negative or positive.
you can use groupby of month and temp together
df.groupby(['Month'])['Temperature'].mean().reset_index(name = 'avg')

How to average monthly data using a specific method in Matlab?

I have the following vector of monthly values (vectorA). I put the date related info next to it to help illustrate the task but I work with just the vector itself
dates month_in_q vectorA
31/01/2020 1 10
29/02/2020 2 15
31/03/2020 3 6
30/04/2020 1 8
31/05/2020 2 4
30/06/2020 3 3
How can I create a new vectorNEW according to this algorithm
In each quarter the first month is the original first month
In each quarter the second month is the average of first and second month
In each quarter the third month is the average of all three months
So that I get the following vectorNEW by manipulating the original vectorA in a loop given this the re-occuring pattern above
dates month_in_q vectorA vectorNEW
31/01/2020 1 10 10
29/02/2020 2 15 AVG(10+15)
31/03/2020 3 6 AVG(10+15+6)
30/04/2020 1 8 8
31/05/2020 2 4 AVG(8+4)
30/06/2020 3 3 AVG(8+4+3)
... ... ... ...
An elegant solution was provided by the user dpb on mathworks website.
vectorNEW=reshape(cumsum(reshape(vectorA,3,[]))./[1:3].',[],1);
Further info below
https://uk.mathworks.com/matlabcentral/answers/823055-how-to-average-monthly-data-using-a-specific-method-in-matlab

daily to monthly sum in matlab of multiple years

My data is excel column. In excel sheet one column contain last 50 years date (no missing date; dd/mm/yyyy format) and in other column everyday rainfall (last 50 years; no blank).
I want to calculate what is the sum of monthly rainfall for every month of last 50 years in Matlab. Remember, there four types of month ending date: 30, 31, 28 and 29. Upto now I am able read the dates and rainfall value from excel file like below
filename = 'rainfalldate.xlsx';
% Extracts the data from each column of textData
[~,DateString ]= xlsread(filename,'A:A')
formatIn = 'dd/mm/yyyy';
DateVector = datevec(DateString,formatIn)
rainfall = xlsread(filename,'C:C');
what is the next step so that I can see every months of last fifty years rainfall sum?
I mean suppose July/1986 rainfall sum... Any new solutions?Code or loop base in Matlab 2014a
As #Daniel suggested, you could use accumarray for this.
Since you want the monthly rainfall for multiple years, you could combine year and month into one single variable, ym.
It is possible then to identify if a value belongs to both a determined year and month at once, and "tag" it using histc function. Values with similar "tags" are summed by the accumarray function.
As a bonus, you do not have to worry about the number of days in a month. Take a look at this example:
% Variables initialization
y=[2014 2014 2015 2015 2015 2014 2014 2015 2015];
m=[1 1 1 2 2 3 3 3 3]; % jan, feb and march
values = 1:9;
% Identifyier creation and index generation
ym = y*100+m;
[~,subs]=histc(ym,unique(ym));
% Sum
total = accumarray(subs',values);
The total variable is the monthly rainfall already sorted by year and month (yyyymm).

How to convert Number of hours into number of work-hours days in postgreSql?

i have a column which has data in "12:19:04" this format.
i.e 12 hours 19 min and 4 sec.
i want to convert this into number of days considering working hours as one day i.e 10 hr
so the above time should be displayed as 1 day 2 hr 19 min and 4 sec
and also if the hours value is too huge then month days hours and minutes should be displayed.
Kindly help me in this.
Easiest way: Use epoch time, divide by 10 days in seconds, convert remainder back to seconds.
WITH s(secs) AS (SELECT extract(epoch from interval '12:19:04'))
SELECT justify_interval(
floor(secs / 36000) * INTERVAL '1' DAY
+ (secs::integer % 36000) * INTERVAL '1' SECOND
) FROM s;

Calculating MAX(DATE) for Value Groups Where Values Go Back and Forth

I have another challenge that I am trying to resolve but unable to get the solution yet. Here is the scenario. Pardon the formatting if it messes up at the time of posting.
ACCT_NUM CERT_ID Code Date Desired Output
A 1 10 1/1/2007 1/1/2008
A 1 10 1/1/2008 1/1/2008
A 1 20 1/1/2009 1/1/2010
A 1 20 1/1/2010 1/1/2010
A 1 10 1/1/2011 1/1/2012
A 1 10 1/1/2012 1/1/2012
A 2 20 1/1/2007 1/1/2008
A 2 20 1/1/2008 1/1/2008
A 2 10 1/1/2009 1/1/2010
A 2 10 1/1/2010 1/1/2010
A 2 30 1/1/2011 1/1/2011
A 2 10 1/1/2012 1/1/2013
A 2 10 1/1/2013 1/1/2013
As you can see, I need to do a MAX on the date based on each group of code values (apart from ACCT_NUM and CERT_ID) before the value changes. If the same value repeats, I need to a MAX of the data again for that group separately. For example, for CERT_ID of '1', I cannot group all four rows of Code 10 to get a MAX date of 1/1/2012. I need to get the MAX for the first two rows and then another MAX for the next two rows separately since there is another code in between. I am trying to accomplish this in Cognos Framework Manager.
Gurus, please advise.
The syntax for getting the max value for CERT_ID is:
maximum(Date for CERT_ID)
If you want additional level/s for max you can use the following syntax:
maximum(Date for ACCT_NUM,CERT_ID,Code)
In general, it is best practice to group and summarize values in report, not in framework manager.