Export the lcopt model as a SimaPro csv file.

        The file will be called "<ModelName>_database_export.csv"

def database_to_SimaPro_csv(self):

        """
        Export the lcopt model as a SimaPro csv file.

        The file will be called "<ModelName>_database_export.csv"
        """
        self.parameter_scan()

        csv_args = {}
        csv_args['processes'] = []
        db = self.database['items']
        
        product_filter = lambda x: db[x]['type'] == 'product'
        process_filter = lambda x: db[x]['type'] == 'process'

        processes = list(filter(process_filter, db))
        products = list(filter(product_filter, db))

        created_exchanges = []

        project_input_params = []
        project_calc_params = []

        for k in processes:
            item = db[k]

            current = {}
            current['name'] = item['name']
            current['id'] = (self.name.replace(" ", "") + "XXXXXXXX")[:8] + ('00000000000' + str(randint(1, 99999999999)))[-11:]
            current['unit'] = item['unit']
            current['exchanges'] = []
            
            process_params = []
            
            production_filter = lambda x: x['type'] == 'production'
            
            output_code = list(filter(production_filter, item['exchanges']))[0]['input'][1]
            
            for e in item['exchanges']:

                if e['type'] == 'technosphere':
                    this_exchange = {}

                    this_code = e['input'][1]

                    formatted_name = self.get_name(this_code)
                    this_exchange['formatted_name'] = formatted_name
                    
                    param_key = (this_code,  output_code)
                    #print(param_key)
                    #param_check = (formatted_name, item['name'])
                    this_param = self.parameter_map[param_key]
                    
                    process_params.append(this_param)
                   
                    this_exchange['amount'] = this_param 

                    this_exchange['unit'] = self.get_unit(this_code)

                    current['exchanges'].append(this_exchange)

                elif e['type'] == 'production':
                    this_code = e['input'][1]
                    name = self.get_name(this_code)
                    current['output_name'] = name

                    created_exchanges.append(name)
                    
                # process parameters
                
            for p in process_params:
                if self.params[p]['function'] is None:
                    project_input_params.append({'name': p, 'comment': self.params[p]['description']})
                else:
                    project_calc_params.append({'name': p, 'comment': self.params[p]['description'], 'formula': self.params[p]['function']})

            csv_args['processes'].append(current)
            
        for k in products:
            this_item = db[k]
            this_name = this_item['name']
            
            if this_item['name'] in created_exchanges:
                #print ('{} already created'.format(this_name))
                pass
            else:
                #print ('Need to create {}'.format(this_name))
                
                current = {}
                current['name'] = this_name
                current['output_name'] = this_name
                current['id'] = (self.name.replace(" ", "") + "XXXXXXXX")[:8] + ('00000000000' + str(randint(1, 99999999999)))[-11:]
                current['unit'] = this_item['unit']
                #current['exchanges'] = []
                
                if 'ext_link' in this_item.keys():
                    
                    ext_link = this_item['ext_link']
                    if ext_link[0] != self.database['name']:
                        db_filter = lambda x: x['name'] == ext_link[0]
                        extdb = list(filter(db_filter, self.external_databases))[0]['items']

                        ext_item = extdb[ext_link]
                        if ext_link[0] != self.biosphereName:
                            ref_prod = ext_item['reference product']
                            name = ext_item['name'].replace(" " + ref_prod, "")
                            location = ext_item['location']
                            system_model = "Alloc Def"
                            process_type = "U"
                            unit = unnormalise_unit(ext_item['unit'])

                            simaPro_name = "{} {{{}}}| {} | {}, {}".format(ref_prod.capitalize(), location, name, system_model, process_type)

                            #print ('{} has an external link to {}'.format(this_name, simaPro_name))
                            
                            current['exchanges'] = [{'formatted_name': simaPro_name, 'unit': unit, 'amount': 1}]
                        else:
                            #print('{} has a biosphere exchange - need to sort this out'.format(this_name))
                            #print(ext_item)
                            unit = unnormalise_unit(ext_item['unit'])
                            formatted_name = ext_item['name']

                            if 'air' in ext_item['categories']:
                                current['air_emissions'] = [{'formatted_name': formatted_name, 'subcompartment': '', 'unit': unit, 'amount': 1, 'comment': 'emission of {} to air'.format(formatted_name)}]

                            elif 'water' in ext_item['categories']:
                                current['water_emissions'] = [{'formatted_name': formatted_name, 'subcompartment': '', 'unit': unit, 'amount': 1, 'comment': 'emission of {} to water'.format(formatted_name)}]

                            elif 'soil' in ext_item['categories']:
                                current['soil_emissions'] = [{'formatted_name': formatted_name, 'subcompartment': '', 'unit': unit, 'amount': 1, 'comment': 'emission of {} to soil'.format(formatted_name)}]

                            else:
                                print('{} has a biosphere exchange that isnt to air water or soil')
                                print(ext_item)
                        
                    else:
                        warnings.warn('{} has NO internal or external link - it is burden free'.format(this_name))
                    
                csv_args['processes'].append(current)
                created_exchanges.append(this_name)
            
        #print(csv_args)
        #print(created_exchanges)
        
        csv_args['project'] = {}
        
        #NOTE - currently external parameters can only be constants

        csv_args['project']['calculated_parameters'] = project_calc_params
        
        #add the external parameters to the input parameter list        
        for p in self.ext_params:
            project_input_params.append({'name': p['name'], 'comment': p['description'], 'default': p['default']})
        
        csv_args['project']['input_parameters'] = project_input_params

        #print (csv_args)

        env = Environment(
            loader=PackageLoader('lcopt', 'templates'),
        )

        filename = "{}_database_export.csv".format(self.name.replace(" ", "_"))

        csv_template = env.get_template('export.csv')
        
        output = csv_template.render(**csv_args)

        if self.save_option == 'curdir':
            base_dir = os.getcwd()
        else:
            base_dir = os.path.join(storage.simapro_dir, self.name.replace(" ", "_"))

        if not os.path.isdir(base_dir):
            os.mkdir(base_dir)

        efn = os.path.join(base_dir, filename)

        with open(efn, "w") as f:
            f.write(output)
        
        return efn