| Title: | Miscellaneous R Utilities Useful to CRP |
|---|---|
| Description: | A collection of miscellaneous utilities that are useful for various research activities conducted by the Cetacean Research Program (CRP) at NOAA NMFS Pacific Islands Fisheries Science Center. This includes utilities for working with latitude and longitude data, gpx file creation, and more to come. |
| Authors: | Selene Fregosi [aut, cre] (ORCID: <https://orcid.org/0000-0002-2685-3736>), Yvonne Barkley [aut] |
| Maintainer: | Selene Fregosi <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.3.0022 |
| Built: | 2026-05-29 08:16:14 UTC |
| Source: | https://github.com/PIFSC-Protected-Species-Division/crputils |
convert a .csv of vessel track segments to a .gpx file.
The input data.frame must have 1 column 'uid' containing a unique identifier for that track/segment, 2 columns for start latitude and longitude, and 2 columns for stop latitude and longitude. Additional columns for cruise, date, vessel name, etc will be ignored
This is a modified version of trackToGPX.R that is included in the
'cruise-maps-live' repository that tries to generalize the function to
convert any input data.frame, not just the output from a .das file.
CSVToGPX_track(inCSV, outGPX)CSVToGPX_track(inCSV, outGPX)
inCSV |
filename of csv containing track data to be processed. Must include the following columns: uid, startLat, startLon, stopLat, stopLon where uid is a unique track identifier, startLat/startLon is the track start location and stopLat/stopLon is the track end location. All locations should be in decimal degrees. Can optionally include a startDateTime and stopDateTime column. Assumes UTC timezone for GPX output. |
outGPX |
fullpath filename of gpx to be written
example: |
none, will write a file
Selene Fregosi
create a .gpx file based on the effort track data recorded in
.DAS files. The track data needs to have been extracted from the
.DAS file using extractDASTrack() which creates the 'et' dataframe
Track segments within days are plotted separately but are connected. Tracks across days are not connected.
This was generalized from the cruise-maps-live repository's trackToGPX()
DASTrackToGPX(et, outGPX)DASTrackToGPX(et, outGPX)
et |
data.frame of effort as tracks, can be 'et' cumulative over a survey or 'et' for just a single DAS |
outGPX |
fullpath filename to save
example: |
none, will write a file
Selene Fregosi
Utility to convert latitude and longitude coordinates from decimal degrees to degrees decimal minutes
decdeg2degmin(decdeg)decdeg2degmin(decdeg)
decdeg |
N-by-1 vector of coordinates in decimal degrees
e.g., |
N-by-2 data.frame of coordinates with columns 'deg' for degrees and 'min' for decimal minutes
Selene Fregosi
# single input coordinates decdeg <- 30.48667 degmin <- decdeg2degmin(decdeg) # multiple input coordinates decdeg <- c(30.48667, -155.61496) degmin <- decdeg2degmin(decdeg)# single input coordinates decdeg <- 30.48667 degmin <- decdeg2degmin(decdeg) # multiple input coordinates decdeg <- c(30.48667, -155.61496) degmin <- decdeg2degmin(decdeg)
Utility to convert latitude and longitude coordinates from degrees minutes to degrees minutes second
decdeg2degminsec(decdeg)decdeg2degminsec(decdeg)
decdeg |
N-by-1 vector of coordinates in decimal degrees
e.g., |
N-by-3 data.frame of coordinates with columns 'deg' for degrees, 'min' for minutes, and 'sec' for decimal seconds
Selene Fregosi
# single input coordinates decdeg <- 30.48667 degminsec <- decdeg2degminsec(decdeg) # multiple input coordinates decdeg <- c(30.48667, -155.61496) degminsec <- decdeg2degminsec(decdeg)# single input coordinates decdeg <- 30.48667 degminsec <- decdeg2degminsec(decdeg) # multiple input coordinates decdeg <- c(30.48667, -155.61496) degminsec <- decdeg2degminsec(decdeg)
Utility to convert latitude and longitude coordinates from degrees minutes to decimal degrees
degmin2decdeg(degmin)degmin2decdeg(degmin)
degmin |
N-by-2 matrix of coordinates with each column representing
degrees and minutes.
e.g., |
N-by-1 vector of coordinates in decimal degrees
Selene Fregosi
# single input coordinates degmin <- matrix(c(30, 29.2020), nrow = 1, ncol = 2) decdeg <- degmin2decdeg(degmin) # multiple input coordinates degmin <- matrix(c(30, 29.2020, -155, 36.8973), nrow = 2, ncol = 2, byrow = TRUE) decdeg <- degmin2decdeg(degmin)# single input coordinates degmin <- matrix(c(30, 29.2020), nrow = 1, ncol = 2) decdeg <- degmin2decdeg(degmin) # multiple input coordinates degmin <- matrix(c(30, 29.2020, -155, 36.8973), nrow = 2, ncol = 2, byrow = TRUE) decdeg <- degmin2decdeg(degmin)
Utility to convert latitude and longitude coordinates from degrees minutes to degrees minutes seconds
degmin2degminsec(degmin)degmin2degminsec(degmin)
degmin |
N-by-2 matrix of coordinates with each column representing
degrees and minutes
e.g., |
N-by-3 data.frame of coordinates with columns 'deg for degrees and 'min' for minutes and 'sec' for decimal seconds
Selene Fregosi
# single input coordinates degmin <- matrix(c(30, 29.2020), nrow = 1, ncol = 2) degminsec <- degmin2degminsec(degmin) # multiple input coordinates degmin <- matrix(c(30, 29.2020, -155, 36.8973), nrow = 2, ncol = 2, byrow = TRUE) degminsec <- degmin2degminsec(degmin)# single input coordinates degmin <- matrix(c(30, 29.2020), nrow = 1, ncol = 2) degminsec <- degmin2degminsec(degmin) # multiple input coordinates degmin <- matrix(c(30, 29.2020, -155, 36.8973), nrow = 2, ncol = 2, byrow = TRUE) degminsec <- degmin2degminsec(degmin)
Utility to convert latitude and longitude coordinates from degrees minutes seconds to decimal degrees
degminsec2decdeg(degminsec)degminsec2decdeg(degminsec)
degminsec |
N-by-3 matrix of coordinates with each column representing
degrees, minutes, and seconds
e.g., |
N-by-1 vector of coordinates in decimal degrees
Selene Fregosi
# single input coordinates degminsec = matrix(c(30, 29, 12), nrow = 1, ncol = 3) decdeg = degminsec2decdeg(degminsec) # multiple input coordinates degminsec = matrix(c(30, 29, 12, -155, 36, 53.838), nrow = 2, ncol = 3, byrow = TRUE) decdeg = degminsec2decdeg(degminsec)# single input coordinates degminsec = matrix(c(30, 29, 12), nrow = 1, ncol = 3) decdeg = degminsec2decdeg(degminsec) # multiple input coordinates degminsec = matrix(c(30, 29, 12, -155, 36, 53.838), nrow = 2, ncol = 3, byrow = TRUE) decdeg = degminsec2decdeg(degminsec)
Utility to convert latitude and longitude coordinates from degrees minutes seconds to degrees decimal minutes
degminsec2degmin(degminsec)degminsec2degmin(degminsec)
degminsec |
N-by-3 matrix of coordinates with each column representing
degrees, minutes, and seconds
e.g., |
N-by-2 data.frame of coordinates with columns 'deg' for degrees and 'min' for minutes
Selene Fregosi
# single input coordinates degminsec <- matrix(c(30, 29, 12), nrow = 1, ncol = 3) degmin <- degminsec2degmin(degminsec) # multiple input coordinates degminsec <- matrix(c(30, 29, 12, -155, 36, 53.838), nrow = 2, ncol = 3, byrow = TRUE) degmin <- degminsec2degmin(degminsec)# single input coordinates degminsec <- matrix(c(30, 29, 12), nrow = 1, ncol = 3) degmin <- degminsec2degmin(degminsec) # multiple input coordinates degminsec <- matrix(c(30, 29, 12, -155, 36, 53.838), nrow = 2, ncol = 3, byrow = TRUE) degmin <- degminsec2degmin(degminsec)
recursively download all files and subdirectories in a google drive folder
downloadGoogleDriveFolder(folder_id, local_path)downloadGoogleDriveFolder(folder_id, local_path)
folder_id |
Google Drive folder ID, parsed as the 'id' column finding the drive info using googleddrive::drive_get or using using googledrive::as_id and the folder URL |
local_path |
full path to the folder to save the downloaded files |
Selene Fregosi
Summarize clicks for a single AcousticStudy event, to be used within the event_summary_report Rmd. Summarizes total number of clicks, calculates SNR and defines 'good clicks' as those with SNR > 15 dB, and calculates some summary statistics (peak and center frequency, 3 and 10 dB bandwidths, and click duration) output into a nicely formatted table.
er_clickSummary(asF, eventUID)er_clickSummary(asF, eventUID)
asF |
AcousticStudy that has been filtered to remove noise |
eventUID |
ID of a single acoustic event |
'cl' list with 7 elements: total number of clicks, PAMpal click data for all clicks, PAMpal spec data for all clicks (output from PAMpal::calculateAvereageSpectra()), snr for all clicks, the nubmer of clicks above the 15 dB SNR threshold, PAMpal click data for these 'good' clicks only, and 'mt', the formatted table of median summary statistics
## Not run: cl <- er_clickSummary(detsFilt, eventUID) ## End(Not run)## Not run: cl <- er_clickSummary(detsFilt, eventUID) ## End(Not run)
Model the slope of peak frequencies of clicks sorted by peak peak frequency as displayed in a concatenated spectrogram using linear regression. This function models the slope in two ways (1) for all clicks and (2) for just clicks within the 25-75th percentiles of the mean.
er_concatenatedSlope(cl, quantLimit = NULL)er_concatenatedSlope(cl, quantLimit = NULL)
cl |
list returned by clickSummary() that has a elements spec and snr |
quantLimit |
optional argument to limit the clicks to a particular quantile range e.g., only clicks in the 25-75th quantiles, 5-95th, etc. Default is NULL (all clicks), input in format c(.25, .75) |
'cs' the linear regression model output
# all clicks ## Not run: cs <- concatenatedSlope(cl) # 25-75th quantile only ## Not run: cs <- concatenatedSlope(cl, c(0.25, 0.75))# all clicks ## Not run: cs <- concatenatedSlope(cl) # 25-75th quantile only ## Not run: cs <- concatenatedSlope(cl, c(0.25, 0.75))
Plot histogram of click durations of high SNR clicks only
er_plotClickDurationHist(cl)er_plotClickDurationHist(cl)
cl |
list returned by clickSummary() that has a elements spec and snr |
## Not run: er_plotClickDurationHist ## End(Not run)## Not run: er_plotClickDurationHist ## End(Not run)
Plot histogram of click SNR for all clicks with a vertical dashed line at the specified cutoff
er_plotClickSNRHist(cl, snrThreshold)er_plotClickSNRHist(cl, snrThreshold)
cl |
list returned by clickSummary() that has a elements spec and snr |
snrThreshold |
SNR in dB to plot as the threshold for 'good' clicks |
## Not run: er_plotClickSNRHist(cl, 15) ## End(Not run)## Not run: er_plotClickSNRHist(cl, 15) ## End(Not run)
Plot spectra of all clicks - mean, median, noise, and any specified reference spectra
er_plotSpectra( avgSpec, cl, refSpecs = NULL, xLims = c(0, 100), snrThreshold = 15 )er_plotSpectra( avgSpec, cl, refSpecs = NULL, xLims = c(0, 100), snrThreshold = 15 )
avgSpec |
list output of PAMpal::calculateAvereageSpectra that includes variables freq, avgSpec, avgNoise |
cl |
list returned by clickSummary() that has a elements spec and snr (if SNR is added as an output to calculateAverageSpectra this can be removed) |
refSpecs |
optional argument with list of reference spectra to include as overlays in the plot |
xLims |
optional argument for frequency limits. Default is 0 to 100. Define with format c(0, 100) |
snrThreshold |
optional argument for SNR threshold (in dB) for filtering clicks used in calculation of median spectrum. Default is 15 |
## Not run: er_plotSpectra(avgSpec, cl, refSpecs = refSpecs, xLims = c(0,100), snrThreshold = 15) ## End(Not run)## Not run: er_plotSpectra(avgSpec, cl, refSpecs = refSpecs, xLims = c(0,100), snrThreshold = 15) ## End(Not run)
Plot histogram begin frequency of whistles
er_plotWhistleBeginFreqHist(wl)er_plotWhistleBeginFreqHist(wl)
wl |
list returned by whistleSummary() that has a elements wh |
## Not run: er_plotWhistleBeginFreqHist(wl) ## End(Not run)## Not run: er_plotWhistleBeginFreqHist(wl) ## End(Not run)
Plot all whistle contours
er_plotWhistleContours(dets, eventUID, xMax = 1.5, yMax = 20)er_plotWhistleContours(dets, eventUID, xMax = 1.5, yMax = 20)
dets |
AcousticStudy containing whistle detections |
eventUID |
unique ID for the event being processed (to reference within the AcousticStudy) |
xMax |
optional argument for upper limit of x axis, in seconds. Default is 1.5 seconds |
yMax |
optional argument for upper limit of y axis, in kHz. Default is 20 kHz |
## Not run: er_plotWhistleContours(dets, eventUID, xMax = 1.5, yMax = 20) ## End(Not run)## Not run: er_plotWhistleContours(dets, eventUID, xMax = 1.5, yMax = 20) ## End(Not run)
Plot histogram median frequency of whistles
er_plotWhistleMedFreqHist(wl)er_plotWhistleMedFreqHist(wl)
wl |
list returned by whistleSummary() that has a elements wh |
## Not run: er_plotWhistleMedFreqHist(wl) ## End(Not run)## Not run: er_plotWhistleMedFreqHist(wl) ## End(Not run)
Summarize whistles for a single AcousticStudy event, to be used within the event_summary_report Rmd. Summarizes total number of whistles and calculates some summary statistics (begin and end frequency, mean frequency, duration, and frequency range) output into a nicely formatted table.
er_whistleSummary(asF, eventUID)er_whistleSummary(asF, eventUID)
asF |
AcousticStudy that has been filtered to remove noise |
eventUID |
ID of a single acoustic event |
'wl' list with 3 elements: total number of whistles, PAMpal whistle data for each whistle, and 'mt', the formatted table of median summary statistics
## Not run: wl <- er_whistleSummary(detsFilt, eventUID) ## End(Not run)## Not run: wl <- er_whistleSummary(detsFilt, eventUID) ## End(Not run)
Pull effort tracks from a relatively raw daily .das file into
a simpler dataframe. Utilizes the package swfscDAS and then cleans up those
outputs a bit. Generalized from cruise-maps-live's extractTrack()
extractDASTrack(df_proc)extractDASTrack(df_proc)
df_proc |
processed das file (with |
a dataframe of effort tracks with date and lat/lon
Selene Fregosi
Utility to decode FLAC files to WAV format using the command line flac.exe encoder.
This function converts a directory of .flac files to .wav and saves the new files to the specified output directory. It requires the free FLAC codec/executable which can be downloaded from: https://xiph.org/flac/download.html.
flac2wav(path_flac, inDir, outDir, numCh = 1)flac2wav(path_flac, inDir, outDir, numCh = 1)
path_flac |
Character. Path to flac executable
e.g., |
inDir |
Character. Path to the folder containing FLAC files |
outDir |
Character. Path to the folder where WAV files will be saved. This path MUST end in a final slash. A check will ensure that it does. |
numCh |
numeric. Number of channels. Default is 1 |
None. Writes WAV files to disk.
Selene Fregosi
tk_choose.dir
Note: This function uses tcltk::tk_choose.dir() which may require a
working installation of Tcl/Tk.
On Linux and macOS, you may need to install Tcl/Tk separately.
# single channel data ## Not run: flac2wav('C:\\users\\user.name\\programs\\flac-1.5.0-win\\Win64\\flac.exe', 'F:\\flacFiles', 'F:\\wavFiles\\'); ## End(Not run) # multichannel data ## Not run: flac2wav('C:\\users\\user.name\\programs\\flac-1.5.0-win\\Win64\\flac.exe', 'F:\\flacFiles', 'F:\\wavFiles\\', 4); ## End(Not run)# single channel data ## Not run: flac2wav('C:\\users\\user.name\\programs\\flac-1.5.0-win\\Win64\\flac.exe', 'F:\\flacFiles', 'F:\\wavFiles\\'); ## End(Not run) # multichannel data ## Not run: flac2wav('C:\\users\\user.name\\programs\\flac-1.5.0-win\\Win64\\flac.exe', 'F:\\flacFiles', 'F:\\wavFiles\\', 4); ## End(Not run)
Re-install a set of saved packages (created with
saveInstalledPackages()) after updating R
installSavedPackages(fileName)installSavedPackages(fileName)
fileName |
filename of saved packages. This can be the full file path
with filename or if no path is specified it will look for the file in your
current working directory. You can use |
Yvonne Barkley and Selene Fregosi
Utility to calculate the distance (in kilometers) between a pair of latitude/longitude coordinates. It returns two distances: d1km which is calculated using the Haversine formula which accounts for the shape of the earth and d2km based on Pythagoras' theorem and is not accurate over long distances. Over short distances d1km and d2km will be very similar.
lldistkm(latlon1, latlon2)lldistkm(latlon1, latlon2)
latlon1 |
1-by-2 numeric vector of lat/lon coordinates (in decimal
degrees) with latitude first, longitude second
e.g., |
latlon2 |
1-by-2 numeric vector of lat/lon coordinates (in decimal
degrees) with latitude first, longitude second
e.g., |
list with d1km (distance calculated by haversine formula) and d2km (distance calculated based on Pythagorean theorem)
Selene Fregosi
#short distance #create input coordinates ll1 <- c(21, -157) ll2 <- c(22, -158) #calculate distance dkm <- lldistkm(ll1, ll2) dkm # $d1km # [1] 151.8795 # # $d2km # [1] 151.8809 #long distance #calculate distance dkm <- lldistkm(c(21, -157), c(13, 144)) dkm # $d1km # [1] 6304.003 # # $d2km # [1] 32019.57 #d2km not acccurate at longer distances#short distance #create input coordinates ll1 <- c(21, -157) ll2 <- c(22, -158) #calculate distance dkm <- lldistkm(ll1, ll2) dkm # $d1km # [1] 151.8795 # # $d2km # [1] 151.8809 #long distance #calculate distance dkm <- lldistkm(c(21, -157), c(13, 144)) dkm # $d1km # [1] 6304.003 # # $d2km # [1] 32019.57 #d2km not acccurate at longer distances
Load in multiple Pamguard binary files and compile the data lists into a single list. For now it works with WMD data...
loadMultiBinaries(wmBinFiles)loadMultiBinaries(wmBinFiles)
wmBinFiles |
character array of whistle moan binary files to be loaded |
binOut a list of binary WMD data
## Not run: # map the needed binary files binFiles <- dets@events[[eventUID]]@files$binaries wmFileIdx <- grep(pattern = '^.*WhistlesMoans_Whistle_and_Moan_Detector_Contours.*\\.pgdf$', binFiles) wmFiles <- dets@events[[eventUID]]@files$binaries[wmFileIdx] # now load the binaries loadMultiBinaries(wmFiles) ## End(Not run)## Not run: # map the needed binary files binFiles <- dets@events[[eventUID]]@files$binaries wmFileIdx <- grep(pattern = '^.*WhistlesMoans_Whistle_and_Moan_Detector_Contours.*\\.pgdf$', binFiles) wmFiles <- dets@events[[eventUID]]@files$binaries[wmFileIdx] # now load the binaries loadMultiBinaries(wmFiles) ## End(Not run)
Save a vector of currently installed R packages as a RDS named
fileName.Run this before updating R, then update R, then run
installSavedPackages to re-install all packages
saveInstalledPackages(fileName)saveInstalledPackages(fileName)
fileName |
filename to save to (can be full file path with name or if
just a name is specified then will save in the current working directory use
|
installedPkgs a dataframe of effort tracks with date and lat/lon
Yvonne Barkley
Utility to convert WAV files to FLAC format using the command line flac.exe encoder.
This function converts a directory of .wav files to .flac and saves the new files to the specified output directory. It requires the free FLAC codec/executable which can be downloaded from: https://xiph.org/flac/download.html.
wav2flac(path_flac, inDir, outDir, numCh = 1)wav2flac(path_flac, inDir, outDir, numCh = 1)
path_flac |
Character. Path to flac executable
e.g., |
inDir |
Character. Path to the folder containing WAV files |
outDir |
Character. Path to the folder where FLAC files will be saved. This path MUST end in a final slash. A check will ensure that it does. |
numCh |
numeric. Number of channels. Default is 1 |
None. Writes FLAC files to disk.
Selene Fregosi
tk_choose.dir
Note: This function uses tcltk::tk_choose.dir() which may require a
working installation of Tcl/Tk.
On Linux and macOS, you may need to install Tcl/Tk separately.
# single channel data ## Not run: wav2flac('C:\\users\\user.name\\programs\\flac-1.5.0-win\\Win64\\flac.exe', 'F:\\wavFiles', 'F:\\flacFiles\\') ## End(Not run) # multichannel data ## Not run: wav2flac('C:\\users\\user.name\\programs\\flac-1.5.0-win\\Win64\\flac.exe', 'F:\\wavFiles', 'F:\\flacFiles\\', 4) ## End(Not run)# single channel data ## Not run: wav2flac('C:\\users\\user.name\\programs\\flac-1.5.0-win\\Win64\\flac.exe', 'F:\\wavFiles', 'F:\\flacFiles\\') ## End(Not run) # multichannel data ## Not run: wav2flac('C:\\users\\user.name\\programs\\flac-1.5.0-win\\Win64\\flac.exe', 'F:\\wavFiles', 'F:\\flacFiles\\', 4) ## End(Not run)