function Dat=MakDat(Prm);
%**************************************************************************
%Illustration of non-stationary extreme value modelling in MATLAB accompanying 
%Threshold modeling of nonstationary extremes
%Paul Northrop, Philip Jonathan and David Randell
%in 
%Extreme Value Modeling and Risk Analysis: Methods and Applications
%Dipak Dey and Jun Yan (Editors)
%2015
%**************************************************************************
%
%**************************************************************************
%GENERATE TEST DECK
%
%1. Typical directional extremes
%2. Covariates Dat.X specified on [0,360)
%3. Extreme value threshold (Thr), exceedance rate (Rat) and generalised Pareto
%shape (Shp) and scale (Scl) all simple sinusiodal
%4. Sample is plotted
%**************************************************************************

%Specify covariate domain
nBin=Prm.PssnBin; %number of covariate segments on domain
x=linspace(0,360,nBin+1);  %segment edges
xMid=(x(1:end-1)+x(2:end))/2; %segment midpoints

%Specify non-stationary extreme value behaviour
%Rat=@(x)6*sin(x./(180)*pi)+8; %function to generate rate
%Thr=@(x)ones(length(x),1); %function to generate threshold
%Shp=@(x)(sin((x-30)./(180)*pi))/10-0.2; %function to generate shape
%Scl=@(x)abs(sin(x./(180)*pi))+2;  %function to generate scale
%[ones(ceil(length(x)/2),1)*15;ones(floor(length(x)/2),1)*5];
%Specify non-stationary extreme value behaviour
Dat.Rat=@(x)20*sin(x./(180)*pi)+30; %function to generate rate
Dat.Thr=@(x)abs(sin(x./(180)*pi))+4; %function to generate threshold
Dat.Shp=@(x)(sin((x-30)./(180)*pi))/10-0.2; %function to generate shape
Dat.Scl=@(x)abs(sin(x./(180)*pi))+2;  %function to generate scale

%Generate numbers of occurrences per covariate segment
C=poissrnd(Dat.Rat(xMid));  %counts for each bin; poissrnd is a MATLAB statistics toolbox function

%Initialise output
Dat.n=sum(C);
Dat.X=NaN(Dat.n,1);
Dat.Y=NaN(Dat.n,1);

%Generate covariate values per segment
count=0;
for i=1:nBin
   I=(count+1):(count+C(i));
   count=count+C(i);
   Dat.X(I)=rand(C(i),1)*(x(i+1)-x(i))+x(i);  %space uniformly within segment
end
Dat.X=sort(Dat.X); %sort covariate values for convenience

%Generate extreme values per segment
Dat.Y=gprnd(Dat.Shp(Dat.X),Dat.Scl(Dat.X),Dat.Thr(Dat.X)); %gprnd is a MATLAB statistics toolbox function

%Plot the data
figure(1); plot(Dat.X,Dat.Y,'k.','markersize',15); axis('tight');

return;