function corr_inst_depth(fname,WD,inst_ht,note,datum) % % function to fix the water_depth attributes within a netcdf file based % on a better estimate of the water depth. Will take the current water % depth value in the file, and fix all the attributes based on the % difference between the two. Allows the user to input instrument height in % case it was missing. This program forces all variables to have the same % inst_height so will mess things up for files with sensors at several % heights- use fix_tripod_depthKJR for these instead. % inputs: fname- name of the file to open % WD - the correct water depth for that mooring % inst_ht - the installed height of the instrument above the seafloor % note - note on the source of water depth info, as a string, e.g. 'Water % depth from mean pressure on tripod' or 'Water depth from MLLW % of NOAA survey' % datum - datum of water depth, e.g., MSL, MLLW, MTL, etc. % % emontgomery@usgs.gov 3/11/11 % % note - this program will crash if a file with no depth variable is used. if nargin<1 help(mfilename) return end tstr = datestr(now,30); outfile = ['Fix',fname,'on',tstr,'.txt']; diary(sprintf(outfile)) sprintf(['Run on ' tstr]) disp(['New water depth: ' num2str(WD)]) disp(note) disp(['Datum of new water depth: ' datum]) if strcmp(fname,'ep_standard.nc') disp('File ep_standard skipped') %dont want to work on ep_standard return end; disp(['--Checking ' fname ' for attributes to fix']) nc = netcdf(fname,'write'); %first check to see if depth exists, i.e., is it just a raw %data file with no attributes vnames = ncnames(var(nc));vars = var(nc); Gdepth = nc.WATER_DEPTH(:); if isnan(Gdepth) Gdepth=0; end diff = WD - Gdepth; %if the difference is less than 10cm, it's not really worth %changing, because the tripod could settle that much on %deployment... wdep_eq=0; % set a flag to not mess with depth variable if WD's are equal if abs(diff)<0.1, keepon=input('---Difference in water depth is minimal, do you want to continue? ','s') wdep_eq=1; if strcmp(lower(keepon(1)),'n') disp('as requested, exiting program') close(nc) retun end end disp(['----Fixing global attributes of file ' fname ]) % only mess with the WATER_DEPTH attribute if it's different if ~wdep_eq % save the original value, insert new WATER_DEPTH & add comments if ~isempty(nc.WATER_DEPTH) water_depth_ori = nc.WATER_DEPTH(:); elseif ~isempty(nc.water_depth) water_depth_ori = nc.water_depth(:); else water_depth_ori = 'unknown'; end nc.WATER_DEPTH_ORI = water_depth_ori; if isempty(nc.WATER_DEPTH_SOURCE) nc.WATER_DEPTH_ORI_NOTE = ncchar(nc.WATER_DEPTH_NOTE); elseif ~isempty(nc.WATER_DEPTH_SOURCE) %i.e. this is an adcp file nc.WATER_DEPTH_ORI_NOTE = ncchar(nc.WATER_DEPTH_SOURCE); nc.WATER_DEPTH_SOURCE = []; end % if water_depth doesn't exist it shows up as empty % we don't want the lc. water_depth anyway- use WATER_DEPTH if ~isempty(nc.water_depth(:)); nc.water_depth=[]; end % now put the new info in the exising variables nc.WATER_DEPTH(:)=WD; newnote = strcat([note ' Datum: ' datum]); if(isempty(nc.WATER_DEPTH_NOTE)) nc.WATER_DEPTH_NOTE = ncchar(newnote); else nc.WATER_DEPTH_NOTE = ncchar(newnote); end end if (nc{'depth'}(1) - (WD-inst_ht) ~= 0) % now insert the new measurement depth newdep = WD-inst_ht; nc{'depth'}.ori_water_depth=water_depth_ori; nc{'depth'}(:) = newdep; nc{'depth'}.CMNT=ncchar('adjusted using new water_depth'); elseif (length(nc{'depth'}(:)) > 1) % for vectors, use diff to correct dep = nc{'depth'}(:); newdep = dep + diff; nc{'depth'}.ori_water_depth=water_depth_ori; nc{'depth'}(:) = newdep; nc{'depth'}.CMNT=ncchar('adjusted using new water_depth'); end if isempty(nc.initial_instrument_height(:)) nc.initial_instrument_height=ncfloat(inst_ht); else nc.initial_instrument_height(:)=inst_ht; end disp('----Now fixing variable attributes') for k = 6:length(vars) vark = vnames{k};ncobj = nc{vark}; if isempty(ncobj.initial_sensor_height(:)) ncobj.initial_sensor_height=ncfloat(inst_ht); end if ~isempty(ncobj.sensor_depth) if isnan(ncobj.sensor_depth(:)) % waves should all be here ncobj.initial_sensor_height(:)=inst_ht; ncobj.sensor_depth(:) = nc.WATER_DEPTH(:) - ncobj.initial_sensor_height(:); disp(['-----Variable ' char(vnames(k)) ' sensor depth and inst ht fixed']) else %newdepth = ncobj.sensor_depth + diff; %ncobj.sensor_depth = newdepth; ncobj.sensor_depth(:) = nc.WATER_DEPTH(:) - ncobj.initial_sensor_height(:); ncobj.initial_sensor_height(:)=inst_ht; disp(['-----Variable ' char(vnames(k)) ' sensor depth attribute changed']) end elseif isempty(ncobj.sensor_depth)&&~isempty(ncobj.initial_sensor_height) ncobj.sensor_depth = nc.WATER_DEPTH(:) - ncobj.initial_sensor_height; disp(['-----Variable ' char(vnames(k)) ' sensor depth attribute added']) end if ~isempty(ncobj.sensor_depth)&&isempty(ncobj.initial_sensor_height) ncobj.initial_sensor_height = nc.WATER_DEPTH(:) - ncobj.sensor_depth; disp(['-----Variable ' char(vnames(k)) ' sensor height above bottom attribute added']) end end % now add info to the history to record the change hist = nc.history(:);cmnt = 'Water depth variable and attributes changed by corr_inst_depth'; nc.history = ncchar([cmnt ';' hist]); close(nc) disp(['Done fixing water depth attributes for ' fname]) diary off