Skip to content

Commit

Permalink
Merge pull request #76 from sfregosi/sf-bug-fixes
Browse files Browse the repository at this point in the history
Lots of bug fixes
  • Loading branch information
sfregosi authored Sep 15, 2024
2 parents 5b9dcca + e96e245 commit 6d08969
Show file tree
Hide file tree
Showing 13 changed files with 522 additions and 346 deletions.
23 changes: 14 additions & 9 deletions agate/example_workflows/workflow_downloadScript.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
% checking on the glider status
%
% It has the following sections:
% (1) any new basestation files to the local computer, including .nc,
% .log, .dat, cmdfiles, pdos any acoustic .eng or detection files
% (1) any new basestation files to the local computer. This currently
% includes .nc, .log, .dat, .eng files and WISPR (ws*) files. PMAR
% files (pm*) are untested and cmdfiles/pdoscmds.bat files are not
% updated since the switch to basestation3.
% (2) extracts useful data from local basestation .nc and .log files
% and compiles into a summary table, variable 'pp', and saves to a
% .xlsx and .mat
Expand All @@ -18,6 +20,9 @@
% duration
%
% It requires an agate configuration file during agate initialization
% that includes the basestation configuration section, the acoustic
% configuration section (if running an acoustic system), and the
% plotting configuration section.
%
% Notes
%
Expand All @@ -29,7 +34,7 @@
% Created with MATLAB ver.: 9.13.0.2166757 (R2022b) Update 4
%
% FirstVersion: 01 June 2023
% Updated: 06 September 2024
% Updated: 10 September 2024
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% initialize agate
Expand All @@ -39,7 +44,7 @@
% set up nested folders for basestation files and piloting outputs
path_status = fullfile(CONFIG.path.mission, 'flightStatus'); % where to store output plots/tables
path_bsLocal = fullfile(CONFIG.path.mission, 'basestationFiles'); % local copy of basestation files
% this also should be set as CONFIG.path.bsLocal in the mission cnf file
% this also can be set as CONFIG.path.bsLocal in the mission cnf file

% make the dirs if they don't exist
mkdir(path_status);
Expand All @@ -54,8 +59,7 @@
%% (2) extract piloting parameters

% create piloting parameters (pp) table from downloaded basestation files
pp = extractPilotingParams(CONFIG, fullfile(CONFIG.path.mission, 'basestationFiles'), ...
fullfile(CONFIG.path.mission, 'flightStatus'), 0);
pp = extractPilotingParams(CONFIG, CONFIG.path.bsLocal, path_status, 0);
% change last argument from 0 to 1 to load existing data and append new dives/rows

% save it to the default location as .mat and .xlsx
Expand Down Expand Up @@ -108,11 +112,12 @@
printErrors(CONFIG, size(pp,1), pp)

% print avg speed and rough estimate of total mission duration
tm = printTravelMetrics(CONFIG, pp, fullfile(CONFIG.path.mission, 'targets'), 1);
tm = printTravelMetrics(CONFIG, pp, fullfile(CONFIG.path.mission, ...
'targets'), 1);

% specify planned recovery date and time
recovery = '2023-00-00 00:00:00';
recTZ = 'America/Los_Angeles';
tm = printRecoveryMetrics(CONFIG, pp, fullfile(CONFIG.path.mission, 'targets'), ...
recovery, recTZ, 1);
tm = printRecoveryMetrics(CONFIG, pp, fullfile(CONFIG.path.mission, ...
'targets'), recovery, recTZ, 1);

24 changes: 13 additions & 11 deletions agate/example_workflows/workflow_missionTrackPlanning.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
% S. Fregosi <[email protected]> <https://github.com/sfregosi>
%
% FirstVersion: 05 April 2023
% Updated: 07 August 2024
% Updated: 11 September 2024
%
% Created with MATLAB ver.: 9.13.0.2166757 (R2022b) Update 4
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -45,9 +45,9 @@
radius = 2000;

% create targets file, 3 options to name waypoints
% (1) prefix-based automated naming
prefix = 'WP'; % Any two letters make easy to reference and read options
targetsOut = makeTargetsFile(CONFIG, kmlFile, prefix, radius);
% (1) alphanumeric/prefix-based automated naming
alphaNum = 'WP'; % Any two letters make easy to reference and read options
targetsOut = makeTargetsFile(CONFIG, kmlFile, alphaNum, radius);
% OR
% (2) use a text file with list of waypoint names; will prompt to select .txt
targetsOut = makeTargetsFile(CONFIG, kmlFile, 'file', radius);
Expand All @@ -64,9 +64,9 @@
% use targetsOut file from above as input targets file
targetsFile = targetsOut;

% create plot
mapPlannedTrack(CONFIG, targetsFile, CONFIG.glider, bathyOn, figNum)

% create plot - single track only
mapPlannedTrack(CONFIG, targetsFile, CONFIG.glider, bathyOn, [], figNum)
% 5th argument blank uses default color (orange)

% get file name only for plot saving
[~, targetsName, ~] = fileparts(targetsFile);
Expand All @@ -89,10 +89,12 @@
%% (3) Plot bathymetry profile of targets file

% can specify bathymetry file
bathyFile = 'C:\GIS\etopo\ETOPO2022_bedrock_30arcsec_MHI.tiff';
plotTrackBathyProfile(CONFIG, targetsFile, bathyFile, figNum)
% OR leave empty to prompt to select file
plotTrackBathyProfile(CONFIG, targetsFile, [], figNum)
bathyFile = 'C:\GIS\etopo\ETOPO2022_bedrock_30arcsec.tiff';
plotTrackBathyProfile(CONFIG, 'targetsFile', targetsFile, ...
'bathyFile', bathyFile)
% OR leave that argument out to default to CONFIG.map.bathyFile if
% available or prompt if not available
plotTrackBathyProfile(CONFIG, 'targetsFile', targetsFile)

% save as .png
exportgraphics(gcf, fullfile(CONFIG.path.mission, [CONFIG.glider '_' ...
Expand Down
123 changes: 99 additions & 24 deletions agate/utils/createBasemap.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function [baseFig] = createBasemap(CONFIG, bathyOn, contourOn, figNum)
function [baseFig] = createBasemap(CONFIG, varargin)
% CREATEBASEMAP Create a basemap of the bathymetry for the mission area
%
% Syntax:
% BASEFIG = CREATEBASEMAP(CONFIG, BATHYON, CONTOURON, FIGNUM, OUTFIG)
% BASEFIG = CREATEBASEMAP(CONFIG, VARARGIN)
%
% Description:
% Function to create a basemap for a glider mission, using the lat
Expand All @@ -12,13 +12,25 @@
% states at this time). It can be saved as a .fig file that can be
% added to (add glider path, labels, and acoustic encounters).
%
% Bathymetry files can be downloaded from NCEI. For more info on
% selecting a bathymetry file visit:
% https://sfregosi.github.io/agate/#basemap-rasters
%
% Inputs:
% CONFIG [struct] mission/agate configuration variable.
% Required fields: CONFIG.map entries
% bathyOn [double] set to 1 to plot bathymetry or 0 to only plot
% land
% contourOn [double] set to 1 to plot bathymetry contours or 0 for
% no contour lines
%
% all varargins are specified using name-value pairs
% e.g., 'bathyOn', 1, 'figNum', 12
%
% bathy optional argument for bathymetry plotting
% [double] Set to 1 to plot bathymetry or 0 to only plot
% land. Will look for bathy file in CONFIG.map.bathyFile
% [char] Path to the bathymetry file (if you want to use
% a different one than specified in CONFIG or it is not
% specified in CONFIG
% contourOn [double] optional argument. Set to 1 to plot bathymetry
% contours or 0 for no contour lines. Default is on (1)
% figNum [double] optional to specify figure number so won't
% create repeated versions when updated
%
Expand All @@ -27,29 +39,53 @@
%
% Examples:
% % Create a basemap that includes bathymetry but no contour lines
% baseFig = createBasemap(CONFIG, 1, 0);
% baseFig = createBasemap(CONFIG, 'contourOn', 0);
%
% See also
%
% Authors:
% S. Fregosi <[email protected]> <https://github.com/sfregosi>
%
% FirstVersion: 09 March 2024
% Updated: 06 August 2024
% Updated: 11 September 2024
%
% Created with MATLAB ver.: 9.13.0.2166757 (R2022b) Update 4
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%
% for testing
% figNum = 82;
%%%%%%%%%%%%%%

% specify a figure number
if nargin < 4
figNum = [];
% argument checks
narginchk(1, inf)

% set defaults
bathyOn = 1;
bathyFile = [];
contourOn = 1;
figNum = [];

% parse arguments
vIdx = 1;
while vIdx <= length(varargin)
switch varargin{vIdx}
case 'bathy'
if ischar(varargin{vIdx+1}) || isstring(varargin{vIdx+1})
bathyOn = 1;
bathyFile = varargin{vIdx+1};
elseif isnumeric(varargin{vIdx+1})
bathyOn = varargin{vIdx+1};
bathyFile = [];
end
vIdx = vIdx+2;
case 'contourOn'
contourOn = varargin{vIdx+1};
vIdx = vIdx+2;
case 'figNum'
figNum = varargin{vIdx+1};
vIdx = vIdx+2;
otherwise
error('Incorrect argument. Check inputs.');
end
end


%% set up the figure

if isempty(figNum)
Expand All @@ -58,7 +94,7 @@
baseFig = figure(figNum);
end

% mapFigPosition = [100 100 800 600];
% mapFigPosition = [100 50 800 600];
% baseFig.Position = mapFigPosition;
baseFig.Name = 'Base map';

Expand Down Expand Up @@ -96,13 +132,23 @@

if contourOn == 1 || bathyOn == 1 % if either, load raster data

if isfield(CONFIG.map, 'bathyFile')
bathyFile = CONFIG.map.bathyFile;
else % prompt to choose file
% if not file specified, try to use CONFIG, otherwise prompt
if isempty(bathyFile)
if isfield(CONFIG.map, 'bathyFile')
bathyFile = CONFIG.map.bathyFile;
else % prompt to choose file
[fn, path] = uigetfile(fullfile(CONFIG.path.shp, '*.tif;*.tiff'), ...
'Select etopo raster file');
bathyFile = fullfile(path, fn);
end
end
% check that the specified one exists, otherwise prompt
if ~exist(bathyFile, 'file')
[fn, path] = uigetfile(fullfile(CONFIG.path.shp, '*.tif;*.tiff'), ...
'Select etopo raster file');
bathyFile = fullfile(path, fn);
end

[Z, refvec] = readgeoraster(bathyFile, 'OutputType', 'double', ...
'CoordinateSystemType', 'geographic');
[Z, refvec] = geocrop(Z, refvec, CONFIG.map.latLim, CONFIG.map.lonLim);
Expand All @@ -123,10 +169,10 @@
brighten(.4);

% colorbar for bathymetry is too unreliable
% cb = colorbar;
% cb.Location = 'eastoutside';
% cb.Label.String = 'depth [m]';
% cbPosit = cb.Position;
% cb = colorbar;
% cb.Location = 'eastoutside';
% cb.Label.String = 'depth [m]';
% cbPosit = cb.Position;
end

if contourOn == 1 % plot it
Expand All @@ -143,5 +189,34 @@
'BoundingBox', [CONFIG.map.lonLim' CONFIG.map.latLim']);
geoshow(states, 'FaceColor', [0 0 0], 'EdgeColor', 'k');


% NATURAL EARTH DATA - MINOR ISLANDS PLOTTING %
% % plot land
% % try this default path
% landFile = fullfile(CONFIG.path.shp, 'NaturalEarthData', 'ne_10m_land_scale_rank.shp');
% % if that's no good, prompt for new file
% if ~exist(landFile, 'file')
% [fn, path] = uigetfile([CONFIG.path.shp '*.shp'], 'Select ne_10m_land_scale_rank.shp');
% landFile = fullfile(path, fn);
% end
% land = shaperead(landFile, 'BoundingBox', [CONFIG.map.lonLim' CONFIG.map.latLim'], ...
% 'UseGeoCoords', true);

% % and any minor islands if needed (e.g., for SBI)
% % try this default path
% minIslFile = fullfile(CONFIG.path.shp, 'NaturalEarthData', 'ne_10m_minor_islands.shp');
% % if that's no good, prompt for new file
% if ~exist(minIslFile, 'file')
% [fn, path] = uigetfile([CONFIG.path_shp '*.shp'], 'Select ne_10m_minor_islands.shp');
% minIslFile = fullfile(path, fn);
% end
% landmi = shaperead(minIslFile, 'BoundingBox', [CONFIG.map.lonLim' CONFIG.map.latLim'], ...
% 'UseGeoCoords', true);
%
% geoshow(land, 'FaceColor', [0 0 0], 'EdgeColor', 'k')
% geoshow(landmi, 'FaceColor', [0 0 0], 'EdgeColor', 'k')
%


end

78 changes: 62 additions & 16 deletions agate/utils/downloadBasestationFiles.m
Original file line number Diff line number Diff line change
@@ -1,21 +1,67 @@
function downloadBasestationFiles1(CONFIG)
%downloadBasestationFiles1 download new glider files from basestation
%
% downloadBasestationFiles1(CONFIG)
% Download all .log, .nc, .asc, .eng, .dat, WISPR (ws*), and PMAR (pm*) files
% from a basestation described in CONFIG.bs. This is a replacement for the
% older downloadBasestationFiles() routine. In addition to CONFIG.bs, also
% uses CONFIG.path.bsLocal, which says where to deposit the downloaded files.
function downloadBasestationFiles(CONFIG)
%DOWNLOADBASESTATIONFILES download basestation files locally via SFTP
%
% Syntax:
% DOWNLOADBASESTATIONFILES(CONFIG)
%
% Description:
% Download variety of glider piloting files from the remote
% basestation using SFTP. Downloads all new (not previously
% downloaded) .nc, .log, .eng, .asc, .dat, (glider data files) and
% WISPR (ws*) files.
%
% Download of PMAR (pm*) files/folders is untested.
% Previous version also processed/unzipped ws* files using
% `processWisprDetFile` but that is currently not working so that
% step is commented out.
% Previous version also downloaded pdos and cmdfiles (glider piloting
% files) but that is not included here.
%
% To redownload a file (e.g., if corrupt due to incomplete call in),
% delete the file and re-run. The previous version generated a
% 'cache' file that listed all previously downloaded files for speed.
% This could be implemented in the future.
%
% Inputs:
% CONFIG [struct] mission/agate configuration variable.
% Required fields: CONFIG.bs.cnfFile (path to basestation
% configuration file containing url, username and either
% password or SSH key pair), CONFIG.path.bsLocal (folder
% to deposit downloaded files), and CONFIG.path.bsRemote
% (folder on basestation that contains the relevant files
% e.g., the 'current' folder if using
% basestation3/seaglider.pub)
%
% Outputs:
% Downloads files directly to specified CONFIG.path.bsLocal
%
% Examples:
% downloadBasestationFiles(CONFIG)
%
% See also
%
% Authors:
% S. Fregosi <[email protected]> <https://github.com/sfregosi>
% D. Mellinger <[email protected]> <https://github.com/DMellinger>
%
% FirstVersion: 7/22/2016.
% Originally for AFFOGATO project/CatBasin deployment
% Updated: 10 September 2024
%
% Created with MATLAB ver.: 9.9.0.1524771 (R2020b) Update 2
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Get the right sftp() function. Since sftp can exist in multiple places, we
% need the right one - the one MATLAB has in its 'io' toolbox. (I thought using
% "matlab.io.sftp()" would work, but it doesn't.)
% Get the right sftp() function. Since sftp can exist in multiple places,
% we need the right one - the one MATLAB has in its 'io' toolbox. (I
% thought using "matlab.io.sftp()" would work, but it doesn't.)
%
% Find the right sftp and get a handle to it. To find it among all the places
% where sftp() exists, find one whose path includes "toolbox/matlab" (or
% "toolbox\matlab"). Then cd to that directory, make a function handle (which
% will always be to a function in the current directory), and cd back. There has
% to be a better way to do this!
% Find the right sftp and get a handle to it. To find it among all the
% places where sftp() exists, find one whose path includes "toolbox/matlab"
% (or"toolbox\matlab"). Then cd to that directory, make a function handle
% (which will always be to a function in the current directory), and cd
% back. There has to be a better way to do this!

originalDir = pwd();
try % use try/catch to cd back to originalDir in case of error
all_sftps = which('-all', 'sftp'); % cell array of all sftp.m locations
Expand Down
Loading

0 comments on commit 6d08969

Please sign in to comment.