Skip to content

Instantly share code, notes, and snippets.

@n-a-t-e
Last active February 5, 2021 19:18
Show Gist options
  • Save n-a-t-e/25f9b7dcf112a9a11851be70d7def465 to your computer and use it in GitHub Desktop.
Save n-a-t-e/25f9b7dcf112a9a11851be70d7def465 to your computer and use it in GitHub Desktop.
Read an ODF file in Python using rpy2 to interface with R libraries
#!/usr/bin/env python3
"""
Use OCE R package from Python3 to read an ODF file
You will need to install R and libraries 'oce' and 'RJSONIO' by running
the following commands in R. I am using R version 3.6.2
install.packages("devtools")
library(devtools)
install_github("dankelley/oce", ref="develop")
install.packages("RJSONIO", dependencies = TRUE)
library(RJSONIO)
Also, install python packages:
pip install pandas xarray rpy2
"""
import json
import pandas as pd
from rpy2.robjects.packages import importr
oce = importr('oce')
rjsonio = importr('RJSONIO')
def read_odf_py(filename):
"""
interfaces with the the OCE R library by serializing the oce
object to/from json
"""
odf = oce.read_odf(
filename)
json_odf = rjsonio.toJSON(odf)
json_odf_unescaped = str(json_odf).encode(
'utf-8').decode('unicode_escape').replace('[1] "', '')[0:-2]
odf = json.loads(json_odf_unescaped)
return odf
odf_dict = read_odf_py(
'~/dev/odf/odf/CTD_ExampleData_BBMP/2016/CTD_BCD2016667_011_01_DN.ODF')
# load data into Pandas dataframe
df = pd.DataFrame.from_dict(odf_dict['data'])
# metadata is a nested dictionary
metadata = odf_dict['metadata']
print(metadata)
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment