optical_constants.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. # GPL license from Smuthi project
  3. """Provide functionality to read optical constants in format provided by `refractiveindex.info <https://refractiveindex.info/>`_ website"""
  4. from scipy.interpolate import interp1d
  5. import io
  6. import numpy as np
  7. import yaml
  8. def read_refractive_index_from_yaml(filename, vacuum_wavelength, units="mkm", kind=1):
  9. """Read optical constants in format provided by refractiveindex.info website.
  10. Args:
  11. filename (str): path and file name for yaml data
  12. downloaded from refractiveindex.info
  13. vacuum_wavelength (float or np.array): wavelengths where refractive
  14. index data is needed
  15. units (str): units for wavelength. currently, microns ('mkm' or 'um')
  16. and nanometers ('nm') can be selected
  17. kind (int): order of interpolation
  18. Returns:
  19. A pair (or np.array of pairs) of wavelength and
  20. corresponding refractive index (complex)
  21. """
  22. if units == "nm":
  23. factor = 1000
  24. elif units in ("mkm", "um"):
  25. factor = 1
  26. else:
  27. raise NotImplementedError("Converting wavelength into '"+units
  28. +"' units for refractive index data"
  29. +" was not implemented.")
  30. the_file = yaml.load(open(filename))['DATA'][0]
  31. data_type = the_file['type']
  32. if data_type != 'tabulated nk':
  33. raise NotImplementedError("Input data type '"+data_type
  34. +"' available in file "+filename
  35. +" was not implemented.")
  36. data = the_file['data'].splitlines()
  37. data_split = []
  38. for wl in data:
  39. data_split.append(wl.split())
  40. data_num = []
  41. for wl in data_split:
  42. record = []
  43. for val in wl:
  44. record.append(float(val))
  45. data_num.append(record)
  46. data_np = np.array(data_num)
  47. data_wl = data_np[:,0]*factor
  48. eps_re = data_np[:,1]
  49. eps_im = data_np[:,2]
  50. f_re = interp1d(data_wl, eps_re, kind=kind)
  51. f_im = interp1d(data_wl, eps_im, kind=kind)
  52. data_out = np.transpose(np.vstack((vacuum_wavelength, f_re(vacuum_wavelength)+f_im(vacuum_wavelength)*1j)))
  53. if len(data_out) == 1:
  54. return data_out[0]
  55. return data_out