function get_latest_met(info) % get_latest_met(info) % % Last modified: Time-stamp: <2004-10-13 09:42:30 haines> % % Abstract: Function that loads, appends new data, and saves with all data % % Usage: get_latest_met(info) % Processing steps % (1) load previous month and current month data from netCDF % (2) append current to previous % (3) subset data for latest 6 hours near top of the hour % (4) calc u and v, and air_pressure_at_sea_level, and other times % (5) create and write new text file of data to be encoded for NDBC % one for each hour record % Author: Sara Haines, Oct 11, 2004 have_this_month = 0; have_prev_month = 0; x = dir([info.ncdir '/*.nc']); for i=1:length(x) % x(i).name if strfind(x(i).name, [info.platform_name '_' info.month_str '.nc']) have_this_month = 1; ncfile_this_month = [info.ncdir '/' x(i).name]; elseif strfind(x(i).name, [info.platform_name '_' info.prev_month_str '.nc']) have_prev_month = 1; ncfile_prev_month = [info.ncdir '/' x(i).name]; end end % have_this_month % have_prev_month % (1) load previous and current months of data if (have_this_month & have_prev_month) % if have both append this month data to previous month data nc_prev = netcdf(ncfile_prev_month, 'nowrite'); nc_this = netcdf(ncfile_this_month, 'nowrite'); % get variables % (2) append current to previous dn = [nc_prev{'time'}(:); nc_this{'time'}(:)]; % size(dn) awspd = [nc_prev{'WSPDA'}(:); nc_this{'WSPDA'}(:)]; awdir = [nc_prev{'WDIRA'}(:); nc_this{'WDIRA'}(:)]; wgust = [nc_prev{'WGSTA'}(:); nc_this{'WGSTA'}(:)]; atemp = [nc_prev{'ATEMP'}(:); nc_this{'ATEMP'}(:)]; rh = [nc_prev{'RH'}(:); nc_this{'RH'}(:)]; abaro = [nc_prev{'BARO'}(:); nc_this{'BARO'}(:)]; elseif (have_this_month & ~have_prev_month) % if only this month just only use this month nc_this = netcdf(ncfile_this_month, 'nowrite'); % get variables dn = nc_this{'time'}(:); % size(dn) awspd = [nc_this{'WSPDA'}(:)]; awdir = [nc_this{'WDIRA'}(:)]; wgust = [nc_this{'WGSTA'}(:)]; atemp = [nc_this{'ATEMP'}(:)]; rh = [nc_this{'RH'}(:)]; abaro = [nc_this{'BARO'}(:)]; else % if don't have, don't write out new netcdf file fprintf(2, ['... ... no current met data to read\n']); end % close the netcdf file if (have_this_month) nc_this = close(nc_this); end if (have_prev_month) nc_prev = close(nc_prev); end % (3) subset data if have_this_month dv = datevec(info.current_time_dn); dt = datenum([dv(1:4) 0 0]); hrs = [0:5]/24; % each hour for last 6 hours (minus two minutes) target_times = (dt*ones(size(hrs))-hrs)-2/60/24; % datestr(target_times) which_latest = []; % for each target time find closest sample time within time limits (tl) for j=1:length(target_times) tt = target_times(j); tl1 = 15/60/24; % 15 minutes tl2 = 2/60/24; % 15 minutes xxx = abs(dn-tt); index_near = find( (dn >= tt-tl1) & (xxx == min(xxx)) & (dn <= tt+tl2) ); if ~isempty(index_near) which_latest = [which_latest; index_near(1)]; end end if ~isempty(which_latest) dn = dn(which_latest); % datestr(dn) awspd = awspd(which_latest); awdir = awdir(which_latest); wgust = wgust(which_latest); atemp = atemp(which_latest); rh = rh(which_latest); abaro = abaro(which_latest); % set missing values to NaN params = {'awspd'; 'awdir'; 'wgust'; 'atemp'; 'rh'; 'abaro'}; [nRows, nCols] = size(params); for j=1:nRows % whichMiss = find(awspd == -9999); % awspd(whichMiss) = deal(NaN); param_str = char(params(j)); eval(['whichMiss = find(' param_str ' <= -9999);']) eval([param_str '(whichMiss) = deal(NaN);']) end % (4) calculations and other time variables for output % (4a) dew point from relative humidity % calc in situ saturation vapor pressure from Buck [1981] from % in situ temperature and static pressure (, and ) % of the air at the humidity sensor: a = 17.368; b = 238.88; es = 6.1121*( 1.0007 + (3.46e-06*abaro) ) .* exp( a*atemp./(atemp+b) ); alpha = (es.*rh)/611; adewp = ( (b*log(alpha))./(a-log(alpha)) ); % (4a) uv wind components (northward and eastward) r=pi/180; WVE = awspd .*(sin((awdir-180)*r)); WVN = awspd .*(cos((awdir-180)*r)); % (4b) air pressure at sea level % this procedure from class notes from Grant Petty % http://rain.aos.wisc.edu/~gpetty/aos330/lab1_slp_adjust.pdf atempK = atemp+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 z0 = info.baro_ht; % height of measurement of abaro H = (Rd.*atempK)/g; % scale height depending on air temperature % air pressure at sea level units of mbar quivalent to hPa (hecto-Pascal) abaro_sealevel = abaro.*(exp(z0./H)); xxx=abaro_sealevel; % [abaro(1:10) abaro_sealevel(1:10)] % (4c) 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 % for each target time for j=1:length(which_latest) [Y, M, D, H, Mi, Se] = datevec(dn(j)); file_timestamp_str = sprintf('%04d%02d%02d_%02d%02d', Y, M, D, H, Mi); % fprintf('%s\n', file_timestamp_str); ndbcfile = [info.out_dir '/ndbc-input-' info.station '-' file_timestamp_str ... '-CMAN']; % if we have not sent this record yet if ~exist([ndbcfile '_sent'], 'file') % and we have data then create and write ndbcfile if have_this_month disp([' ... ... ' ndbcfile]) fid = fopen(ndbcfile, 'w'); ndbc_write; fclose(fid); end end % end % for j=1:length(which_latest) % % delete old file of latest data % if exist(ndbcfile, 'file') % delete(ndbcfile); % end % % (5) create ndbc input file % if have_this_month % fid = open(ndbcfile); % ndbc_create; % close(fid); % end