function retn = fix_mooringdepth(mooring_struct) % fix_mooringdepth adjust all global metadata and depth variable to a new depth % % function to adjust all global metadata to be consistent for one tripod % using a scientist supplied water_depth. The original value should be % that from the mooring log. % % usage : retn_status = fix_mooringdepth(struct_name) % where the retn_status is 1 if no problems % the input structure is comprised of: % mooring number (.moor_no), % new water depth (.new_water_depth) % (next 3 have as many items as instruments) % instrument index (from filenames (.hgtind) % new sensor depth (.new_sens_depth) % new inst height (.new_inst_height) % comment describing source of new water depth (.cmnt) % see do_fix_mooringdepth for how to make the structure % emontgomery@usgs.gov 11/7/08 % % 10/15/07 added modification of nc{'depth'} variable along with the attributes % % For moorings, the heights and depths are pretty nominal, since the % mooring moves around some with the waves and tides. So each instrument % gets one instrument height and sensor_depth % check for arguments and exit if the right number aren't present if nargin ~= 1; help (mfilename); retn=0; return; end % if nothing in the sens_depth field, compute it from instrument heights % from mooring log if (isempty (mooring_struct.new_sens_depth)) mooring_struct.new_sens_depth=mooring_struct.new_water_depth-mooring_struct.new_inst_height; end retn = 1; tripno=num2str(mooring_struct.moor_no); % NOTE : this only works on files in the cwd- % it doesn't traverse dirs to find files to modify !! if ispc eval(['d=dir(''*' tripno '*.nc'');']) for jj=1:length(d) fn{jj}=d(jj).name; end else % for unix eval(['[stat,fnames]=unix(''ls ' tripno '*.nc'');']) xx=findstr(tripno,fnames); xx=[xx length(fnames)+1]; knt=1; for ik=1:length(xx)-1 vv=char(deblank(fnames(xx(ik):xx(ik+1)-1))); if findstr(vv,'old') disp(['not processing ' vv]); else fn{knt}=deblank(fnames(xx(ik):xx(ik+1)-1)); knt=knt+1; end end end % loop through to manipulate each file on the tripod for ik=1:length(fn) ncfilename=fn{ik}; perloc=findstr('.',ncfilename); new_nm=[ncfilename(1:perloc-1) '_old.nc']; result=fcopy(ncfilename,new_nm); nc = netcdf(ncfilename, 'write'); if isempty(nc), disp('read of ncfile failed, try another name'), return, end % modify history & modification date: lfeed = char(10); nc.CREATION_DATE = ncchar(datestr(now,0)); history = ['WATER_DEPTH related attributes corrected by ', mfilename , ': ' nc.history(:)]; ifeed = findstr(history,lfeed); history(ifeed) = ':'; nc.history = ncchar(history); flg=0; % fix global attributes related to WATER_DEPTH ga=att(nc); % 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 % 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(mooring_struct.new_water_depth); if(isempty(nc.WATER_DEPTH_NOTE)) nc.WATER_DEPTH_NOTE = ncchar([mooring_struct.cmnt ': (m) ']); else nc.WATER_DEPTH_NOTE = ncchar([mooring_struct.cmnt ': (m) ' nc.WATER_DEPTH_NOTE]); end 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'); % pick which depth and height to use based on the file name val=find(str2num(ncfilename(4))==(mooring_struct.hgtind)); nc.inst_height=ncfloat(mooring_struct.new_inst_height(val)); nc.inst_depth=ncfloat(mooring_struct.new_sens_depth(val)); % now deal with adjusting the variable attributes ... vn=var(nc); for ik=6:length(vn) nc{name(vn{ik})}.sensor_depth=ncfloat(mooring_struct.new_sens_depth(val)); nc{name(vn{ik})}.sensor_hab=ncfloat(mooring_struct.new_inst_height(val)); end % for each variable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % this section changes the contents of the depth() variable % we're going to change the contents of the variable depth here! % sokeep the original depth info in attributes nc{'depth'}.ori_water_depth=water_depth_ori; dp=nc{'depth'}(:); dnp=num2str(dp(1)); for kk=2:length(dp) dnp=[dnp ', ' num2str(dp(kk))]; end nc{'depth'}.oridepth=dnp; % Finally insert adjusted measurement depths nc{'depth'}(:)=mooring_struct.new_sens_depth(val); nc{'depth'}.CMNT=ncchar('adjusted using new sensor_depth'); close(nc) end % finally, verify that all the variables seem reasonable and match for jj=1:length(fn) ncfilename=fn{jj}; disp_ncdepth(ncfilename); end