Home > mrg > MRG_metstation > mrg_met_parse.m

mrg_met_parse

PURPOSE ^

Parses weatherstation data using regular expressions

SYNOPSIS ^

function [out, outhead] = mrg_met_parse(fname, verify)

DESCRIPTION ^

 Parses weatherstation data using regular expressions

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [out, outhead] = mrg_met_parse(fname, verify)
0002 % Parses weatherstation data using regular expressions
0003 
0004 % TODO: Documentation
0005 % 01/02/2013 - Intial atempt DP
0006 
0007 if ~exist('verify', 'var')
0008     verify = 1;
0009 end
0010 
0011 expressn = ['^'...
0012     '(?<time>\d{2}:\d{2}:\d{2})\s+'...
0013     '(?<date>\d{2}\.\d{2}\.\d{2})\s+(\d{1,2}\s+)?'...
0014     '(?<ligh>\d+\.\d+)?\s+(\d{1,2}\s+)?'...
0015     '(?<wspd>\d+\.\d+)?\s+(\d{1,2}\s+)?'...
0016     '(?<wdir>\d{3,6})?\s+(\d{1,2}\s+)?'...
0017     '(?<pres>\d+\.\d+)?\s+(\d{1,2}\s+)?'...
0018     '(?<vol1>\d+\.\d+)?\s+(\d{1,2}\s+)?'...
0019     '(?<vol2>\d+\.\d+)?\s+(\d{1,2}\s+)?'...
0020     '(?<digi>\d+\.\d+)?\s+(\d{1,2}\s+)?'...
0021     '(?<temp>\d+\.\d+)?'];
0022 
0023 %outhead = {'Line', 'Date', 'Time', 'Light', 'WSpeed', 'WDir', 'Pressure', 'Volt1', 'Vol2', 'Digi', 'Temp'};
0024 %out = repmat({NaN, ' ', ' ', NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN}, 5000, 1); % 5000 seems like enough
0025 outhead = {'Line', 'DateTime', 'Light', 'WSpeed', 'WDir', 'Pressure', 'Volt1', 'Vol2', 'Digi', 'Temp'};
0026 out = NaN(5000, 10);
0027 %fileID = fopen('minicom111212_data.txt','r');
0028 fileID = fopen(fname,'r');
0029 tline = fgetl(fileID);
0030 line = 1;
0031 while ischar(tline)
0032     tline = strtrim(tline);
0033     %disp(tline)
0034     [outline] = regexp(tline,expressn,'names');
0035     if ~isempty(outline)
0036         %disp(outline)
0037         try
0038             mtime = datenum([outline.date, ' ', outline.time], 'dd.mm.yy HH:MM:SS');
0039         catch
0040             disp([outline.date, ' ', outline.time, ' - Conversion Failed (Line ', line,' of original file. Substituting NaN']);
0041             mtime = NaN;
0042         end
0043         light = str2double(outline.ligh);
0044         wspeed = str2double(outline.wspd);
0045         wdir = str2double(outline.wdir);
0046         pressure = str2double(outline.pres);
0047         volt1 = str2double(outline.vol1);
0048         volt2 = str2double(outline.vol2);
0049         digi = str2double(outline.digi);
0050         temp = str2double(outline.temp);
0051         
0052         if verify
0053             % Light: If more than 2500, NaN.  If less -10, NaN.  If -5 to 0, then zero.
0054             if light > 2500 || light < -5
0055                 light = NaN;
0056             elseif light < 0
0057                 light = 0;
0058             end
0059             
0060             % Windspeed: Between 0 and 100 m/s
0061             if wspeed < -0.5 || wspeed > 100
0062                 wspeed = NaN;
0063             elseif wspeed < 0
0064                 wspeed = 0;
0065             end
0066             
0067             % WindDirection: Between 0 and 360
0068             if wdir < 0 || wdir > 360
0069                 wspeed = NaN;
0070             end
0071             
0072             % Pressure: Between 600 and 2000
0073             if pressure < 600 || pressure > 2000
0074                 pressure = NaN;
0075             end
0076             
0077             % Temp between -50 and 50
0078             if temp < -50 || temp > 50
0079                 temp = NaN;
0080             end
0081         end
0082         out(line,:) = [line, mtime, light, wspeed, wdir, pressure, volt1, volt2, digi, temp];
0083     else
0084         out(line,:) = [line,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN];
0085     end
0086     
0087     tline = fgetl(fileID);
0088     line = line+1;
0089 end
0090 out(all(isnan(out(:,2:end)), 2),:) = [];
0091 fclose(fileID);
0092 end

Generated on Thu 29-May-2014 21:29:53 by m2html © 2005