0001 function M = collocation_read_frompipe(hdf5file, fields, query)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 path_to_server = '/storage4/home/gerrit/svn/sources/python/scripts/collocation_read.py';
0030
0031
0032 lockfile = fullfile('/tmp', strrep(hdf5file, '/', '_'));
0033
0034 fields = cellfun(@(c) [c ' '], fields, 'UniformOutput', false);
0035 fields = horzcat(fields{:});
0036 fields = deblank(fields);
0037 scan_format = '%g';
0038
0039 if ~exist(lockfile, 'file')
0040 logtext(atmlab('OUT'), 'Initialising program\n');
0041 c = num2str(round(100*rand));
0042 pipe_in_name = fullfile(atmlab('WORK_AREA'), ['pipereader' strrep(hdf5file, '/', '_') c]);
0043 pipe_out_name = fullfile(atmlab('WORK_AREA'), ['pipewriter' strrep(hdf5file, '/', '_') c]);
0044 log_name = fullfile(atmlab('WORK_AREA'), ['pipelogger' strrep(hdf5file, '/', '_') c]);
0045
0046 command = sprintf([path_to_server ' -vd -i %s -o %s -f %s -l %s'], ...
0047 pipe_in_name, pipe_out_name, hdf5file, log_name);
0048 system(command);
0049 end
0050
0051
0052 wait_for_existence(lockfile, 2, 0.1);
0053 f = fileopen(lockfile, 'r');
0054 [~] = fgets(f);
0055 pipe_in_name = deblank(fgets(f));
0056 pipe_out_name = deblank(fgetl(f));
0057 log_name = deblank(fgetl(f));
0058
0059 wait_for_existence(pipe_out_name, 2, 0.1);
0060 pipe_out = fileopen(pipe_out_name, 'r');
0061 wait_for_existence(log_name, 2, 0.1);
0062 logfile = fileopen(log_name, 'r');
0063 fseek(logfile, -1, 'eof');
0064 wait_for_existence(pipe_in_name, 2, 0.1);
0065 pipe_in = fileopen(pipe_in_name, 'w');
0066
0067
0068 logtext(1, 'Sending query: %s ''FIELDS'' %s\n', query, fields);
0069
0070 fprintf(pipe_in, [query ' FIELDS ' fields '\n']);
0071
0072 i = 0;
0073 while true
0074 i = i + 1;
0075 if mod(i, 100) == 0
0076
0077 fwrite(1, fread(logfile, inf, '*char'));
0078 end
0079 line = fgets(pipe_out);
0080 if strcmp(deblank(line), '.')
0081 break
0082 else
0083 data_in_line = sscanf(line, scan_format);
0084 if i == 1
0085 data_total = data_in_line.';
0086 else
0087 data_total(i, :) = data_in_line;
0088 end
0089 if i == size(data_total, 1)
0090 data_total = [data_total; nan(size(data_total))];
0091 logtext(1, 'Doubling allocation to %d lines\n', size(data_total, 1))
0092 end
0093 end
0094 end
0095 fwrite(1, fread(logfile, inf, '*char'));
0096
0097
0098 M = data_total(~isnan(data_total(:, 1)), :);
0099
0100 end