function get_latest_met(info) % get_latest_metar(info) % % Last modified: Time-stamp: <2005-01-19 17:05:09 haines> % % Abstract: Function that loads, parses metar obs, appends new data, and saves with all data % % Usage: get_latest_metar(info) % Processing steps % (1) load previous month and current month data from raw ascii files, % adding year and month to each line % (2) append current to previous % (3) subset data for latest 48 hours % (4) decode each METAR record to get WDIR, WSPD, ATEMP, DEWP, ALTIMETER % (5) calculate u, v, seallevel pressure (if needed), and time stuff % (5) create new file of global attributes, dimensions, and variables % (6) save (ncclose) file % Author: Sara Haines, Sep 18, 2003 have_this_month = 0; have_prev_month = 0; x = dir([info.rawdirname '/*.dat']); for i=1:length(x) % x(i).name if strfind(x(i).name, info.month_str) have_this_month = 1; rawfile_this_month = [info.rawdirname '/' x(i).name]; elseif strfind(x(i).name, info.prev_month_str) have_prev_month = 1; rawfile_prev_month = [info.rawdirname '/' x(i).name]; end end % have_this_month % have_prev_month % (1) load previous month and current month data from raw ascii files, % adding year and month to each line % make it MM/DD/YYY (e.g. 04/01/2000) this_dv = datevec([info.month_str(6:7) '/01/' info.month_str(1:4)]); this_year = this_dv(1); this_month = this_dv(2); prev_dv = datevec(datenum(this_year,this_month,-1,0,0,0)); % e.g. 2003_04 prev_year = prev_dv(1); prev_month = prev_dv(2); if (have_this_month & have_prev_month) % if have both append this month data to previous month data % read a file into a cell array of strings and read date stamp raw_prev_data = textread(rawfile_prev_month,'%s','delimiter','\n','whitespace',''); % this assumes that *all* METAR obs time of form DDhhmmZ (e.g. 171450Z) [DD hh mm] = textread(rawfile_prev_month, ['%*s %2d%2d%2d%*s %*s%*[^\n]']); dn_prev = datenum(prev_year*ones(size(DD)),prev_month*ones(size(DD)),DD, ... hh, mm, zeros(size(DD))); % read a file into a cell array of strings and read date stamp raw_this_data = textread(rawfile_this_month,'%s','delimiter','\n','whitespace',''); % this assumes that *all* METAR obs time of form DDhhmmZ (e.g. 171450Z) [DD hh mm] = textread(rawfile_this_month, ['%*s %2d%2d%2d%*s %*s%*[^\n]']); dn_this = datenum(this_year*ones(size(DD)),this_month*ones(size(DD)),DD, ... hh, mm, zeros(size(DD))); elseif (have_this_month & ~have_prev_month) % if only this month just only use this month % no previous data raw_prev_data = []; dn_prev = []; % read a file into a cell array of strings and read date stamp raw_this_data = textread(rawfile_this_month,'%s','delimiter','\n','whitespace',''); % this assumes that *all* METAR obs time of form DDhhmmZ (e.g. 171450Z) [DD hh mm] = textread(rawfile_this_month, ['%*s %2d%2d%2d%*s %*s%*[^\n]']); dn_this = datenum(this_year*ones(size(DD)),this_month*ones(size(DD)),DD, ... hh, mm, zeros(size(DD))); else % if don't have any data for this or previous, don't write out new netcdf file fprintf(2, ['... ... no current metar data to read\n']); end if have_this_month % (2) append current to previous % raw_data = [raw_prev_data; raw_this_data]; dn = [dn_prev; dn_this]; % size(dn) % (3) subset data to within the last 48 hours (2 days) current_dn = info.current_time_dn; two_days_ago = current_dn-2; % datenum is in units of days which_latest = find(two_days_ago<=dn & dn0 % this procedure from class notes from Grant Petty % http://rain.aos.wisc.edu/~gpetty/aos330/lab1_slp_adjust.pdf atempK = atemp(which_calc_slp)+273.1; % units deg K Rd = 287.1; % gas constant for dry air (Rd) units of J/(kg K) g = 9.807; % gravitional force units of m/s^2 % height (meters) of airport above sea level z0 = info.location_elev; H = (Rd.*atempK)/g; % scale height depending on air temperature % air pressure at sea level units of mbar quivalent to hPa (hecto-Pascal) slp(which_calc_slp) = baro(which_calc_slp).*(exp(z0./H)); % clean up clear atempK z0 H; end % (5c) time variables for wind product exercise % (i) seconds since 1970-1-1 00:00:00 UTC seacoos_time_1970 = (dn-datenum(1970, 1, 1, 0, 0, 0))*24*60*60; seacoos_time_1970 = round(seacoos_time_1970); % (ii) matlab datenum (source_time) % don't need to do anything. % dn % (iii) date_time_string date_time_string = datestr(dn,'yyyy-mm-dd HH:MM:SS' ); end end ncfile_v20 = [info.out_nc_v20_dir '/nws-' info.platform_name '-' ... info.package_name '-latest.nc']; % delete old netcdf file of latest data if exist(ncfile_v20, 'file') delete(ncfile_v20); end % one or more obs in desired time period? have_dn = exist('dn'); if have_dn have_latest = length(dn)>0; else have_latest = 0; end % (5) create new file of global attributes, dimensions, and variables % if have data then create new netcdf file and populate if have_this_month & have_latest fprintf(2, ['... ... writing %s\n'], ncfile_v20); nc = netcdf(ncfile_v20, 'clobber'); netcdf_create_v20; nc = close(nc); end %