function [a_array,serial_num] = extract_ecodata(p,days) % Extracts SSDS data from IAG's GetOriginalDataServlet % Usage: % [outarray,serialnum] = extract_ecodata('ecobb') % Inputs: p is a structure a result of the call to getprops.m % days: day range, i.e. 7 days, 30, 60 % Outputs: a_array : data output array containing serial date, ref light, % ref, thermistor, siam timestamp, oasis timestamp % serialnum : serial number of ECO sensor as read from data stream % ecobb : WetLabs ECO scattering meter % ecofl : WetLabs ECO fluorometer % Data input is in the following format: %sourceID, GMTTime, systemTime, databuffer... %1301, 20 May 2004 01:34:10 GMT, 1085016850496, 141.0654 05/20/04 01:34:07 1588 4121 562 %05/20/04 01:34:08 1581 4121 561 %05/20/04 01:34:08 1577 4121 561 %05/20/04 01:34:08 1574 4121 561 %05/20/04 01:34:09 1572 4121 561 % %1301, 20 May 2004 01:44:04 GMT, 1085017444938, 141.07227 05/20/04 01:44:03 1590 4121 564 %05/20/04 01:44:03 1582 4122 564 %05/20/04 01:44:03 1578 4121 564 %05/20/04 01:44:04 1576 4121 564 %05/20/04 01:44:04 1573 4121 563 % % Data input for FLNTUS-518 %sourceID, GMTTime, systemTime, databuffer... %1562, 1 Aug 2006 17:39:10 GMT, 1154453950071, 213.73553 mvs 1 %08/01/06 17:38:46 1467 144 1358 148 542 %08/01/06 17:38:47 1468 142 1356 150 541 %08/01/06 17:38:48 1468 144 1356 149 541 %08/01/06 17:38:49 1468 143 1355 149 541 %08/01/06 17:38:49 1468 141 1355 146 541 %mvs 0 % line_cnt = 0; empty_lines=0; %prop = readProperties([cdir]); %devnum=str2num(getProperty(prop,'calibration.dev')); %serial=getProperty(prop,'calibration.serial'); %pdir=getProperty(prop,'process_path'); %ldir=getProperty(prop,'log_path'); % http://ssdspub.mbari.org:8080/access/GetOriginalDataServlet?deviceID=1301& recordTypeID=1&isi=1&numHoursBack=168&noHTMLHeader=0&prependWith=oasis URL1=['http://ssds.shore.mbari.org:8080/access/GetOriginalDataServlet?deviceID=',(p.dev),'&platform=',(p.platform),'&recordTypeID=1&isi=1']; %URL1=['http://new-ssds.mbari.org:8080/servlet/GetOriginalDataServlet?deviceID=',(p.dev),'&recordTypeID=1&isi=1']; if days==1 t=datestr(now-1,30); startD=[t(1:8),'.000000']; t=datestr(now,30); endD=[t(1:8),'.000000']; URL=[URL1, '&startDateTime=',startD,'&endDateTime=',endD,'&noHTMLHeader=0&prependWith=oasis']; else URL=[URL1, '&numHoursBack=',num2str(days*24),'&noHTMLHeader=0&prependWith=oasis']; end %if days==1 disp(['extract_ecodata: Reading ',p.ecotype, ' ', p.dev, ' data from internet...']) disp(URL); ssds_data = urlread(URL); disp(['Read ' num2str(length(ssds_data)) ' bytes.']); if days==1 disp(['Start Date: ',startD,' End Date: ',endD]); end clear startD endD t; pack; %parse out the data file, one line at a time linefeeds = findstr(char(10),ssds_data); %size(linefeeds,2) for index = 1:size(linefeeds,2)-1, %for index = 1:39, % for debug so we don't have to go through the whole dataset line = ssds_data(linefeeds(index)+1:linefeeds(index+1)-1); %test to make sure this is a valid record if ~isempty(line) && line(1) >= '0' && line(1) <= '9' %line(1:4) tmpline=line; tmptoken=''; if line(1:4) == p.dev tcount=0; [serial_num,tmpline]=strtok(tmpline,','); %Grab the serial number and the comma. [tmptoken,tmpline]=strtok(tmpline); [item1,tmpline] = strtok(tmpline,'G'); %Date % Parse the line, when you come to the string DATA go to second while loop to pick up the valid data while ~(isempty(tmpline) | strcmp(tmptoken,'GMT,')) [tmptoken,tmpline]=strtok(tmpline); tcount=tcount+1; %debugging device, count tokens end % while isempty line % The next data should be the timestamps [ts, tmpline] = strtok(tmpline,','); %Siam timestamp [tmptoken,tmpline]=strtok(tmpline); % read passed the comma [os, tmpline] = strtok(tmpline); %Oasis timestamp %[tmptoken,tmpline] = strtok(tmpline); %Device date %[tmptoken,tmpline] = strtok(tmpline); %Device time %[item3,tmpline] = strtok(tmpline); %ref light %[item4,tmpline] = strtok(tmpline); %vol scattering %[item5,tmpline] = strtok(tmpline); %temperature %line_cnt = line_cnt + 1; elseif line(1) >= '0' && line(1) <= '9' % The next five lines should contain data. Have adjusted the loop will continue until a blank line is reached %while ~(isempty(tmpline) | strcmp(tmpline, 'RDY')) [tmptoken,tmpline] = strtok(tmpline); %Device date [tmptoken,tmpline] = strtok(tmpline); %Device time [item3,tmpline] = strtok(tmpline); %chl ref light [item4,tmpline] = strtok(tmpline); %chl vol scattering [item5,tmpline] = strtok(tmpline); %bb ref light [item6,tmpline] = strtok(tmpline); %bb vol scattering [item7,tmpline] = strtok(tmpline); %temperature line_cnt = line_cnt + 1; %Load the items into the array to be returned a_array(line_cnt, 1) = datenum(item1); %date and time a_array(line_cnt, 2) = str2double(item3); %chl ref light a_array(line_cnt, 3) = str2double(item4); % chl signal a_array(line_cnt, 4) = str2double(item5); % bb ref a_array(line_cnt, 5) = str2double(item6); % bb signal a_array(line_cnt, 6) = str2double(item7); % thermistor a_array(line_cnt, 7) = str2double(ts); % siam timestamp a_array(line_cnt, 8) = str2double(os); % oasis timestamp %disp(['line=',int2str(line_cnt),' ts=',ts,' date=', item1, ' ref light=',item3, ' ref=',item4,' temp=',item5]); end % if line(1:4) == devnum else %if ~isempty(line) && line(1) >= '0' && line(1) <= '9' empty_lines = empty_lines + 1; end % ~isempty(line) && line(1) >= '0' && line(1) <= '9' clear item3 item4 item5; end % end for if (empty_lines == length(linefeeds)-1) disp('No data was extracted'); fid=fopen([p.ldir, '/',p.log],'at+'); fprintf(fid,'%s\n',[' 2. EXTRACT_ECODATA: ',p.ecotype, ' Read ',num2str(length(ssds_data)) ' bytes, processed ',num2str(line_cnt), ' lines.' ]); fprintf(fid,'%s\n',[' ERROR: No data was returned']); fclose(fid); %exit; else date_time=a_array(:,1); ref_light=a_array(:,2); ref_data=a_array(:,3); bb_ref=a_array(:,4); bb=a_array(:,5); temp=a_array(:,6); ts=a_array(:,7); os=a_array(:,8); fid=fopen([p.pdir,'/', p.ecotype, '_parsed.csv'],'w+','b'); fprintf(fid,'%s,%s,%s,%s,%s,%s,%s,%s\n','%date','chl_ref','chl_signal','bb_ref','bb_signal','temp','sdn','otime'); [row col]=size(a_array); for J=1: row fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f\n',date_time(J,:),ref_light(J,:),ref_data(J,:),bb_ref(J,:),bb(J,:),temp(J,:),ts(J,:),os(J,:)); end fclose(fid); disp(['Finished processing ' num2str(line_cnt) ' lines. Parsed data written to ',p.pdir]); end % Write to log file fid=fopen([p.ldir, '/',p.log],'at+'); fprintf(fid,'%s\n',[' 2. EXTRACT_ECODATA: ',p.ecotype, ' Read ',num2str(length(ssds_data)) ' bytes, processed ',num2str(line_cnt), ' lines.' ]); %fprintf(fid,'%s\n',[' Start Date: ',startD,' End Date: ',endD]); fprintf(fid,'%s\n',[' URL = ',URL(1:51)]); fprintf(fid,'%s\n',[' ',URL(52:105)]); endurl=length(URL); fprintf(fid,'%s\n',[' ',URL(106:endurl)]); fclose(fid); return;