iiasa / ixmp

The ix modeling platform for integrated and cross-cutting scenario analysis
https://docs.messageix.org/ixmp
Apache License 2.0
37 stars 110 forks source link

Defining regions and changing their attributes #438

Open OFR-IIASA opened 2 years ago

OFR-IIASA commented 2 years ago

In order to add regions, the function add_region() is used. The respective region name, hierarchy and parent can be defined. If a synonym should be added for a given region, then this must be done using add_region_synonym(). The key is though that the definition of the synonym must be done prior to adding the region.

This can be replicated by carrying out the following code snippet:

import message_ix
import ixmp

mp = ixmp.Platform(name="<database_a>")
mp2 = ixmp.Platform(name="<database_b>")

 # Copy units
for u in mp.units():
    mp2.add_unit(u)

df = mp.regions()
for i in df.index:
    mp2.add_region(region=df.iloc[i].region,
                              hierarchy=df.iloc[i].hierarchy,
                              parent=df.iloc[i].parent)
    if df.iloc[i].mapped_to is not None:
        mp2.add_region_synonym(region=df.iloc[i].region,
                                                 mapped_to=df.iloc[i].mapped_to)

The correct way of doing this:

import message_ix
import ixmp

mp = ixmp.Platform(name="<database_a>")
mp2 = ixmp.Platform(name="<database_b>")

 # Copy units
for u in mp.units():
    mp2.add_unit(u)

df = mp.regions()
for i in df.index:
    if df.iloc[i].mapped_to is not None:
        mp2.add_region_synonym(region=df.iloc[i].region,
                                                 mapped_to=df.iloc[i].mapped_to)
    mp2.add_region(region=df.iloc[i].region,
                              hierarchy=df.iloc[i].hierarchy,
                              parent=df.iloc[i].parent)

Note, there is not way of modifying and existing entry using the python api.