function [E_radial_WI,I_radial_WI,fig5,fig6]=radialwaveplot(radialE,radialI,spec,sysinfo) 

%make sure to load the EMEP, IMLM and wavesmon samples
%EMEP=load('emep'), IMLM=load('imlm'), spec=load('wavesmon')
%rangeE=load('EMEP_range'), rangeI=load('IMLM_range')

%set up the wavesmon data in a structure

%what is the magnetic variation to nearest degree
magvar=-10;

%first change to m^2/Hz/deg
wmon.S=spec/(360*1000*1000);

%what is the start angle
heading=sysinfo(18,:);
heading=heading/100;
sangle=heading+magvar;

%what is the frequency and dir resolution for those generated in DIWASP
freqres=0.01;
freqs=[0.01:0.01:.4];
dirres=2;
dirs=[-180:2:180];

%set up the directions
adj_angle=90-sangle+360+180;
wmon.dirs=[adj_angle:-4:-(356-adj_angle)]; 
wmondirres=4;
wmon.xaxisdir=90;
wmon.freqs=[0.00781250:0.00781250:1];
wmonfreqres=0.00781250;

% plot the spectrum generated through DIWASP 
scrsz = get(0,'ScreenSize');
fig5=figure('Position',[scrsz]);
subplot(2,3,1);
subplotspec(radialE,4);
title('EMEP radial vel');

subplot(2,3,2);
subplotspec(radialI,4);
title('IMLM radial vel');

subplot(2,3,3);
subplotspec(wmon,4);
title('Wavesmon output');


%calculate just the dir energy spectrum on a single graph

EMEPdir=sum(radialE.S)*freqres;
IMLMdir=sum(real(radialI.S))*freqres;
wmondir=sum(wmon.S)*wmonfreqres;

%calculate just the frequency energy spectrum
EMEPfreq=sum(radialE.S')*dirres;
IMLMfreq=sum(real(radialI.S)')*dirres;
wmonfreq=sum(wmon.S')*wmondirres;

%Compute the coefficient for the upper and lower error bounds for the
%frequency
%spectrum assuming 95% confidence.  Added on 9/17/08
degF=radialE.degF;
chiUp=chi2inv(.975,degF);
chiLow=chi2inv(.025,degF);
coeffUp=degF/chiLow;
coeffLow=degF/chiUp;

%calculate the conf limits throughout the frequency spectrum
EMEPfreqUP=EMEPfreq*coeffUp;
EMEPfreqLOW=EMEPfreq*coeffLow;

%Find the maximum for the directional spectrum so we can set up the proper
%x-axis
[maxvalue,maxindex] = max(EMEPdir);
maxdir=dirs(maxindex);
% set up the x-axis for all of the spectra depending on the max
if ((100 < maxdir) | (maxdir < -100));
    %for diwasp spectra
    index1=find(dirs < 0);
    index2=find(dirs > -1);
    dirs(index1)=dirs(index1) +360;
    %for wavesmon
    Aindex=find(wmon.dirs < 0);               
    Bindex=find((-1 < wmon.dirs) & (wmon.dirs < 361));
    Bindex2=find(wmon.dirs > 360);
    wmon.dirs(Bindex2)=wmon.dirs(Bindex2)-360;
    wmon.dirs(Aindex)=wmon.dirs(Aindex)+360;
    %plot the directional energy spectrum
    fig6=figure('Position',[scrsz]);
    subplot(1,2,1);
    h1 = plot(dirs(index2),EMEPdir(index2),'b');
    hold on
    h1a= plot(dirs(index1),EMEPdir(index1),'b');
    %plot the wavesmon data
    h2 = plot(wmon.dirs(Bindex2),wmondir(Bindex2),'k');
    h2a = plot(wmon.dirs(Bindex),wmondir(Bindex),'k');
    h2b= plot(wmon.dirs(Aindex),wmondir(Aindex),'k');
    axis(axis);
    h5 = plot(dirs(index2),IMLMdir(index2),'c');
    h5a= plot(dirs(index1),IMLMdir(index1),'c');

else
    %for diwasp spectra do nothing
    
    %for wavesmon
    Aindex=find(wmon.dirs > 180);
    Bindex=find(wmon.dirs < 181);
    wmon.dirs(Aindex)=wmon.dirs(Aindex)-360;
    %plot the directional energy spectrum
    fig6=figure('Position',[scrsz]);
    subplot(1,2,1);
    h1 = plot(dirs,EMEPdir,'b');
    hold on
    %plot the wavesmon data
    h2 = plot(wmon.dirs(Aindex),wmondir(Aindex),'k');
    h2a = plot(wmon.dirs(Bindex),wmondir(Bindex),'k');
    axis(axis);
    h5 = plot(dirs,IMLMdir,'c');
end

legend([h1, h2,h5], 'EMEP radial vel','wavesmon','IMLM radial vel','location','best');
title('directional wave spectrum integrated over frequency');
xlabel('axis angle (degrees true)');
ylabel('m^2 / deg');

%plot the frequency energy spectrum
subplot(1,2,2);
h1 = plot(freqs,EMEPfreq,'b');
hold on
%h2 = plot(freqs,EMEPfreqLOW,'b--');
%h3 = plot(freqs,EMEPfreqUP,'b--');
h4 = plot(wmon.freqs,wmonfreq,'k');
axis(axis);
h5 = plot(freqs,IMLMfreq,'c');
legend([h1,h4,h5],'EMEP radial vel','wavesmon','IMLM radial vel','location','best');
title('directional wave spectrum integrated over direction');
xlabel('frequency in Hz');
ylabel('m^2 / Hz');

% ______Calculate and display the wave parameters SigH, Tp, Dp,DTp_______

%For EMEP radial

%calculate the 0,1,2 moments
m0=sum(EMEPfreq*freqres);                              
m1=sum(freqs.*EMEPfreq*freqres);
m2=sum((freqs.^2).*EMEPfreq*freqres);
% Calculate the Sig wave height
EMEP_radial_Hsig=4*sqrt(m0);
%Use the function HsigConf.m to calculate the sigH confidence limits
EMEP_HsConf=HsigConf(radialE);

% Calculate the peak period Tp
[P,I]=max(EMEPfreq);
EMEP_radial_Tp=1/(freqs(I));

%Calculate the Direction of the peak period DTp
[P,I]=max(real(radialE.S(I,:)));
EMEP_radial_DTp=dirs(I);

%Calculate the Dominant Direction Dp
[P,I]=max(EMEPdir);
EMEP_radial_Dp=dirs(I);

%Display on the screen the SigH,Tp,Dp,DTp
disp(['EMEP radial vel']);
disp(['SigH (meters): ' num2str(EMEP_radial_Hsig)]);
disp(['SigH 95% confidence limits: ' num2str(EMEP_HsConf)]);
disp(['peak period (seconds): ' num2str(EMEP_radial_Tp)]);
disp(['Dir of peak period: ' num2str(compangle(EMEP_radial_DTp, radialE.xaxisdir))]);
disp(['Dominant Direction: ' num2str(compangle(EMEP_radial_Dp, radialE.xaxisdir))]);
disp(['  ']);

E_radial_WI.hsig=EMEP_radial_Hsig;
E_radial_WI.hconf=EMEP_HsConf;
E_radial_WI.tp=EMEP_radial_Tp;
E_radial_WI.dtp=compangle(EMEP_radial_DTp, radialE.xaxisdir);
E_radial_WI.dp=compangle(EMEP_radial_Dp, radialE.xaxisdir);

% For IMLM r

%calculate the 0,1,2 moments
m0=sum(IMLMfreq*freqres);                              
m1=sum(freqs.*IMLMfreq*freqres);
m2=sum((freqs.^2).*IMLMfreq*freqres);
% Calculate the Sig wave height
IMLM_radial_Hsig=4*sqrt(m0);
%Use the function HsigConf.m to calculate the sigH confidence limits
IMLM_HsConf=HsigConf(radialI);

% Calculate the peak period Tp
[P,I]=max(IMLMfreq);
IMLM_radial_Tp=1/(freqs(I));

%Calculate the Direction of the peak period DTp
[P,I]=max(real(radialI.S(I,:)));
IMLM_radial_DTp=dirs(I);

%Calculate the Dominant Direction Dp
[P,I]=max(IMLMdir);
IMLM_radial_Dp=dirs(I);

%Display on the screen the SigH,Tp,Dp,DTp
disp(['IMLM radial vel']);
disp(['SigH (meters): ' num2str(IMLM_radial_Hsig)]);
disp(['SigH 95% confidence limits: ' num2str(IMLM_HsConf)]);
disp(['peak period (seconds): ' num2str(IMLM_radial_Tp)]);
disp(['Dir of peak period: ' num2str(compangle(IMLM_radial_DTp, radialI.xaxisdir))]);
disp(['Dominant Direction: ' num2str(compangle(IMLM_radial_Dp, radialI.xaxisdir))]);
disp(['  ']);

I_radial_WI.hsig=IMLM_radial_Hsig;
I_radial_WI.hconf=IMLM_HsConf;
I_radial_WI.tp=IMLM_radial_Tp;
I_radial_WI.dtp=compangle(IMLM_radial_DTp, radialI.xaxisdir);
I_radial_WI.dp=compangle(IMLM_radial_Dp, radialI.xaxisdir);

% for wavesmon

%calculate the 0,1,2 moments
m0=sum(wmonfreq*wmonfreqres);                              
m1=sum(wmon.freqs.*wmonfreq*wmonfreqres);
m2=sum((wmon.freqs.^2).*wmonfreq*wmonfreqres);
% Calculate the Sig wave height
Hsig=4*sqrt(m0);

% Calculate the peak period Tp
[P,I]=max(wmonfreq);
Tp=1/(wmon.freqs(I));

%Calculate the Direction of the peak period DTp
[P,I]=max(real(wmon.S(I,:)));
DTp=wmon.dirs(I);

%Calculate the Dominant Direction Dp
[P,I]=max(wmondir);
Dp=wmon.dirs(I);

%Display on the screen the SigH,Tp,Dp,DTp
disp(['Wavesmon output']);
disp(['SigH (meters): ' num2str(Hsig)]);
disp(['peak period (seconds): ' num2str(Tp)]);
disp(['Dir of peak period: ' num2str(compangle(DTp, wmon.xaxisdir))]);
disp(['Dominant Direction: ' num2str(compangle(Dp, wmon.xaxisdir))]);
disp(['  ']);


%function to change from axis angles to compass bearings

function angle=compangle(angle,xaxisdir)
angle=xaxisdir*ones(size(angle))-angle;
angle=angle+360*(angle<0);
angle=angle-360*(angle>360);





