from pathlib import Path
import pygaps.parsing as pgp

# Get directory paths
base_path = Path.cwd() / 'data' / 'parsing'

# Find files
aif_file_paths = list((base_path / 'aif').rglob('*.aif'))
json_file_paths = list((base_path / 'json').rglob('*.json'))
xl_file_paths = list((base_path / 'excel').rglob('*.xls'))
csv_file_paths = list((base_path / 'csv').rglob('*.csv'))

"""
Explanation: Parsing examples
Some examples on parsing to and from supported formats. More info about all
parsing methods can be found in the manual section.
Declare paths
First, let's do all the necessary imports and generate the paths that we'll use
for file import and export.
End of explanation
"""


cfld = base_path / "commercial"
micromeritics = pgp.isotherm_from_commercial(cfld / "mic" / "Sample_A.xls", 'mic', 'xl')
belsorp_dat = pgp.isotherm_from_commercial(cfld / "bel" / "BF010_DUT-13_CH4_111K_run2.DAT", 'bel', 'dat')
belsorp_xl = pgp.isotherm_from_commercial(cfld / "bel" / "Sample_C.xls", 'bel', 'xl')
threeP_xl = pgp.isotherm_from_commercial(cfld / "3p" / "AC_ref_filter_Ar_87K_run 3_rep.xlsx", '3p', 'xl')

"""
Explanation: Manufacturer import
Many report files from various adsorption device manufacturers can be imported
directly using pyGAPS. Here are some examples.
End of explanation
"""


# Import all
isotherms = [pgp.isotherm_from_aif(path) for path in aif_file_paths]

# Display an example file
print(isotherms[1])

"""
Explanation: AIF Parsing
AIF Import
Adsorption information files are fully supported in pyGAPS, both for import and
exports. Isotherms can be imported from an .aif as:
End of explanation
"""


# module function
for isotherm in isotherms:
    filename = f'{isotherm.material} {isotherm.adsorbate} {isotherm.temperature}.aif'
    pgp.isotherm_to_aif(isotherm, base_path / 'aif' / filename)

# save to file with convenience function
isotherms[0].to_aif('isotherm.aif')

# string
isotherm_string = isotherms[0].to_aif()

"""
Explanation: AIF Export
Similarly, an isotherm can be exported as an AIF file or a string, depending on
whether a path is passed. For this purpose use either the module
pygaps.isotherm_to_aif() function or the convenience class function
to_aif().
End of explanation
"""


# Import them
isotherms = [pgp.isotherm_from_json(path) for path in json_file_paths]

# Display an example file
print(isotherms[1])

"""
Explanation: JSON Parsing
JSON Import
Isotherms can be imported either from a json file or from a json string. The
same function is used in both cases.
End of explanation
"""


# module function
for isotherm in isotherms:
    filename = f'{isotherm.material} {isotherm.adsorbate} {isotherm.temperature}.json'
    pgp.isotherm_to_json(isotherm, base_path / 'json' / filename)

# save to file with convenience function
isotherms[0].to_json('isotherm.json')

# string
isotherm_string = isotherms[0].to_json()

"""
Explanation: JSON Export
Exporting to JSON can be done to a file or a string, depending on whether a path
is passed. For this purpose use either the module pygaps.isotherm_to_json()
function or the convenience class function to_json().
End of explanation
"""


# Import them
isotherms = [pgp.isotherm_from_xl(path) for path in xl_file_paths]

# Display an example file
print(isotherms[1])

isotherms[1].plot()

"""
Explanation: Excel Parsing
Excel does not have to be installed on the system in use.
Excel Import
End of explanation
"""


# Export each isotherm in turn
for isotherm in isotherms:
    filename = ' '.join([str(isotherm.material), str(isotherm.adsorbate), str(isotherm.temperature)]) + '.xls'
    pgp.isotherm_to_xl(isotherm, base_path / 'excel' / filename)

# save to file with convenience function
isotherms[0].to_xl('isotherm.xls')

"""
Explanation: Excel Export
End of explanation
"""


# Import them
isotherms = [pgp.isotherm_from_csv(path) for path in csv_file_paths]

# Display an example file
print(isotherms[0])

"""
Explanation: CSV Parsing
CSV Import
Like JSON, isotherms can be imported either from a CSV file or from a CSV string. The same function is used in both cases.
End of explanation
"""


# Export each isotherm in turn
for isotherm in isotherms:
    filename = ' '.join([str(isotherm.material), str(isotherm.adsorbate), str(isotherm.temperature)]) + '.csv'
    pgp.isotherm_to_csv(isotherm, base_path / 'csv' / filename)

# save to file with convenience function
isotherms[0].to_csv('isotherm.csv')

# string representation
isotherm_string = isotherms[0].to_csv()

"""
Explanation: CSV Export
End of explanation
"""


