Re: Retrieving Specific Data From a .txt file - PVWAVE [message #80014] |
Wed, 25 April 2012 13:03 |
Helder Marchetto
Messages: 520 Registered: November 2011
|
Senior Member |
|
|
Hi,
I don't know about pv-wave, but below is the IDL code I would use and I'm not an expert... I just try to make things work. Please notice:
- that the is no control that the inserted number is in fact a number. In case it is not, it will either complain or go through the procedure without getting any hit.
- if you're trying to get the last shot number and the file ends on the last line (or on the line just after), the last input line will not be found. Add to your file a line with something like "end" to be on the safe side
- if the number of lines for each shot is high, then the code line MyInputData = [MyInputData,Str] will become very inefficient. If you know how many lines you will expect it is best to make the array beforehand, otherwise you might have to loop through and count the lines...
Hope it helps.
Cheers, Helder
PRO ReadShots
ShotNr = 0
READ, PROMPT='Enter Shot Number: ', ShotNr
ShotNr = LONG(ShotNr)
OPENR, LunShots, 'F:\YourDirectory\Shots.txt', /GET_LUN
Str = ''
MyInputData = ''
WHILE ~EOF(LunShots) DO BEGIN
READF, LunShots, Str
Pos = STRPOS(STRUPCASE(Str), 'SHOT')
IF Pos GE 0 THEN BEGIN
SplitStr = STRSPLIT(Str, /EXTRACT)
IF LONG(SplitStr[1]) EQ ShotNr THEN BEGIN
READF, LunShots, Str
Pos = STRPOS(STRUPCASE(Str), 'SHOT')
WHILE Pos EQ -1 && ~EOF(LunShots) DO BEGIN
MyInputData = [MyInputData,Str]
READF, LunShots, Str
Pos = STRPOS(STRUPCASE(Str), 'SHOT')
ENDWHILE
GOTO, ExitFileSearchLoop
ENDIF
ENDIF
ENDWHILE
ExitFileSearchLoop:CLOSE, LunShots
FREE_LUN, LunShots
nFound = N_ELEMENTS(MyInputData)
IF (nFound GT 1) THEN BEGIN
Header = STRARR(nFound-1)
FirstNum = LONARR(nFound-1)
SecondNum = LONARR(nFound-1)
ThirdNum = LONARR(nFound-1)
FOR I=1,nFound-1 DO BEGIN
Res = STRSPLIT(MyInputData[I],/EXTRACT)
Header[I-1] = Res[0]
FirstNum[I-1] = Res[1]
SecondNum[I-1] = Res[2]
ThirdNum[I-1] = Res[2]
ENDFOR
PRINT, Header
PRINT, FirstNum
PRINT, SecondNum
PRINT, ThirdNum
ENDIF
END
|
|
|