Wave reference
ssmith911
Function AverageNephData()
SETDataFolder root:working
Variable avgTime
Prompt avgtime,"Enter Averaging Time in Minutes"
Doprompt "Data Averaging Time",avgTime
String VariPrefix = num2str(AvgTime)+"_Min_avg"//, noisevari
variable a,b,c,d
//Print avgTime, VariPrefix
//Sets up wave lists
Wave Neph_450_STP
Wave Corr_450,Corr_550,Corr_700,Neph_D8Time //These are 5 second data
Make/T/O/N=3 source={"Corr_450","Corr_550","Corr_700"}
Make/T/O/N=3 result={"Corr_450","Corr_550","Corr_700"}
Make/T/O/N=3 noise={"Corr_450_stdev","Corr_550_stdev","Corr_700_stdev"}
//Makes wave names: attaches averaging prefix to the beginning of the wave name
FOR(a=0;a<3; a+=1)
result[a] = variPrefix+"_"+source[a]
noise[a] = variPrefix+"_"+noise[a]
ENDFOR
// MAke/O/D/N=3 localwaveavg, localwavesdev
Print source[0]
variable pointer, Min1, Min2
string timestring1,minute1,timestring2,minute2 //averging window starttime, startminute, averaging window endtime, endminute
Make/D/O/N=(numpnts(Neph_D8Time)/(12*Avgtime)) WinStart, WinEnd //These are the start and ending indices for each averaging win\ow
Make/T/O/N=(numpnts(Neph_D8Time)) NephTimeString //This will hold the D8Time wave as a string
NephTimeString = secs2Time(Neph_D8Time,1) //Converts time, in seconds, to a string time format
c=0; d=0 //c holds the window start index as fixed. d increments and then the average is taken from the c and indices
Make/D/O/N=(numpnts(Neph_D8Time)/(12*avgtime)) AveragedTime //Creates the time wave for the averaged data
FOR(pointer=0;pointer<(numpnts(result));pointer+=1)
Wave localwaveavg = $result[pointer]
Wave localwavesdev = $noise[pointer]
FOR(b=0;b<(numpnts(neph_D8Time));b+=1) //Loops through the length of the Neph_D8Time wave
Timestring1 = NephTimeString[c]; Timestring2 = NephTimeString[b]
Minute1 = TimeString1[2,3]; Minute2 = TimeString2[2,3]
Min1 = str2num(Minute1); Min2 = str2num(Minute2)
IF(Min1+avgtime == Min2 || Min1+avgtime == Min2+60) //Will execute when the minutes are different by the amount of the averaging time
Wavestats/Q/R=[c,b] $source[pointer] //The OR part of the IF covers the time when the minutes go from 59 back to 0.
localwaveavg[d]=v_avg
localwavesdev[d]=v_sdev
AveragedTime[d] = Neph_D8Time[c]
d+=1
//IF(Timestring2 - Timestring1 > 10*avgtime)
//ENDIF
c=b //This sets the index to the beginning of the next averaging window
ENDIF
ENDFOR
ENDFOR
END
SETDataFolder root:working
Variable avgTime
Prompt avgtime,"Enter Averaging Time in Minutes"
Doprompt "Data Averaging Time",avgTime
String VariPrefix = num2str(AvgTime)+"_Min_avg"//, noisevari
variable a,b,c,d
//Print avgTime, VariPrefix
//Sets up wave lists
Wave Neph_450_STP
Wave Corr_450,Corr_550,Corr_700,Neph_D8Time //These are 5 second data
Make/T/O/N=3 source={"Corr_450","Corr_550","Corr_700"}
Make/T/O/N=3 result={"Corr_450","Corr_550","Corr_700"}
Make/T/O/N=3 noise={"Corr_450_stdev","Corr_550_stdev","Corr_700_stdev"}
//Makes wave names: attaches averaging prefix to the beginning of the wave name
FOR(a=0;a<3; a+=1)
result[a] = variPrefix+"_"+source[a]
noise[a] = variPrefix+"_"+noise[a]
ENDFOR
// MAke/O/D/N=3 localwaveavg, localwavesdev
Print source[0]
variable pointer, Min1, Min2
string timestring1,minute1,timestring2,minute2 //averging window starttime, startminute, averaging window endtime, endminute
Make/D/O/N=(numpnts(Neph_D8Time)/(12*Avgtime)) WinStart, WinEnd //These are the start and ending indices for each averaging win\ow
Make/T/O/N=(numpnts(Neph_D8Time)) NephTimeString //This will hold the D8Time wave as a string
NephTimeString = secs2Time(Neph_D8Time,1) //Converts time, in seconds, to a string time format
c=0; d=0 //c holds the window start index as fixed. d increments and then the average is taken from the c and indices
Make/D/O/N=(numpnts(Neph_D8Time)/(12*avgtime)) AveragedTime //Creates the time wave for the averaged data
FOR(pointer=0;pointer<(numpnts(result));pointer+=1)
Wave localwaveavg = $result[pointer]
Wave localwavesdev = $noise[pointer]
FOR(b=0;b<(numpnts(neph_D8Time));b+=1) //Loops through the length of the Neph_D8Time wave
Timestring1 = NephTimeString[c]; Timestring2 = NephTimeString[b]
Minute1 = TimeString1[2,3]; Minute2 = TimeString2[2,3]
Min1 = str2num(Minute1); Min2 = str2num(Minute2)
IF(Min1+avgtime == Min2 || Min1+avgtime == Min2+60) //Will execute when the minutes are different by the amount of the averaging time
Wavestats/Q/R=[c,b] $source[pointer] //The OR part of the IF covers the time when the minutes go from 59 back to 0.
localwaveavg[d]=v_avg
localwavesdev[d]=v_sdev
AveragedTime[d] = Neph_D8Time[c]
d+=1
//IF(Timestring2 - Timestring1 > 10*avgtime)
//ENDIF
c=b //This sets the index to the beginning of the next averaging window
ENDIF
ENDFOR
ENDFOR
END
The reference to localwaveavg or localwavesdev are not being creating using the list from $result[pointer] and noise[pointer]. I've used this technique in other code. I'm sure it's something simple that I'm missing. localwaveavg should be a named wave from the the list of waves in result. Any help would be greatly appreciated. Thanks.
Have you used the debugger to step through the code? You may need to do put the text content in ().
wave localwaveavg = $(result[pointer])
What happens when you use intermediate strings to assign the terms first?
string rstr = result[pointer]
string nstr = noise[pointer]
wave localwaveavg = $rstr
...
October 18, 2024 at 02:28 pm - Permalink
I don't think there is anything wrong with making the wave references from the text wave. The following code seems to work fine.
make/O/N=10 testw
testw=x*gnoise(2)
Make/O/T/N=1 textw
textw[0]="testw"
variable pointer=0
wave tw=$textw[pointer]
display tw
End
I did notice that in the line
NephTimeString = secs2Time(Neph_D8Time,1)
The first parameter, Neph_D8Time, is a wave reference, while secs2Time() requires the first parameter to be a number. Maybe that hepls?
October 18, 2024 at 03:41 pm - Permalink
I suspect that the wave references are failing because the waves do not exist. Here is a simplified test that shows that the technique you are using works if the waves exist:
if (flag == 0)
KillWaves/Z Corr_450, Corr_550, Corr_700
else
Make/O Corr_450, Corr_550, Corr_700
endif
Make/T/O/N=3 result={"Corr_450","Corr_550","Corr_700"}
int pointer
for(pointer=0;pointer<(numpnts(result));pointer+=1)
Wave/Z localwaveavg = $result[pointer]
if (WaveExists(localwaveavg))
Printf "Wave %s exists\r", NameOfWave(localwaveavg)
else
Printf "Wave %d does not exist\r", pointer
endif
endfor
End
October 18, 2024 at 05:28 pm - Permalink
This is the error from the debugger: error: WAVE reference to "localwaveavg" failed. Adding () around the $[pointer]doesn't work.
October 22, 2024 at 05:58 am - Permalink
hm, and you are sure the wave(s) exists in the current datafolder?
October 22, 2024 at 08:22 am - Permalink
I thought that these lines created the waves programmatically. I'm now thinking that is not the case. I'm trying to create waves that are programmatically named based on the the user input averaging time.
Wave localwaveavg = $result[pointer]
Wave localwavesdev = $noise[pointer]
October 22, 2024 at 08:52 am - Permalink
To create a new wave with a name contained in a string (in this case, element of a text wave) you need something like this:
WAVE/T result
WAVE/T noise
Variable pointer
Variable npnts=10
Make/D/N=(npnts) $(result[pointer])/WAVE=localwaveavg
Make/D/N=(npnts) $(noise[pointer])/WAVE=localwavesdev
end
If you have a pre-existing wave that is a template for the waves you need to create, you can replace `Make/D/N=(npnts)` with `Duplicate`
October 22, 2024 at 09:31 am - Permalink
Awesome, I'll work with this and let you know how it goes. Thanks for the help.
October 22, 2024 at 10:04 am - Permalink