def write_to_template(parvals,tpl_file,in_file):
    """ write parameter values to model input files using template files

    Parameters
    ----------
    parvals : dict or pandas.Series
        a way to look up parameter values using parameter names
    tpl_file : str
        template file
    in_file : str
        input file

    """
    f_in = open(in_file,'w')
    f_tpl = open(tpl_file,'r')
    header = f_tpl.readline().strip().split()
    assert header[0].lower() in ["ptf", "jtf"], \
        "template file error: must start with [ptf,jtf], not:" + \
        str(header[0])
    assert len(header) == 2, \
        "template file error: header line must have two entries: " + \
        str(header)

    marker = header[1]
    assert len(marker) == 1, \
        "template file error: marker must be a single character, not:" + \
        str(marker)
    for line in f_tpl:
        if marker not in line:
            f_in.write(line)
        else:
            line = line.rstrip()
            par_names = line.lower().split(marker)[1::2]
            par_names = [name.strip() for name in par_names]
            start,end = get_marker_indices(marker,line)
            assert len(par_names) == len(start)
            new_line = line[:start[0]]
            between = [line[e:s] for s,e in zip(start[1:],end[:-1])]
            for i,name in enumerate(par_names):
                s,e = start[i],end[i]
                w = e - s
                if w > 15:
                    d = 6
                else:
                    d = 3
                fmt = "{0:" + str(w)+"."+str(d)+"E}"
                val_str = fmt.format(parvals[name])
                new_line += val_str
                if i != len(par_names) - 1:
                    new_line += between[i]
            new_line += line[end[-1]:]
            f_in.write(new_line+'\n')
    f_tpl.close()
    f_in.close()
def write_to_template ( parvals , tpl_file , in_file ) : f_in = open ( in_file , 'w' ) f_tpl = open ( tpl_file , 'r' ) header = f_tpl . readline ( ) . strip ( ) . split ( ) assert header [ 0 ] . lower ( ) in [ "ptf" , "jtf" ] , "template file error: must start with [ptf,jtf], not:" + str ( header [ 0 ] ) assert len ( header ) == 2 , "template file error: header line must have two entries: " + str ( header ) marker = header [ 1 ] assert len ( marker ) == 1 , "template file error: marker must be a single character, not:" + str ( marker ) for line in f_tpl : if marker not in line : f_in . write ( line ) else : line = line . rstrip ( ) par_names = line . lower ( ) . split ( marker ) [ 1 : : 2 ] par_names = [ name . strip ( ) for name in par_names ] start , end = get_marker_indices ( marker , line ) assert len ( par_names ) == len ( start ) new_line = line [ : start [ 0 ] ] between = [ line [ e : s ] for s , e in zip ( start [ 1 : ] , end [ : - 1 ] ) ] for i , name in enumerate ( par_names ) : s , e = start [ i ] , end [ i ] w = e - s if w > 15 : d = 6 else : d = 3 fmt = "{0:" + str ( w ) + "." + str ( d ) + "E}" val_str = fmt . format ( parvals [ name ] ) new_line += val_str if i != len ( par_names ) - 1 : new_line += between [ i ] new_line += line [ end [ - 1 ] : ] f_in . write ( new_line + '\n' ) f_tpl . close ( ) f_in . close ( )
write parameter values to model input files using template files

    Parameters
    ----------
    parvals : dict or pandas.Series
        a way to look up parameter values using parameter names
    tpl_file : str
        template file
    in_file : str
        input file