#pragma rtGlobals=3 // Use modern global access method and strict wave access. Function ImportXPS_bin() //Read .spe files from Phy Multipak XPS Variable vFileRef Variable vIndex = 0 Variable vLoc Variable vNumSpectra Variable vPointsInSpectrum Variable vReadVal String sFilePath String sLine String sSpectrumWaveName //Get name/path to file to open Open/D/R/F="Multipak Files (*.spe):.spe;"/M="Select SPE-CSV File" vFileRef sFilePath = S_fileName if( StrLen( S_fileName ) == 0 ) return -1 //user cancelled file selection endif Open/R vFileRef as sFilePath //Text Header Make/O/T/N=0 wHeader //Read the File Header one line at at time; read terminates on LF/CR combinations, per the help file //In this case the terminator is LF Do FReadLine vFileRef, sLine FStatus vFileRef if( V_filePos > V_logEOF ) print "Attempting to read past end of file" return -1 //attempting to read past end of file endif sLine = RemoveEnding( sLine, "\r" ) InsertPoints vIndex, 1, wHeader wHeader[vIndex] = sLine //get number of spectra in file from header containing if( stringmatch( sLine,"NoSpectralReg:*" ) == 1 ) vLoc = strsearch( sLine, ":", 0 ) vNumSpectra = str2num( sLine[vLoc + 1, inf ] ) print "Number of Spectra in file: ", vNumSpectra endif //End of header is given by "EOFH" string; quit when this is found if( stringmatch( sLine,"EOFH" ) == 1 ) print "End of header found" FStatus vFileRef print "Current file position: ", V_filePos break endif vIndex += 1 While( 1 ) //Binary headers //brief (16 bytes) header; seems to be 4 4 byte unsigned integer values, second valued may give number of spectra //otherwise the purpose of this part is not clear Make/O/N=4 wBinHeader_1 //each spectrum gets 24 unsigned 4 byte integers; fifth value is number of points in corresponding spectrum Make/O/N=( 24 * vNumSpectra ) wSpectraHeader //read the binary headers FBinRead/B=0/F=3/U vFileRef, wBinHeader_1 FStatus vFileRef print "Current file position after bin header read: ", V_filePos FBinRead/B=0/F=3/U vFileRef, wSpectraHeader FStatus vFileRef print "Current file position after spectra header read: ", V_filePos //Mystery value at end of each spectrum... //4 byte floating value at end of spectrum; don't know what it is for; read it anyway Make/O/N=( vNumSpectra ) wMysteryBytes //load each spectrum into a separate wave for( vIndex = 0; vIndex < vNumSpectra; vIndex += 1 ) //name for spectrum wave; name is "wSpectrum_" plus number corresponding to position in file sprintf sSpectrumWaveName, "wSpectrum_%02d", vIndex + 1 print sSpectrumWaveName //get number of points to load for the spectrum vPointsInSpectrum = wSpectraHeader[ 5 + vIndex * 24 ] //make the wave for the current spectrum Make/O/N=( vPointsInSpectrum ) $sSpectrumWaveName Wave wSpectrum = $sSpectrumWaveName //read spectrum; format is 64 bit float FBinRead/B=0/F=5 vFileRef, wSpectrum //deal with 4 bytes at end of a spectrum; either read into wave just for this or skip //code for skipping is commented out //read 4 bytes at end of spectrum, interpret at 32 bit float FBinRead/B=0/F=4 vFileRef, vReadVal wMysteryBytes[ vIndex ] = vReadVal // //next 4 bytes have unknown purpose, skip for now // FStatus vFileRef // FSetPos vFileRef, V_filePos + 4 endfor Close vFileRef End