1

I have a simple csv file like this:

wavelength exposure
550 2
560 3
570 10
580 2
590 5
600 6

I am trying to do a simple calculation between each item in the wavelength column and then eventually time.sleep for the related exposure time. The if loop for idx==0 seems to work well. But I cannot figure out how to better write the idx > 0: part. The calculation doesn't work well for the second items in the column. I can't figure out how to update the starting wavelength and subract it from the next item in the list. I want it to do 550-631.26 update currentwave to 550 then do 560-550 but in a loop so it automatically does 570-560 and so on

filelist = pd.read_csv(filename)
wavelist=filelist['wavelength']
exposurelist=filelist['exposure']
home = 631.26 #nm
rev = 9000 #steps = 1 nm conversion factor for wavelength to mechanical step, used for serial commands
for idx, wl in enumerate(wavelist): 
    if idx == 0:
        diffwave = float(wl)-home
        diffstep = diffwave*rev
        diffstep = round(diffstep,0)
        diffstep = int(diffstep)
        if diffstep > 0:
            diffstepstr = str('+' f'{diffstep}' + ' \r')
            diffstepbyte = bytes(diffstepstr, 'ascii')
        if diffstep <= 0:
            diffstepstr = str(f'{diffstep}' + ' \r')
            diffstepbyte = bytes(diffstepstr, 'ascii')
        print(f"scan controller is moving by {diffstep} steps")
        print(f"Moving scan controller {diffwave}nm to {wl}nm")
        print(f"Holding for current exposure {exposurelist[idx]} seconds")
        print(f"The exposure for {wl}nm was taken")
        currentwave = wl
    if idx > 0:
        diffwave = wl-currentwave
        diffstep = diffwave*rev
        if diffstep > 0:
            diffstepstr = str('+' f'{diffstep}' + ' \r')
            diffstepbyte = bytes(diffstepstr, 'ascii')
        if diffstep <= 0:
            diffstepstr = str(f'{diffstep}' + ' \r')
            diffstepbyte = bytes(diffstepstr, 'ascii')
        print(f"scan controller is moving by {diffstep} steps")
        print(f"Moving scan controller {diffwave}nm to {wl}nm")
        print(f"Holding for current exposure {exposurelist[idx]} seconds")
        print(f"The exposure for {wl}nm was taken")
        if idx < len(wavelist)-1:
            currentwave = [idx-1]
Nick ODell
  • 15,465
  • 3
  • 32
  • 66

1 Answers1

0

You place a list object into the variable currentwave in the last line of your code currentwave = [idx-1] which produces an error the next time you subtract the variable with a number. Quick fix is you replace that line with currentwave = wl.

Actually, after looking at your code, you don't really need to break the loop down into idx==0 and idx >0, instead,

filelist = pd.read_csv(filename)

home = 631.26 #nm
rev = 9000 #steps = 1 nm conversion factor for wavelength to mechanical step, used for serial commands

currentwave = home
for wl, exp in filelist.to_numpy():
    diffwave = float(wl) - currentwave
    diffstep = diffwave * rev
    diffstep = round(diffstep, 0)
    diffstep = int(diffstep)
    
    if diffstep > 0:
        diffstepstr = str('+' f'{diffstep}' + ' \r')
    else:
        diffstepstr = str(f'{diffstep}' + ' \r')
        
    diffstepbyte = bytes(diffstepstr, 'ascii')
    print(f"scan controller is moving by {diffstep} steps")
    print(f"Moving scan controller {diffwave}nm to {wl}nm")
    print(f"Holding for current exposure {exp} seconds")
    print(f"The exposure for {wl}nm was taken")
    currentwave = wl
Raymond Kwok
  • 2,461
  • 2
  • 9
  • 11