%----------Changed by Yas. Ogawa at 11 July, 1999. ------
function [Time,Ran,Alt,Ne,Te,Ti,Vi,Neerr,Teerr,Tierr,Vierr,Status,name_expr] = load_param(START_TIME,END_TIME,data_path,limit_az_el)
%--------------------------------------------------------
% @(#)load_param.m	1.2 98/08/04
% Function to read the plasma parameters from
% GUISDAP result files
% Please specify the data directory in the configuration file config.m
% The data directory must contain GUISDAP MAT-files and a file
% named filelist.dat
%
% The Geographical Latitudes are calculated using Asko Huuskonen's loc2gg.m
% routine from Guisdap v1.60
%
% Philippe Gamet - EISCAT Group 96/07/08
%
% 8/98 Modified to return NCAR convention velocity signs

if ~strcmp(data_path(length(data_path)),'/')
	data_path(length(data_path)+1) = '/';
end;

load([data_path, 'filelist.dat']);
%----------Appended by Yas. Ogawa at 15 March, 2000. ------
plus=0;
startsec=toepoch(START_TIME);  % From 1970
if (START_TIME(1,1) > 80 & START_TIME(1,1) < 100)   % 98
year = 1900+START_TIME(1:1);
end
if (START_TIME(1,1) < 80)         % 01
year = 2000 + START_TIME(1:1);
end
if (START_TIME(1,1) > 100)  %1999
year = START_TIME(1:1);
end
diff_yr = year-1970; % (19)99-(19)70

for i = 1:diff_yr
        if rem(1969+i,4)==0
        plus = plus + 31622400;
        else
        plus = plus + 31536000;
        end
end     
startsec=startsec-plus;  
endsec=toepoch(END_TIME); 
endsec=endsec-plus;   
filelist=filelist;

[dif_start,startnum]= min(abs(filelist - startsec));
[dif_end,endnum]= min(abs(filelist - endsec)); 
filelist=filelist(startnum:endnum); % data loaded were limitted.

%if filelist(startnum) <= startsec 
%filelist=filelist(startnum:endnum);  
%elseif exist(filelist(startnum-1))
%filelist=filelist(startnum-1:endnum);  
%end

%--------------------------------------------------------

n	= size(filelist);
n_tot	= n(1)*2;

disp(' ');
disp('Source directory:');
disp(data_path);
	file	= int2str(filelist(1));
	s	= size(file);

        for ipos = s(2):7           % pre-pad filename to 8 characters
            file = ['0' file];   
        end

	eval(['load ',data_path,file]);
	s		= size(r_param);
	n_alt		= s(1);
	s1		= size(r_pprange);
	n_alt1		= s1(1);

Ne		= ones(n_alt,n_tot);
Ti		= ones(n_alt,n_tot);
Te		= ones(n_alt,n_tot);
Vi		= zeros(n_alt,n_tot);
Neerr		= ones(n_alt,n_tot);
Tierr		= ones(n_alt,n_tot);
Teerr		= ones(n_alt,n_tot);
Vierr		= zeros(n_alt,n_tot);
Time		= zeros(n_tot,1);
Alt		= zeros(n_alt,n_tot);
Ran		= zeros(n_alt,n_tot);
EL		= zeros(n_tot,1);
AZ		= zeros(n_tot,1);

%----------Appended by Yas. Ogawa at 08 Jan., 2001. ------
counts=1;
if isempty(limit_az_el) 
    limit_az_el =[-360 360 0 180 0];
end
%---------------------------------------------------------

  for i=1:n_tot/2;
	file	= int2str(filelist(i));
	s	= size(file);

        for ipos = s(2):7           % pre-pad filename to 8 characters
            file = ['0' file];   
        end

	disp(['loading data from file ',file,'.mat (',num2str(i),'/',num2str(n(1)),')']);
	eval(['load ',data_path,file]);

	j = (i-1)*2 + 1;

%----------Appended by Yas. Ogawa at 08 Jan., 2001. ------
	if ( (    r_az > limit_az_el(1) &     r_az < limit_az_el(2)) | ...
	     (360+r_az > limit_az_el(1) & 360+r_az < limit_az_el(2)) ) & ...
	   ( (    r_el > limit_az_el(3) &     r_el < limit_az_el(4)) | ...
	     (180-r_el > limit_az_el(3) & 180-r_el < limit_az_el(4)) )
	jj = (counts-1)*2 + 1;
	counts=counts+1;
%----------------------------------------------------------

	Ne(:,jj)		=	real(log10(r_param(:,1)));
	Ne(:,jj+1)		=	real(log10(r_param(:,1)));
	Ti(:,jj)	 	=	r_param(:,2);
	Ti(:,jj+1) 		=	r_param(:,2);
	Te(:,jj)	 	=	r_param(:,2).*r_param(:,3);
	Te(:,jj+1)		=	r_param(:,2).*r_param(:,3);
	Neerr(:,jj)		=	real(log10(r_error(:,1)));
	Neerr(:,jj+1)		=	real(log10(r_error(:,1)));
	Tierr(:,jj)		=	r_error(:,2);
	Tierr(:,jj+1) 		=	r_error(:,2);
	Teerr(:,jj)	 	=	r_param(:,2).*r_error(:,3)+r_param(:,3).*r_error(:,2);
	Teerr(:,jj+1)		=	r_param(:,2).*r_error(:,3)+r_param(:,3).*r_error(:,2);
	Vierr(:,jj)	 	=	r_error(:,5);
	Vierr(:,jj+1)		=	r_error(:,5);
	
% if the data file contains NCAR velocities, use them, otherwise
% invert the sign of the velocity since the GUISDAP convention is
% opposite to that specified by NCAR

	if exist('r_NCARvel_PosAway')
        	Vi(:,jj)	 	=	r_NCARvel_PosAway;
        	Vi(:,jj+1)		=	r_NCARvel_PosAway;
	else  
        	Vi(:,jj)	 	=	-r_param(:,5);
        	Vi(:,jj+1)	 	=	-r_param(:,5);
	      end;
	      
	Alt(:,jj) 		= r_h(:,1);
	Alt(:,jj+1) 		= r_h(:,1);
	Ran(:,jj) 		= r_range(:,1);
	Ran(:,jj+1) 		= r_range(:,1);
	Time(jj,1)		= toepoch(r_time(1,:));
	if limit_az_el(5) == 1 & jj ~= 1 
	Time(jj,1)		= Time(jj-1,1);
	end; % limit_az_el(5)
	Time(jj+1,1)		= toepoch(r_time(2,:));
	Status(:,jj) 	        = r_status(:,1);
	Status(:,jj+1)   	= r_status(:,1);

        end; % if AZ
  end;% for i

disp(['Done. ',num2str(counts),'/',num2str(n(1))]);
