generate simulation files for lattice analysis,
            e.g. ".lte" for elegant, ".madx" for madx

            input parameters:
            :param beamline: keyword for beamline
            :param filename: name of lte/mad file,
                if None, output to stdout;
                if 'sio', output to a string as return value;
                other cases, output to filename;
            :param format: madx, elegant,
                'elegant' by default, generated lattice is for elegant tracking

def generateLatticeFile(self, beamline, filename=None, format='elegant'):
        """ generate simulation files for lattice analysis,
            e.g. ".lte" for elegant, ".madx" for madx

            input parameters:
            :param beamline: keyword for beamline
            :param filename: name of lte/mad file,
                if None, output to stdout;
                if 'sio', output to a string as return value;
                other cases, output to filename;
            :param format: madx, elegant,
                'elegant' by default, generated lattice is for elegant tracking
        """

        """
        if not self.isBeamline(beamline):
            print("%s is a valid defined beamline, do not process." % (beamline))
            return False
        """

        if filename is None:
            f = sys.stdout
        elif filename == 'sio':
            f = StringIO()
        else:
            f = open(os.path.expanduser(filename), 'w')

        # write filehead, mainly resolving prefix string lines
        cl1 = "This file is automatically generated by 'generateLatticeFile()' method,"
        cl2 = 'could be used as ' + format + ' lattice file.'
        cl3 = 'Author: Tong Zhang (zhangtong@sinap.ac.cn)'
        cl4 = 'Generated Date: ' + time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime())

        f.write('!{str1:<73s}!\n'.format(str1='-' * 73))
        f.write('!{str1:^73s}!\n'.format(str1=cl1))
        f.write('!{str1:^73s}!\n'.format(str1=cl2))
        f.write('!{str1:^73s}!\n'.format(str1='-' * 24))
        f.write('!{str1:^73s}!\n'.format(str1=cl3))
        f.write('!{str1:^73s}!\n'.format(str1=cl4))
        f.write('!{str1:<73s}!\n'.format(str1='-' * 73))
        f.write('\n')

        """ do not need to dump stoed variables now, 2016-03-21
        # write global variables
        f.write('! {str1:<73s}\n'.format(str1= 'Global variable definitions:'))
        f.write('\n'.join(self.all_elements['_prefixstr']))
        f.write('\n')
        f.write('\n')
        """

        # write EPICS control configuration part if contains '_epics' key
        if '_epics' in self.all_elements:
            f.write('! {str1:<73s}\n'.format(str1='EPICS control definitions:'))
            for k, v in self.all_elements['_epics'].items():
                f.write('!!epics {k:<10s}:{v:>50s}\n'.format(k=k, v=json.dumps(v)))
            f.write('\n')

        # write element definitions and lattice
        f.write('! {str1:<72s}\n'.format(str1='Element definitions:'))
        elelist = self.getFullBeamline(beamline, extend=True)
        if self.getElementType(elelist[0]) != 'CHARGE':
            elelist.insert(0, self.getChargeElement())
        for ele in sorted(set(elelist)):
            elestring = self.rinseElement(ele)['name']
            f.write(self.formatElement(elestring, format='elegant') + '\n')

        # write beamline lattice definition
        f.write('\n')
        f.write('! {str1:<72s}\n'.format(str1='Beamline definitions:'))
        f.write('{bl:<10s}: line = ({lattice})'.format(bl=beamline.upper(),
                                                       lattice=', '.join(elelist)))
        if filename == 'sio':
            retval = f.getvalue()
        else:
            retval = True
        f.close()

        # if everything's OK, return True or string ('sio') mode
        return retval