input: Write a function to perform a series of calculations and generate plots based on input data. The function should load input data, perform calibration, calculate various values, and generate plots for absorbed power vs emitted power, absorbed power vs reflectivity, and dissipated power vs wavelength. The function should also calculate thermal resistance and display it on the plot. The function should read plotting instructions from a separate text file and execute them accordingly.
  
  
  def perform_calculations_and_plot(logfile, calib_folder, instrfile):
      # Load necessary modules and functions
      import numpy as np
      import matplotlib.pyplot as plt
      import errorvalues as ev
      from VECSELsetup.eval.varycolor import varycolor
      from VECSELsetup.eval.gen_functions import load, extract, plotinstructions_write, plotinstructions_read, lut_from_calibfolder, lut_interp_from_calibfolder, thermal_resistance
  
      # Calibration
      emis_lut = lut_from_calibfolder(calib_folder, identifiers=['Laser'], ignore_error=False)
      pump_lut, refl_lut = lut_interp_from_calibfolder(calib_folder, identifiers=['Pump', 'Refl'])
  
      # Load measurement
      current_set, current, pump, refl, laser, spectra, meantemp = extract(logfile, identifiers=['Current', 'Pump', 'Refl', 'Laser', 'Spectra', 'Temperature'])
      Temperatures = sorted(current_set.keys())
      T_out = dict((T, meantemp[T].round(1)) for T in Temperatures)
  
      # Calculate using calibration
      absorbed, reflected, emitted, pumped, dissipated = {}, {}, {}, {}, {}
      for T in Temperatures:
          reflected[T] = refl_lut(refl[T])
          pumped[T] = pump_lut(pump[T])
          absorbed[T] = pumped[T] - reflected[T]
          emitted[T] = emis_lut(laser[T])
          dissipated[T] = absorbed[T] - emitted[T]
  
      # Retrieve instructions
      instr = plotinstructions_read(instrfile)
      str2lst = lambda s: list(map(float, s[1:-1].split(',')))
  
      textx = float(instr['textx'])
      fontsize = float(instr['fontsize'])
      title = instr['title']
      xlim = str2lst(instr['xlim'])
      ylim1 = str2lst(instr['ylim1'])
      ylim2 = str2lst(instr['ylim2'])
      xlim3 = str2lst(instr['xlim3'])
      ylim3 = str2lst(instr['ylim3'])
      plot_temps_for_3 = str2lst(instr['plot_temps_for_3'])
      textx3 = float(instr['textx3'])
      texty3 = str2lst(instr['texty3'])
      llow0, lhigh0, texty1 = {}, {}, {}
      for T in Temperatures:
          llow0[T] = sum(absorbed[T].v() < float(instr[f'llow0[{T}]']))
          lhigh0[T] = sum(absorbed[T].v() < float(instr[f'lhigh0[{T}]']))
          texty1[T] = float(instr[f'texty1[{T}]'])
  
      # Plotting
      cols = varycolor(3 * len(Temperatures))
      plt.subplot(3, 1, 1)
      cnt = 0
      q0, m0 = {}, {}
      for T in Temperatures:
          q0[T], m0[T] = ev.linreg(absorbed[T].v()[llow0[T]:lhigh0[T]],
                                  emitted[T].v()[llow0[T]:lhigh0[T]],
                                  emitted[T].e()[llow0[T]:lhigh0[T]],
                                  overwrite_zeroerrors=True)
          emax, emaxi = ev.max(emitted[T], True)
          amax = absorbed[T][emaxi]
          print(f'Max emission at ({T_out[T]}) degC at ({amax}) W absorbed power: ({emax}) W')
          plt.errorbar(absorbed[T].v(), emitted[T].v(),
                       xerr=absorbed[T].e(), yerr=emitted[T].e(),
                       c=cols[cnt], linestyle=' ')
          plt.plot(absorbed[T].v(), m0[T].v() * absorbed[T].v() + q0[T].v(), c=cols[cnt + 1])
          plt.text(textx, texty1[T],
                   f'${T_out[T]}$$^\circ$C: ${m0[T].round(3) * 100}$ %',
                   color=cols[cnt], fontsize=fontsize)
          cnt += 3
      plt.title(title)
      plt.xlabel('Absorbed power (W)')
      plt.ylabel('Emited power (W)')
      plt.xlim(xlim)
      plt.ylim(ylim1)
      plt.grid('on')
  
      plt.subplot(3, 1, 2)
      cnt = 0
      for T in Temperatures:
          relref = reflected[T] / pumped[T] * 100
          plt.errorbar(absorbed[T].v(), relref.v(),
                       xerr=absorbed[T].e(), yerr=relref.e(),
                       c=cols[cnt], linestyle=' ')
          cnt += 3
      plt.title(title)
      plt.xlabel('Absorbed power (W)')
      plt.ylabel('Reflectivity (%)')
      plt.xlim(xlim)
      plt.ylim(ylim2)
      plt.grid('on')
  
      plt.subplot(3, 1, 3)
      cnt = 0
      q3, m3 = {}, {}
      for T in Temperatures:
          if T in plot_temps_for_3:
              q3[T], m3[T] = ev.linreg(dissipated[T].v()[llow0[T]:lhigh0[T]],
                                      spectra[T][1].v()[llow0[T]:lhigh0[T]],
                                      spectra[T][1].e()[llow0[T]:lhigh0[T]],
                                      overwrite_zeroerrors=True)
              plt.errorbar(dissipated[T].v(), spectra[T][1].v(),
                           xerr=dissipated[T].e(), yerr=spectra[T][1].e(),
                           c=cols[cnt], linestyle=' ')
          cnt += 3
      plt.title(title)
      plt.xlim(xlim3)
      plt.ylim(ylim3)
      plt.xlabel('Dissipated power (W)')
      plt.ylabel('Wavelength (nm)')
      plt.grid('on')
  
      wavelength = ev.errvallist([q3[T] for T in plot_temps_for_3])
      slopes = ev.errvallist([m3[T] for T in plot_temps_for_3])
      T_active = ev.errvallist([T_out[T] for T in plot_temps_for_3])
      dldD, dldT, l0 = thermal_resistance(T_active, wavelength, slopes)
      R_th = dldD / dldT
      for T in Temperatures:
          if T in plot_temps_for_3:
              plt.plot(dissipated[T].v(), l0.v() + dldT.v() * T_out[T].v() + dldD.v() * dissipated[T].v(), c=cols[cnt + 1])
          cnt += 3
      plt.text(textx3, texty3[0],
               f'$\\lambda=$' + f'$({dldT.round(3)})$' + '$T_{hs}+$' + f'$({dldD.round(3)})$' + '$D+$' + f'${l0.round(3)}$',
               color='k')
      R_th = R_th.round(2)
      therm_imp = f'Thermal impedance: $({R_th})$ K/W'
      plt.text(textx3, texty3[1],
               therm_imp, color='k')
      print(therm_imp)
  
      for T in Temperatures:
          print(meantemp[T])
  
      plt.show()
  
  assert perform_calculations_and_plot('../24_LL_ev/20150211_sample21-1-d6/spot333um.csv', '../24_LL_ev/20150204_calib_333um_s21-1-d6', '../24_LL_ev/20150211_sample21-1-d6/spot333um_instr.csv')
  
output: 
  def perform_calculations_and_plot(logfile, calib_folder, instrfile):
      import numpy as np
      import matplotlib.pyplot as plt
      import errorvalues as ev
      from VECSELsetup.eval.varycolor import varycolor
      from VECSELsetup.eval.gen_functions import load, extract, plotinstructions_write, plotinstructions_read, lut_from_calibfolder, lut_interp_from_calibfolder, thermal_resistance
  
      emis_lut = lut_from_calibfolder(calib_folder, identifiers=['Laser'], ignore_error=False)
      pump_lut, refl_lut = lut_interp_from_calibfolder(calib_folder, identifiers=['Pump', 'Refl'])
  
      current_set, current, pump, refl, laser, spectra, meantemp = extract(logfile, identifiers=['Current', 'Pump', 'Refl', 'Laser', 'Spectra', 'Temperature'])
      Temperatures = sorted(current_set.keys())
      T_out = dict((T, meantemp[T].round(1)) for T in Temperatures)
  
      absorbed, reflected, emitted, pumped, dissipated = {}, {}, {}, {}, {}
      for T in Temperatures:
          reflected[T] = refl_lut(refl[T])
          pumped[T] = pump_lut(pump[T])
          absorbed[T] = pumped[T] - reflected[T]
          emitted[T] = emis_lut(laser[T])
          dissipated[T] = absorbed[T] - emitted[T]
  
      instr = plotinstructions_read(instrfile)
      str2lst = lambda s: list(map(float, s[1:-1].split(',')))
  
      textx = float(instr['textx'])
      fontsize = float(instr['fontsize'])
      title = instr['title']
      xlim = str2lst(instr['xlim'])
      ylim1 = str2lst(instr['ylim1'])
      ylim2 = str2lst(instr['ylim2'])
      xlim3 = str2lst(instr['xlim3'])
      ylim3 = str2lst(instr['ylim3'])
      plot_temps_for_3 = str2lst(instr['plot_temps_for_3'])
      textx3 = float(instr['textx3'])
      texty3 = str2lst(instr['texty3'])
      llow0, lhigh0, texty1 = {}, {}, {}
      for T in Temperatures:
          llow0[T] = sum(absorbed[T].v() < float(instr[f'llow0[{T}]']))
          lhigh0[T] = sum(absorbed[T].v() < float(instr[f'lhigh0[{T}]']))
          texty1[T] = float(instr[f'texty1[{T}]'])
  
      cols = varycolor(3 * len(Temperatures))
      plt.subplot(3, 1, 1)
      cnt = 0
      q0, m0 = {}, {}
      for T in Temperatures:
          q0[T], m0[T] = ev.linreg(absorbed[T].v()[llow0[T]:lhigh0[T]],
                                  emitted[T].v()[llow0[T]:lhigh0[T]],
                                  emitted[T].e()[llow0[T]:lhigh0[T]],
                                  overwrite_zeroerrors=True)
          emax, emaxi = ev.max(emitted[T], True)
          amax = absorbed[T][emaxi]
          plt.errorbar(absorbed[T].v(), emitted[T].v(),
                       xerr=absorbed[T].e(), yerr=emitted[T].e(),
                       c=cols[cnt], linestyle=' ')
          plt.plot(absorbed[T].v(), m0[T].v() * absorbed[T].v() + q0[T].v(), c=cols[cnt + 1])
          plt.text(textx, texty1[T],
                   f'${T_out[T]}$$^\circ$C: ${m0[T].round(3) * 100}$ %',
                   color=cols[cnt], fontsize=fontsize)
          cnt += 3
      plt.title(title)
      plt.xlabel('Absorbed power (W)')
      plt.ylabel('Emited power (W)')
      plt.xlim(xlim)
      plt.ylim(ylim1)
      plt.grid('on')
  
      plt.subplot(3, 1, 2)
      cnt = 0
      for T in Temperatures:
          relref = reflected[T] / pumped[T] * 100
          plt.errorbar(absorbed[T].v(), relref.v(),
                       xerr=absorbed[T].e(), yerr=relref.e(),
                       c=cols[cnt], linestyle=' ')
          cnt += 3
      plt.title(title)
      plt.xlabel('Absorbed power (W)')
      plt.ylabel('Reflectivity (%)')
      plt.xlim(xlim)
      plt.ylim(ylim2)
      plt.grid('on')
  
      plt.subplot(3, 1, 3)
      cnt = 0
      q3, m3 = {}, {}
      for T in Temperatures:
          if T in plot_temps_for_3:
              q3[T], m3[T] = ev.linreg(dissipated[T].v()[llow0[T]:lhigh0[T]],
                                      spectra[T][1].v()[llow0[T]:lhigh0[T]],
                                      spectra[T][1].e()[llow0[T]:lhigh0[T]],
                                      overwrite_zeroerrors=True)
              plt.errorbar(dissipated[T].v(), spectra[T][1].v(),
                           xerr=dissipated[T].e(), yerr=spectra[T][1].e(),
                           c=cols[cnt], linestyle=' ')
          cnt += 3
      plt.title(title)
      plt.xlim(xlim3)
      plt.ylim(ylim3)
      plt.xlabel('Dissipated power (W)')
      plt.ylabel('Wavelength (nm)')
      plt.grid('on')
  
      wavelength = ev.errvallist([q3[T] for T in plot_temps_for_3])
      slopes = ev.errvallist([m3[T] for T in plot_temps_for_3])
      T_active = ev.errvallist([T_out[T] for T in plot_temps_for_3])
      dldD, dldT, l0 = thermal_resistance(T_active, wavelength, slopes)
      R_th = dldD / dldT
      for T in Temperatures:
          if T in plot_temps_for_3:
              plt.plot(dissipated[T].v(), l0.v() + dldT.v() * T_out[T].v() + dldD.v() * dissipated[T].v(), c=cols[cnt + 1])
          cnt += 3
      plt.text(textx3, texty3[0],
               f'$\\lambda=$' + f'$({dldT.round(3)})$' + '$T_{hs}+$' + f'$({dldD.round(3)})$' + '$D+$' + f'${l0.round(3)}$',
               color='k')
      R_th = R_th.round(2)
      therm_imp = f'Thermal impedance: $({R_th})$ K/W'
      plt.text(textx3, texty3[1],
               therm_imp, color='k')
  
      plt.show()
  