X
68 Rate this article:
No rating

INTERNAL: Reading multiple data types from ASCII to IDL

Anonym
Topic:
This is an example of reading multiple data types from an ASCII text file into IDL. You can do it with a fomatted read, with or without using a data structure variable.Discussion:
The data file the customer was trying to read is:

960316a
window 0 71.44 90.89 278.33 504.67 503.56
window 1 61.67 92.78 296.67 529.78 527.00
window 2 64.67 98.11 301.22 530.56 529.33
window 3 68.33 111.33 299.44 529.00 528.44
window 4 55.00 78.00 301.67 535.67 534.78
window 5 59.78 85.44 314.56 540.11 538.33
window 6 62.22 89.11 312.56 534.56 533.11
window 7 67.11 90.89 289.00 520.44 520.67
window 8 58.22 84.56 312.89 541.33 538.89
window 9 62.44 89.00 314.22 538.78 537.89

Here are the example programs fro reading the data:Solution:
 pro READ1, data
;
; If all you want are the 5 floats on each line:
;

OpenR, lun, 'data_file', /Get_LUN

;
; skip the date header
;
header=" "
ReadF, lun, header

;
; initialize a counter
;
count=0

;
; make an array bigger than you'll need
;     
data=fltarr(5,1000)  

;
; read in the data in a WHILE loop
; since you may not know exactly how
; many lines it has
;
WHILE not EOF(lun) DO BEGIN
temp = fltarr(5)
ReadF, lun, temp, format='(13X, 5(F7.2))'
data(*,count) = temp
count = count+1
ENDWHILE

;
; free the LUN
;
Free_LUN, lun

;
; trim the data array to its true size
;
data = data(*,0:count-1)

END

***************************************************

pro READ2, data
;
; to read all the data into its own array in IDL
; using a data structure
;

OpenR, lun, 'data_file', /Get_LUN

;
; skip the date header
;
header=" "
ReadF, lun, header

;
; initialize a counter
;
count=0

;
; make the data structure
;
struct1 = {win_name: " ", win_id: 0, win_nums: fltarr(5)}

;
; make a structure-array bigger than you'll need
;     
data=REPLICATE(struct1,1000)  

;
; read in the data in a WHILE loop
; since you may not know exactly how
; many lines it has
;
WHILE not EOF(lun) DO BEGIN
ReadF, lun, struct1, format='(A6, I7, 5(F7.2))'
data(count)=struct1
count = count+1
ENDWHILE

;
; free the LUN
;
Free_LUN, lun

;
; trim the structure array to its true size
;
data = data(0:count-1)

END