You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to take out extreme event annually from the data.
1 view (last 30 days)
Show older comments
sir i have program who calculate extreme event annually for whole 95 years but this program works only for those who does not include leap years it means data size is 20x22x34675(lon,lat,time)(365*95) but i want to calculate extreme event annually for those data who include leap years (it mean 365 Or 366 then 365 Or 366 ....each separate years) then what will be change in this program for doing this? my data size is (prec_all_years) 20x22x34698(lon,lat,time)(365*95+23).this is daily data.please help me sir.thank you.
prec_all_years = prec_all_years*86400; % rainfall in mm/day
bins = [64.5 124.5 244.5 Inf];
% Reshape into lat/lon/day/year, and bin over 3rd dimension (days)
counts = histc(reshape(prec_all_years,20,22,365,95),bins,3);
% Extract the counts for each of the 3 categories
hr = squeeze(counts(: , : , 1 , : ));
vhr = squeeze(counts(: , : , 2 , : ));
ehr = squeeze(counts(: , : , 3 , : ));
% Extract a region
xe = 13:20; ye = 11:16;% xe is x for east,ye is y for east
% Extract counts for each category
count_hr_east = sum(reshape(hr(xe , ye , : ) , [] , 95 ));
count_vhr_east = sum(reshape(vhr(xe , ye , : ) , [] , 95 ));
count_ehr_east = sum(reshape(ehr(xe , ye , : ) , [] , 95 ));
Accepted Answer
dpb
on 17 Apr 2014
Edited: dpb
on 20 Apr 2014
I really, Really, REALLY wish you'd quit this incessant creation of new threads and just carry on a linear conversation...I'm thinking this is about the last shot you've got with me, any way.
OK, Ravi, let's try this an entirely different way -- instead of the complexity of the reshape over multiple dimensions that you just don't seem to be grasping the basics of, let's just go back to brute force and stubborness and maybe you can at least then get to an answer, letting code refinement come later...
IIUC, you've a 3D data set of precipitation predictions on a daily basis for a 2D matrix of geographical locations with the 3rd dimension representing each day over a period of years. Your present objective is to return the maximum predicted rainfall for each year by year over the entire time period.
In our previous thread on data selection, we agreed that there is a corollary vector of time data that can be generated as
dn = datenum([2006 1 1 ;2100 12 31]); % Matlab datenum corresponding to planes
Y = datevec(dn); % the year of each of the above
ERRATUM:
Y = datevec(dn); % the year of each of the above
should be
[Y,~] = datevec([dn(1):dn(2)]);
--dpb
So, given that, to find the maximum for each year by year is easy enough...
max_precip=zeros(size(precip,1),size(precip,2),Y(end)-Y(1)+1,1); % preallocate
iyr=0; % counter for the array
for yr=Y(1):Y(end) % loop over all the years in range
iyr=iyr+1; % increment the counter
max_precip(:,:,iyr)=max(precip(:,:,yr==Y),3); % max over the years
end
Now you'll have a lat x lon x 95 array of the yearly maxima each plane being the max for each year in the dataset at each point. You can simply modify the two lat/lon indices to select various regions rather than globally.
ADDENDUM:
I did think about the dataset option a little -- it's possible but would require duplicating a lot of data to convert the 3D storage to 2D as the dataset object isn't quite as flexible as I was hoping perhaps it might be.
27 Comments
ravi
on 18 Apr 2014
Edited: ravi
on 18 Apr 2014
i am very sorry sir but i am in very big problem.i am trying this last 6 or 7 days but i got every time wrong result.
sir i want to same output as given my above program.please sir help me.this is my humble request.again i am very sorry sir.i am new in this field so this is not easy for me.
ADDENDUM:
sir this code giving this result
X1 20x1 160 double
Y 2x6 96 double
Y1 22x1 176 double
dn 2x1 16 double
iyr 1x1 8 double
max_precip 0x1 0 double
prec_all_years 20x22x34698 122136960 double
prec_all_years1 20x22x34698 122136960 double
yr 0x0 0 double
ravi
on 20 Apr 2014
when sir i am trying this line
[Y,~] = datevec([dn(1):dn(2)].');
then this is giving this result
??? [Y,~] = datevec([dn(1):dn(2)].');
|
Error: Unbalanced or unexpected parenthesis or bracket.
dpb
on 20 Apr 2014
Good catch, Star--if in editor an incompleted nesting on the previous could show up here...
Star Strider
on 20 Apr 2014
Always happy to help! This happened to me a few times, and it was a initially a challenge to find the error. If the actual line looked OK, I quickly learned to look at the previous one.
Voting this Answer up out of sympathy!
ravi
on 20 Apr 2014
first write this
dn = datenum([2006 1 1 ;2100 12 31]);
then this
Y = datevec(dn);
when write this
[Y,~] = datevec([dn(1):dn(2)]);
then this will giving this error
??? [Y,~] = datevec([dn(1):dn(2)].'); | Error: Unbalanced or unexpected parenthesis or bracket.
dpb
on 20 Apr 2014
Edited: dpb
on 21 Apr 2014
Ewww...what version of Matlab are you using?
It looks like it must be quite old. The problem is that your version doesn't support the ~ operator to indicate to not return the alternate arguments.
Try
Y = datevec([dn(1):dn(2)]); Y=Y(:,1);
instead. This will return the whole nx6 array but then just save the first column, the year, which is all we need (or want).
BTW, you don't need (or want) the preceding line
Y=datevec(dn);
at all, either--it's where I made the previous oversight we corrected three or so iterations ago.
And, you're learning--you did post the pertinent stuff in context enough to see what actually happened (that was something I was not expecting). Keep it up... :)
ravi
on 21 Apr 2014
Edited: ravi
on 21 Apr 2014
sir when i m write this
max_precip=zeros(size(precip,1),size(precip,2),Y(end)-Y(1)+1,1); % preallocate
iyr=0; % counter for the array
for yr=Y(1):Y(end) % loop over all the years in range
iyr=iyr+1; % increment the counter
max_precip(:,:,iyr)=max(precip(:,:,yr==Y),3); % max over the years
end
t
this code then giving this error
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
whos
Name Size Bytes Class
Attributes
X1 20x1 160
Y 1x34698 277584
Y1 22x1 176
dn 2x1 16
iyr 1x1 8
max_precip 20x22x95 334400
prec_all_years 20x22x34698 122136960
precip 20x22x34698 122136960
yr 1x1 8
ravi
on 21 Apr 2014
thank you Walter Roberson sir this is working perfectly.
dpb sir i want take out extreme event annually for each lat and lon.it mean
inds1 = find(precip >=64.5 & precip<= 124.5);
inds2 = find(precip > 124.5 & precip <=244.5);
inds3 = find(precip > 244.5);
at each lat and lon annually
ravi
on 23 Apr 2014
Edited: ravi
on 23 Apr 2014
sorry sir for disturbing you again but sir i am trying from yesterday but i got every time wrong answer please sir help me.thank you.
bins = [2.5 64.5 124.5 244.5 Inf];
precip = prec_all_years*86400;
dn = datenum([2006 1 1 ;2100 12 31]);
Y = datevec(dn);
[Y,~] = datevec([dn(1):dn(2)].');
max_precip=zeros(size(precip,1),size(precip,2),Y(end)-Y(1)+1,1);
iyr=0;
for yr=Y(1):Y(end)
iyr=iyr+1;
max_precip(:,:,iyr)=histc(precip(:,:,yr==Y),[],bins,3);
end
then this giving error
Error using histc
Too many input arguments.
Image Analyst
on 23 Apr 2014
There is no hictc function, unless you wrote one yourself, in which case you should know what arguments it needs.
dpb
on 23 Apr 2014
Edited: dpb
on 23 Apr 2014
max_precip(:,:,iyr)=histc(precip(:,:,yr==Y),[],bins,3);
READ the doc for histc...it says the input arguments are
histc(X,EDGES,DIM) operates along the dimension DIM.
Why did you try to use the arguments for the call for max unaltered for histc?
If you do this as shown then you're going to trash the results just found for the maxima by year; is that intended?
Wouldn't you rather then look at the histogram counts in addition to the maxima, not instead of? Sotoo
cnts_precip(:,:,iyr)=histc(precip(:,:,yr==Y),bins,3);
? Just askin', don't know what you all want/need but seems to me...
ravi
on 24 Apr 2014
dpb sir i want take out extreme event annually for each lat and lon.it mean
inds1 = find(precip >=64.5 & precip<= 124.5);
inds2 = find(precip > 124.5 & precip <=244.5);
inds3 = find(precip > 244.5);
at each lat and lon annually i want to this result so please help.thank you
Walter Roberson
on 24 Apr 2014
Edited: Walter Roberson
on 24 Apr 2014
[counts, inds] = histc(precip, [-inf, 64.5, 124.5, 244.5, inf]);
Now each location at which inds == 1, the precip was below 64.5 . Each location at which inds == 2, the precip was 64.5 exactly or up to 124.5. Each location at which inds == 3, the precip was 124.5 exactly or up to 244.5 . Each location at which inds == 4, the precip was 244.5 exactly or more.
precip may be an array for the above command, even a 3D array.
ravi
on 24 Apr 2014
Edited: ravi
on 24 Apr 2014
Walter sir my main problem is this
sir i have program who calculate extreme event annually for whole 95 years but this program works only for those who does not include leap years it means data size is 20x22x34675(lon,lat,time)(365*95) but i want to calculate extreme event annually for those data who include leap years (it mean 365 Or 366 then 365 Or 366 ....each separate years) then what will be change in this program for doing this? my data size is (prec_all_years) 20x22x34698(lon,lat,time)(365*95+23).this is daily data.please help me sir.thank you.
prec_all_years = prec_all_years*86400; % rainfall in mm/day
bins = [64.5 124.5 244.5 Inf];
% Reshape into lat/lon/day/year, and bin over 3rd dimension (days)
counts = histc(reshape(prec_all_years,20,22,365,95),bins,3);
% Extract the counts for each of the 3 categories
hr = squeeze(counts(: , : , 1 , : ));
vhr = squeeze(counts(: , : , 2 , : ));
ehr = squeeze(counts(: , : , 3 , : ));
% Extract a region
xe = 13:20; ye = 11:16;% xe is x for east,ye is y for east
% Extract counts for each category
count_hr_east = sum(reshape(hr(xe , ye , : ) , [] , 95 ));
count_vhr_east = sum(reshape(vhr(xe , ye , : ) , [] , 95 ));
count_ehr_east = sum(reshape(ehr(xe , ye , : ) , [] , 95 ));
sir see this link also
http://www.mathworks.com/matlabcentral/answers/125393-how-to-take-out-extreme-event-annually-from-the-data
please sir help me,thank you.
dpb
on 24 Apr 2014
Edited: dpb
on 24 Apr 2014
Help comes to those who help themselves...
We've passed that previous form as you never seemed to be able to grasp the idea of the reshape operations as I provided you the selection logic to select the two year classes (leap vs non-) some time ago and the guidelines to using them.
Unfortunately, a very big disadvantage of the propensity you have of generating multiple threads and using multiple persona is that I can now no longer find that response to redirect you there and I'm not going to spend time to regenerate it here. But, in short, use the isLeapYear() function from before to generate the logical vector to select the years, then adjust the reshape for those years for the proper number of days/year and number of years found instead of the 365 and 95 hardcoded into your present code. As I suggested before, you need to do this twice; once of leap and once again for non-leap years; saving the two results and then combine them as wanted.
But, when that seemed beyond your grasp earlier, I retreated to a different tack and so now we have provided you working code that selects each year and does what you've asked -- provide a max precip value by year for each location in your grid and another that will provide you counts for your binning request of levels again by year and by location.
I also in the previous gave you the logic expression to determine which years are/are not leap years with which you can select the proper number of days over which to do averages and the like.
You have (and have had for some time) all the pieces required; now your mission should you choose to accept it is take those and use them...that's your job.
ravi
on 25 Apr 2014
Thank you so much dpb sir for your support.sorry dpb sir but this link question is different because this program (question) only for those who does not include leap years,this is not same thread but i wrote this link here because i want to same thing for those data who include leap years sir please help me.
dpb
on 25 Apr 2014
Edited: dpb
on 25 Apr 2014
I've done everything I know to do except write a complete application...as said in the previous thread we discussed at length the way to select between leap- and non-leap years; I don't know how to put it any differently and see no point in rehashing the same thing after about the fifth or sixth iteration.
You presently have an array of precip values by year by location--all you have to do is select the planes using the isLeapYear logic function I provided you some time back and process them then do the other set. You then combine the two results as needed.
ADDENDUM:
OK, I did find the previous thread; for your convenience the essence of the isLpYr function is reproduced below--
isLpYr=(eomday(Y,2)==29); % logical vector T for Y==leap year
The full discussion where I demonstrate its use and that it returns the expected numbers for each case is at
I suggest you study it well; the answer to the universe is within... :)
Star Strider
on 25 Apr 2014
dpb — I’m going to take the unusual action of accepting this for you. You’ve more than earned it.
dpb
on 25 Apr 2014
Strider -- thanks, I needed that! :)
I had hopes but I've finally come to the sad conclusion that perhaps your previous AFTERTHOUGHT may be all too accurate an assessment... :(
Star Strider
on 25 Apr 2014
My pleasure!
I don’t remember that particular ‘AFTERTHOUGHT’, but I appreciate your mentioning it.
ravi
on 25 Apr 2014
Again thank you so much dpb sir for your support.you are very nice person,thank you so much sir.
More Answers (0)
See Also
Categories
Find more on Calendar 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)