;+ ; NAME: ; nthline ; ; PURPOSE: (one line) ; Return the nth line in a file ; ; DESCRIPTION: ; Return the nth line in a file, using a lookup file called ; filename.lookup . ; ; CATEGORY: ; File IO ; ; CALLING SEQUENCE: ; line = nthline(filename, linenum) ; ; INPUTS: ; filename - file from which to get lines ; linenum - the line number to return (long or array of long) ; ; OPTIONAL INPUT PARAMETERS: ; None ; ; KEYWORD INPUT PARAMETERS: ; ; KEYWORD OUTPUT PARAMETERS: ; ; OUTPUTS: ; If input is a scalar, then output is a single string. ; If input is a vector, then output is an array of strings. ; If a line is not found (linenum out of range), returned value is ''. ; ; COMMON BLOCKS: ; None ; ; SIDE EFFECTS: ; None ; ; PROCEDURES: ; spawned call to sed ; ; RESTRICTIONS: ; Output file is different based on big vs. little-endian system. ; For consistency, it is recommended that all files be written ; big-endian. x86 Linux is this way, but OS X is not. ; ; EXAMPLE: ; print, nthline('foo',[430L, 1000L]) ; ; MODIFICATION HISTORY: ; Written 23 Apr 2006 Leslie Young SwRI ; Modified 26 Apr 2006 Henry Throop SwRI. Creates lookup file if it ; does not exist. Improved other error handling. ; ; 2007 May 4 LAY swap if little endian ;- function nthline, filename, linenum, VERBOSE=verbose nline = n_elements(linenum) lines = strarr(nline) if not(file_exists(filename)) then begin if keyword_set(VERBOSE) then $ print, 'Error: file ' + filename + ' does not exist' return, '' end if not(file_exists(filename + '.lookup')) then begin if keyword_set(VERBOSE) then $ print, 'Creating lookup file for ' + filename nthline_lookup, filename end openr,lunf, filename, /get_lun openr,lunl, filename+'.lookup', /get_lun, /swap_if_big a = assoc(lunl, lonarr(2)) maxline = (file_info(filename+'.lookup')).size/8L ; 2 4-byte longs per line for i = 0L, nline-1 do begin n = linenum[i] if n ge 0L and n lt maxline then begin b = a[n] point_lun, lunf, b[0] barr = bytarr(b[1]) readu, lunf, barr lines[i] = string(barr) end end close, lunf & free_lun, lunf close, lunl & free_lun, lunl return, lines end