;+ ; NAME: ; nthline ; ; PURPOSE: (one line) ; Return the nth line in a file ; ; DESCRIPTION: ; Return the nth line in a file ; ; 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: ; NSTARS -- Returns the number of successfully-retrieved lines. ; ; 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: ; ; EXAMPLE: ; print, nthline(102,[430L, 1000L]) ; ; MODIFICATION HISTORY: ; Written 2006 Apr 23 Leslie Young SwRI ; 2006 July 3 LAY swap if little endian ;- function nthline, filename, linenum nline = n_elements(linenum) lines = strarr(nline) 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