%---------------------------------------------------------------------
% 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:
% Original deviceid = 20116
deviceid = 23215;
%data(1).sensorID = 4250; % temperature
%data(2).sensorID = 4248; % oxygen
%data(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)   
% build the data structure with the sensorIDs   
for isensor = 1:length(sensorIDs)
    data(isensor).sensorID = sensorIDs(isensor);
end
    
% Enter start date and end date for desired time series using Oracle
% timestamp data type. For example:
startdate='17-October-2013 12:00:00 AM';
enddate='18-October-2013 11:59:00 PM';

%Provide the required information in the searchDetails structure
searchDetails.subsampletype=NaN;   % subsampletype options: NaN = none, 1=Averaged Data, 2=Decimated Data, 3=Min/Max Data
searchDetails.subsampleperiod=NaN; % subsampleperiod options (seconds): NaN = none, 60 for minute, 3600 for hourly, 86400 for daily, 604800 for weekly
%searchDetails.networkid=2;         % NaN for default, 1 for venus, 2 for neptune - affects the logo used in the plots only!
searchDetails.dataproductid=1;
searchDetails.searchtypeid=1;      
    
% RETRIEVE TIME SERIES AND BASIC METADATA FOR EACH SENSOR
%[data,meta,nd] = getData(data,startdate,enddate,deviceID,searchDetails);
% NOTES: If your time series is too long, you may experience out of memory errors.  
[data,meta,nd]=getData(data,startdate,enddate,deviceid,searchDetails);
% data 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.
 
%These fields have been modified since the last release, but plotScalarTimeSeries.m was not
%updated to the new API. This code bridges the gap and may be deleted after the next release
%(late september 2012). If left in, this code won't cause issues. plotScalarTimeSeries in the template
%is currently superseded by the version that's in the code library. That will also change.
meta.subsampleTypeID = data(1).subsampleType; 
meta.subsamplePeriod_sec = data(1).subsamplePeriod_sec;
meta.deviceName = meta.deviceDetails(1).deviceName;
meta.deviceID = meta.deviceDetails(1).deviceID;
meta.siteName = meta.location(1).siteName;
meta.lat_degrees = meta.location(1).lat_degrees;
meta.lon_degrees = meta.location(1).lon_degrees;
meta.depth_metres = meta.location(1).depth_metres;
for i = 1:length(data)
    data(i).name = data(i).sensorName(i);
end

% 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(data,meta)
      
    % Generate dataproduct filename using the unique devicecode and timestamp 
    ds = datestr(data(1).time(1),30);
    fileroot = [meta.deviceDetails(1).deviceCode '_' ds 'Z'];
     
    % Save the figure as a PNG
    filename = [fileroot '.png'];
    print('-dpng','-r300',fileroot)
end
  
toc