Loading spectroscopic data into igor from general text

Hello all,

I've to load a ascii file with x and y waves into igor. A typical file looks like:

Area1

SUR
1
1399.0000,7242.7033
1398.2000,8228.6017
1397.4000,7379.5712
1396.6000,7491.0938
1395.8000,7897.0668
1395.0000,7162.2372
1394.2000,7876.3272
1393.4000,7402.1067
1392.6000,7568.9746

Area1

N1s
1
408.0000,101.1882
407.9500,101.3558
407.9000,96.7365
407.8500,96.9283
407.8000,94.1598
407.7500,94.8345
407.7000,95.6612
407.6500,95.3786
407.6000,94.0064
407.5500,95.0241
407.5000,98.0092
407.4500,99.6761


There are several of such datablocks with different numbers of rows in my ascii file. Each datablock begins with "Area1" followed by a free line and some information from the spectra "SUR, N1s ....". The real data starts after the "1".
Now I want igor to load the data and give the waves special names. Let's say the ascii file is called "Spec1.txt". Then all the xwaves should have the prefix "Spec1_EB_" and all the y waves should be named like "Spec1_Int_". After the last underscore I want to have some data from the header in the wavename. In my example I want the waves to be named "Spec1_EB_SUR", "Spec1_Int_SUR" for the first block and "Spec1_EB_N1s", "Spec1_Int_N1s" for the second block.
My igor skills so far are not good enough to get this job done. I don't want you to give me the total code for this. I'm satisfied with some hints so I can learn how to do this.

Thanks in advance.
It is easier to write the code than to explain it:
Menu "Load Waves"
    "LoadMcIlhennyData...", LoadMcIlhennyData("", "")  
End

//  FindSectionNameAndDataStartLine(refNum, name, dataStartLine, numDataLines)
Function FindSectionNameAndDataStartLine(refNum, currentLine, sectionName, dataStartLine, numDataLines)
    Variable refNum
    Variable &currentLine           // Input and output
    String &sectionName         // Output - name of file section
    Variable &dataStartLine         // Output line where data starts
    Variable &numDataLines          // Output - number of data lines in this section
   
    String text
    do
        FReadLine refNum, text
        if (strlen(text) == 0)
            return -1                   // End of file
        endif
        if (CmpStr(text[0,4],"Area1") == 0)
            FReadLine refNum, text      // Skip the blank line
            if (strlen(text) == 0)
                return -2               // Unexpected end of file
            endif
            currentLine += 1
            FReadLine refNum, text      // Get the name line
            if (strlen(text) == 0)
                return -2               // Unexpected end of file
            endif
            currentLine += 1
            text = RemoveEnding(text, "\r")     // Remove CR
            sectionName = text
            dataStartLine = currentLine
           
            // Find number of lines in this section
            numDataLines = 0
            do
                FReadLine refNum, text
                text = RemoveEnding(text, "\r")     // Remove CR
                if (strlen(text) == 0)
                    break                           // Blank line or end of file
                endif
                currentLine += 1
                numDataLines += 1
            while(1)
            break
        endif
        currentLine += 1
    while(1)   
   
    return 0
End

Function/S LoadMcIlhennyData(pathName, fileName)    // Returns list of loaded waves
    String pathName     // Name of an Igor symbolic path or "".
    String fileName         // Name of file or full path to file.

    Variable refNum

    // First get a valid reference to a file.
    if ((strlen(pathName)==0) || (strlen(fileName)==0))
        // Display dialog looking for file.
        Open/D/R/P=$pathName refNum as fileName
        fileName = S_fileName           // S_fileName is set by Open/D
        if (strlen(fileName) == 0)      // User cancelled?
            return ""
        endif
    endif

    // Open file for searching for wave names and data
    Open /R /P=$pathName refNum as fileName
   
    String fileBaseName = ParseFilePath(3, fileName, ":", 0, 0) // File name without extension
    fileBaseName = CleanupName(fileBaseName, 0)

    Variable sectionsLoaded = 0, sectionsSkipped = 0
    Variable result = 0
    String waveNames = ""
   
    Variable currentLine = 0
    String sectionName
    Variable dataStartLine, numDataLines
   
    do
        result = FindSectionNameAndDataStartLine(refNum, currentLine, sectionName, dataStartLine, numDataLines)
        if (result == -1)
            break           // Normal end of file
        endif
        if (result != 0)
            Abort "Unexpected end of file"
        endif
       
        String xName, yName
        String columnInfo = ""
        xName = fileBaseName + "_EB_" + sectionName
        columnInfo += "N='" + xName + "';"          // e.g., "N='Spec1_EB_N1s';"
        yName = fileBaseName + "_Int_" + sectionName
        columnInfo += "N='" + yName + "';"          // e.g., "N='Spec1_EB_N1s';N='Spec1_Int_N1s';"
           
        LoadWave /G /P=$pathName /A /O/B=columnInfo /L={0,dataStartLine,numDataLines,0,0} /Q fileName
        if (V_flag == 2)    // V_flag is set by LoadWave to the number of waves loaded.
            Printf "Loaded '%s' and '%s' from line %d of \"%s\"\r", xName, yName, dataStartLine, fileName
            waveNames += S_waveNames
            sectionsLoaded += 1
        else
            Printf "Error in LoadWave while loading the file \"%s\"\r", fileName
            sectionsSkipped += 1
        endif
    while (1)                                       // Until IndexedFile runs out of files
   
    Close refNum
   
    Printf "%d sections loaded, %d sections skipped\r", sectionsLoaded, sectionsSkipped
   
    return waveNames    // Return list of loaded waves
End

Thank you very much! I think I just have to change the code a little bit, because I have to use Igor 4 which doesn't support all the commands you used.
Hello,

Please forgive my ignorance, but this has been bothering me for a while today. If each line in the text file has a "\r" at the end of the line, how will the following if statement return a true value?

hrodstein wrote:
        FReadLine refNum, text
        if (strlen(text) == 0)
            return -1                   // End of file
        endif


I have the same question for finding the end of a text file. Is there a simple way to determine the end of a file?

Thanks for your help!
The code in question is meant to terminate the Do/While loop when the end of file is reached. Maybe this helps with your other question as well?