Extract contour waves using a function?

From a function, I want to automate a curve-fitting procedure on a contour's wave. I know that #include<Extract Contours As Waves> adds the wave-from-contour extraction operation to the Graph menu, but this must be performed manually. I did try inspecting the code in the included ipf, but it is pretty opaque (to me).

As an alternative the Manual suggests
Another way to copy the traces into a normal wave is to use the Data Browser to browse the saved experiment; the contour traces are saved as waves in a temporary data folder whose name begins with “WM_CTraces_” and ends with the contour’s “contour instance name”.
Since I created the custom contour level , I think I can construct the necessary instance name to use in a function. However, after I created a test image with contours, this folder was nowhere to be found. If it is temporary, how can it be accessed?

I would appreciate any suggestions.
(IP 6.22A, WinVista)
The heart of the Extract Contours As Waves procedure file is the function WMDuplicateTraceWave(). It uses TraceNameToWaveRef and XWaveRefFromTrace to get wave references for the underlying hidden waves that define a contour trace. It then uses the wave references to duplicate those waves to create non-hidden waves. The trick is to get the right name for the trace at the contour level you want. You can make that job a little easier using the /F=formatStr with AppendMatrixContour or AppendXYZContour to specify the format of the contour line naming.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
John,

Thanks for your help. I initially thought I had solved the problem with the following example:
#pragma rtGlobals=1     // Use modern global access method.
#include<Extract Contours As Waves>

function SetupGraphWithContours()
    make/O/N=(100,100) wave0
    setscale x, -2, 2,"" wave0
    setscale y, -2,2, "" wave0
    wave0=exp(-(x^2+y^2)/0.5)
    NewImage wave0
    ModifyGraph width=0,height={Plan,1,left,top}
    ModifyGraph width = 400
    DoWindow/F Graph0
    AppendMatrixContour/W=Graph0/L/T/F="%s=%1.4f" wave0
    ModifyContour/W=Graph0 wave0 autoLevels={*,*,5}
end

function/S TestContourExtract(MyNewLevel)
    variable MyNewLevel
    WAVE wave0
    string  MyNewLevelString
    sprintf MyNewLevelString, "%s=%1.4f", NameOfWave(wave0), MyNewLevel
    ModifyContour/W=Graph0 wave0 , morelevels=0, morelevels={(MyNewLevel)}; DelayUpdate
    return WMDuplicateTraceWave("Graph0", MyNewLevelString)
//  string with extracted .x .y contour waves
end


•SetupGraphWithContours()
print TestContourExtract(0.05)
  wave0=0.0500.x,wave0=0.0500.y

The new contour level was added to the Graph0 image plot, and the new extracted contour waves appeared in the Data Browser. However, these new waves each had 0 points. What did I do incorrectly?
It's the old gotcha- you need DoUpdate right after ModifyContour. Igor doesn't actually compute the values of the contour until it redraws the graph.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Thanks, John. Replacing 'DelayUpdate' with 'DoUpdate' in the above example fixed the problem.

I was confused in the application of the AppendMatrixContour help file contents:

In a procedure, to modify the appearance of contour levels before the contour is calculated and displayed with the default values, append ";DelayUpdate" and immediately follow the AppendMatrixContour command with the appropriate ModifyContour commands. All but the last ModifyContour command should also have ;DelayUpdate appended.

This test example was somewhat artificial. However, fitting a contour shape of real data can be useful.
That is a tad confusing, isn't it. That bit would apply to a Procedure (that is, something declared as Macro or Proc or Window, etc.). The key is that interpreted code actually does an update between lines, so the default contour plot would be produced right away. In a Function, updates are suppressed so that explanation doesn't apply.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com