function [ v S iso gama nair ] = Hitran_read_0_2( mol, vmin, vmax, isot )
% Read HITRAN data file for one molecule
%
% v0.1 - using this script have read the entire CO2 file and saved into 'co2.mat'
% v0.2 - read one molecule at a time into arrays
%        mol is molecule number, vmin - vmax is range of wavenumbers, isot
%        is the highest isotopologue number, if =1 then only main
%        isotopologue is chosen, if =0, all are chosen
%        Note: tried to read complete hitran database and save, but not
%        enough memory space, so needs to be one at a time and cut down to
%        subset

%        Note - Important - the Hitran file needs to be checked to ensure
%        the wavenumber doesn't go over 9999 - as the code below assumes
%        a white space exists (between molecule-isotope & wavenumber)

% filename is e.g. 01_hit08.par
fname=num2str(mol);
lmon=length(fname);
if lmon==1   % then we need to pad a leading zero
    fname=['0' fname];
end
fname=[fname '_hit08.par'];

fid=fopen(fname);
if fid==-1
    disp(['File: ' fname ' cannot be opened']);
    return
end

% read a subset of the data from each record 
C=textscan(fid,'%s %f %f %s %s %*[^\n]');

% 1st field, molecule and isotope, can be 3 characters for a 2 digit
% molecule number, or 2 characters for a single digit molecule number
% so lmon is used to work out whether isotope is 2nd or 3rd character

is1=char(C{1}); % molecule and isotope
iso=str2num(is1(:,2+(lmon-1))); %iso = isotope number in series

v=C{2}; % extract the wavenumber
S=C{3}; % line strength
lt=char(C{4}); % string array
gama=str2num(lt(:,10:14)); % air-broadened half-width

lt1=char(C{5}); % string array with length the same for each line, but it pads 
               % the spaces at the end, not back at the start so needs
               % reformatting
ul=size(lt1);               
for i=1:ul(1) % take each value (row) in turn
    us=deblank(lt1(i,:)); % remove trailing blanks if any
    if length(us)<21  % if any blanks removed add them back to the front
        lt1(i,:)=[blanks(21-length(us)) us];
    end
end
nair=str2num(lt1(:,10:13));  % temperature dependence exponent of gama

fclose(fid);

% now eliminate the out of range wavenumber values
iv=find(v>vmin & v<vmax); % this is the index of values we want
iso=iso(iv); v=v(iv); S=S(iv); gama=gama(iv); nair=nair(iv);

% now sum the number of lines per isotopologue - used only if needed
%sumiso=zeros(1,max(iso)); % allocate space and clear array
%for i=1:max(iso)
%    sumiso(i)=length(find(iso==i));
%end

% now eliminate the isotopologues above "isot", unless isot=0
if isot>0
    iv=find(iso<=isot); % these are the isotopologues we want
    % so shrink down the arrays to the subset
    iso=iso(iv)'; v=v(iv)'; S=S(iv)'; gama=gama(iv)'; nair=nair(iv)';
end

end

