Exemplo n.º 1
0
def read_lens(filename, inpt, **kwargs):
    ''' given inpt str of a Zemax .zmx file, return an OpticalModel  '''
    global _glass_handler, _cmd_not_handled, _track_contents
    _cmd_not_handled = util.Counter()
    _track_contents = util.Counter()
    logging.basicConfig(filename='zmx_read_lens.log',
                        filemode='w',
                        level=logging.DEBUG)

    # create an empty optical model; all surfaces will come from .zmx file
    opt_model = opticalmodel.OpticalModel(do_init=False)

    input_lines = inpt.splitlines()

    _glass_handler = ZmxGlassHandler(filename)

    for i, line in enumerate(input_lines):
        process_line(opt_model, line, i + 1)

    post_process_input(opt_model, filename, **kwargs)
    _glass_handler.save_replacements()
    _track_contents.update(_glass_handler.track_contents)

    opt_model.update_model()

    info = _track_contents, _glass_handler.glasses_not_found
    return opt_model, info
Exemplo n.º 2
0
 def __init__(self, filename):
     self.glass_catalogs = []
     self.glasses_not_found = util.Counter()
     self.track_contents = util.Counter()
     self.filename = None
     if filename:
         self.filename = filename.with_suffix('.smx')
         self.glasses_not_found = self.load_replacements(self.filename)
     self.no_replacements = not self.glasses_not_found
Exemplo n.º 3
0
def read_lens(filename, **kwargs):
    ''' given a CODE V .seq filename, return an OpticalModel  '''
    global _glass_handler, _track_contents
    global _reading_private_catalog
    logging.basicConfig(filename='cv_cmd_proc.log',
                        filemode='w',
                        level=logging.DEBUG)
    _reading_private_catalog = False
    _track_contents = util.Counter()
    opt_model = opticalmodel.OpticalModel()
    _glass_handler = CVGlassHandler(filename)
    cmds = cvr.read_seq_file(filename)
    for i, c in enumerate(cmds):
        cmd_fct, tla, qlist, dlist = process_command(c)
        if cmd_fct:
            eval_str = cmd_fct + '(opt_model, tla, qlist, dlist)'
            eval(eval_str)
        else:
            logging.info('Line %d: Command %s not supported', i + 1, c[0])

    _glass_handler.save_replacements()
    _track_contents.update(_glass_handler.track_contents)

    opt_model.update_model()

    info = _track_contents, _glass_handler.glasses_not_found
    return opt_model, info
Exemplo n.º 4
0
def read_lens(filename, **kwargs):
    """ given a CODE V .seq filename, return an OpticalModel
    
    Args:
        filename (pathlib.Path): a CODE V .seq file path
        kwargs (dict): keyword args passed to the reader functions

    Returns:
        an OpticalModel instance and a info tuple
    """
    global _glass_handler, _track_contents
    global _reading_private_catalog
    logging.basicConfig(filename='cv_cmd_proc.log',
                        filemode='w',
                        level=logging.DEBUG)
    _reading_private_catalog = False
    _track_contents = util.Counter()
    opt_model = opticalmodel.OpticalModel(do_init=False)
    _glass_handler = CVGlassHandler(filename)
    cmds = cvr.read_seq_file(filename)
    for i, c in enumerate(cmds):
        cmd_fct, tla, qlist, dlist = process_command(c)
        if cmd_fct:
            eval_str = cmd_fct + '(opt_model, tla, qlist, dlist)'
            eval(eval_str)
        else:
            logging.info('Line %d: Command %s not supported', i + 1, c[0])

    post_process_input(opt_model, filename, **kwargs)
    _glass_handler.save_replacements()
    _track_contents.update(_glass_handler.track_contents)

    opt_model.update_model()

    info = _track_contents, _glass_handler.glasses_not_found
    return opt_model, info
Exemplo n.º 5
0
 def load_replacements(self, filename):
     glasses_not_found = util.Counter()
     if filename.exists():
         with filename.open('r') as file:
             glasses_not_found = json.load(file)
     return glasses_not_found
Exemplo n.º 6
0
def read_lens(inpts):
    global _track_contents

    def read_float(s):
        if s == 'Infinity':
            return float('inf')
        elif isanumber(s):
            return float(s)
        else:
            if s == 'undefined':
                return float('nan')
            elif s == 'AS':
                return 0.
            elif s == '':
                return 0.
            else:
                try:
                    return float(read_float(var_dists[s][0]))
                except:
                    return 0.

    _track_contents = util.Counter()
    constants_inpt = inpts['constants']
    constants = {c_item[0]: c_item[1:] for c_item in constants_inpt}
    var_dists_inpt = inpts['variable distances']
    var_dists = {var_dist[0]: var_dist[1:] for var_dist in var_dists_inpt}

    thi_obj = 0.
    if 'd0' in var_dists:
        thi_obj = read_float(var_dists['d0'][0])
        if thi_obj == float('inf'):
            thi_obj = 1.0e10
    conj_type = 'finite'
    if thi_obj > 1.0e8:
        conj_type = 'infinite'
    _track_contents['conj type'] = conj_type

    specsheet = ss.create_specsheet(conj_type)

    imager_inputs = dict(specsheet.imager_inputs)
    if conj_type == 'finite':
        imager_inputs['m'] = read_float(var_dists['Magnification'][0])
        specsheet.frozen_imager_inputs = [False] * 5
    else:  # conj_type == 'infinite'
        imager_inputs['s'] = -float('inf')
        efl = read_float(var_dists['Focal Length'][0])
        if efl != 0:
            imager_inputs['f'] = efl
        specsheet.frozen_imager_inputs = [True, True, True, True, False]

    etendue_inputs = specsheet.etendue_inputs
    ape_key = ('aperture', 'image', 'f/#')
    ape_value = read_float(var_dists['F-Number'][0])
    etendue_inputs[ape_key[0]][ape_key[1]][ape_key[2]] = ape_value

    fld_key = ('field', 'image', 'height')
    fld_value = read_float(var_dists['Image Height'][0]) / 2
    etendue_inputs[fld_key[0]][fld_key[1]][fld_key[2]] = fld_value
    specsheet.generate_from_inputs(imager_inputs, etendue_inputs)

    opt_model = OpticalModel(do_init=True,
                             radius_mode=True,
                             specsheet=specsheet)
    sm = opt_model['sm']
    sm.do_apertures = False
    sm.gaps[0].thi = thi_obj

    osp = opt_model['osp']
    osp['fov'].is_relative = True
    osp['fov'].set_from_list([0., .707, 1.])
    osp['wvls'] = WvlSpec(wlwts=[('F', .5), ('d', 1.), ('C', .5)], ref_wl=1)

    if 'lens data' in inpts:
        input_lines = inpts['lens data']
        _track_contents['# surfs'] = len(input_lines)
        for line in input_lines:
            inpt = []
            inpt.append(read_float(line[1]))  # radius
            inpt.append(read_float(line[2]))  # thi
            if line[3] == '':
                inpt.append('')
                inpt.append('')
            else:
                inpt.append(read_float(line[3]))  # nd
                inpt.append(read_float(line[5]))  # vd
            inpt.append(read_float(line[4]) / 2)  # sd
            sm.add_surface(inpt)
            if line[1] == 'AS':
                sm.set_stop()
    if 'aspherical data' in inpts:
        if 'AsphericalOddCount' in constants:
            typ = 'AsphericalOddCount'
        elif 'AsphericalA2' in constants:
            typ = 'AsphericalA2'
        else:
            typ = 'Aspherical'

        input_lines = inpts['aspherical data']
        _track_contents[typ] = len(input_lines)
        for line in input_lines:
            if typ == 'AsphericalOddCount':
                asp_coefs = [read_float(item) for item in line[3:]]
                asp_coefs = [0., 0.] + asp_coefs
            elif typ == 'AsphericalA2':
                asp_coefs = [read_float(item) for item in line[3:]]
            else:
                asp_coefs = [read_float(item) for item in line[2:]]
                asp_coefs[0] = 0.
            if typ == 'AsphericalOddCount':
                asp = RadialPolynomial(r=read_float(line[1]),
                                       cc=read_float(line[2]),
                                       coefs=asp_coefs)
            else:
                asp = EvenPolynomial(r=read_float(line[1]),
                                     cc=read_float(line[2]),
                                     coefs=asp_coefs)
            idx = int(line[0])
            sm.ifcs[idx].profile = asp

    if 'diffractive data' in inpts:
        input_lines = inpts['diffractive data']
        _track_contents['# doe'] = len(input_lines)
        for line in input_lines:
            coefs = [read_float(item) for item in line[3:]]
            dif_elem = DiffractiveElement(coefficients=coefs,
                                          ref_wl=read_float(line[1]),
                                          order=read_float(line[2]),
                                          phase_fct=doe.radial_phase_fct)
            idx = int(line[0])
            sm.ifcs[idx].phase_element = dif_elem
    if 'descriptive data' in inpts:
        input_lines = inpts['descriptive data']
        descripts = {
            input_line[0]: input_line[1:]
            for input_line in input_lines
        }
        if 'title' in descripts:
            opt_model['sys'].title = descripts['title'][0]

    opt_model.update_model()
    return opt_model