Skip to content

Instantly share code, notes, and snippets.

@Omnistic
Last active February 24, 2022 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Omnistic/ce519a11aafec1c7348e7b85a06b5eec to your computer and use it in GitHub Desktop.
Save Omnistic/ce519a11aafec1c7348e7b85a06b5eec to your computer and use it in GitHub Desktop.
Read all glass catalogs in OpticStudio and compute refractive index at given wavelength
import os
# Path to {Documents}\Zemax\Glasscat
path = r'C:\Users\David Nguyen\Documents\Zemax\Glasscat'
# Initialize return array, which will contain the glass names and vendors
allglass = []
# Dispersion formula flag
dispersionform = -1
# Wavelength of interest in micrometres
wave = 0.7
# Loop over the files in the GLASSCAT folder
for glasscat in os.listdir(path):
# Select files with the *.AGF extension
if glasscat.lower().endswith('.agf'):
# Save vendor name
vendor = glasscat[:-4]
# Read the file
with open(os.path.join(path, glasscat)) as glasstxt:
# Search for the lines starting with NM (contains the glass name)
for line in glasstxt:
if line.lower().startswith('nm'):
# Split the parameters of the glass name entry
glassnamedata = line.split()
# Save the glass name
glassname = glassnamedata[1]
# Dispersion formula
dispersionform = int(float(glassnamedata[2]))
else:
# Are we returning the refractive index?
if line.lower().startswith('cd') and dispersionform != -1:
# Schott dispersion formula
if dispersionform == 1:
dispersioncoef = line.split()
# Coefficients
a0 = float(dispersioncoef[1])
a1 = float(dispersioncoef[2])
a2 = float(dispersioncoef[3])
a3 = float(dispersioncoef[4])
a4 = float(dispersioncoef[5])
a5 = float(dispersioncoef[6])
# Refractive index
nschott = a0 + a1 * wave**2 + a2 * wave**-2 +\
a3 * wave**-4 + a4 * wave**-6 + a5 * wave**-8
nschott = nschott**0.5
allglass.append((vendor, glassname, str(nschott)))
# Reset flag
dispersionform = -1
# Display results
allglasstxt = ''
for glass in allglass:
allglasstxt += 'Vendor: ' + glass[0] + ' | Glass: ' + glass[1]
allglasstxt += ' | Refractive index (Schott): ' + glass[2] + '\n'
with open('allglass.txt', 'w') as glasstxt:
glasstxt.writelines(allglasstxt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment