def changeValueinTemp(tempfile, Reference, Bin, Xmin, Xmax, resultFolder):
    nex.ModifyTemplate(doc, tempfile, "Bin (sec)", Bin)
    nex.ModifyTemplate(doc, tempfile, "Reference", Reference)
    nex.ModifyTemplate(doc, tempfile, "XMin (sec)", Xmin)
    nex.ModifyTemplate(doc, tempfile, "XMax (sec)", Xmax)
    nex.ModifyTemplate(
        doc, tempfile, 'Send to Excel',
        'None')  #no need send to excel, but save result in csv file
    nex.ApplyTemplate(doc, tempfile)
    resultPath = resultFolder + '/' + Reference + '_Bin' + Bin + '_Xmin' + Xmin + '_Xmax' + Xmax + '.csv'
    nex.SaveNumResults(doc, resultPath)
        FP_variables.append(x)

#variable selection
#first deselects all, then adds only the variables cued by FP_variables

nex.DeselectAll(doc)
for i in FP_variables:
    x = nex.GetVarByName(doc, i)
    nex.Select(doc, x)

#moves into the destination directory
os.chdir(os.path.join(destination, newdir))

#modifies the analysis to te specified start and end times

nex.ModifyTemplate(doc, template, "Select Data From (sec)", str(start))
nex.ModifyTemplate(doc, template, "Select Data To (sec)", str(end))

#ensures no other interval filters are specified
nex.ModifyTemplate(doc, template, "Interval Filter", 'None')

#creates a new folder for each interval
#sets the analysis to for the correct interval

for interval in intervals:
    nex.ModifyTemplate(doc, template, "Interval Filter", interval)
    os.mkdir(interval)
    os.chdir(interval)

    #loops over the FP variables and uses each as a reference once
    #applies the coherence template
# 4. The bin size for analysis, in seconds:
#The bin size must be no larger than the analysis period (end-start)
binsize = 30

#####################################################
variables = list(doc.ContinuousNames())
FP_variables = []
for x in variables:
    if x[:2] == 'FP':
        FP_variables.append(x)
nex.DeselectAll(doc)
for i in FP_variables:
    x = nex.GetVarByName(doc, i)
    nex.Select(doc, x)

nex.ModifyTemplate(doc, template, "Interval Filter", 'None')

#build a series of dictionaries with intervals as keys, values are:
interval_starts = {}  #start time of each interval segment
interval_ends = {}  #end time of each interval segment
interval_absolute_start = {}  #start of the first segment of the interval
interval_absolute_end = {}  #end of the last segment of the interval
interval_lengths = {}  #length of each segment of each interval
interval_lengths_overbinsize = {
}  #whether or not each interval segment is longer than the binsize
interval_bin_starts = {}  #where each bin of anlysis starts for each interval
interval_good_bins = {
}  #whether or not each bin is good, i.e. is fully inside of the interval

for interval in intervals:
    interval_starts[interval] = doc[interval].Intervals()[0]
variables = list(doc.ContinuousNames())
FP_variables = []
for x in variables:
    if x[:2] == 'FP':
        FP_variables.append(x)

#variable selection
#first deselects all, then adds only the variables cued by FP_variables

nex.DeselectAll(doc)
for i in FP_variables:
    x = nex.GetVarByName(doc, i)
    nex.Select(doc, x)

#makres sure interval filter is unselected
nex.ModifyTemplate(doc, template, "Interval Filter", "None")

#main loop for extracting data

#loops over bin_starts, and makes a subdirectory based on the bin
#moves into that subfolder
#modifies the template to run the coherence analysis from the start to the end of the bin
#does this using each wire as a reference
#moves back into the superdirectory

for m in range(len(bin_starts)):
    subdir = str(m) + "_" + str(
        bin_starts[m]) + "-" + str(bin_starts[m] + binsize)
    os.makedirs(subdir)
    os.chdir(subdir)
    nex.ModifyTemplate(doc, template, "Select Data From (sec)",