function onchydrophone_getPubData(startTimeStr, endTimeStr, deviceName, dataFormat, outDir)
startTimeStr='2009-02-12 00:00:00.0';
startTimeStr='2011-05-11 14:00:00.0';

endTimeStr='2009-09-27 12:00:00.0';
endTimeStr='2011-10-02 17:00:00.0';

deviceName='IOS3 Hydrophone Array 01';
deviceName='Ocean Sonics icListen LF Hydrophone 201-109';

dataFormat='png';
dataFormat='wav';

outDir='./5';
outDir='./1';
%% ONCHYDROPHONE_GETPUBDATA Downloads public data from the desired Hydrophone system
% from the ONC data archive between startTimeStr and endTimeStr to the
% directory outDir.
%
% startTimeStr, endTimeStr = 'yyyy-mmm-dd HH:MM:SS'
% deviceName =
%                 'IOS3 Hydrophone Array 01'
%                 'IOS3 Hydrophone Array 02'
%                 'IOS3 Hydrophone Array 03'
%                 'Naxys Ethernet Hydrophone 02345 Ti 052'
%                 'Naxys High Gain Hydrophone 2'
%                 'Naxys Hydrophone 1'
%                 'Naxys Hydrophone 2'
%                 'Naxys Hydrophone 4'
%                 'Ocean Sonics icListen HF Hydrophone 1205'
%                 'Ocean Sonics icListen HF Hydrophone 1251'
%                 'Ocean Sonics icListen HF Hydrophone 1253'
%                 'Ocean Sonics icListen HF Hydrophone 1252'
%                 'Ocean Sonics icListen LF Hydrophone 201-109'
%                 'Ocean Sonics icListen LF Hydrophone 203'
%                 'Ocean Sonics icListen LF Hydrophone 216'
%                 'Ocean Sonics icListen LF Hydrophone 224'
% dataFormat = 'png' for spectrogram images
%              'wav' for audio WAV files
%              'mp3' for mp3 files from IOS3 Hydrophone Arrays only
%              'fft' for the Arctic Cambridge Bay Hydrophone only.
% outDir = the directory where files are downloaded.
%
% NOTES: 
% - Make sure you have enough space to download files before you start.
% WAV files can be up to 55MB each and files are usually 5 minutes in
% length.  This routine will generate an error if there isn't enough space
% for the files.
% - Please start off by requesting smaller time periods.  This routine will take longer to run if the time period is too
% large.  On a simple PC this routine takes ~8 seconds to download one 55MB file.  
% - This routine depends on services that reside on http://dmas.uvic.ca.
% If the server is unavailable, this routine will generate errors.  Contact
% info is provided below for issue reporting.
% - There are data gaps between DateFrom and DateTo in the deployment history
% below.
% - Deployment history only lists devices where data have been
% collected.
% - OIC(Ocean Innovation Can) represents the ONC Ocean Innovation group
% locations.
% - Delta Dynamics Laboratory location = DDL
%
%
% VENUS DEPLOYMENT HISTORY, Strait of Georgia East (only includes devices and sites where data have been collected)
% deviceName                                   Location          Depth(m)  DateFrom               DateTo
% Ocean Sonics icListen LF Hydrophone 201-109  DDL-04	         108       2011-05-11 14:00:00.0  2011-10-02 17:00:00.0
% Ocean Sonics icListen LF Hydrophone 203      Hyd-06            170       2011-12-11 21:00:00.0  2012-08-09 21:00:00.0
% Ocean Sonics icListen LF Hydrophone 203      DDL OIC-01-HydLF	 146       2013-05-03 17:00:00.0  2013-10-21 23:45:00.0
% Ocean Sonics icListen LF Hydrophone 216      OIC-01-HydLF      170       2013-05-05 17:00:00.0  2013-10-21 20:52:00.0
% IOS3 Hydrophone Array 01                     Hyd-02	         170       2009-02-12 00:00:00.0  2009-09-27 12:00:00.0
% IOS3 Hydrophone Array 01                     Hyd-08            170       2013-10-25 20:17:00.0  present
% IOS3 Hydrophone Array 02	                   Hyd-01	         169       2007-10-20 00:00:00.0  2009-02-12 14:00:00.0
% IOS3 Hydrophone Array 02	                   Hyd-03	         170       2009-09-27 12:00:01.0  2010-12-10 18:00:00.0
% IOS3 Hydrophone Array 02                     Hyd-04            170       2011-05-10 21:00:00.0  2011-07-02 23:00:00.0
% IOS3 Hydrophone Array 02	         	       Hyd-06	         170       2011-12-12 21:00:00.0  2012-08-09 21:00:00.0
% IOS3 Hydrophone Array 02		               Hyd-07	         170       2013-05-02 02:00:00.0  2013-08-12 13:14:00.0
% IOS3 Hydrophone Array 03	                   DDL-03	         107       2010-02-20 03:00:00.0  2010-06-21 19:00:00.0
% IOS3 Hydrophone Array 03	                   Hyd-05	         170       2011-07-03 15:00:00.0  2011-12-11 23:30:00.0
%
% VENUS DEPLOYMENT HISTORY, Strait of Georgia Central (only includes devices and sites where data have been collected)
% deviceName                                   Location          Depth(m)  DateFrom               DateTo
% IOS3 Hydrophone Array 01                     Hyd-03	         300       2011-05-12 00:00:00.0  2011-07-03 04:00:00.0
% IOS3 Hydrophone Array 01		               Hyd-04	         300       2011-10-02 20:00:00.0  2012-08-10 15:00:00.0
% IOS3 Hydrophone Array 03	                   Hyd-01	         300       2008-09-24 00:00:00.0  2009-02-15 03:00:00.0
% IOS3 Hydrophone Array 03		               Hyd-02	         300       2010-12-11 23:00:00.0  2011-05-10 00:00:00.0
%
% VENUS DEPLOYMENT HISTORY, Saanich Inlet Central (only includes devices and sites where data have been collected)
% deviceName                                   Location          Depth(m)  DateFrom               DateTo
% IOS3 Hydrophone Array 01	                   Hyd-01	         94        2006-02-26 00:00:00.0  2006-11-09 00:00:00.0
% IOS3 Hydrophone Array 01                     Hyd-02	         100       2007-02-01 00:00:00.0  2007-09-12 00:00:00.0
%
% NEPTUNE DEPLOYMENT HISTORY (only includes devices and sites where data have been collected)
% deviceName                                   Location                                 Depth(m)  DateFrom               DateTo
% Ocean Sonics icListen HF Hydrophone 1253     Folger Passage-Deep_Hydrophone_2013-05	105	      2013-05-20 17:13:53.0  present 
% Ocean Sonics icListen LF Hydrophone 224      ODP1027-Hydrophone_2013-06               2662	  2013-06-23 21:44:41.0  present
% Ocean Sonics icListen HF Hydrophone 1205     FolgerPassage-Deep_Hydrophone_2012-06	95	      2012-06-12 11:13:42.0  2013-05-17 19:14:00.0
% Naxys High Gain Hydrophone 2	               Folger Passage-Deep_Hydrophone_2009-08	100	      2009-08-28 01:26:55.0  2011-07-19 01:54:14.0
%
% NEPTUNE DEPLOYMENT HISTORY, Barkley Canyon (only includes devices and sites where data have been collected)
% deviceName                                   Location                                 Depth(m)  DateFrom               DateTo
% Ocean Sonics icListen HF Hydrophone 1251     UpperSlopeIPPod2_Hydrophone_2013-05      392	      2013-05-11 21:24:09.0  present 
% Naxys Ethernet Hydrophone 02345 Ti 052	   UpperSlope_VPS_2012-06	                396	      2012-05-28 15:45:00.0  2013-05-19 18:24:00.0                                             
% Naxys Hydrophone 1	                       UpperSlope_Hydrophone_2009-07            396.5	  2009-07-26 17:14:55.0  2010-05-17 01:28:28.0
% Naxys Hydrophone 2                           CanyonAxis_Hydrophone_2009-08	        982.4	  2009-08-27 04:48:28.0  2009-09-11 00:27:51.0
% Naxys Hydrophone 2                           CanyonAxis_Hydrophone_2009-09	        982.4	  2009-09-11 00:27:51.0  2010-05-16 13:49:01.0
% Naxys Hydrophone 2                           CanyonAxis_Hydrophone_2010-05	        981	      2010-05-17 17:33:14.0  2011-07-11 06:28:04.0
% Naxys Hydrophone 4                           UpperSlope_Hydrophone_2010-05	        397	      2010-05-18 03:57:20.0  2011-07-17 00:54:37.0
%
% ARCTIC DEPLOYMENT HISTORY, Cambridge Bay (due to bandwidth limitations, only one WAV file per hour and FFT data are available for downloading at this time)
% deviceName                                   Location                                 Depth(m)  DateFrom               DateTo
% Ocean Sonics icListen HF Hydrophone 1252     CambridgeBay_Hydrophone_2013             7.6       2013-08-03 00:00:00.0  present
%
%
% EXAMPLES:
% Examples of how to run this routine:
%
% >onchydrophone_getPubData('2011-Jul-01 00:00:00','2011-Jul-01 02:00:00','Ocean Sonics icListen LF Hydrophone 201-109', 'wav', 'C:\Data\Hydrophone\');
% >onchydrophone_getPubData('2013-Jul-01 00:00:00','2013-Jul-01 02:00:00','IOS3 Hydrophone Array 02', 'png','C:\Data\Hydrophone\');
% >onchydrophone_getPubData('2011-Jan-01 00:00:00','2011-Jan-01 02:00:00','Naxys Hydrophone 4', 'wav', 'C:\Data\Hydrophone\');
%
% 2013-Nov
% Jaklyn Vervynck
% Data Manager, Ocean Networks Canada (ONC)
% University of Victoria
% www.oceannetworks.ca
% jverv@uvic.ca
% (250) 472-5367
%
% EDIT HISTORY-------------------------------------------------------------
% 
% 2013-Nov-15 Jaklyn Vervynck - Updated deployment history table.  Added
% PRIVACY notes.  Removed download of thumbnails.
%%

rst=now;
wspath='dmas.uvic.ca'; % Path to DMAS web services server.

if exist(outDir, 'dir')~=7
    mkdir(outDir);
    disp(['Created Directory: ', outDir]);
end

% Get a list of hydrophone files between start time and end
% time using FileNameService and getFilenames method.
fnameservice = ['http://', wspath, '/services/FilenameService?wsdl'];

createClassFromWsdl(fnameservice);
obj = FilenameServiceService;

% Get deviceId
switch deviceName
    case 'IOS3 Hydrophone Array 01'
        deviceid=22;
    case 'IOS3 Hydrophone Array 02'
        deviceid=1007;
    case 'IOS3 Hydrophone Array 03'
        deviceid=22605;
    case 'Naxys Ethernet Hydrophone 02345 Ti 052'
        deviceid=23085;
    case 'Naxys High Gain Hydrophone 2'
        deviceid=21601;
    case 'Naxys Hydrophone 1'
        deviceid=12601;
    case 'Naxys Hydrophone 2'
        deviceid=11207;
    case 'Naxys Hydrophone 3'
        deviceid=20602;
    case 'Naxys Hydrophone 4'
        deviceid=22601;
    case 'Ocean Sonics icListen HF Hydrophone 1205'
        deviceid=13203;
    case 'Ocean Sonics icListen HF Hydrophone 1251'
        deviceid=23157;
    case 'Ocean Sonics icListen HF Hydrophone 1252'
        deviceid=23155;
    case 'Ocean Sonics icListen HF Hydrophone 1253'
        deviceid=23156;
    case 'Ocean Sonics icListen LF Hydrophone 201-109'
        deviceid=22780;
    case 'Ocean Sonics icListen LF Hydrophone 203'
        deviceid=1230;
    case 'Ocean Sonics icListen LF Hydrophone 215'
        deviceid=23113;
    case 'Ocean Sonics icListen LF Hydrophone 216'
        deviceid=23168;
    case 'Ocean Sonics icListen LF Hydrophone 223'
        deviceid=23158;
    case 'Ocean Sonics icListen LF Hydrophone 224'
        deviceid=23159;
    otherwise
        error('deviceName not recognized.  Please check the routine header for a valid device name.');
end

hydfilestr = getFilenames(obj,deviceid,startTimeStr, endTimeStr,lower(dataFormat)); % Note: times need to be in 'yyyy-mmm-dd HH:MM:SS'
filelist = textscan(hydfilestr, '%s', 'delimiter', ',');
hydfiles=filelist{:};
findthumb=strfind(hydfiles, '-small'); % Find all the thumbnails.
clear fnameservice obj startDateString endDateString ext hydfilestr filelist
disp('FilenameService returned a list of file names')
clear tt hydfilestr

% % Get the DMAS dataFileTypeId
% if strcmp(lower(dataFormat), 'wav')==1
%     dataFileTypeId=21;
% elseif strcmp(lower(dataFormat),'png')==1
%     dataFileTypeId=10;
% elseif strcmp(lower(dataFormat),'mp3')==1
%     dataFileTypeId=7;
% elseif strcmp(lower(dataFormat), 'fft')==1
%     dataFileTypeId=50;
% else
%     error('dataFormat not recognized.  Please use ''mp3'', ''wav'' or ''png''.');
% end

if strcmp(hydfiles{1,:}, 'No Data')==1
    disp(['No files for ', deviceName, ' were returned by FilenameService.'])
    disp('Check the deployment history in the header and choose another time period.')
else
    for n=1:length(hydfiles)
        hydfile=hydfiles{n,:};
        
        findthumb=strfind(hydfile, '-small'); % Don't download file if it's thumbnail
        if ~isempty(findthumb)
            continue;
        end
        clear findthumb
        
        % Call AdFile service to import file from aD
        urlString = ['http://' wspath '/AdFile?filename=' hydfile];
        fullFileName=[outDir, filesep, hydfile];
        urlwrite(urlString, fullFileName);

        isGz = isgz(fullFileName);
        if isGz==1
            gzipFileName=[fullFileName, '.gz'];
            movefile(fullFileName,gzipFileName); % Rename the file.  .gz extention is required for gunzip.
            if isunix 
                % Use unix system command gunzip to unzip because MATLAB's gunzip is buggy -
                % it may truncate data without warning.
                system(['gunzip ', gzipFileName]);
            else % Assume Windows operating system.  Use MATLAB gunzip here but be aware data may get truncated.
                gunzip(gzipFileName); 
            end
            % If gzipFileName still exists, delete it
            if exist(gzipFileName, 'file')
                delete(gzipFileName);
            end
        end
        disp(['Downloaded File: ', fullFileName, ' from the ONC Data Archive.'])
        
    end
    disp(['All files between ', startTimeStr, ' and ', endTimeStr, ' have been downloaded to ', outDir]);
    disp([num2str(length(hydfiles)), ' files took: ', num2str((now-rst)*(24*60)), ' minutes to download.'])
end

function [isGz] = isgz(inFile)
%
% isgz.m--Returns true if named file is a gzipped file. Uses the file's
% "magic number", rather than depending on the filename extension.
%
% Syntax: isGz = isgz(inFile)
%
% e.g.,   isGz = isgz('readme.txt')

% Developed in Matlab 7.11.0.584 (R2010b) on GLNX86
% for the VENUS project (http://venus.uvic.ca/).
% Kevin Bartlett (kpb@uvic.ca), 2011-08-05 13:34
%-------------------------------------------------------------------------

fid = fopen(inFile,'rt');

if fid < 1
    error([mfilename '.m--Could not open specified input file.']);
end % if

% Read in 2 bytes of data.
[c,numRead] = fread(fid,2,'uint8');

if numRead < 2
    error([mfilename '.m--Failed to read required 2 bytes required for valid magic number.']);
end % if

fclose(fid);

% From http://en.wikipedia.org/wiki/Gzip, gzip's "magic number" is
% '0x1f8b'.
GZIP_MAGIC_NUMBER = [hex2dec('1f'); hex2dec('8b')];

if isequal(c,GZIP_MAGIC_NUMBER)
    isGz = true;
else
    isGz = false;
end % if