Exemple #1
0
def plexos_update(plexos_db,
                  fuel_objects,
                  base_scenarios,
                  gas_scenario,
                  project_name,
                  prices=None):

    # Connect to the PLEXOS input dataset
    cxn = DatabaseCore()
    cxn.DisplayAlerts = False
    cxn.Connection(plexos_db)
    cxn.DataSource = plexos_db

    # price updates
    if plexos_update_prices(cxn, fuel_objects, gas_scenario, prices):
        print('Updated PLEXOS gas prices')
    else:
        print('No update for PLEXOS gas prices')

    # update project and models
    if plexos_update_project(cxn, project_name, base_scenarios, gas_scenario):
        print('Updated PLEXOS project and models')
    else:
        print('No update to PLEXOS project and models')

    # close and save
    cxn.Close()
def modify_model_horizon(datafile, modelname, start):

    tempfile = '{}{:%m%d%Y%H%M%S}.xml'.format(datafile[:-4], dt.datetime.now())

    if not os.path.exists(datafile):
        raise Exception('"{}" does not exist'.format(datafile))

    # delete the modified file if it already exists
    if os.path.exists(tempfile):
        os.remove(tempfile)

    # copy the PLEXOS input file
    copyfile(datafile, tempfile)

    # Create an object to store the input data
    db = DatabaseCore()
    db.Connection(tempfile)
    '''
    String[] GetChildMembers(
    	CollectionEnum nCollectionId,
    	String strParent
    	)
    '''
    getchildren = db.GetChildMembers[CollectionEnum, String]
    '''
    Boolean UpdateAttribute(
    	ClassEnum nClassId,
    	String strObjectName,
    	Int32 nAttributeEnum,
    	Double dNewValue <--- always a Double... need to convert DateTime to Double
                         <--- using the ToOADate() method of the DateTime class
    	)
    '''

    # aliases for UpdateAttribute and AddAttribute methods
    update = db.UpdateAttribute[ClassEnum, String, Int32, Double]
    add = db.AddAttribute[ClassEnum, String, Int32, Double]

    horizons = getchildren.__invoke__((CollectionEnum.ModelHorizon, modelname))
    for hor in horizons:
        update.__invoke__(
            (ClassEnum.Horizon, hor, int(HorizonAttributeEnum.DateFrom),
             DateTime(start.Year, 1, 1).ToOADate()))
        update.__invoke__(
            (ClassEnum.Horizon, hor, int(HorizonAttributeEnum.StepType), 4))
        update.__invoke__(
            (ClassEnum.Horizon, hor, int(HorizonAttributeEnum.ChronoDateFrom),
             start.ToOADate()))

    # save the data set
    db.Close()

    # return the path of the newly created plexos input file
    return tempfile
                'Load Participation Factor', 1)
add_plexos_prop(db, ClassEnum.System, ClassEnum.Node,
                CollectionEnum.SystemNodes, 'System', 'B',
                'Load Participation Factor', 1)

# One line
add_plexos_prop(db, ClassEnum.System, ClassEnum.Line,
                CollectionEnum.SystemLines, 'System', 'AB', 'Max Flow', 90)

# Memberships
'''
Int32 AddMembership(
	CollectionEnum nCollectionId,
	String strParent,
	String strChild
	)
'''
db.AddMembership(CollectionEnum.GeneratorNodes, 'A1', 'A')
db.AddMembership(CollectionEnum.GeneratorNodes, 'A2', 'A')
db.AddMembership(CollectionEnum.GeneratorNodes, 'B1', 'B')
db.AddMembership(CollectionEnum.GeneratorNodes, 'B2', 'B')

db.AddMembership(CollectionEnum.NodeRegion, 'A', 'A')
db.AddMembership(CollectionEnum.NodeRegion, 'B', 'B')

db.AddMembership(CollectionEnum.LineNodeFrom, 'AB', 'A')
db.AddMembership(CollectionEnum.LineNodeTo, 'AB', 'B')

# save the data set
db.Close()
def create_datafile_object(plexosfile, datafilename, datafilepath, copyfileto=''):
    if not os.path.exists(plexosfile):
        raise Exception('"{}" does not exist'.format(plexosfile))
        
    if not copyfileto == '':
        if os.path.exists(copyfileto):
            os.remove(copyfileto)
        copyfile(plexosfile, copyfileto)
        plexosfile = copyfileto
        
    # Create an object to store the input data
    db = DatabaseCore()
    db.Connection(plexosfile)

    # Add a scenario
    '''
    Int32 AddObject(
    	String strName,
    	ClassEnum nClassId,
    	Boolean bAddSystemMembership,
    	String strCategory[ = None],
    	String strDescription[ = None]
    	)
    '''
    db.AddObject(datafilename, ClassEnum.DataFile, True)

    # Create data and tag it with the scenario
    '''
    Int32 AddProperty(
    	Int32 MembershipId,
    	Int32 EnumId,
    	Int32 BandId,
    	Double Value,
    	Object DateFrom,
    	Object DateTo,
    	Object Variable,
    	Object DataFile,
    	Object Pattern,
    	Object Scenario,
    	Object Action,
    	PeriodEnum PeriodTypeId
    	)
    '''
    # alias
    add_prop = db.AddProperty[Int32,Int32,Int32,Double,Object,Object, \
                              Object,Object,Object,Object,Object,PeriodEnum]
    
    # parameters
    mem_id = db.GetMembershipID(CollectionEnum.SystemDataFiles, 'System', datafilename)
    enum_id = int(SystemDataFilesEnum.Filename) 
    
    # we'll add three property rows... monthly gas prices for 3 months
    params = [(mem_id, enum_id, 1, 0, None, None, None, datafilepath, None, None, None, PeriodEnum.Interval),]
    
    # invoke
    for p in params:
        add_prop.__invoke__(p)
    
    # Add the scenario to a model
    '''
    Int32 AddMembership(
    	CollectionEnum nCollectionId,
    	String strParent,
    	String strChild
    	)
    db.AddMembership(CollectionEnum.ModelScenarios, 'Q1 DA', scenario)
    '''
    
    # save the data set
    db.Close()
    
    # pass the plexos file path (.xml) whether copied or not
    return plexosfile
def attach_datafile_to_object(plexosfile, datafilename, collectionenum, propertyenum, objectname, parentname = 'System'):
    if not os.path.exists(plexosfile):
        raise Exception('"{}" does not exist'.format(plexosfile))
        
    # Create an object to store the input data
    db = DatabaseCore()
    db.Connection(plexosfile)

    '''
    Recordset GetPropertiesTable(
    	CollectionEnum CollectionId,
    	String ParentNameList[ = None],
    	String ChildNameList[ = None],
    	String TimesliceList[ = None],
    	String ScenarioList[ = None],
    	String CategoryList[ = None]
    	)
    '''
    res = db.GetPropertiesTable(collectionenum, parentname, objectname)
    
    '''
    Int32 RemoveProperty(
	Int32 MembershipId,
	Int32 EnumId,
	Int32 BandId,
	Object DateFrom,
	Object DateTo,
	Object Variable,
	Object DataFile,
	Object Pattern,
	Object Scenario,
	Object Action,
	PeriodEnum PeriodTypeId
	)
    '''
    mem_id = db.GetMembershipID(collectionenum, parentname, objectname)
    enum_id = int(propertyenum) 

    remove_prop = db.RemoveProperty[Int32, Int32, Int32, Object, Object, Object, Object, \
                                    Object, Object, Object, PeriodEnum]
    while not res.EOF:
        fields = dict([(res.Fields[i].Name.replace('_x0020_', ''), res.Fields[i].Value) for i in range(res.Fields.Count)])
        if fields['Property'] == str(propertyenum):
            remove_prop.__invoke__((mem_id, enum_id, \
                                   1 if 'Band' not in fields else int(fields['Band']), \
                                    None if 'DateFrom' not in fields else fields['DateFrom'], \
                                    None if 'DateTo' not in fields else fields['DateTo'], \
                                    None if 'Variable' not in fields else fields['Variable'], \
                                    None if 'DataFile' not in fields else fields['DataFile'], \
                                    None if 'Pattern' not in fields else fields['Pattern'], \
                                    None if 'Scenario' not in fields else fields['Scenario'], \
                                   '=' if 'Action' not in fields else fields['Action'], \
                                   PeriodEnum.Interval))
        print fields
        
        res.MoveNext()
    
    
    
    # Create data and tag it with the scenario
    '''
    Int32 AddProperty(
    	Int32 MembershipId,
    	Int32 EnumId,
    	Int32 BandId,
    	Double Value,
    	Object DateFrom,
    	Object DateTo,
    	Object Variable,
    	Object DataFile,
    	Object Pattern,
    	Object Scenario,
    	Object Action,
    	PeriodEnum PeriodTypeId
    	)
    '''
    # alias
    add_prop = db.AddProperty[Int32,Int32,Int32,Double,Object,Object, \
                              Object,Object,Object,Object,Object,PeriodEnum]
    
    # parameters
    mem_id = db.GetMembershipID(collectionenum, parentname, objectname)
    enum_id = int(propertyenum) 
    
    # we'll add three property rows... monthly gas prices for 3 months
    params = [(mem_id, enum_id, 1, 0, None, None, None, datafilename, None, None, None, PeriodEnum.Interval),]
    
    # invoke
    for p in params:
        add_prop.__invoke__(p)
    
    # Add the scenario to a model
    '''
    Int32 AddMembership(
    	CollectionEnum nCollectionId,
    	String strParent,
    	String strChild
    	)
    db.AddMembership(CollectionEnum.ModelScenarios, 'Q1 DA', scenario)
    '''
    
    # save the data set
    db.Close()