function [p_up,p_down] = strnfit(filename, n)
% Strnfit reads a STRPLT.rxx file and calculates and plots the coefficents
% for the Imag vs GL.  The fit order is n.  
%   

[Imag, sImag, SL, sSL, TF, sTF, THsp, sTHsp] = textread(filename, '%f %f %f %f %f %f %f %f','headerlines',1);

% Determine run number

      r_in_run = strfind(filename,'.r');
      first_run_num = filename(r_in_run+2);
      second_run_num = filename(r_in_run+3);

       if (strcmp(first_run_num, 'u') == 1)
          run_number = second_run_num;
      else run_number = strcat(first_run_num,second_run_num);
       end  

% Find index of maximum current.  
[C,I] = max(Imag);

% Seperate data into up and down 
Imag_up = Imag(1:I);
SL_up = SL(1:I);
sigSL_up = sSL(1:I);

Imag_down = Imag(I:end);
SL_down = SL(I:end);
sigSL_down = sSL(I:end);

%  Get up and down polynomials and plot  

Title_up = ['GL up Run ',run_number];
format short g

[p_up,dp_up] = plot_polyfit_res(Imag_up,SL_up,1,n,'Imag',Title_up, '  (A)', '  (Tm)')

Title_down = ['GL down Run ',run_number];

[p_down,dp_down] = plot_polyfit_res(Imag_down,SL_down,1,n,'Imag',Title_down, '  (A)', '  (Tm)');

