Home > m > mysql > ltpda_getAOsInTimeSpan.m

ltpda_getAOsInTimeSpan

PURPOSE ^

LTPDA_GETAOSINTIMESPAN performs high-level queries to retrieve AOs from an LTPDA repository.

SYNOPSIS ^

function varargout = ltpda_getAOsInTimeSpan(varargin)

DESCRIPTION ^

 LTPDA_GETAOSINTIMESPAN performs high-level queries to retrieve AOs from an LTPDA repository.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: LTPDA_GETAOSINTIMESPAN performs high-level queries to retrieve AOs 
              from an LTPDA repository. The AOs for that channel in that time-span
              are joined together and then split on the requested
              interval. The resulting AO covers the requested interval,
              assuming that the repositoy contains a contiguos set of AOs
              spanning the requested time.

 CALL:        b = ltpda_getAOsInTimeSpan(pl)

 INPUTS:      pl - a plist containing parameters as described below
 
 OUTPUTS:     b  - a vector of AOs resulting from the query
 
 PARAMETERS:
 
     choose from
              'hostname'       - hostname/IP address of LTPDA Repository
              'database'       - the name of the database to query
          or
              'conn'           - a database connection object
       
    in addition, we need
              'channels'       - a cell array of AO names
              'timespan'       - a timespan object to specify the
                                 time-interval of interest
 
 The timespan is applied start <= data < end.
 
 OPTIONAL PARAMETERS:
 
    further restrict the search with the following conditions
 
               'username'   - specify the username of the person who
                              submitted the AO
               'submitted'  - specify the date the AO was submitted
 
 EXAMPLES:
 
 1) Find all AOs which are called 'ChannelX' submitted after 1st May 2007
 
    pl = plist('hostname', 'localhost', 'database', 'ltpda_test', ...
               'channels', {'channelX'}, ...
               'timespan', timespan('2007-05-01 00:00:00', '3000-01-01 12:34:50', ...
               );
 
    a = ltpda_getAOsInTimeSpan(pl);
 
 2) Find all AOs for ChannelX and ChannelY on 1st May 2007 submitted by
    hewitson
  
    pl = plist('hostname', 'localhost', 'database', 'ltpda_test', ...
               'channels', {'channelX'}, ...
               'timespan', timespan('2007-05-01 00:00:00', '2007-05-02 00:00:00', ...
               'username', 'hewitson');
 
    a = ltpda_getAOsInTimeSpan(pl);
 
 VERSION:     $Id: ltpda_getAOsInTimeSpan.m,v 1.1 2008/02/25 19:47:22 hewitson Exp $

 The following call returns a parameter list object that contains the
 default parameter values:

 >> pl = ltpda_getAOsInTimeSpan('Params')

 The following call returns a string that contains the routine CVS version:

 >> version = getAOsInTimeSpan('Version')

 The following call returns a string that contains the routine category:

 >> category = ltpda_getAOsInTimeSpan('Category')

 HISTORY: 25-02-08 M Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = ltpda_getAOsInTimeSpan(varargin)
0002 
0003 % LTPDA_GETAOSINTIMESPAN performs high-level queries to retrieve AOs from an LTPDA repository.
0004 %
0005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0006 %
0007 % DESCRIPTION: LTPDA_GETAOSINTIMESPAN performs high-level queries to retrieve AOs
0008 %              from an LTPDA repository. The AOs for that channel in that time-span
0009 %              are joined together and then split on the requested
0010 %              interval. The resulting AO covers the requested interval,
0011 %              assuming that the repositoy contains a contiguos set of AOs
0012 %              spanning the requested time.
0013 %
0014 % CALL:        b = ltpda_getAOsInTimeSpan(pl)
0015 %
0016 % INPUTS:      pl - a plist containing parameters as described below
0017 %
0018 % OUTPUTS:     b  - a vector of AOs resulting from the query
0019 %
0020 % PARAMETERS:
0021 %
0022 %     choose from
0023 %              'hostname'       - hostname/IP address of LTPDA Repository
0024 %              'database'       - the name of the database to query
0025 %          or
0026 %              'conn'           - a database connection object
0027 %
0028 %    in addition, we need
0029 %              'channels'       - a cell array of AO names
0030 %              'timespan'       - a timespan object to specify the
0031 %                                 time-interval of interest
0032 %
0033 % The timespan is applied start <= data < end.
0034 %
0035 % OPTIONAL PARAMETERS:
0036 %
0037 %    further restrict the search with the following conditions
0038 %
0039 %               'username'   - specify the username of the person who
0040 %                              submitted the AO
0041 %               'submitted'  - specify the date the AO was submitted
0042 %
0043 % EXAMPLES:
0044 %
0045 % 1) Find all AOs which are called 'ChannelX' submitted after 1st May 2007
0046 %
0047 %    pl = plist('hostname', 'localhost', 'database', 'ltpda_test', ...
0048 %               'channels', {'channelX'}, ...
0049 %               'timespan', timespan('2007-05-01 00:00:00', '3000-01-01 12:34:50', ...
0050 %               );
0051 %
0052 %    a = ltpda_getAOsInTimeSpan(pl);
0053 %
0054 % 2) Find all AOs for ChannelX and ChannelY on 1st May 2007 submitted by
0055 %    hewitson
0056 %
0057 %    pl = plist('hostname', 'localhost', 'database', 'ltpda_test', ...
0058 %               'channels', {'channelX'}, ...
0059 %               'timespan', timespan('2007-05-01 00:00:00', '2007-05-02 00:00:00', ...
0060 %               'username', 'hewitson');
0061 %
0062 %    a = ltpda_getAOsInTimeSpan(pl);
0063 %
0064 % VERSION:     $Id: ltpda_getAOsInTimeSpan.m,v 1.1 2008/02/25 19:47:22 hewitson Exp $
0065 %
0066 % The following call returns a parameter list object that contains the
0067 % default parameter values:
0068 %
0069 % >> pl = ltpda_getAOsInTimeSpan('Params')
0070 %
0071 % The following call returns a string that contains the routine CVS version:
0072 %
0073 % >> version = getAOsInTimeSpan('Version')
0074 %
0075 % The following call returns a string that contains the routine category:
0076 %
0077 % >> category = ltpda_getAOsInTimeSpan('Category')
0078 %
0079 % HISTORY: 25-02-08 M Hewitson
0080 %             Creation
0081 %
0082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0083 
0084 ALGONAME = mfilename;
0085 VERSION  = '$Id: ltpda_getAOsInTimeSpan.m,v 1.1 2008/02/25 19:47:22 hewitson Exp $';
0086 CATEGORY = 'Constructor';
0087 
0088 % Check if this is a call for parameters
0089 if nargin == 1
0090   if ischar(varargin{1})
0091     in = char(varargin{1});
0092     if strcmp(in, 'Params')
0093       varargout{1} = getDefaultPlist();
0094       return
0095     elseif strcmp(in, 'Version')
0096       varargout{1} = VERSION;
0097       return
0098     elseif strcmp(in, 'Category')
0099       varargout{1} = CATEGORY;
0100       return
0101     end
0102   end
0103 end
0104 
0105 % Collect input ao's, plist's and ao variable names
0106 in_names = {};
0107 for ii = 1:nargin
0108   in_names{end+1} = inputname(ii);
0109 end
0110 [as, pl, invars] = collect_inputs(varargin, in_names);
0111 
0112 % Process the input plist
0113 conn     = find(pl, 'conn');
0114 hostname = find(pl, 'hostname');
0115 database = find(pl, 'database');
0116 channels = find(pl, 'channels');
0117 tspan    = find(pl, 'timespan');
0118 
0119 % Check inputs
0120 if isempty(hostname) || ~ischar(hostname)
0121   if isempty(conn) || ~isa(conn, 'database')
0122     error(sprintf('### Plist should contain ''hostname'' and ''database'' parameters,\n or a ''conn'' parameter.'));
0123   end
0124 end
0125 if isempty(database) || ~ischar(database)
0126   if isempty(conn) || ~isa(conn, 'database')
0127     error(sprintf('### Plist should contain ''hostname'' and ''database'' parameters,\n or a ''conn'' parameter.'));
0128   end
0129 end
0130 if isempty(channels)
0131   error('### Plist should contain a ''channels'' parameter.');
0132 end
0133 if isempty(tspan) || ~isa(tspan, 'timespan')
0134   error('### Plist should contain a ''timespan'' parameter.');
0135 end
0136 
0137 % Check the channels value is a cell
0138 if ~iscell(channels)
0139   channels = {channels};
0140 end
0141 
0142 % Get a connection to the database
0143 iConnected = 0;
0144 if ~isa(conn, 'database')
0145   % then we connect
0146   try
0147     conn = mysql_connect(hostname, database);
0148     iConnected = 1;
0149   catch
0150     error('### Failed to connect to server: %s/%s', hostname, database);
0151   end
0152 end
0153 
0154 % Collect the data
0155 try
0156   aout =  hlq(conn, channels, tspan);
0157 catch
0158   % Do we need to close database connection?
0159   if iConnected
0160     close(conn);
0161   end
0162   error('### Error retrieving objects.');
0163 end
0164 
0165 % Do we need to close database connection?
0166 if iConnected
0167   close(conn);
0168 end
0169 
0170 % set outputs
0171 varargout{1} = aout;
0172 
0173 %--------------------------------------------------------------------------
0174 % A prototype for the kind of high-level query functions we might need
0175 function objsOut = hlq(conn, channels, ts)
0176 
0177 % prepare output
0178 objsOut = [];
0179 
0180 % Loop over the channels
0181 for j=1:length(channels)
0182 
0183   % this channel
0184   channel = channels{j};
0185 
0186   % the query
0187   q =      ['select objmeta.obj_id from objmeta,ao,tsdata ' ...
0188             'where objmeta.obj_id=ao.obj_id ' ...
0189             'and ao.data_id=tsdata.id ' ...
0190     sprintf('and objmeta.name=''%s''', channel) ...
0191     sprintf('and tsdata.t0+INTERVAL tsdata.nsecs SECOND  >= ''%s''', ts.start.time_str) ...
0192     sprintf('and tsdata.t0 <= ''%s''', ts.end.time_str)];
0193 
0194   % execute query
0195   info = ltpda_dbquery(conn, q);
0196   
0197   % collect objects
0198   objs = ltpda_obj_retrieve(conn, [info{:}]);
0199   
0200   % join these up
0201   ojn = join([objs{:}]);
0202   
0203   % split out the bit we want
0204   os = split(ojn, plist('split_type', 'interval', 'timespan', ts));
0205   
0206   % add to outputs
0207   objsOut = [objsOut os];  
0208 end % end loop over channels
0209 
0210 %--------------------------------------------------------------------------
0211 % Returns the default plist
0212 function pl = getDefaultPlist(varargin)
0213 
0214 pl = plist('hostname', 'localhost', ...
0215            'database', 'ltpda_test',  ...
0216            'channels', {'chan1', 'chan2'}, ...
0217            'timespan', timespan('1970-01-01 00:00:00', '2010-10-12 12:45:32') ...
0218            );
0219 
0220 
0221 % END
0222

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003