Load YAML Files

Here is code to load a folder of YAML files and recast each file in it into a (N, 2) text wave. To make this code backwards compatible with IP8 or below, remove the ListToTextWave conversion and replace the for (fName : YAMList) with an explicit for (ic ...) loop on the list elements. The import_...Folder imports an entire folder. The load_... loads just one file.

Function import_YAMLFolder(string mstr, string vstr, [variable cleanup])

	variable nfiles, err
	string fileFilters = "(*.yml,*.yaml):.yml,.yaml;"
	string fName, fldrName, fList
	
	// open a file in the folder (dialog only)
	NewPath/O/Q/M=mstr YAMLFolder
	if (v_flag != 0)
		return -1
	endif
	
	// set the file list to import
	fList = IndexedFile(YAMLFolder,-1,".yml") + IndexedFile(YAMLFolder,-1,".yaml")
	nfiles = ItemsInList(fList)
	if (nfiles == 0)
		return -2
	endif

	// import the files
	PathInfo YAMLFolder
	fldrName = CleanUpName(ParseFilePath(0,s_path,":",1,0),0)
	NewDataFolder/O/S $fldrName
	wave/T YAMLList = ListToTextWave(fList,";")
	for (fName : YAMLList)
		err = load_YAML("",vstr,auto=fName)
		if (err >= 0)
			wave/T YAMLWave
			recast_YAML(YAMLWave)
		endif
	endfor
	
	// clean up if desired
	if (!ParamIsDefault(cleanup))
		killwaves/Z YAMLWave
	endif
	SetDataFolder ::
	
	return 0
end

Function load_YAML(string mstr, string vstr, [string auto])

	variable refNum, slen
	string fileFilters = "(*.yml,*.yaml):.yml,.yaml;"
	string fName, line = ""
	
	// make storage text wave
	make/N=(0,2)/T/O YAMLwave
	YAMLwave = ""

	// open the file	
	if (ParamIsDefault(auto))
		open/R/F=(fileFilters)/M=mstr refNum
		if (strlen(s_fileName) == 0)
			return -1
		endif
	else
		open/R/P=YAMLFolder refNum as auto
	endif
	fName = s_fileName
	
	// validate the first line
	FReadLine/Q refnum, line
	slen = strlen(line)
	if (slen != 0)
		if (cmpstr(line[0,slen-3],vstr) != 0)		
			close refNum
			return -1
		endif
	else
		close refNum
		return -1
	endif
	
	close refNum
	
	// load the text wave
	loadwave/A=YAML/J/K=2/M/V={":","",0,1}/Q fName
	fName = StringFromList(0,s_wavenames)
	wave/T yw = $fName
	if (DimSize(yw,0) != 0)
		duplicate/O/T yw YAMLwave
	endif
	killwaves/Z yw
	
	return 0
end

Function recast_YAML(wave/T ywave)

	variable npts = DimSize(ywave,0)
	string wname
	
	duplicate/FREE/T ywave twave

	// remove tab characters and spaces before + after text
	twave[][] = TrimString(ReplaceString("\t",twave[p][q],""))

	// validate that name exists
	if (cmpstr(twave[1][0],"name") != 0)
		return -1
	endif
	
	// clean up wave first and last blank
	DeletePoints 0,1, twave
	if (cmpstr(twave[npts-2][0],"") == 0)
		DeletePoints (npts-2),1, twave
	endif

	// create new text wave
	wname = CleanUpName(twave[0][1],0)
	duplicate/O twave, $wname
	
	return 0
end

Nice! I did have to make a small change for load_YAML() to find my .yaml files. Your file filter string needs to have no spaces in between the different file extensions, like this:

string fileFilters = "(*.yml,*.yaml):.yml,.yaml;"

Forum

Support

Gallery

Igor Pro 10

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More