%
%   CORVRAIN_HWDEMO.M    MATLAB file to process summary data concerning the 
%                    amount of rainfall in Corvallis, Oregon from 1890 to 1994
%
%   Several tasks will be performed here given the data file corvrain.dat:
%    (file obtained from the NMM toolbox at www.me.pdx.edu/~gerry/nmm/ )
%     1. read the data file corvrain.dat using the loadColData.m file (also
%          from the NMM toolbox)
%     2. compute and plot the total yearly precipitation in inches
%     3. determine the average, maximum, and minimum annual precipitation
%        also add a line showing the average value to the plot from Task 2
%     4. compute, plot, and tabulate the average rainfall by month
%
%   Reference:  The idea for this problem was obtained from Prob. 3.28 on 
%   pgs. 145-146 of the text "Numerical Methods with Matlab: Implementation
%   and Application," by G. Recktenwald, Prentice Hall, Inc., 2000.
%
%   File prepared by J. R. White, UMass-Lowell (last update: Sept. 2017)
%

      clear all,   close all,   nfig = 0;
%
%   Task 1:  use loadColData to read the desired data file
%   --> resulting data:  yr = vector containing the date (year)
%                      rain = matrix containing the raninfall (in hundreds of 
%                             an inch) for each month and year
      [yr,rain] = loadColData('corvrain.dat',13,1,1);
%
%   Task 2:  compute and plot the yearly total precipitation (inches)
      rain = rain/100;               % converts hundreds of inch to inches
      yrrain = sum(rain');           % sums 12 months of data to get yearly total
%
      nfig = nfig+1;   figure(nfig)
      plot(yr,yrrain,'r-','LineWidth',2), grid, hold on
      title('CorvRain\_HWDemo:  Yearly Rainfall for Corvallis, Oregon')
      xlabel('year'),ylabel('amount of rain (inches)')
%
%   Task 3:  determine the average, maximum, and minimum annual precipitation
%            also add a line showing the average value to the plot from above
      nyr = length(yr);              % number of years
      averain = sum(yrrain)/nyr;     % average value
      [minrain,imin] = min(yrrain);  yrmin = yr(imin);   % min rain and year it occurred
      [maxrain,imax] = max(yrrain);  yrmax = yr(imax);   % max rain and year it occurred
%
      fout = 1;
      fprintf(fout,'\n\n');
      fprintf(fout,'   CorvRain_HWDemo:  Summary Rainfall Results (in inches) for Corvallis, Oregon \n\n');
      fprintf(fout,'     average annual rainfall:            %8.1f  \n',averain);
      fprintf(fout,'     minimum annual rainfall and year:   %8.1f   %5d \n',minrain, yrmin);
      fprintf(fout,'     maximum annual rainfall and year:   %8.1f   %5d \n',maxrain, yrmax);
%     
      plot([1880 2000],[averain averain],'b-','LineWidth',2)
      gtext(['average rainfall = ',num2str(averain,'%4.1f'),' inches']),  hold off
%
%   Task 4:  compute, plot, and tabulate the average rainfall by month
      monthrain = sum(rain);        % sums over all years to get monthly totals
      avemrain = monthrain/nyr;     % ave value by month
%
      nfig = nfig+1;   figure(nfig)
      bar(avemrain), grid, range = axis;  range(2) = 13;  axis(range)
      title('CorvRain\_HWDemo:  Monthly Rainfall for Corvallis, Oregon')
      xlabel('month of the year'),ylabel('amount of rain (inches)')
%
      months = ['Jan. ';'Feb. ';'March';'April';'May  ';'June ';
                'July ';'Aug. ';'Sept.';'Oct. ';'Nov. ';'Dec. '];
      fprintf(fout,'\n');
      fprintf(fout,'     Average Monthly Rainfall Values (in inches)\n');
      fprintf(fout,'          Month        Amount  \n');
      for n = 1:12
        fprintf(fout,'       %8s     %8.1f \n',months(n,:),avemrain(n));
      end
%
%   end of program

   
