%---------------------------------------------------------------------
% Purpose: To demonstrate how to retrieve data for scalar sensors from 
%          the database
% Modify Date/Author:  20101201, R.Jenkyns
%---------------------------------------------------------------------

% Get current time for a reference of processing time
tic

% Device and Sensor IDs: If you do not know your device or sensor IDs,
% refer to the Device Listing at http://dmas.uvic.ca/DeviceListing, and
% select the device to get a list of sensors and their ids 
% For example, for the Aanderaa Optode at Folger Deep:
deviceid=20116;
neptune(1).sensorID=4250; % temperature
neptune(2).sensorID=4248; % oxygen
neptune(3).sensorID=4249; % oxygen saturation

% If you only know the deviceid, you can also get the sensorids (for which data is available) with the following function
sensorids=getDeviceSensorIDs(deviceid)
    
% Enter start date and end date for desired time series using Oracle
% timestamp data type. For example:
startdate='15-June-2010 12:00:00 AM';
enddate='15-June-2010 11:59:00 PM';
    
% RETRIEVE TIME SERIES AND BASIC METADATA FOR EACH SENSOR
%[neptune,meta,nd]=getData(neptune,startdate,enddate,deviceID,subsampletype,subsampleperiod);
% For subsampled data, use the last two optional arguments
% subsampletype options: 1=Averaged Data, 2=Decimated Data, 3=Min/Max Data
% subsampleperiod options (seconds): 60 for minute, 3600 for hourly, 86400 for daily, 604800 for weekly
% NOTES: If your time series is too long, you may experience out of memory errors.  
[neptune,meta,nd]=getData(neptune,startdate,enddate,deviceid,1,60);
 % neptune is a structure array containing the data for each sensor
 % meta is a structure containing basic metadata
 % nd is a boolean which will be 1 if there is time-series data, and 0 otherwise.

% Create a plot of the results if data returned. The plot will appear in the results folder of the current package you are using.
% The plotScalarTimeSeries function is only one option for plotting, but to keep your plot make sure to save the results, since
% a Figure window will not be generated like when running Matlab in a desktop environment.
% Notice that the icon for the plot (once generated) is white. White indicates it is only temporarily saved
% To save any results for the longer term, you must right click on the file and choose Save To Repository. The icon will turn blue. 
if ~nd
    figure(1);
    clf;
    plotScalarTimeSeries(neptune,meta)
      
    % Generate dataproduct filename using the unique devicecode and timestamp 
    ds=datestr(neptune(1).time(1),30);
    fileroot=[meta.deviceCode,'_',ds];
     
    % Savethe figure as a PNG
    filename=[fileroot,'.png'];
    print('-dpng','-r300',fileroot)
end
  
toc