/*
WHAT THIS DOES

For finding y values:
It lets you type in the x value (time in ms) of a plot and displays the corresponding y value (in units of the vector).

For finding x values:
Specifies 'start' and 'stop' times (in ms) of where you want to search for the x values corresponding to your specified y value.
Specifies the range (float_epsilon) above and below your specified y value that is considered a match.
At the moment, please look at terminal for print out of the x values.
(I will fix this when I have time...)

Note:	You need to open a new window for each signal you want to measure values.
	If recording a vector (as opposed to picking one after), you need to select the vector
	before hitting "Init & Run"


HOW TO USE

If using this tool individually (not via extra.hoc),
place this file along with other model hoc files.
In the oc> prompt, type...
load_file("xyvalues.hoc")

Find the xy Values window in 'Tools' -> 'Miscellaneous'


-- Leo Ng
   July 8, 2004
*/


begintemplate XYValues

// rec record values of the vector.
// vec points to either rec or picked vector hoc_obj
public Select, x1, y1, y2, start, finish, range, rec, box, vec
external hoc_obj_
objref this, rec, sc, box, vec
strdef sig, cmd, x2values, title

// Sets the default values and creates the vectors.
proc init() {
	x1 = 0
	y1 = 0
	y2 = 0
	start = 0
	finish = 0
	range = 1
	sig = ""

	rec = new Vector()
	vec = rec
	sc = new SymChooser("Choose Vector")
	CreatePanel()
}

// Creates the GUI.
proc CreatePanel() {
	box = new VBox()
	box.ref(this)
	box.intercept(1)

	xpanel("xy Values")
	xbutton("Select Vector", "Select()")
	xvarlabel(sig)
	xvarlabel("")
	xradiobutton("Use recorded vector (above)", "vec = rec", 1)
	xradiobutton("Use picked vector", "vec = hoc_obj_[0]", 0)
	xvarlabel("")
	xvarlabel("Finding the corresponding y value:")
	xpvalue("Time (ms)", &x1, 1, "y1 = vec.x[x1/dt]")
	xvalue("Corresponding y Value", "y1", 2)
	xvarlabel("")
	xvarlabel("Finding the corresponding x value:")
	xpvalue("Interval Start (ms)", &start, 1)
	xpvalue("Interval End (ms)", &finish, 1)
	xpvalue("Float Epsilon", &range, 1)
	xpvalue("y Value", &y2, 1, "FindxValue()")
	xvarlabel("See terminal for x values")
	xpanel()

	box.intercept(0)
	sprint(title, "%s", this)
	box.map(title)
}

// Find x values within specified time interval.
// x values are found for values within 'range' of 'y2'
proc FindxValue() { local i, tmp
	tmp = float_epsilon
	float_epsilon = range
	print "The corresponding x values (t in ms) to ", y2, "in ", vec, " are:"
	for (i = start/dt; i <= (finish/dt - 1); i += 1) {
		if (vec.x[i] == y2) {
			print i*dt
		}
	}
	print " "
	float_epsilon = tmp
}

// Loads the signal choosing menu.
proc Select() {
	// Opens the SymChooser menu.
	// Stores the chosen variable in 'sig'.
	// Tells rec to record 'sig'.
	if (sc.run()) {
		sc.text(sig)
		sprint(cmd, "%s.rec.record(&%s)", this, sig)
		execute(cmd)
	}
}

endtemplate XYValues

// Adds windows to NEURON's main menu.
objref xyValues
proc MakexyValues() {
	xyValues = new XYValues()
	objref xyValues
}
nrnmainmenu_.miscellaneous_add("xy Values", "MakexyValues()")