-
Notifications
You must be signed in to change notification settings - Fork 1
/
kreadtext.pro
103 lines (81 loc) · 2.32 KB
/
kreadtext.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
FUNCTION KREADTEXT, filename, header, silent=silent
;object=(strsplit(data_file,'.txt',/extract,/regex))[0]
;IF ~keyword_set(silent) THEN print, object
n_lines=FILE_LINES(filename)
openr, unit, filename,ERROR=error, /get_lun
if error NE 0 then begin
message,/con,' ERROR - Unable to locate file ' + filename
return, -1
endif
;
; READ IN HEADER
;MODIFED FROM READFITS
;
doheader = arg_present(header)
hbuf=180
block = string(replicate(32b,80,36))
w = [-1]
header = strarr(hbuf)
headerblock = 0L
i = 0L
while w[0] EQ -1 do begin
if EOF(unit) then begin
message,/ CON, $
'EOF encountered attempting to read extension '; + strtrim(ext,2)
free_lun,unit
return,-1
endif
readf, unit, block
headerblock = headerblock + 1
w = where(strlen(block) NE 80, Nbad)
if (Nbad GT 0) then begin
message,'Warning-Invalid characters in header',/INF,NoPrint=Silent
message, 'Attempting to fix',/INF,NoPrint=Silent
for j=0,nbad-1 do begin
bad_header=block[w[j]]
new_header=string(replicate(32b, 80))
strput,new_header,bad_header
block[w[j]]=new_header
ENDFOR
endif
w = where(strmid(block, 0, 8) eq 'END ', Nend)
if (headerblock EQ 1) or doheader then begin
if Nend GT 0 then begin
if headerblock EQ 1 then header = block[0:w[0]] $
else header = [header[0:i-1],block[0:w[0]]]
endif else begin
header[i] = block
i = i+36
if i mod hbuf EQ 0 then $
header = [header,strarr(hbuf)]
endelse
endif
endwhile
;
;END FROM READFITS
;
;Read in blanks + first line of data
;point_lun, unit, 0
;skip_lun, unit, n_headerlines, /lines
;comment=''
;blank=' '
;n_blanklines=0
;WHILE blank eq ' ' DO BEGIN
; READF, unit, comment
; blank = strmid(comment,0,1)
; n_blanklines=++n_blanklines
;ENDWHILE
;n_blanklines=n_blanklines-1
;ENDIF; header exists
close, unit
free_lun,unit
n_headerlines=n_elements(header)
data_length = sxpar(header,'naxis1')
skiplines=n_lines-data_length
;read in two columns of data
READCOL, filename, wavelength, flux, FORMAT='F,F',skipline=skiplines ;n_headerlines+n_blanklines
spectrum=DBLARR(2, data_length)
spectrum[0,*] = wavelength
spectrum[1,*] = flux
RETURN, spectrum
END