Home > mrg > MRG_metstation > mrg_met_output.m

mrg_met_output

PURPOSE ^

Write met data to file(s) and upload to a webpage

SYNOPSIS ^

function str = mrg_met_output(out)

DESCRIPTION ^

 Write met data to file(s) and upload to a webpage

 INPUT
   out     A string read from RS232 (by mrg_met_control) that will be 
           coerced into data

 OUTPUT
   str     A string reporting the outcome of coercing, uploading and 
           saving the data 
   This function also produces the following files:
       met_raw_data_yyyy_mmm.txt - Raw Data provided as 'out'
       met_data_yyyy_mmm.csv - Nicely formatted data (with header text)
   Where yyyy and mmm are the year and abbreviated month name, repectivly.
   The function tries very hard to write something to file each time.  If
   the data don't pass required checks then MIKE by DHI delete values
   (-1e-30) are written in place.  

 NOTES
   This function reads a password from a file named
   'wunderground.password', which is assumed to be in the current working
   directory.  This must have a single line only with the password for
   your wunderground account.
       
 REQUIREMENTS
   Requires mrg_met_urlread

 AUTHORS
   Bjoern Elsaesser
   Daniel Pritchard

 LICENCE
   Code distributed as part of the MRG toolbox from the Marine Research
   Group at Queens Univeristy Belfast (QUB) School of Planning
   Architecture and Civil Engineering (SPACE). Distributed under a
   creative commons CC BY-SA licence, retaining full copyright of the
   original authors.

   http://creativecommons.org/licenses/by-sa/3.0/
   http://www.qub.ac.uk/space/
   http://www.qub.ac.uk/research-centres/eerc/

 DEVELOPMENT
   v 1.0   2010
           First version. BE.
   v 2.0   02 Sept 2011. BE
           Raw data output corrected.
           Some error handling added and a proper output string added.
   v 2.1   26 Jan 2012 DP
           Modified the defualt version of urlread to include a timeout.  
           Old version copied to urlread_old.m
   v 2.2     21 March 2012 DP
           Modified conversion factor for WUnderground windspeed from 
           1.151 to 2.2369. Raw data is not affected but uploaded 
           windspeed was too low.
   v 3.0   August 2013
           Major re-write.
           Clean up and document. Move into MRG toolbox.
           urlread is now included in the toolbox as mrg_met_urlread
           CSV file now has a header row, indicating what is what
           Password is now read from file (excluded from toolbox repository)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function str = mrg_met_output(out)
0002 % Write met data to file(s) and upload to a webpage
0003 %
0004 % INPUT
0005 %   out     A string read from RS232 (by mrg_met_control) that will be
0006 %           coerced into data
0007 %
0008 % OUTPUT
0009 %   str     A string reporting the outcome of coercing, uploading and
0010 %           saving the data
0011 %   This function also produces the following files:
0012 %       met_raw_data_yyyy_mmm.txt - Raw Data provided as 'out'
0013 %       met_data_yyyy_mmm.csv - Nicely formatted data (with header text)
0014 %   Where yyyy and mmm are the year and abbreviated month name, repectivly.
0015 %   The function tries very hard to write something to file each time.  If
0016 %   the data don't pass required checks then MIKE by DHI delete values
0017 %   (-1e-30) are written in place.
0018 %
0019 % NOTES
0020 %   This function reads a password from a file named
0021 %   'wunderground.password', which is assumed to be in the current working
0022 %   directory.  This must have a single line only with the password for
0023 %   your wunderground account.
0024 %
0025 % REQUIREMENTS
0026 %   Requires mrg_met_urlread
0027 %
0028 % AUTHORS
0029 %   Bjoern Elsaesser
0030 %   Daniel Pritchard
0031 %
0032 % LICENCE
0033 %   Code distributed as part of the MRG toolbox from the Marine Research
0034 %   Group at Queens Univeristy Belfast (QUB) School of Planning
0035 %   Architecture and Civil Engineering (SPACE). Distributed under a
0036 %   creative commons CC BY-SA licence, retaining full copyright of the
0037 %   original authors.
0038 %
0039 %   http://creativecommons.org/licenses/by-sa/3.0/
0040 %   http://www.qub.ac.uk/space/
0041 %   http://www.qub.ac.uk/research-centres/eerc/
0042 %
0043 % DEVELOPMENT
0044 %   v 1.0   2010
0045 %           First version. BE.
0046 %   v 2.0   02 Sept 2011. BE
0047 %           Raw data output corrected.
0048 %           Some error handling added and a proper output string added.
0049 %   v 2.1   26 Jan 2012 DP
0050 %           Modified the defualt version of urlread to include a timeout.
0051 %           Old version copied to urlread_old.m
0052 %   v 2.2     21 March 2012 DP
0053 %           Modified conversion factor for WUnderground windspeed from
0054 %           1.151 to 2.2369. Raw data is not affected but uploaded
0055 %           windspeed was too low.
0056 %   v 3.0   August 2013
0057 %           Major re-write.
0058 %           Clean up and document. Move into MRG toolbox.
0059 %           urlread is now included in the toolbox as mrg_met_urlread
0060 %           CSV file now has a header row, indicating what is what
0061 %           Password is now read from file (excluded from toolbox repository)
0062 
0063 %% First, dump the raw data to file...
0064 fileID_raw = fopen(['met_raw_data_',datestr(date,'yyyy_mmm'),'.txt'],'a');
0065 fprintf(fileID_raw,[out,'\r\n']);
0066 fclose(fileID_raw);
0067 
0068 %% Parse data and prepare to write useful data to a CSV file
0069 [C,position] = textscan(out,'%s %s %f %f %f %f %f %f %f %f %f %f');
0070 
0071 % Try to coerce the first two strings to a MATLAB datenum.
0072 % If not we use the current date and time...
0073 try
0074     K(1) = datenum([char(C{2}),' ',char(C{1})],'dd.mm.yy HH:MM:SS');
0075 catch err
0076     K(1) = now;
0077 end
0078 
0079 % Format a date string for the CSV file...
0080 DateS = datestr(K(1),'dd/mm/yyyy HH:MM:SS');
0081 % Get a filename for the CSV file
0082 fnameCSV = ['met_data_',datestr(date,'yyyy_mmm'),'.csv'];
0083 % If the CSV file doesn't exist, then write a header line...
0084 if ~exist(fnameCSV, 'file')
0085     csv_hdr = 'DateTime,PAR,WindSpeed,WindDirection,AirPressure,AirTemp,TidalLevel,WaterTemp\r\n';
0086     fileID_csv = fopen(fnameCSV,'a');
0087     fprintf(fileID_csv,csv_hdr);
0088     fclose(fileID_csv);
0089 end
0090 % Create a CSV error string (to fill the gap if an error is found)...
0091 % Needs to be a date an 7 delete values
0092 csv_err = [DateS,',-1e-30,-1e-30,-1e-30,-1e-30,-1e-30,-1e-30,-1e-30\r\n'];
0093 
0094 %% Data Checks
0095 if position < 98 % Check length of string. If too short data is missing.
0096     % Write delete values to file
0097     fileID_csv = fopen(fnameCSV,'a');
0098     fprintf(fileID_csv,csv_err);
0099     fclose(fileID_csv);
0100     % Report error
0101     str = [DateS, ': ERROR 01 - The string was too short! Delete values written in place'];
0102     return
0103 end
0104 
0105 if any(cellfun('isempty',C)) % Check if all cells contain a value.
0106     fileID_csv = fopen(fnameCSV,'a');
0107     fprintf(fileID_csv,csv_err);
0108     fclose(fileID_csv);
0109     str = [DateS, ': ERROR 02 - Some values were missing! Delete values written in place'];
0110     return
0111 end
0112 
0113 %% Data is OK...
0114 K(2:11) = cell2mat(C(3:12));
0115 
0116 %% Calibrate wind direction data
0117 % 270 on vane = 318 in reality
0118 % Equals 48 degree offset
0119 % So 'North' equals 312 on vane
0120 if K(5)<312
0121     K(5) = K(5)+48;
0122 else
0123     K(5) = K(5)-312;
0124 end
0125 
0126 %% Construct CSV string and write to file
0127 % August 2013
0128 % Added K(8) and K(9), which should be tidal level and water temperature.
0129 csv_str = [DateS,',',...
0130     num2str(K(3)),',',...
0131     num2str(K(4)),',',...
0132     num2str(K(5)),',',...
0133     num2str(K(7)),',',...
0134     num2str(K(11)),',',...
0135     num2str(K(8)),',',...
0136     num2str(K(9)),'\r\n'];
0137 
0138 fileID_csv = fopen(fnameCSV,'a');
0139 fprintf(fileID_csv,csv_str);
0140 fclose(fileID_csv);
0141 str = [DateS, ': OK. Data written to CSV file.'];
0142 
0143 %% Create a text string for web
0144 try
0145     passfname = fopen('wunderground.password','r');
0146     password = fgetl(passfname);
0147     fclose(passfname);
0148 catch err
0149     error('Problem reading the password file. \r\n %s', err.message)
0150 end
0151 URL1 = ['http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?ID=INORTHER18&PASSWORD=',password,'&'];
0152 URL2 = ['dateutc=',datestr(K(1),'yyyy-mm-dd'),'+',datestr(K(1),'HH'),...
0153     '%3A',datestr(K(1),'MM'),'%3A',datestr(K(1),'SS'),'&'];
0154 URL3 = ['winddir=',num2str(K(5)),'&windspeedmph=',num2str(K(4)*2.2369),...
0155     '&tempf=',num2str(K(11)*9/5+32),'&baromin=',num2str((K(7)-11.9)*0.029967)];
0156 URL = [URL1,URL2,URL3];
0157 
0158 %% Attempt an upload
0159 try
0160     web_str = deblank(mrg_met_urlread(URL));
0161     if strcmp(web_str,'success')
0162         web_str = [DateS, ': OK. mrg_met_urlread returned: ', web_str];
0163     else
0164         web_str = [DateS, ': WARNING. mrg_met_urlread returned: ', web_str];
0165     end
0166 catch err
0167     web_str = [DateS, ': FAIL. mrg_met_urlread failed (possible timeout?). The error was: ', err.message];
0168 end
0169 
0170 str = [str, '\r\n', web_str];
0171 
0172 end

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