function fix_tripod_depthKJR(MOORING,WD,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. Make sure you are in the root directory for % the mooring, and this will go through each folder, look for netcdf % files, and fix the water depth values of each file. If the water depth % supplied is not significantly different than the water depth in the % file, it won't change that file. % % inputs: MOORING - the mooring number, as a string, e.g. '847' % WD - the correct water depth for that mooring % 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. if nargin<1 help(mfilename) return end if isnumeric(MOORING) MOORING = num2str(MOORING); end tstr = datestr(now,30); outfile = ['FixWaterDepth',MOORING,'on',tstr,'.txt']; diary(sprintf(outfile)) sprintf(['Run on ' tstr]) disp(['Checking mooring ' MOORING ' for files to have depth corrected']) disp(['New water depth: ' num2str(WD)]) disp(note) disp(['Datum of new water depth: ' datum]) D = dir; for i = 3:length(dir); %go through the folders, skipping the first two (i.e., '.' and '..'); if D(i).isdir disp(['Looking in ' D(i).name ' for files to be corrected']) cd(D(i).name) ncfiles = dir('*.nc');cdffiles = dir('*.cdf');files = [cdffiles;ncfiles]; if length(files)<1, disp(['There are no netCDF data files in folder ' D(i).name ' to correct; moving on']) break elseif length(files)>=1 disp(['-There are ' num2str(length(files)) ' netCDF files to check']) for j = 1:length(files) fname = files(j).name; if strcmp(files(j).name,'ep_standard.nc') disp('File ep_standard skipped') %dont want to work on ep_standard break 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 if isempty(nc{'depth'}) disp('---No depth variable exists, moving on...') elseif ~isempty(nc{'depth'}) vnames = ncnames(var(nc));vars = var(nc); Gdepth = nc.WATER_DEPTH(:); 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... if abs(diff)<0.1, disp('---Difference in water depth is too minimal, moving on....') close(nc) elseif abs(diff)>0.1 disp(['----Fixing global attributes of file ' files(j).name ]) % 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 nc.WATER_DEPTH=ncfloat(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 if ~isempty(nc.INST_DEPTH)&&~isempty(nc.INST_HEIGHT) nc.INST_DEPTH = nc.WATER_DEPTH-nc.INST_HEIGHT; nc.INST_HEIGHT_NOTE=ncchar('height in meters above bottom: nominal for moorings'); nc.INST_DEPTH_NOTE=ncchar('inst_depth = (water_depth - inst_height); nominal depth below the surface'); end nc.WATER_DEPTH = nc.WATER_DEPTH_ORI(:) + diff; %now fix the variable 'depth' - doesn't matter if depth %is a vector or a scalar, still want to remove the same %difference 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'); disp('----Now fixing variable attributes') for k = 1:length(vars) vark = vnames{k};ncobj = nc{vark}; if ~isempty(ncobj.sensor_depth) newdepth = ncobj.sensor_depth + diff; ncobj.sensor_depth = newdepth; disp(['-----Variable ' char(vnames(k)) ' sensor depth attribute changed']) 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 to reflect more accurate values'; nc.history = ncchar([cmnt ';' hist]); close(nc) end end end end cd .. end end disp(['Done checking files for mooring ' MOORING ' for water depth attributes to be corrected.']) diary off