load IR data from many text files

hello Wavemetrics team and Igor pro lovers,

I need to load a large amount of IR data from text files having 2 column, first volume is wavemumber and second is absorbante. I need to extract them, remane them using the file name, scale them and duplicata them twice. I wrote a proc but cannot figure out where it bugged.
could anyone give me an hand with my short macro ? I have attached the .ipf.
thank you,
Sylvie

Frog_FTIR.ipf (2.96 KB)
Does the function give an error message or is the outcome not what you expect?
Load_IR() references the waves Wavenumber and Absorbance in the present data folder. Do they exist before you run the function?

Maybe it's easier if you can post also the raw data.
it is a txt file, I attach a typical file. I need to rename each file by removing 0.txt and replace by _A for absorbance.
thank you for spending time on my macro.
sylvie

H17ol1.txt (34.71 KB)
and I get this message error "while executing SetScale, the following error occured: expected wave name".
I made some progress with the loadwave fonction, bu my scalling and duplicat waves still don't work. I put the new ipf file in attachment.

// Load the actual waves.
String name = ParseFilePath(3, path, ":", 0, 0) // Extract the file name without extension.
name = CleanupName(name, 0)
LoadWave/A=$name/D/J/W/K=0/V={" "," $",0,0}/L={0,0,0,1,1} path

// addiitonal treaments: scalling and duplicate
wave newIR=$name
setscale /I x, 5000, 700, $newIR
duplicate newIR newIR_N
duplicate newIR newIR_F

help anyone ?
Frog_FTIR_0.ipf (1.21 KB) Frog_FTIR_0.ipf (1.21 KB) Frog_FTIR_0.ipf (1.21 KB)
I think this should do it - one issue was that you specified the parameters filename and pathname not in the correct order. Note, that I changed the Setscale from hard-coded numbers to WaveMin, and -Max of the wavenumber wave. You needed the /I flag here instead of /P.
You need the same wave three times?

Function Load_all_txt_Files(pathName)
    String pathName // Name of an Igor symbolic folder created by Misc->NewPath
    String fileName
   
    Variable index=0
    if (strlen(pathName) == 0)
        String message = "Select a directory containing only Sylvie data files"
        NewPath/O/M=message SylvieDataPath      // This displays a dialog in which you can select a folder
        if (V_flag != 0)
            return V_flag                           // -1 means user canceled
        endif
        pathName = "SylvieDataPath"
    endif
    Variable result= 0
    do                                                  // Loop through each file in folder
        fileName = IndexedFile($pathName, index, ".txt")        // catch .txt files in folder
        if (strlen(fileName) == 0)                                  // No more files ?
            break                                       // Break out of loop                   
        endif
        result = Load_IR(fileName, pathname)
        if( result == -1 )
            break                           // user cancelled or no waves in this file (which we take to be an error)
        endif
        index += 1
    while (1)
    if (Exists("SylvieDataPath")) // Kill temp path if it exists
        KillPath temporaryPath
    endif
    return result // 0 Signifies success.
End



 Function Load_IR(fileName, pathName)
    String fileName// File name to load, not including the path, or "" to get dialog
    String pathName // Name of path or "" to get dialog
 
    // Strip the extension from the file name to get the generic name.
    String genericname = fileName[0, strsearch(fileName, ".txt", inf, 1) - 1]
   
    String Abstr="_A"
    String AbstrN="_N"
    String AbstrF="_F"
    String Wavenumberwave=genericname
    String Absorbancewave=genericname+Abstr
    String AbsorbancewaveN=genericname+AbstrN
    String AbsorbancewaveF=genericname+AbstrF
 
    //NewPath/O path, pathName
    LoadWave/A/G/D/O/P=$PathName/B="C=1, N=Wavenumber; C=1, N=Absorbance;" fileName
    Wave Wavenumber, Absorbance
   
    SetScale/I x, WaveMax(Wavenumber), WaveMin(Wavenumber), "", Absorbance
    Duplicate/O Absorbance $Absorbancewave, $AbsorbancewaveN, $AbsorbancewaveF
   
    KillWaves/Z Wavenumber, Absorbance
    return 0                                // Signifies success.
End



hm….Sylvie … IR data….is that you??

Cheers
Christian….formerly at BGI ;-)



yes, it's me, the world is too small.....
let me try the new macro. ..coming back soon
Hey Christian
nop, the macro does not load the files. Anyway, I have a new version, way shorter, and which load the file succesffully, and rename them, BUT the scaling and duplicata still bugged. do you know why ?
thank again for your time !

in attachment new macro.

// Load the actual waves.
String name = ParseFilePath(3, path, ":", 0, 0) // Extract the file name without extension.
name = CleanupName(name, 0)
LoadWave/A=$name/D/J/W/K=0/V={" "," $",0,0}/L={0,0,0,1,1} path

// additional treaments: scalling and duplicate
String AbstrN="N"
String AbstrF="F"
String newIR=name
String newIRN=name+AbstrN
String newIRF=name+AbstrF
setscale /I x, 5000, 700, "", $newIR
Duplicate/O newIR $newIRN, $newIRF
Frog_FTIR2.ipf (1.3 KB)
To see names of the waves that LoadWave creates, add
Print S_waveNames
after the LoadWave command.

Or use the Igor debugger. For details execute:
DisplayHelpTopic "The Debugger"

You will then see that the string variable name does not contain the name of any wave loaded. It is just the base name (e.g., "wave" is the base name of "wave0", "wave1").

After the LoadWave command add this:
String first = StringFromList(0, S_waveNames)  // Name of first wave created by LoadWave
String second = StringFromList(0, S_waveNames)  // Name of second wave created by LoadWave


Then use either first or second instead of your name variable.

Also, since newIR is t he name of a string variable containing a wave name you must use a $ before newIR in your Duplicate command. For details execute:
DisplayHelpTopic "Converting a String into a Reference Using $"
It works great ! thank you.

I post the .ipf file below for other users

I have another question then, when the .txt is load, it add a 0 at the end of the file name ( 'olivine1.txt' becomes 'olivine10' )
I try to use

Print RemoveEnding("second" , "0")

but I could not get the syntaxe right.
any advise?
sylvie
Frog_FTIR3.ipf (1.8 KB)
I recommend that you use the LoadWave /B flag to set the wave names, not the /A flag. Something like:
String name = <file name>
String firstName = name + "First"
String secondName = name + "Second"
String columnInfoStr
sprintf columnInfoStr, "N='%s';N='%s';", firstName, secondName
LoadWave/B=columnInfoStr ...
<pre><code class="language-igor">