function Earthquake_Plotter_v4(filename, directory, starttime,endtime, speed_factor, ColorBar_Upper_Limit, ColorBar_Lower_Limit, FramesPerSecond)

% Preparing SAC data:
% The data should be saved as .SAC, .SAC.bp, or .SAC.hp1
% Before running Earthquake_Plotter, you need to make the following changes
% in SAC:

% Divide out the Gain to convert amplitude from counts to cm/s; be sure to
% verify your units!

% Apply a Bandpass filter:
% SAC> r BK.PKD.HHT.SAC
% SAC> rmean
% SAC> bp p 2 n 4 c 2 16
% SAC> w append.bp

% Appy a highpass filter:
% SAC> r BK.PKD.HHT.SAC
% SAC> rmean
% SAC> hp p 2 n 4 c 0.5
% SAC> w append .hp1


% Along with the SAC files, the following other files must be in the same
% directory as Earthquake_Plotter for it to run:
%   fget_sac.m
%   sac2wav.m
%   sac.m
%   linex.m
% If you encountor an error message saying that there is no variable or 
% function, check to make sure it is in the directory.

% To run Earthquake_Plotter, you will need to provide it with parameter 
% values in this order:

% 1. filename - This is your SAC file and should end in .SAC
% 2. directory - This will be the name of the folder where your images and 
%                audio will be stored.  
% 3. starttime - Time in seconds which the seismograph starts
% 4. endtime - Time in seconds which the seismograph ends
% 5. speed_factor - The number of times the audio file will be speed up
% 6. ColorBar_Upper_Limit - The upper limit of the color bar of the
%                           spectrogram.
% 7. ColorBar_Lower_Limit - The lower limit of the color bar of the 
%                           spectrogram.
% 8. FramesPerSecond - This will determine how many frames are generated

% As well as inputing these values before running the code, you will be
% promted twice while running the code. The first will prompt you to enter
% a title for your plot.  The second will prompt you to enter the Hz range
% of the bp. 

% NB: Label placement in matlab is not always the positions of the
% objects in the actual image.  Check the saved image befor changing the
% postions of an object.

% After rendering the audio file and images, use quicktime pro to create a
% .mov file. Be sure to set the frames per second of the images to be the
% same value of variable FramesPerSecond from Earthquake_Plotter.
%

%-----------------------------------------
%
% Default parameters
%
%-----------------------------------------


    if nargin < 8, FramesPerSecond =10; end
    if nargin < 7, ColorBar_Lower_Limit = -80; end
    if nargin < 6, ColorBar_Upper_Limit = 0; end
    if nargin < 5, speed_factor = 500; end
    if nargin < 4, endtime = 2000; end
    if nargin < 3, starttime = 0; end
    if nargin < 2, directory = 'Movie';end 
    if nargin < 1, filename = 'BK.PKD.HHT.SAC'; end
 
%-------------------------------------------
%
% Check For Supporting Files
%
%-------------------------------------------

    
   if ~exist('fget_sac.m', 'file');

        error('"fget_sac.m" could not be found.  Please copy it into this directory')
   end
   
   if ~exist('sac2wav.m', 'file');

        error('"sac2wav.m" could not be found.  Please copy it into this directory')
   end
   
   if ~exist('sac.m', 'file');
        error('"sac.m" could not be found Please copy it into this directory')
   end
    
   if ~exist('linex.m', 'file');
        error('"linex.m" could not be found Please copy it into this directory')
   end
   
%---------------------------------------
%
% Create Necessary Folders and Variables
%
%---------------------------------------

    fileRenderName = [ directory '/Images/image'];
    icount=99;
    
    %check to see if folders exist; if not create folders
    if ~exist(filename,'dir')
        string_mkdir = ['mkdir ' directory];
        system(string_mkdir);
        system([string_mkdir '/Audio']);
        system([string_mkdir '/Images']);
    end

    %if the 'Audio' folder has been deleted from the specified directory, create
    if ~exist([filename '/Audio'], 'dir')
        string_mkdir = ['mkdir ' directory];
        system([directory '/Audio']);
    end

    %if the 'Image' folder has been deleted from the specified directory, create
    if ~exist([directory '/Images'], 'dir')
        string_mkdir = ['mkdir ' directory];
        system([string_mkdir '/Images']);
    end

%---------------------------------------------
%
% Read SAC Data into Matlab
%
%---------------------------------------------
     filenamebp = sprintf('%s.bp', filename);
    
     [time,data,hdr] = fget_sac(filename); 
    
     [timebp,databp,hdrbp] = fget_sac(filenamebp);
     
     filenamehp1=sprintf('%s.hp1', filename);
     if exist(filenamehp1, 'file');
         [timehp1, datahp1, hdrhp1]=fget_sac(filenamehp1);
         hp1exists = 1;
     end 

%--------------------------------------------
%
% Generate Audio File
%
%--------------------------------------------
    
    scale = 1.2;
    audiofile=[directory '/Audio/audioFile.wav'] ;
    
    sac2wav(filename, audiofile, speed_factor, scale, starttime, endtime);
    
    [wave, fs] =wavread(audiofile);
    [M,N]=size(wave);
    soundLenght = M/fs;

%--------------------------------------------    
%
%Boundary & Interval Conditions
%
%--------------------------------------------

    FramesTotal = soundLenght*FramesPerSecond;
    
    [r,c]=find(time<starttime);
    [RowLength1,ColumnLenght1]=size(r);
    [r,c]=find(time<endtime);
    [RowLength2, ColumnLenght2] = size(r);
    Timeinterval=RowLength2-RowLength1;
    interval = Timeinterval/(FramesTotal-1);
    interval=round(interval);
    
%-----------------------------------------
%
% Generate Spectrogram
%
%-----------------------------------------

    nfft = 256;
    window = nfft;
    noverlap = nfft*0.75;
    sample_rate = round(1/hdr.times.delta);
    
    if hp1exists == 1,

        [B,F,T] = specgram(datahp1(RowLength1:RowLength2),nfft,sample_rate,window,noverlap);
       
    else
        [B,F,T] = specgram(data(RowLength1:RowLength2),nfft,sample_rate,window,noverlap);
    end;
    
    Bmax = max(max(B));
     

%---------------------------------------
%
% Create Labels
%
%---------------------------------------

    Title = input('Plot Title: ', 's');
    if isempty(Title),
      Title = filename;
    end


    %Plot 1 - Seismograph
    Plot1_Label2 = 'Surface Wave';


    Hz= input('Range of Hz used in band-pass filter: ', 's');
    if isempty(Hz),
        Plot2_Label2 ='Band-Pass ';
    else
        one = strfind(Hz, 'HZ');
        two = strfind(Hz, 'Hz');
        three = strfind(Hz, 'hz');
        if ~isempty(one); 
            Hertz=regexprep(Hz, ' HZ', 'Hz');
        elseif ~isempty(two);
            Hertz=Hz;
        elseif ~isempty(three);
            Hertz=regexprep(Hz, ' hz', 'Hz');
        else
            Hertz = [Hz ' Hz'];
        end
            Plot2_Label2 = {'Band-Pass ', Hertz};
    end
    %Plot 3 - Spectrogram
    Plot3_Label2 = 'Spectrogram';

    Plot3_yaxis_upper_limit = max(F);
    max_data = max(data(time>=starttime & time<=endtime)); 
 
     max_databp = max(databp(time>=starttime & time<=endtime));  
  
  
  
    %Plot Positions
    Plot1_Length = 0.75;
    Plot1_Height = 0.25;
    Plot1_Xposition = 0.10;
    Plot1_Yposition = 0.65;
    
    Plot2_Length = 0.75;
    Plot2_Height = 0.25;
    Plot2_Xposition = 0.10;
    Plot2_Yposition = 0.40;
    
    Plot3_Length = 0.75;
    Plot3_Height = 0.25;
    Plot3_Xposition = 0.10;
    Plot3_Yposition = 0.15;
    
    
           
        Spectrogram_yaxis = 'Frequency (Hz)';
        Xaxis = ['Time (s), ' num2str(speed_factor) ' times faster than true speed'];
%--------------------------------------
%
%Create Images
%
%--------------------------------------
  
clf;
   for frame=RowLength1:interval:RowLength2+interval;
        icount=icount+1;
        figure(1);
        set(gcf,'Position',[400,500,800,500], 'PaperPosition', [0.25 2.5 7.0 5.25]);
        
      
      %Seismography (Plot 1)
      
      subplot 311
       
        plot(time(RowLength1:RowLength2),data(RowLength1:RowLength2), 'Color', [.8 .8 .8]);
        
        hold on
        plot(time(RowLength1:frame),data(RowLength1:frame),'Color', [.2 .2 .2]);
        h = linex(time(frame));
        set(h,'Color','magenta','LineWidth',3); 
        hold off
        
        xlim([starttime, endtime]);
        ylim([-1.1 1.1].*max_data);
        set(gca,'Position',[Plot1_Xposition Plot1_Yposition Plot1_Length Plot1_Height],'XTickLabel',[])
        
        title(Title, 'FontSize', 14)
       
        y = xlabel('Amplitude (cm/s)');
        set(y, 'Position', [(starttime-(endtime-starttime)*0.1), (max_data -2*(max_data)), 1], 'FontSize', 12, 'Rotation', 90)
        
        xposition =(endtime-(endtime/5.5));
        yposition1 = (max_data-(max_data/4));
        text(xposition, yposition1, Plot1_Label2)
      
      %Band Pass Seismograph (Plot 2)
      
      subplot 312
        
        plot(timebp(RowLength1:RowLength2),databp(RowLength1:RowLength2), 'Color', [.8 .8 .8]);
        
        hold on
        plot(timebp(RowLength1:frame),databp(RowLength1:frame),'Color', [.2 .2 .2]);
        h = linex(time(frame));
        set(h,'Color','magenta','LineWidth',3); 
        hold off
        
        xlim([starttime,endtime]);
        ylim([-1.1 1.1].*max_databp);
        set(gca,'Position',[Plot2_Xposition Plot2_Yposition Plot2_Length Plot2_Height],'XTickLabel',[])
        
        xposition =(endtime-(endtime/5.5));
        yposition1 = (max_databp-(max_databp/4));
        text(xposition, yposition1, Plot2_Label2)
        
      %Spectrogram (Plot 3)
      subplot 313
        if hp1exists ==1,
            imagesc(timehp1(RowLength1:RowLength2),F,log10(abs(B./Bmax)).*(10))
        else
            imagesc(time(RowLength1:RowLength2),F,log10(abs(B./Bmax)).*(10));
        end;
        
        caxis([ColorBar_Lower_Limit, ColorBar_Upper_Limit]);
        axis xy,
        colormap(jet);
        hc = colorbar('Location','EastOutside');
        
        
        hold on
          h=linex(time(frame));
          set(h,'Color','magenta','LineWidth',3); 
          hold off;
          
        xlim([starttime,endtime]);
        ylim([0, Plot3_yaxis_upper_limit])
        set(gca,'Position',[Plot3_Xposition Plot3_Yposition Plot3_Length Plot3_Height]);
        
        a=ylabel(Spectrogram_yaxis);
        set(a, 'Position', [(starttime-(endtime-starttime)*0.05) (Plot3_yaxis_upper_limit*.5) 1], 'FontSize', 12);
        
        h = xlabel(Xaxis);
        set(h, 'Position', [(starttime+((endtime-starttime)/2)), (0-(Plot3_yaxis_upper_limit/4)) 1], 'FontSize', 12)
        
        xposition =(endtime-(endtime/5.5));
        yposition1 = (Plot3_yaxis_upper_limit-(Plot3_yaxis_upper_limit/4));
        text(xposition, yposition1, Plot3_Label2)

      %save image
      eval(['print -djpeg100 ' fileRenderName num2str(icount) '.jpeg']);
clf
   end



   disp('Render Finished')
   

 
    
    
    
    