8. Examples
Examples showing how to simply benefit from cpforager package with you biologging data.
8.1. GPS
# ======================================================= #
# LIBRARIES
# ======================================================= #
import os
import pandas as pd
from cpforager import parameters, utils, GPS
# ======================================================= #
# DIRECTORIES
# ======================================================= #
root_dir = os.getcwd()
data_dir = os.path.join(root_dir, "data")
test_dir = os.path.join(root_dir, "tests")
# ======================================================= #
# PARAMETERS
# ======================================================= #
# set metadata
fieldwork = "BRA_FDN_2016_09"
colony = "BRA_FDN_MEI"
file_name = "BRA_FDN_MEI_2016-09-15_SSUL_01_T32840_NA_GPS_IGU120_BR023_LOC.csv"
file_id = file_name.replace(".csv", "")
file_path = os.path.join(data_dir, fieldwork, file_name)
# get parameters dictionaries
plot_params = parameters.get_plot_params()
params = parameters.get_params(colony)
# ======================================================= #
# BUILD GPS OBJECT
# ======================================================= #
# load raw data
df = pd.read_csv(file_path, sep=",")
# add a "datetime" column of type datetime64
df["datetime"] = pd.to_datetime(df["date"] + " " + df["time"], format="mixed", dayfirst=False)
# if time is at UTC, convert it to local datetime
if "_UTC" in file_name: df = utils.convert_utc_to_loc(df, params.get("local_tz"))
# build GPS object (df must have "datetime", "longitude" and "latitude" columns)
gps = GPS(df=df, group=fieldwork, id=file_id, params=params)


8.2. TDR
# ======================================================= #
# LIBRARIES
# ======================================================= #
import os
import pandas as pd
from cpforager import parameters, utils, TDR
# ======================================================= #
# DIRECTORIES
# ======================================================= #
root_dir = os.getcwd()
data_dir = os.path.join(root_dir, "data")
test_dir = os.path.join(root_dir, "tests")
# ======================================================= #
# PARAMETERS
# ======================================================= #
# set metadata
fieldwork = "BRA_FDN_2022_04"
colony = "BRA_FDN_MEI"
file_name = "BRA_FDN_MEI_2022-04-26_SDAC_01_U61556_F_TDR_G5_RT10_UTC.csv"
file_id = file_name.replace(".csv", "")
file_path = os.path.join(data_dir, fieldwork, file_name)
# get parameters dictionaries
plot_params = parameters.get_plot_params()
params = parameters.get_params(colony)
# ======================================================= #
# BUILD TDR OBJECT
# ======================================================= #
# load raw data
df = pd.read_csv(file_path, sep=",")
# add a "datetime" column of type datetime64
df["datetime"] = pd.to_datetime(df["date"] + " " + df["time"], format="mixed", dayfirst=False)
# if time is at UTC, convert it to local datetime
if "_UTC" in file_name: df = utils.convert_utc_to_loc(df, params.get("local_tz"))
# build TDR object (df must have "datetime", "pressure" and "temperature" columns)
tdr = TDR(df=df, group=fieldwork, id=file_id, params=params)

8.3. AXY
# ======================================================= #
# LIBRARIES
# ======================================================= #
import os
import pandas as pd
from cpforager import parameters, utils, AXY
# ======================================================= #
# DIRECTORIES
# ======================================================= #
root_dir = os.getcwd()
data_dir = os.path.join(root_dir, "data")
test_dir = os.path.join(root_dir, "tests")
# ======================================================= #
# PARAMETERS
# ======================================================= #
# set metadata
fieldwork = "BRA_FDN_2022_04"
colony = "BRA_FDN_MEI"
file_name = "BRA_FDN_MEI_2022-04-26_SDAC_01_U61556_F_GPS_AXY_RT10_UTC.csv"
file_id = file_name.replace(".csv", "")
file_path = os.path.join(data_dir, fieldwork, file_name)
# get parameters dictionaries
plot_params = parameters.get_plot_params()
params = parameters.get_params(colony)
# ======================================================= #
# BUILD AXY OBJECT
# ======================================================= #
# load raw data
df = pd.read_csv(file_path, sep=",")
# add a "datetime" column of type datetime64
df["datetime"] = pd.to_datetime(df["date"] + " " + df["time"], format="%Y-%m-%d %H:%M:%S.%f", dayfirst=False)
# if time is at UTC, convert it to local datetime
if "_UTC" in file_name: df = utils.convert_utc_to_loc(df, params.get("local_tz"))
# build AXY object (df must have "datetime", "ax", "ay", "az", "longitude", "latitude", "pressure" and "temperature" columns)
axy = AXY(df=df, group=fieldwork, id=file_id, params=params)


8.4. GPS_TDR
8.5. GPS_Collection
# ======================================================= #
# LIBRARIES
# ======================================================= #
import os
import pandas as pd
from cpforager import parameters, utils, misc, GPS, GPS_Collection
# ======================================================= #
# DIRECTORIES
# ======================================================= #
root_dir = os.getcwd()
data_dir = os.path.join(root_dir, "data")
test_dir = os.path.join(root_dir, "tests")
# ======================================================= #
# PARAMETERS
# ======================================================= #
# set metadata
fieldworks = ["PER_PSC_2012_11", "PER_PSC_2013_11", "BRA_FDN_2016_09", "BRA_FDN_2018_09", "BRA_SAN_2022_03"]
colonies = ["PER_PSC_PSC", "PER_PSC_PSC", "BRA_FDN_MEI", "BRA_FDN_MEI", "BRA_SAN_FRA"]
# get parameters dictionaries
plot_params = parameters.get_plot_params()
# ======================================================= #
# BUILD GPS_COLLECTION OBJECT
# ======================================================= #
# loop over fieldworks
gps_collection = []
for (fieldwork, colony) in zip(fieldworks, colonies):
# determine list of gps files
files = misc.grep_pattern(os.listdir(os.path.join(data_dir, fieldwork)), "_GPS_IGU")
n_files = len(files)
# get structure of parameters according to colony code
params = parameters.get_params(colony)
# loop over gps files
for k in range(n_files):
# set file infos
file_name = files[k]
file_id = file_name.replace(".csv", "")
file_path = os.path.join(data_dir, fieldwork, file_name)
# load raw data
df = pd.read_csv(file_path, sep=",")
# produce "datetime" column of type datetime64
df["datetime"] = pd.to_datetime(df["date"] + " " + df["time"], format="mixed", dayfirst=False)
# if time is at UTC, convert it to local datetime
if "_UTC" in file_name: df = utils.convert_utc_to_loc(df, params.get("local_tz"))
# build GPS object
gps = GPS(df=df, group=fieldwork, id=file_id, params=params)
# append gps to the overall collection
gps_collection.append(gps)
# build GPS_Collection object
gps_collection = GPS_Collection(gps_collection)


8.6. TDR_Collection
# ======================================================= #
# LIBRARIES
# ======================================================= #
import os
import pandas as pd
from cpforager import parameters, utils, misc, TDR, TDR_Collection
# ======================================================= #
# DIRECTORIES
# ======================================================= #
root_dir = os.getcwd()
data_dir = os.path.join(root_dir, "data")
test_dir = os.path.join(root_dir, "tests", "tdr_collection")
# ======================================================= #
# PARAMETERS
# ======================================================= #
# set metadata
fieldworks = ["PER_PSC_2008_11", "BRA_FDN_2017_04", "BRA_FDN_2022_04"]
colonies = ["PER_PSC_PSC", "BRA_FDN_MEI", "BRA_FDN_MEI"]
# set parameters dictionaries
plot_params = parameters.get_plot_params()
# ======================================================= #
# TEST TDR_COLLECTION CLASS
# ======================================================= #
# loop over fieldworks
tdr_collection = []
for (fieldwork, colony) in zip(fieldworks, colonies):
# list of files to process
files = misc.grep_pattern(os.listdir(os.path.join(data_dir, fieldwork)), "_TDR_")
n_files = len(files)
# get structure of parameters
params = parameters.get_params(colony)
# loop over files in directory
for k in range(n_files):
# set file infos
file_name = files[k]
file_id = file_name.replace(".csv", "")
file_path = os.path.join(data_dir, fieldwork, file_name)
# load raw data
df = pd.read_csv(file_path, sep=",")
# produce "datetime" column of type datetime64
df["datetime"] = pd.to_datetime(df["date"] + " " + df["time"], format="mixed", dayfirst=False)
# if time is at UTC, convert it to local datetime
if "_UTC" in file_name: df = utils.convert_utc_to_loc(df, params.get("local_tz"))
# if sensor model is G5, convert dbar to hPa
if "_TDR_G5_" in file_name: df["pressure"] = 100*df["pressure"]
# build TDR object
tdr = TDR(df=df, group=fieldwork, id=file_id, params=params)
# append tdr to the overall collection
tdr_collection.append(tdr)
# build TDR_Collection object
tdr_collection = TDR_Collection(tdr_collection)

8.7. AXY_Collection
# ======================================================= #
# LIBRARIES
# ======================================================= #
import os
import pandas as pd
from cpforager import parameters, utils, misc, AXY, AXY_Collection
# ======================================================= #
# DIRECTORIES
# ======================================================= #
root_dir = os.getcwd()
data_dir = os.path.join(root_dir, "data")
test_dir = os.path.join(root_dir, "tests", "axy_collection")
# ======================================================= #
# PARAMETERS
# ======================================================= #
# set metadata
fieldwork = "BRA_FDN_2022_04"
colony = "BRA_FDN_MEI"
# set parameters dictionaries
plot_params = parameters.get_plot_params()
params = parameters.get_params(colony)
# ======================================================= #
# TEST AXY_COLLECTION CLASS
# ======================================================= #
# list of files to process
files = misc.grep_pattern(os.listdir(os.path.join(data_dir, fieldwork)), "_GPS_AXY_")
n_files = len(files)
# loop over files in directory
axy_collection = []
for k in range(n_files):
# set file infos
file_name = files[k]
file_id = file_name.replace(".csv", "")
file_path = os.path.join(data_dir, fieldwork, file_name)
# load raw data
df = pd.read_csv(file_path, sep=",")
# produce "datetime" column of type datetime64
df["datetime"] = pd.to_datetime(df["date"] + " " + df["time"], format="mixed", dayfirst=False)
# if time is at UTC, convert it to local datetime
if "_UTC" in file_name: df = utils.convert_utc_to_loc(df, params.get("local_tz"))
# build AXY object
axy = AXY(df=df, group=fieldwork, id=file_id, params=params)
# append axy to the overall collection
axy_collection.append(axy)
# build AXY_Collection object
axy_collection = AXY_Collection(axy_collection)
8.8. GPS_TDR_Collection