-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTotalConversion.m
362 lines (304 loc) · 15.9 KB
/
TotalConversion.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
%% TotalConversion_main.m
% This application reads the HFR database for collecting information about
% the total data files to be converted into the European standard data
% model and calls the conversion functions.
% Author: Lorenzo Corgnati
% Date: June 8, 2018
% E-mail: [email protected]
%%
warning('off', 'all');
TC_err = 0;
disp(['[' datestr(now) '] - - ' 'TotalConversion started.']);
%%
%% Connect to database
try
conn = database(sqlConfig.database,sqlConfig.user,sqlConfig.password,'Vendor','MySQL','Server',sqlConfig.host);
disp(['[' datestr(now) '] - - ' 'Connection to database successfully established.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
%%
%% Query the database for retrieving the networks managed by the HFR provider username
% Set and exectute the query
try
HFRPusername_selectquery = ['SELECT network_id FROM account_tb WHERE username = ' '''' HFRPusername ''' '];
HFRPusername_curs = exec(conn,HFRPusername_selectquery);
disp(['[' datestr(now) '] - - ' 'Query to account_tb table for retrieving the networks managed by the HFR provider username successfully executed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Fetch data
try
HFRPusername_curs = fetch(HFRPusername_curs);
HFRPusername_data = HFRPusername_curs.Data;
disp(['[' datestr(now) '] - - ' 'Data of the networks managed by the HFR provider username successfully fetched from account_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Close cursor
try
close(HFRPusername_curs);
disp(['[' datestr(now) '] - - ' 'Cursor to account_tb table successfully closed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
%%
%% Retrieve networks ID managed by the HFR provider username
try
HFRPnetworks = regexp(HFRPusername_data{1}, '[ ,;]+', 'split');
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
%%
%% Query the database for retrieving data from managed networks
% Set and exectute the query
try
network_selectquery = 'SELECT * FROM network_tb WHERE (network_id = ''';
for HFRPntw_idx=1:length(HFRPnetworks)-1
network_selectquery = [network_selectquery HFRPnetworks{HFRPntw_idx} ''' OR network_id = ' ''''];
end
network_selectquery = [network_selectquery HFRPnetworks{length(HFRPnetworks)} ''') AND EU_HFR_processing_flag=0'];
network_curs = exec(conn,network_selectquery);
disp(['[' datestr(now) '] - - ' 'Query to network_tb table for retrieving data of the managed networks successfully executed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Fetch data
try
network_curs = fetch(network_curs);
network_data = network_curs.Data;
disp(['[' datestr(now) '] - - ' 'Data of the managed networks successfully fetched from network_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Retrieve column names
try
network_columnNames = columnnames(network_curs,true);
disp(['[' datestr(now) '] - - ' 'Column names from network_tb table successfully retrieved.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Retrieve the number of networks
try
numNetworks = rows(network_curs);
disp(['[' datestr(now) '] - - ' 'Number of managed networks successfully retrieved from network_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Close cursor
try
close(network_curs);
disp(['[' datestr(now) '] - - ' 'Cursor to network_tb table successfully closed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
%%
%% Scan the networks and convert the related total files
try
% Find the index of the network_id field
network_idIndexC = strfind(network_columnNames, 'network_id');
network_idIndex = find(not(cellfun('isempty', network_idIndexC)));
% Find the index of the input file path field
inputPathIndexC = strfind(network_columnNames, 'total_input_folder_path');
inputPathIndex = find(not(cellfun('isempty', inputPathIndexC)));
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Scan the networks
try
for network_idx=1:numNetworks
TC_err = 0;
% Retrieve information on the stations belonging to the current network
try
station_selectquery = ['SELECT * FROM station_tb WHERE network_id = ' '''' network_data{network_idx,network_idIndex} ''''];
station_curs = exec(conn,station_selectquery);
disp(['[' datestr(now) '] - - ' 'Query to station_tb table for retrieving the stations of the ' network_data{network_idx,network_idIndex} ' network successfully executed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Fetch data
try
station_curs = fetch(station_curs);
station_data = station_curs.Data;
disp(['[' datestr(now) '] - - ' 'Data of the stations of the ' network_data{network_idx,network_idIndex} ' network successfully fetched from station_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Retrieve column names
try
station_columnNames = columnnames(station_curs,true);
disp(['[' datestr(now) '] - - ' 'Column names from station_tb table successfully retrieved.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Retrieve the number of stations belonging to the current network
try
numStations = rows(station_curs);
disp(['[' datestr(now) '] - - ' 'Number of stations belonging to the ' network_data{network_idx,network_idIndex} ' network successfully retrieved from station_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Close cursor to station_tb table
try
close(station_curs);
disp(['[' datestr(now) '] - - ' 'Cursor to station_tb table successfully closed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Retrieve the total files to be converted
try
toBeConvertedTotals_selectquery = ['SELECT * FROM total_input_tb WHERE datetime>' '''' startDate ''' AND network_id = ' '''' network_data{network_idx,network_idIndex} ''' AND NRT_processed_flag = 0'];
toBeConvertedTotals_curs = exec(conn,toBeConvertedTotals_selectquery);
disp(['[' datestr(now) '] - - ' 'Query to total_input_tb table for retrieving the total files from ' network_data{network_idx,network_idIndex} ' network to be converted successfully executed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Fetch data
try
toBeConvertedTotals_curs = fetch(toBeConvertedTotals_curs);
toBeConvertedTotals_data = toBeConvertedTotals_curs.Data;
disp(['[' datestr(now) '] - - ' 'Data of the total files from ' network_data{network_idx,network_idIndex} ' network to be converted successfully fetched from total_input_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Retrieve column names
try
toBeConvertedTotals_columnNames = columnnames(toBeConvertedTotals_curs,true);
disp(['[' datestr(now) '] - - ' 'Column names from total_input_tb table successfully retrieved.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Retrieve the number of totals to be converted for the current network
try
numToBeConvertedTotals = rows(toBeConvertedTotals_curs);
disp(['[' datestr(now) '] - - ' 'Number of total files from ' network_data{network_idx,network_idIndex} ' network to be converted successfully retrieved from total_input_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Close cursor to total_input_tb table
try
close(toBeConvertedTotals_curs);
disp(['[' datestr(now) '] - - ' 'Cursor to total_input_tb table successfully closed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
try
% Find the index of the filename field
filenameIndexC = strfind(toBeConvertedTotals_columnNames, 'filename');
filenameIndex = find(not(cellfun('isempty', filenameIndexC)));
% Find the index of the filepath field
filepathIndexC = strfind(toBeConvertedTotals_columnNames, 'filepath');
filepathIndex = find(not(cellfun('isempty', filepathIndexC)));
% Find the index of the extension field
extensionIndexC = strfind(toBeConvertedTotals_columnNames, 'extension');
extensionIndex = find(not(cellfun('isempty', extensionIndexC)));
% Find the index of the timestamp field
timestampIndexC = strfind(toBeConvertedTotals_columnNames, 'timestamp');
timestampIndex = find(not(cellfun('isempty', timestampIndexC)));
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Scan the tuv files to be converted
for toBeConverted_idx=1:numToBeConvertedTotals
TC_err = 0;
try
if (strcmp(toBeConvertedTotals_data{toBeConverted_idx,extensionIndex}, '.tuv')) % Codar data
% v2.2
[TC_err, network_data(network_idx,:), outputFilename, outputFilesize] = tuv2netCDF_v22([toBeConvertedTotals_data{toBeConverted_idx,filepathIndex} filesep toBeConvertedTotals_data{toBeConverted_idx,filenameIndex}],toBeConvertedTotals_data{toBeConverted_idx,timestampIndex},network_data(network_idx,:),network_columnNames,station_data,station_columnNames);
elseif (strcmp(toBeConvertedTotals_data{toBeConverted_idx,extensionIndex}, '.cur_asc')) % WERA data
% v2.2
[TC_err, network_data(network_idx,:), outputFilename, outputFilesize] = curAsc2netCDF_v22([toBeConvertedTotals_data{toBeConverted_idx,filepathIndex} filesep toBeConvertedTotals_data{toBeConverted_idx,filenameIndex}],toBeConvertedTotals_data{toBeConverted_idx,timestampIndex},network_data(network_idx,:),network_columnNames,station_data,station_columnNames);
elseif (strcmp(toBeConvertedTotals_data{toBeConverted_idx,extensionIndex}, '.asc')) % WERA data
% v2.2
[TC_err, network_data(network_idx,:), outputFilename, outputFilesize] = ascTot2netCDF_v22([toBeConvertedTotals_data{toBeConverted_idx,filepathIndex} filesep toBeConvertedTotals_data{toBeConverted_idx,filenameIndex}],toBeConvertedTotals_data{toBeConverted_idx,timestampIndex},network_data(network_idx,:),network_columnNames,station_data,station_columnNames);
end
if(TC_err==0)
disp(['[' datestr(now) '] - - ' outputFilename ' total netCDF v2.2 file successfully created and stored.']);
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Update NRT_processed_flag in total_input_tb table
try
if(TC_err==0)
% Find the index of the NRT_processed_flag field
NRT_processed_flagIndex = find(not(cellfun('isempty', strfind(toBeConvertedTotals_columnNames, 'NRT_processed_flag'))));
% Define a cell array containing the column names to be updated
updateColnames = {'NRT_processed_flag'};
% Define a cell array that contains the data for insertion
updateData = {1};
% Update the total_input_tb table on the database
tablename = 'total_input_tb';
whereclause = ['WHERE filename = ' '''' toBeConvertedTotals_data{toBeConverted_idx,filenameIndex} ''''];
update(conn,tablename,updateColnames,updateData,whereclause);
disp(['[' datestr(now) '] - - ' 'total_input_tb table successfully updated with NRT processed flag for timestamp ' toBeConvertedTotals_data{toBeConverted_idx,timestampIndex} '.']);
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
% Insert converted total info in total_HFRnetCDF_tb table
try
if((TC_err==0) && (exist('outputFilename','var') ~= 0))
% Delete the eventually present entry with the same name from the database
total_deletequery = ['DELETE FROM total_HFRnetCDF_tb WHERE filename = ' '''' outputFilename ''''];
total_deletecurs = exec(conn,total_deletequery);
% Evaluate datetime from Time Stamp
[t2d_err,DateTime] = timestamp2datetime(toBeConvertedTotals_data{toBeConverted_idx,timestampIndex});
% Define a cell array containing the column names to be added
addColnames = {'filename' 'network_id' 'timestamp' 'datetime' 'creation_date' 'filesize' 'input_filename' 'check_flag'};
% Define a cell array that contains the data for insertion
addData = {outputFilename,network_data{network_idx,network_idIndex},toBeConvertedTotals_data{toBeConverted_idx,timestampIndex},DateTime,(datestr(now,'yyyy-mm-dd HH:MM:SS')),outputFilesize,toBeConvertedTotals_data{toBeConverted_idx,filenameIndex},0};
% Append the product data into the total_HFRnetCDF_tb table on the database.
tablename = 'total_HFRnetCDF_tb';
datainsert(conn,tablename,addColnames,addData);
disp(['[' datestr(now) '] - - ' outputFilename 'total file information successfully inserted into total_HFRnetCDF_tb table.']);
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
clear outputFilename outputFilesize;
end
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
%%
%% Close connection
try
close(conn);
disp(['[' datestr(now) '] - - ' 'Connection to database successfully closed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TC_err = 1;
end
%%
if(TC_err==0)
disp(['[' datestr(now) '] - - ' 'TotalConversion successfully executed.']);
end
pause(1200);