function epname = mavs_avg(infile, outfile) % function epname = mavs_avg(infile, outfile) % calibrates MAVS data from the BASS variables from the average % records of MIDAS data. % epname is a cell array of the names of the variables created. % infile is the name of a netcdf file of raw data, to which % calibration constants have been attached as attributes. % outfile is the name of an EPIC compliant netcdf file containing % the variables listed in epname. % Fran Hotchkiss 22 Jun 2000. calcomment = ''; nc = netcdf(infile)%; disp ('mavs_avg.m is running')%******************** if isempty(nc) [infile,inpath] = uigetfile('*.cdf','Choose input file'); nc = netcdf(infile); if isempty(nc) disp (['Cannot open input file ' infile]); disp (['Execution ends.']); return; end end bass1 = nc{'bass_1'}(:); bass2 = nc{'bass_2'}(:); burst_per_avg = nc{'burst_per_avg'}(:); bass_error1 = nc{'bass_error_1'}(:); bass_error2 = nc{'bass_error_2'}(:); mavsnum = nc{'bass_1'}.instrument_number(:); mavszeros = [0 0 0 0]; ivar = nc{'bass_1'}; % Choose MAVS calibration routine zeros. MAVS.axes = {{'All','A omitted','B omitted','C omitted',... 'D omitted'},1}; MAVS.zeros={{'pre-deployment','post-deployment','provided'},2}; MAVS.zero_A = 0.; MAVS.zero_B = 0.; MAVS.zero_C = 0.; MAVS.zero_D = 0.; if running(batch) str = get(batch); eval(['MAVS.axes{2} = ' str ';']); str = get(batch); eval(['MAVS.zeros{2} = ' str ';']); str = get(batch); eval(['MAVS.zero_A = ' str ';']); str = get(batch); eval(['MAVS.zero_B = ' str ';']); str = get(batch); eval(['MAVS.zero_C = ' str ';']); str = get(batch); eval(['MAVS.zero_D = ' str ';']); else B = MAVS; MAVS = guido(B,'Select MAVS calibration parameters'); if isempty(MAVS) disp ('MAVS Parameter Menu must be completed. Execution ends.') close (nc) return end end switch MAVS.zeros{2} case 1 mavszeros(1) = ivar.pre_zero_offset_A(1); mavszeros(2) = ivar.pre_zero_offset_B(1); mavszeros(3) = ivar.pre_zero_offset_C(1); mavszeros(4) = ivar.pre_zero_offset_D(1); calcomment = [calcomment 'Pre zeroes used. ']%; case 2 mavszeros(1) = ivar.post_zero_offset_A(1); mavszeros(2) = ivar.post_zero_offset_B(1); mavszeros(3) = ivar.post_zero_offset_C(1); mavszeros(4) = ivar.post_zero_offset_D(1); calcomment = [calcomment 'Post zeroes used. ']%; otherwise mavszeros(1) = MAVS.zero_A; mavszeros(2) = MAVS.zero_B; mavszeros(3) = MAVS.zero_C; mavszeros(4) = MAVS.zero_D; calcomment = [calcomment 'User-provided zeroes. ']%; end switch MAVS.axes{2} case 1 [Ve, Vn, Vup, H, P, R, mavszeros] = mavs2earth(bass1, bass2, burst_per_avg,... bass_error1, bass_error2, mavsnum, mavszeros, 0); otherwise disp ('MAVS calibration with less than 4 axes not implemented.') close (nc) return end % Put data in normal units. Ve = Ve/10; Vn = Vn/10; Vup = Vup/10; P = P/100; R = R/100; % Now make speed and direction. [vdir,vspd] = cart2pol(Vn,Ve); ilow = find(vdir < 0)%; vdir(ilow) =vdir(ilow) + 2*pi; vdir = vdir .* 180 ./ pi; epname = {'Ptch_1216','Roll_1217','Hdg_1215','u_1205','v_1206','CD_310','CS_300','w_1204'}; datname = {'P','R','H','Ve','Vn','vdir','vspd','Vup'}; vardesc = 'Ptch:Roll:Hdg:u:v:CD:CS:w'; nvarout = 8%; % Make EPIC time from MIDAS raw data set time. % Input data and base time for time. ivar = nc{'time'}; rawdat = ivar(:); yr = ivar.base_date(1); mo = ivar.base_date(2); da = ivar.base_date(3); hr = ivar.base_date(4); mi = ivar.base_date(5); sc = ivar.base_date(6); % Find decimal day numbers for base time and time data. base = datenum(yr,mo,da,hr,mi,sc); rtime = base + rawdat./86400; % Call function to make EPIC time. alltime = EP_Time(rtime); time = alltime(:,1); time2 = alltime(:,2); stime = ep_datenum(alltime(1,:)); [n m] = size(alltime); ltime = ep_datenum(alltime(n,:)); % Get mooring log parameters. if running(batch) str = get(batch); eval(['epicat.experiment = ' str ';']); str = get(batch); eval(['epicat.project = ' str ';']); str = get(batch); eval(['epicat.comment = ' str ';']); str = get(batch); eval(['epicat.MAVS_pod_depth = ' str ';']); else epicat.experiment = 'ECOHAB'; epicat.project = 'WHFC'; epicat.comment = 'Boston BASS MIDAS'; epicat.MAVS_pod_depth = 2.9; e = epicat; epicat = guido(e,'Input mooring log parameters.'); end % Open output cdf outc = netcdf(outfile,'noclobber')%; if isempty(outc) [outfile,outpath] = uiputfile('*.nc','Choose output file'); outc = netcdf(outfile,'noclobber'); if isempty(outc) disp (['Cannot open output file ' outfile]); disp (['Data will be parked in temporary.nc']); outc = netcdf('temporary.nc','clobber'); end end % Copy global attributes from ep_standard.nc to output cdf. eps = netcdf('ep_standard.nc')%; if isempty(eps) [epsfile,epspath] = uigetfile('*.nc','Choose epic standard file'); eps = netcdf(epsfile); if isempty(eps) try mkep_standard; eps = netcdf('ep_standard.nc')%; catch disp (['Cannot open epic standard file ' epsfile]); disp (['Execution ends.']); ncclose; return; end end end disp ('ep_standard.nc is open')%***************** stdatts = att(eps); for i = 1, length(stdatts); copy(stdatts(i),outc); end %% Modify Global attributes: outc.DATA_ORIGIN = nc.data_source(:); outc.EXPERIMENT = epicat.experiment; outc.PROJECT = epicat.project; outc.MOORING = nc.mooring_number(:); outc.DELTA_T = ncchar(num2str(nc.average_interval(1))); outc.DATA_CMNT = epicat.comment; outc.WATER_DEPTH = nc.water_depth(1); outc.DESCRIPT = nc.comment(:); outc.FILL_FLAG = nclong(0); outc.VAR_FILL = ncfloat(NaN); history =['Calibrated using mavs_avg.m. ' calcomment ':' nc.history(:)]%; disp (['history is ' history]) %******************** outc.history = history; outc.start_time = datestr(stime,0); outc.stop_time = datestr(ltime,0); outc.latitude = dms2deg(nc.lat_ddmmss(:)); outc.longitude = -dms2deg(nc.lon_ddmmss(:)); outc.water_depth = nc.water_depth(1); outc.CREATION_DATE = ncchar(datestr(now,0)); outc.INST_TYPE = nc.instrument_type(:); outc.instrument_number = nc.instrument_number(:); outc.magnetic_variation = nc.magnetic_variation(1); outc.VAR_DESC = ncchar(vardesc); outc.MAVS_zero_offset_A = mavszeros(1); outc.MAVS_zero_offset_B = mavszeros(2); outc.MAVS_zero_offset_C = mavszeros(3); outc.MAVS_zero_offset_D = mavszeros(4); %% Dimensions: outc('time') = 0; outc('depth') = 1; outc('lon') = 1; outc('lat') = 1; %% Variables and attributes: copy(eps{'time'},outc,0,1); copy(eps{'time2'},outc,0,1); copy(eps{'depth'},outc,0,1); copy(eps{'lon'},outc,0,1); copy(eps{'lat'},outc,0,1); for i = 1: nvarout; ivar = eps{epname{i}}%; disp (['Writing attributes for variable ' epname{i}])%******* if ~isempty(ivar) copy(ivar,outc,0,1); else display (['Variable ' epname{i} ' not found in ep_standard.nc. Execution ends.']) ncclose return end ivar = nc{'bass_1'}; ovar = outc{epname{i}}; ovar.sensor_type = ivar.instrument_type(:); ovar.sensor_depth = ncfloat(epicat.MAVS_pod_depth); ovar.serial_number = ivar.instrument_number(:); eval (['ovar.minimum = ncfloat(min(' datname{i} '));']); eval (['ovar.maximum = ncfloat(max(' datname{i} '));']); end close (eps) disp ('ep_standard.nc is closed')%****************************** endef(outc) n=length(time)%; outc{'time'}(1:n) = time; outc{'time2'}(1:n) = time2; outc{'lat'}(1) = nc{'lat'}(1); outc{'lon'}(1) = nc{'lon'}(1); outc{'depth'}(1) = epicat.MAVS_pod_depth; for i = 1:nvarout ovar = outc{epname{i}}%; eval (['ovar(1:n) = ' datname{i} ';' ]); end close (nc)%; close (outc)%;