comparison m-toolbox/classes/+utils/@mysql/getAOsInTimeSpan.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % GETAOSINTIMESPAN performs high-level queries to retrieve AOs from an LTPDA repository.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: GETAOSINTIMESPAN performs high-level queries to retrieve AOs
5 % from an LTPDA repository. The AOs for that channel in that time-span
6 % are joined together and then split on the requested
7 % interval. The resulting AO covers the requested interval,
8 % assuming that the repositoy contains a contiguos set of AOs
9 % spanning the requested time.
10 %
11 % CALL: b = getAOsInTimeSpan(pl)
12 %
13 % INPUTS: pl - a plist containing parameters as described below
14 %
15 % OUTPUTS: b - a vector of AOs resulting from the query
16 %
17 % PARAMETERS:
18 %
19 % choose from
20 % 'hostname' - hostname/IP address of LTPDA Repository
21 % 'database' - the name of the database to query
22 % or
23 % 'conn' - a database connection object
24 %
25 % in addition, we need
26 % 'binary' - retrieve binary data (true or false)
27 % 'channels' - a cell array of AO names
28 % 'timespan' - a timespan object to specify the
29 % time-interval of interest
30 %
31 % The timespan is applied start <= data < end.
32 %
33 % OPTIONAL PARAMETERS:
34 %
35 % further restrict the search with the following conditions
36 %
37 % 'username' - specify the username of the person who
38 % submitted the AO
39 % 'submitted' - specify the date the AO was submitted
40 %
41 % EXAMPLES:
42 %
43 % 1) Find all AOs which are called 'ChannelX' submitted after 1st May 2007
44 %
45 % pl = plist('hostname', 'localhost', 'database', 'ltpda_test', ...
46 % 'channels', {'channelX'}, ...
47 % 'timespan', timespan('2007-05-01 00:00:00', '3000-01-01 12:34:50', ...
48 % );
49 %
50 % a = getAOsInTimeSpan(pl);
51 %
52 % 2) Find all AOs for ChannelX and ChannelY on 1st May 2007 submitted by
53 % hewitson
54 %
55 % pl = plist('hostname', 'localhost', 'database', 'ltpda_test', ...
56 % 'channels', {'channelX'}, ...
57 % 'timespan', timespan('2007-05-01 00:00:00', '2007-05-02 00:00:00', ...
58 % 'username', 'hewitson');
59 %
60 % a = getAOsInTimeSpan(pl);
61 %
62 % VERSION: $Id: getAOsInTimeSpan.m,v 1.3 2010/01/22 12:46:08 ingo Exp $
63 %
64 % HISTORY: 25-02-08 M Hewitson
65 % Creation
66 %
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68
69 function varargout = getAOsInTimeSpan(varargin)
70
71 error('### Obsolete as of LTPDA version 2.2. Replaced my AO model retrieve_in_timespan');
72 % Collect input ao's, plist's and ao variable names
73 in_names = {};
74 for ii = 1:nargin
75 in_names{end+1} = inputname(ii);
76 end
77 [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names);
78
79 % Process the input plist
80 conn = find(pl, 'conn');
81 hostname = find(pl, 'hostname');
82 database = find(pl, 'database');
83 channels = find(pl, 'channels');
84 tspan = find(pl, 'timespan');
85 useBinary = find(pl, 'binary');
86
87 % Check inputs
88 if isempty(hostname) || ~ischar(hostname)
89 if isempty(conn) || ~isa(conn, 'database')
90 error(sprintf('### Plist should contain ''hostname'' and ''database'' parameters,\n or a ''conn'' parameter.'));
91 end
92 end
93 if isempty(database) || ~ischar(database)
94 if isempty(conn) || ~isa(conn, 'database')
95 error(sprintf('### Plist should contain ''hostname'' and ''database'' parameters,\n or a ''conn'' parameter.'));
96 end
97 end
98 if isempty(channels)
99 error('### Plist should contain a ''channels'' parameter.');
100 end
101 if isempty(tspan) || ~isa(tspan, 'timespan')
102 error('### Plist should contain a ''timespan'' parameter.');
103 end
104
105 % Check the channels value is a cell
106 if ~iscell(channels)
107 channels = {channels};
108 end
109
110 % Get a connection to the database
111 iConnected = 0;
112 if ~isa(conn, 'database')
113 % then we connect
114 try
115 conn = utils.mysql.connect(hostname, database);
116 iConnected = 1;
117 catch
118 error('### Failed to connect to server: %s/%s', hostname, database);
119 end
120 end
121
122 % Collect the data
123 try
124 aout = hlq(conn, channels, tspan, useBinary);
125 catch
126 lasterr
127 % Do we need to close database connection?
128 if iConnected
129 close(conn);
130 end
131 error('### Error retrieving objects.');
132 end
133
134 % Do we need to close database connection?
135 if iConnected
136 close(conn);
137 end
138
139 % set outputs
140 varargout{1} = aout;
141 end
142
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % A prototype for the kind of high-level query functions we might need
145 function objsOut = hlq(conn, channels, ts, useBinary)
146
147 % prepare output
148 objsOut = [];
149
150 % Loop over the channels
151 for j=1:length(channels)
152
153 % this channel
154 channel = channels{j};
155
156 % the query
157 q = ['select objmeta.obj_id from objmeta,ao,tsdata ' ...
158 'where objmeta.obj_id=ao.obj_id ' ...
159 'and ao.data_id=tsdata.id ' ...
160 sprintf('and objmeta.name=''%s''', channel) ...
161 sprintf('and tsdata.t0+INTERVAL tsdata.nsecs SECOND >= ''%s''', char(ts.startT)) ...
162 sprintf('and tsdata.t0 <= ''%s''', char(ts.endT))];
163
164 % execute query
165 info = utils.mysql.dbquery(conn, q);
166
167 % collect objects
168 if useBinary
169 objs = ltpda_uo.retrieve(conn, 'binary', [info{:}]);
170 else
171 objs = ltpda_uo.retrieve(conn, [info{:}]);
172 end
173
174 % join these up
175 ojn = join([objs{:}]);
176
177 % split out the bit we want
178 os = split(ojn, plist('split_type', 'interval', 'timespan', ts));
179
180 % add to outputs
181 objsOut = [objsOut os];
182 end % end loop over channels
183 end
184
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 % Returns the default plist
187 function pl = getDefaultPlist(varargin)
188
189 pl = plist('hostname', 'localhost', ...
190 'database', 'ltpda_test', ...
191 'channels', {'chan1', 'chan2'}, ...
192 'timespan', timespan('1970-01-01 00:00:00', '2010-10-12 12:45:32') ...
193 );
194 end
195
196