<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Loading, saving and exporting data
Pymrio includes several functions for data reading and storing. This section presents the methods to use for saving and loading data already in a pymrio compatible format. For parsing raw MRIO data see the different tutorials for working with available MRIO databases.
Here, we use the included small test MRIO system to highlight the different function. The same functions are available for any MRIO loaded into pymrio. Expect, however, significantly decreased performance due to the size of real MRIO system.
Step1:  Basic save and read
To save the full system, use
Step2:  To read again from that folder do
Step3:  The fileio activities are stored in the included meta data history field
Step4:  Storage format
Internally, pymrio stores data in csv format, with the 'economic core' data in the root and each satellite account in a subfolder. Metadata as file as a file describing the data format ('file_parameters.json') are included in each folder.
Step5:  The file format for storing the MRIO data can be switched to a binary pickle format with
Step6:  This can be used to reduce the storage space required on the disk for large MRIO databases.
Archiving MRIOs databases
To archive a MRIO system after saving use pymrio.archive
Step7:  Data can be read directly from such an archive by
Step8:  Currently data can not be saved directly into a zip archive.
It is, however, possible to remove the source files after archiving
Step9:  Several MRIO databases can be stored in the same archive
Step10:  When loading from an archive which includes multiple MRIO databases, specify
one with the parameter 'path_in_arc'
Step11:  The pymrio.load function can be used directly to only a specific satellite account 
of a MRIO database from a zip archive
Step12:  The archive function is a wrapper around python.zipfile module.
There are, however, some differences to the defaults choosen in the original
Step13:  This can then be loaded again as separate satellite account
Step14:  As all data in pymrio is stored as pandas DataFrame, the full pandas stack for exporting tables is available. For example, to export a table as excel sheet use
<ASSISTANT_TASK:>
Python Code:
import pymrio
import os
io = pymrio.load_test().calc_all()
Explanation: Loading, saving and exporting data
Pymrio includes several functions for data reading and storing. This section presents the methods to use for saving and loading data already in a pymrio compatible format. For parsing raw MRIO data see the different tutorials for working with available MRIO databases.
Here, we use the included small test MRIO system to highlight the different function. The same functions are available for any MRIO loaded into pymrio. Expect, however, significantly decreased performance due to the size of real MRIO system.
End of explanation
save_folder_full = '/tmp/testmrio/full'
io.save_all(path=save_folder_full)
Explanation: Basic save and read
To save the full system, use:
End of explanation
io_read = pymrio.load_all(path=save_folder_full)
Explanation: To read again from that folder do:
End of explanation
io_read.meta
Explanation: The fileio activities are stored in the included meta data history field:
End of explanation
import os
os.listdir(save_folder_full)
Explanation: Storage format
Internally, pymrio stores data in csv format, with the 'economic core' data in the root and each satellite account in a subfolder. Metadata as file as a file describing the data format ('file_parameters.json') are included in each folder.
End of explanation
save_folder_bin = '/tmp/testmrio/binary'
io.save_all(path=save_folder_bin, table_format='pkl')
os.listdir(save_folder_bin)
Explanation: The file format for storing the MRIO data can be switched to a binary pickle format with:
End of explanation
mrio_arc = '/tmp/testmrio/archive.zip'
# Remove a potentially existing archive from before
try:
    os.remove(mrio_arc)
except FileNotFoundError:
    pass
    
pymrio.archive(source=save_folder_full, archive=mrio_arc)
Explanation: This can be used to reduce the storage space required on the disk for large MRIO databases.
Archiving MRIOs databases
To archive a MRIO system after saving use pymrio.archive:
End of explanation
tt = pymrio.load_all(mrio_arc)
Explanation: Data can be read directly from such an archive by:
End of explanation
tmp_save = '/tmp/testmrio/tmp'
# Remove a potentially existing archive from before
try:
    os.remove(mrio_arc)
except FileNotFoundError:
    pass
io.save_all(tmp_save)
print("Directories before archiving: {}".format(os.listdir('/tmp/testmrio')))
pymrio.archive(source=tmp_save, archive=mrio_arc, remove_source=True)
print("Directories after archiving: {}".format(os.listdir('/tmp/testmrio')))
Explanation: Currently data can not be saved directly into a zip archive.
It is, however, possible to remove the source files after archiving:
End of explanation
# Remove a potentially existing archive from before
try:
    os.remove(mrio_arc)
except FileNotFoundError:
    pass
tmp_save = '/tmp/testmrio/tmp'
io.save_all(tmp_save)
pymrio.archive(source=tmp_save, archive=mrio_arc, path_in_arc='version1/', remove_source=True)
io2 = io.copy()
del io2.emissions
io2.save_all(tmp_save)
pymrio.archive(source=tmp_save, archive=mrio_arc, path_in_arc='version2/', remove_source=True)
Explanation: Several MRIO databases can be stored in the same archive:
End of explanation
io1_load = pymrio.load_all(mrio_arc, path_in_arc='version1/')
io2_load = pymrio.load_all(mrio_arc, path_in_arc='version2/')
print("Extensions of the loaded io1 {ver1} and of io2: {ver2}".format(
     ver1=sorted(io1_load.get_extensions()),
     ver2=sorted(io2_load.get_extensions())))
Explanation: When loading from an archive which includes multiple MRIO databases, specify
one with the parameter 'path_in_arc':
End of explanation
emissions = pymrio.load(mrio_arc, path_in_arc='version1/emissions')
print(emissions)
Explanation: The pymrio.load function can be used directly to only a specific satellite account 
of a MRIO database from a zip archive:
End of explanation
save_folder_em= '/tmp/testmrio/emissions'
io.emissions.save(path=save_folder_em)
Explanation: The archive function is a wrapper around python.zipfile module.
There are, however, some differences to the defaults choosen in the original:
In contrast to zipfile.write, 
   pymrio.archive raises an
   error if the data (path + filename) are identical in the zip archive.
   Background: the zip standard allows that files with the same name and path
   are stored side by side in a zip file. This becomes an issue when unpacking
   this files as they overwrite each other upon extraction.
The standard for the parameter 'compression' is set to ZIP_DEFLATED 
   This is different from the zipfile default (ZIP_STORED) which would
   not give any compression. 
   See the zipfile docs 
   for further information. 
   Depending on the value given for the parameter 'compression' 
   additional modules might be necessary (e.g. zlib for ZIP_DEFLATED). 
   Futher information on this can also be found in the zipfile python docs.
Storing or exporting a specific table or extension
Each extension of the MRIO system can be stored separetly with:
End of explanation
emissions = pymrio.load(save_folder_em)
emissions
emissions.D_cba
Explanation: This can then be loaded again as separate satellite account:
End of explanation
io.emissions.D_cba.to_excel('/tmp/testmrio/emission_footprints.xlsx')
Explanation: As all data in pymrio is stored as pandas DataFrame, the full pandas stack for exporting tables is available. For example, to export a table as excel sheet use:
End of explanation
<END_TASK>