Home > atmlab > arts > various > arts_read_linefile.m

arts_read_linefile

PURPOSE ^

------------------------------------------------------------------------

SYNOPSIS ^

function L = arts_read_linefile( filename, flims, sorted, ilim )

DESCRIPTION ^

------------------------------------------------------------------------
 NAME:     arts_read_linefile

           Reads a line file in the ARTS format.

           The output is in the format expected by ARTS_WRITE_LINEFILE.
           See further ARTS_WRITE_LINEFILE for definition of the structure
           field names.

           The optional arguments can be used to restrict the reading.

 FORMAT:   L = arts_read_linefile( filename [, flims, sorted, ilim ] )

 OUT:      L          Structure array with line data.
 IN:       filename   Name on file to create.
 OPTIONAL: flims      Frequency limits, as [f_low,f_high], for transitions
                      to consider. Default is [0 Inf].
           sorted     Flag indicating if transitions are sorted in
                      frequency. If set to true, the reading is terminated
                      as soon as a transition above flims(2) is found.
                      Default is false.
           ilim       Threshold for transition intensity. Transitions with
                      an intensity below *ilim* are ignored. Default is 0.
------------------------------------------------------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

arts_read_linefile.m

SOURCE CODE ^

0001 %------------------------------------------------------------------------
0002 % NAME:     arts_read_linefile
0003 %
0004 %           Reads a line file in the ARTS format.
0005 %
0006 %           The output is in the format expected by ARTS_WRITE_LINEFILE.
0007 %           See further ARTS_WRITE_LINEFILE for definition of the structure
0008 %           field names.
0009 %
0010 %           The optional arguments can be used to restrict the reading.
0011 %
0012 % FORMAT:   L = arts_read_linefile( filename [, flims, sorted, ilim ] )
0013 %
0014 % OUT:      L          Structure array with line data.
0015 % IN:       filename   Name on file to create.
0016 % OPTIONAL: flims      Frequency limits, as [f_low,f_high], for transitions
0017 %                      to consider. Default is [0 Inf].
0018 %           sorted     Flag indicating if transitions are sorted in
0019 %                      frequency. If set to true, the reading is terminated
0020 %                      as soon as a transition above flims(2) is found.
0021 %                      Default is false.
0022 %           ilim       Threshold for transition intensity. Transitions with
0023 %                      an intensity below *ilim* are ignored. Default is 0.
0024 %------------------------------------------------------------------------
0025 
0026 % HISTORY:  040908  Copied to Atmlab from AMI by PE.
0027 %           020101  Created by Patrick Eriksson (PE).
0028 
0029 
0030 function L = arts_read_linefile( filename, flims, sorted, ilim )
0031 
0032 
0033 %=== Default values
0034 %
0035 flims_DEFAULT  = [0 Inf];
0036 sorted_DEFAULT = 0;
0037 ilim_DEFAULT   = 0;
0038 %
0039 set_defaults;
0040 
0041 
0042 
0043 %=== Present version number
0044 %
0045 vers = 3;
0046 
0047 
0048 %=== Open the file
0049 fid = fopen( filename, 'r' );
0050 if fid < 0
0051   error(sprintf('The file %s could not be opened.',filename));
0052 end
0053 
0054 
0055 %=== Check that this is a ARTSCAT file and the version number is correct
0056 %
0057 s     = fgets( fid );
0058 %
0059 if ~strncmp( s, 'ARTSCAT-', 8 )
0060   error('An ARTS line file must start with "ARTSCAT-"');
0061 end
0062 %
0063 stest = sprintf('ARTSCAT-%d', vers );
0064 %
0065 if ~strncmp( s, stest, length(stest) )
0066   serr = sprintf('The line file has not the correct version number\n');
0067   serr = sprintf('%s(it should be %d)',serr,vers);
0068   error( serr );
0069 end
0070 
0071 
0072 
0073 %=== Read lines
0074 %
0075 S     = next_line( fid );
0076 n     = 0;
0077 %
0078 L     = [];
0079 %
0080 while ~isempty(S)
0081 
0082   f = sscanf( S{2}, '%f' );
0083 
0084   if sorted  &  f >= flims(2)
0085     break
0086   end
0087   
0088   i0 = sscanf( S{4}, '%f' );
0089   
0090   if f >= flims(1)  &  f <= flims(2)  &  i0 >= ilim
0091     %
0092     n = n + 1;
0093     %
0094     for i = 1:length(S)
0095   
0096       switch i
0097   
0098        case 1
0099          L{n}.name = S{i};
0100         
0101        case 2
0102          L{n}.f = f;
0103      
0104        case 3
0105          L{n}.psf = sscanf( S{i}, '%f' );
0106      
0107        case 4
0108          L{n}.i0 = i0;
0109      
0110        case 5
0111          L{n}.t_i0 = sscanf( S{i}, '%f' );
0112      
0113        case 6
0114          L{n}.elow = sscanf( S{i}, '%f' );
0115      
0116        case 7
0117          L{n}.agam = sscanf( S{i}, '%f' );
0118      
0119        case 8
0120          L{n}.sgam = sscanf( S{i}, '%f' );
0121      
0122        case 9
0123          L{n}.nair = sscanf( S{i}, '%f' );
0124      
0125        case 10
0126          L{n}.nself = sscanf( S{i}, '%f' );
0127      
0128        case 11
0129          L{n}.t_gam = sscanf( S{i}, '%f' );
0130      
0131        case 12
0132          L{n}.n_aux = sscanf( S{i}, '%f' );
0133          n_aux      = L{n}.n_aux;
0134          %
0135          % Aux variables are handled below otherwise
0136      
0137        case 13 + n_aux
0138          L{n}.df = sscanf( S{i}, '%f' );
0139      
0140        case 14 + n_aux
0141          L{n}.di0 = sscanf( S{i}, '%f' );
0142      
0143        case 15 + n_aux
0144          L{n}.dagam = sscanf( S{i}, '%f' );
0145      
0146        case 16 + n_aux
0147          L{n}.dsgam = sscanf( S{i}, '%f' );
0148      
0149        case 17 + n_aux
0150          L{n}.dnair = sscanf( S{i}, '%f' );
0151      
0152        case 18 + n_aux
0153          L{n}.dnself = sscanf( S{i}, '%f' );
0154      
0155        case 19 + n_aux
0156          L{n}.dpsf = sscanf( S{i}, '%f' );
0157      
0158        case 20 + n_aux
0159          L{n}.qcode = sscanf( S{i}, '%s' );
0160      
0161        case 21 + n_aux
0162          L{n}.qlower = sscanf( S{i}, '%s' );
0163      
0164        case 22 + n_aux
0165          L{n}.qlower = sscanf( S{i}, '%s' );
0166      
0167        case 23 + n_aux
0168          L{n}.if = sscanf( S{i}, '%s' );
0169      
0170        case 24 + n_aux
0171          L{n}.ii0 = sscanf( S{i}, '%s' );
0172      
0173        case 25 + n_aux
0174          L{n}.ilw = sscanf( S{i}, '%s' );
0175      
0176        case 26 + n_aux
0177          L{n}.ipsf = sscanf( S{i}, '%s' );
0178      
0179        case 27 + n_aux
0180          L{n}.iaux = sscanf( S{i}, '%s' );
0181       
0182        otherwise
0183          %
0184          if i <= 12+n_aux
0185            eval(['L{i}.aux',int2str(i-11)]) = sscanf( S{i}, '%f' );
0186          else
0187            error(sprintf('To many fields found for line %d.',n));
0188          end
0189       end
0190     end 
0191   end
0192   %
0193   S = next_line( fid );
0194   %
0195 end
0196 
0197 
0198 fclose( fid );
0199 
0200 return
0201 
0202 
0203 
0204 %------------------------------------------------------------------------------
0205 
0206 function S = next_line( fid )
0207 
0208   S = [];
0209 
0210   %= Just return if EOF
0211   if feof(fid)
0212     return
0213   end
0214 
0215   %= Read next line
0216   sin = fgets( fid );
0217 
0218   %= Return if empty line
0219   if length(sin) < 2
0220     return
0221   end
0222 
0223   %= Read until next line that starts with @ and replace line feeds
0224   %= with a blank
0225   %
0226   if sin(1) ~= '@'
0227     serr = sprintf('Could not read linefile at:\n%s',sin);
0228     error( serr );
0229   end
0230   %
0231   s = [ sin( 3 : (length(sin)-1) ), ' ' ];
0232   %
0233   c1 = fscanf(fid,'%c',1);
0234   %
0235   while ~isempty(c1) & c1~='@'
0236     %
0237     fseek(fid,-1,'cof');   
0238     %
0239     sin = fgets( fid );
0240     s   = [ s, sin( 1 : (length(sin)-1) ), ' ' ];
0241     %
0242     c1 = fscanf(fid,'%c',1);
0243   end
0244 
0245   %= Back one step in the file
0246   fseek(fid,-1,'cof');   
0247 
0248   %= Field index
0249   i = 0;
0250 
0251   while 1
0252 
0253     i = i + 1;
0254 
0255     %= Text field inside " "
0256     %
0257     if s(1) == '"'
0258       %
0259       iprims = find( s == '"' );
0260       %
0261       if length(iprims) < 2
0262         error('Unmatched " found.');
0263       end
0264       %
0265       S{i} = s(2:(iprims(2)-1));
0266       %
0267       l = iprims(2);
0268 
0269     %= Other field
0270     %
0271     else
0272 
0273       %= Find field seperations
0274       iblank = find( s == ' ' );
0275 
0276       %= Pick out text until first blank
0277       l    = iblank(1) - 1;    
0278       S{i} = s(1:l);
0279 
0280     end
0281 
0282     %= Remove used text
0283     s = s( (l+1):length(s) );
0284 
0285     %= Remove blanks at the start of s (if no-blanks, we are ready)
0286     ichars = find( s ~= ' ' );
0287     if isempty( ichars )
0288       return;
0289     end
0290     s = s( ichars(1):length(s) );
0291  
0292   end
0293 return
0294

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005