10beta to be emailed to Eric Firing on Sep 18
authorA.M. Thurnherr <athurnherr@yahoo.com>
Wed, 18 Sep 2013 14:29:58 +0000
changeset 11 d71acdec556a
parent 10 d273b7bacb36
child 12 9797eb641ea0
10beta to be emailed to Eric Firing on Sep 18
HISTORY
edit_data.m
ladcp2cdf.m
loadctd.m
readme.m
savearch.m
--- a/HISTORY	Thu Jul 18 04:12:24 2013 -0400
+++ b/HISTORY	Wed Sep 18 14:29:58 2013 +0000
@@ -1,9 +1,9 @@
 ======================================================================
                     H I S T O R Y 
                     doc: Tue Aug 16 11:41:46 2005
-                    dlm: Thu Jul 18 04:09:59 2013
+                    dlm: Wed Aug 28 12:34:06 2013
                     (c) 2005 A.M. Thurnherr
-                    uE-Info: 214 0 NIL 0 0 72 3 2 4 NIL ofnI
+                    uE-Info: 227 72 NIL 0 0 72 3 2 4 NIL ofnI
 ======================================================================
 
 NB: CHANGE VERSION IN [default.m] BEFORE UPLOADING
@@ -210,5 +210,18 @@
 IX_10:
 	Jun 24, 2013:
 		- updated version to IX_10beta in [default.m] [.hg/hgrc]
-		- re-added blen in [loadrdi.m] (also separate other DL/UL header vars)
-
+		- re-added blen in [loadrdi.m] (also separate other DL/UL header
+		  vars) in order to spit it out as metadata
+	Aug 13, 2013:
+		- fixed bug in [edit_data.m] that prevented sidelobes from being
+		  edited out for UL-only data
+	Aug 15, 2013:
+		- incorporated new [ladcp2cdf.m] contributed by Diana Cardoso, with
+		  modifications
+		- removed [readme.m] which is badly out-of-date
+	Aug 16, 2013:
+		- changed semantics in [loadctd.m] to generate and error
+		  (rather than a warning and an attempted work-around, which does
+		  not work for combined GPS/CTD data) when time lagging fails
+	Aug 28, 2013:
+		- implemented bug fix to [ladcp2cdf.m] provided by Diana Cardoso
--- a/edit_data.m	Thu Jul 18 04:12:24 2013 -0400
+++ b/edit_data.m	Wed Sep 18 14:29:58 2013 +0000
@@ -14,13 +14,14 @@
 %		   provided by Mattew Alford
 %		 - disabled edit_spike_filter by default
 %		 - changed edit_spike_filter_max_curv default value
+%  Aug 13, 2013: - BUG: edit_sidelobes did not work for UL only data
 
 %======================================================================
 %                    E D I T _ D A T A . M 
 %                    doc: Sat Jul  3 17:13:05 2004
-%                    dlm: Thu May 18 12:20:40 2006
+%                    dlm: Tue Aug 13 15:34:27 2013
 %                    (c) 2004 A.M. Thurnherr
-%                    uE-Info: 50 37 NIL 0 0 72 3 2 8 NIL ofnI
+%                    uE-Info: 122 0 NIL 0 0 72 3 2 8 NIL ofnI
 %======================================================================
 
 %----------------------------------------------------------------------
@@ -116,9 +117,19 @@
   
   % first, the uplooker: d.z is -ve distance of ADCP from surface;
   % Cell_length is in cm, i.e. 0.015*Cell_length is 1.5 x bin size
-  % in m --- the same value used by Firing's software
+  % in m --- the same value used by Eric Firing's software
   
-  if length(d.zu > 0)
+  if length(d.zu)==0 && d.zd(1)<0			% UL only (in DL structures)
+
+    for b=1:length(d.zd)
+      zlim(b,:) = (1 - cos(pi*d.down.Beam_angle/180)) * d.z ...
+		- 0.015*d.down.Cell_length;
+    end
+    ibad = find(d.izm > zlim);
+    nbad = nbad + length(find(isfinite(d.weight(ibad))));
+    d.weight(ibad) = NaN; d.ts_edited(ibad) = NaN;
+  
+  elseif length(d.zu > 0)				% DL/UL combo
   
     for b=1:length(d.zu)+length(d.zd)
       zlim(b,:) = (1 - cos(pi*d.up.Beam_angle/180)) * d.z ...
--- a/ladcp2cdf.m	Thu Jul 18 04:12:24 2013 -0400
+++ b/ladcp2cdf.m	Wed Sep 18 14:29:58 2013 +0000
@@ -1,143 +1,176 @@
-function [] = ladcp2cdf(fname,struct,st2,st3,st4,st5,st6,st7);
-% function [] = ladcp2cdf(fname,struct,st2,st3,st4,st5,st6,st7);
+function [] = ladcp2cdf(fname,dr_struct,da,p,ps,f,att);
+% function [] = ladcp2cdf(fname,dr_struct,da,p,ps,f,att);
 %
-% function to save LADCP data into a netcdf file
+% function to save LADCP data into a netcdf file for MatLab version 2012a
+%
+% input  :	fname		- output filename
+%			dr_struct	- main inversion results (velocity profiles)
+%			da..att	- arbitrary metadata structures
 %
-% input  :	fname		- filename
-%		struct		- structure containing the data
-%		struct2		- structure containing the attributes
-%
-% version 0.1	last change 08.03.2002
+% Created By:   Diana Cardoso, Bedford Institute of Oceangraphy
+%               Diana.Cardoso@dfo-mpo.gc.ca
+% Description:  Based on LDEO software to Process LADCP, version IX.8,
+%               script ladcp2cdf.m version 0.1	last change 08.03.2002. 
+%               maintained by A.M. Thurnherr and downloaded from:
+%       http://www.ldeo.columbia.edu/cgi-bin/ladcp-cgi-bin/hgwebdir.cgi
+%       The function ladcp2cdf was changed to run with the the Matlab
+%       version 2012, which now supports netcdf.
+
+%======================================================================
+%                    L A D C P 2 C D F . M 
+%                    doc: Thu Aug 15 10:52:55 2013
+%                    dlm: Wed Aug 28 12:31:16 2013
+%                    (c) 2013 A.M. Thurnherr, from code contributed by D. Cardoso
+%                    uE-Info: 99 0 NIL 0 0 72 0 2 8 NIL ofnI
+%======================================================================
 
-% Felix Tubiana Added Variable Attributes 12.12.03
-% G.Krahmann, LDEO Mar 2002
-% warning off MATLAB:DeprecatedLogicalAPI
+% NOTES:
+%	- This version creates slightly different files than the original version
+%	  created by Visbeck/Krahmann. In the original version, the contents of the
+%	  dr structure end up as top-level variables and the contents of
+%	  the da, p, ps, f and att structures end of as global attributes. In 
+%	  the new version, the latter are saved as sub-structures, with _struct appended
+%	  to the internal names to avoid conflicts.
+
+% HISTORY:
+%   Aug 15, 2013: - incorporated this code, supplied Diana Cardoso, into IX_10beta
+%		  - modified doc in header
+%		  - renamded struct variable to dr_struct
+%		  - removed 'cd' in and out of results directory (pathnames work just fine)
+%		  - delete netcdfile before it is written to (old 'clobber' option)
+%		  - removed 'l' suffix from all dims
+%		  - replaced yes/no logical vals by true/false
+%		  - renamed substructures from st2..st6 to internal names (da,p,ps,f,att)
+%   Aug 28, 2013: - incorporated bug fix provided by Diana Cardoso to prevent lat,lon,name and 
+%		    date to be stored 2cd in the nc file, which can make the code
+%		    bomb if the length of any other var is 6 (or equal to the length of name?)
 
 % check arguments
 if nargin<2
   error('need two input arguments')
 end
-if ~isstruct(struct)
-  error('second argument must be a structure')
+if ~isstruct(dr_struct)
+  error('second argument must be a dr structure')
 end
 
-% check filename
-fname = deblank(fname);
-
-nc = netcdf(fname,'clobber');
-
-if isempty(nc)
-  error('can not open netcdf file')
+netcdfile = deblank(fname); %remove any blanks from string end
+if exist(netcdfile,'file')
+	delete(netcdfile)
 end
 
-
-% look for the three dimension bases
+%Determine dimensions of variables
 lbot = 0;
 lz = 0;
 ltim = 0;
 lsadcp = 0;
-if isfield(struct,'z');
-  lz = length(getfield(struct,'z'));
-end  
-if isfield(struct,'tim');
-  ltim = length(getfield(struct,'tim'));
+
+if isfield(dr_struct,'z');
+  lz = length(getfield(dr_struct,'z'));
 end  
-if isfield(struct,'zbot');
-  lbot = length(getfield(struct,'zbot'));
+if isfield(dr_struct,'tim');
+  ltim = length(getfield(dr_struct,'tim'));
 end  
-if isfield(struct,'z_sadcp');
-  lsadcp = length(getfield(struct,'z_sadcp'));
+if isfield(dr_struct,'zbot');
+  lbot = length(getfield(dr_struct,'zbot'));
+end  
+if isfield(dr_struct,'z_sadcp');
+  lsadcp = length(getfield(dr_struct,'z_sadcp'));
 end  
 
-% define dimensions in netcdf file
-if lz>0
-  nc('z') = fix(lz);
-end
-if ltim>0
-  nc('tim') = fix(ltim);
-end
-if lbot>0
-  nc('zbot') = fix(lbot);
-end
-if lsadcp>0
-  nc('z_sadcp') = fix(lsadcp);
-end
-nc('lat') = 1;
-nc('lon') = 1;
-nc('date') = 6;
-nc('name') = length(getfield(struct,'name'));
-
-% define standard variables
-nc{'lat'} = ncfloat('lat');
-nc{'lon'} = ncfloat('lat');
-nc{'date'} = ncint('date');
-nc{'name'} = ncchar('name');
+% % define dimensions in netcdf file and standard variables
+nccreate(netcdfile,'lat','Dimensions',{'lat' 1},'Datatype','single');
+nccreate(netcdfile,'lon','Dimensions',{'lon' 1},'Datatype','single');
+nccreate(netcdfile,'date','Dimensions',{'date' 6},'Datatype','int32');
+nccreate(netcdfile,'name','Dimensions',{'name' length(getfield(dr_struct,'name'))},'Datatype','char');
 
 % store standard variables
-nc{'lat'}(:) = struct.lat;
-nc{'lon'}(:) = struct.lon;
-nc{'date'}(:) = struct.date;
-nc{'name'}(:) = struct.name;
+ncwrite(netcdfile,'lat',dr_struct.lat);
+ncwrite(netcdfile,'lon',dr_struct.lon);
+ncwrite(netcdfile,'date',dr_struct.date);
+ncwrite(netcdfile,'name',dr_struct.name);
 
 % parse fieldnames, define the proper variable and store it
-fnames = fieldnames(struct);
+fnames = fieldnames(dr_struct);
+nn=strncmp('name',fnames,6); nla=strncmp('lat',fnames,3); 	% find, name, lat,lon,date from fnames
+nlo=strncmp('lon',fnames,3); nda=strncmp('date',fnames,4);
+ntot=[nn+nla+nlo+nda]; Ktot = logical(ntot);
+fnames(Ktot,:)=[];						% remove , name, lat,lon,date from fnames
+
 for n=1:size(fnames,1)
-  dummy = getfield(struct,fnames{n});
+  dummy = getfield(dr_struct,fnames{n});
   if length(dummy)==lz
-    eval(['nc{''',fnames{n},'''} = ncfloat(''z'');'])
-    eval(['nc{''',fnames{n},'''}(:) = dummy;'])
+    nccreate(netcdfile,fnames{n},'Dimensions',{fnames{n} fix(lz)},'Datatype','single');
+    ncwrite(netcdfile,fnames{n},dummy);
   end
   if length(dummy)==ltim
-    eval(['nc{''',fnames{n},'''} = ncfloat(''tim'');'])
-    eval(['nc{''',fnames{n},'''}(:) = dummy;'])
+    nccreate(netcdfile,fnames{n},'Dimensions',{fnames{n} fix(ltim)},'Datatype','single');
+    ncwrite(netcdfile,fnames{n},dummy);
   end
   if length(dummy)==lbot
-    eval(['nc{''',fnames{n},'''} = ncfloat(''zbot'');'])
-    eval(['nc{''',fnames{n},'''}(:) = dummy;'])
+    nccreate(netcdfile,fnames{n},'Dimensions',{fnames{n} fix(lbot)},'Datatype','single');
+    ncwrite(netcdfile,fnames{n},dummy);
   end
   if length(dummy)==lsadcp
-    eval(['nc{''',fnames{n},'''} = ncfloat(''z_sadcp'');'])
-    eval(['nc{''',fnames{n},'''}(:) = dummy;'])
+    nccreate(netcdfile,fnames{n},'Dimensions',{fnames{n} fix(lsadcp)},'Datatype','single');
+    ncwrite(netcdfile,fnames{n},dummy);
   end
 end
+
+add_struct(netcdfile,'da_struct',da)
+add_struct(netcdfile,'p_struct',p)
+add_struct(netcdfile,'ps_struct',ps)
+add_struct(netcdfile,'f_struct',f)
+add_struct(netcdfile,'att_struct',att)
+
+end % function
+
+%----------------------------------------------------------------------
+
+function [] = add_struct(ncf,snm,a)
   
-% parse fieldnames and add them to the netcdf file  
-for i=2:(nargin-1)
-   eval(['a=st',int2str(i),';']);
    fnames = fieldnames(a);
    if isstruct(a)
       if ~isstruct(eval(['a.' fnames{1}])) % No SubStructure
-	 dummy='New Structure';
-	 eval(['nc.',['st',int2str(i)],'=dummy;'])
-	 for n = 1:size(fnames,1)
-	    dummy = getfield(a,fnames{n});
-	    if size(dummy,1)==1
-	       if isstr(dummy)
-		  eval(['nc.',fnames{n},'=dummy;'])
-	       else
-		  eval(['nc.',fnames{n},'=dummy(:);'])
-	       end    
-	    end
-	 end
+	nccreate(ncf,snm,'Datatype','char');
+
+	for n = 1:size(fnames,1)
+		dummy = getfield(a,fnames{n});
+	    	if size(dummy,1)==1
+	       		if isstr(dummy)
+                		ncwriteatt(ncf,snm,fnames{n},dummy);
+	       		elseif islogical(dummy)
+		        	if dummy, dummy='true';
+		               	else, 	  dummy='false';
+		               	end
+				ncwriteatt(ncf,snm,fnames{n},dummy);
+			else
+		                ncwriteatt(ncf,snm,fnames{n},dummy(:));
+			end    
+		end
+	end % for n
       else % SubStructures -> Variable Attributes
-	 for n = 1:size(fnames,1)
-	    atts = eval(['fieldnames(a.' fnames{n} ');']);
-	    for j = 1:size(atts,1)
-	       dummy = eval(['a.' fnames{n} '.' atts{j} ';']);
-	       if size(dummy,1) == 1
-		  if isstr(dummy)
-		     eval(['nc{''',fnames{n}, '''}.' atts{j} '=dummy;'])
-		  else
-		     eval(['nc{''',fnames{n}, '''}.' atts{j} '=dummy(:);'])
-		  end    
-	       end
-	    end	       
-	 end
-      end
-   else
+	for n = 1:size(fnames,1)
+		atts = eval(['fieldnames(a.' fnames{n} ');']);
+	        finfo = ncinfo(ncf);
+        	FieldNames = {finfo. Variables.Name};
+	        existField=strmatch(fnames{n}, FieldNames);
+        	if isempty(existField)
+			nccreate(ncf,fnames{n},'Datatype','char');
+	        end
+		for j = 1:size(atts,1)
+			dummy = eval(['a.' fnames{n} '.' atts{j} ';']);
+			if size(dummy,1) == 1
+				if ischar(dummy)
+		                    ncwriteatt(ncf,fnames{n},atts{j},dummy);
+                		else
+		                    ncwriteatt(ncf,fnames{n},atts{j},dummy(:));
+                		end
+			end
+		end	       
+	 end % for n
+      end % else (substructures or not)
+   else % if issstruct(a)
       disp(' not structure')
    end
-end
+end % function
 
-% close netcdf file
-close(nc)
--- a/loadctd.m	Thu Jul 18 04:12:24 2013 -0400
+++ b/loadctd.m	Wed Sep 18 14:29:58 2013 +0000
@@ -1,9 +1,9 @@
 %======================================================================
 %                    L O A D C T D . M 
 %                    doc: Sat Jun 26 15:56:43 2004
-%                    dlm: Fri Mar  5 15:51:21 2010
+%                    dlm: Fri Aug 16 10:53:40 2013
 %                    (c) 2004 M. Visbeck & A. Thurnherr
-%                    uE-Info: 401 16 NIL 0 0 72 2 2 8 NIL ofnI
+%                    uE-Info: 445 41 NIL 0 0 72 0 2 8 NIL ofnI
 %======================================================================
 
 function [d,p]=loadctd(f,d,p)
@@ -438,6 +438,12 @@
    p.warn(size(p.warn,1)+1,1:length(warn))=warn;
   end
  else
+  error('Cannot determine time offset between CTD and LADCP time series --- aborting');
+
+  % The following old code has been disabled for IX_10 because it is known not to work correctly
+  % for combined NAV/CTD data, because the NAV time series *must* be matched to the LADCP data
+  % for the GPS constraint to be applied.
+
   text(1,0,'LOW CORRELATION not used','color','r')
   disp('WARNING WARNING WARNING')
   warn=(' lag too big or correlation too weak: ignore CTD time series');
--- a/readme.m	Thu Jul 18 04:12:24 2013 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,62 +0,0 @@
-% README for LADCP software
-%
-% Martin Visbeck
-disp(' LADCP software ')
-disp(' ')
-disp(' M. Visbeck  LDEO  ')
-disp(' http://www.ldeo.columbia.edu/~visbeck/ladcp ')
-disp(' ')
-a=which('readme');
-ii=find(a=='/');
-ladcpdir='';
-if length(ii)>0
- ladcpdir=a(1:max(ii));
-end
-disp([' You m-files are installed in directory :',ladcpdir])
-disp(' ')
-disp(' Much of the parameter are documented in DEFAULT.M ')
-disp(' press RETURN to display it ')
-
-pause
-more on
-eval(' type default ')
-pause
-clc
-
-disp(' The data are processed by  LAPROC.M ')
-disp(' press RETURN to display it ')
-
-pause
-eval(' type laproc ')
-
-pause
-clc
-
-disp(' Copy any of the load?.m files to your local directory') 
-eval(['ls ',ladcpdir,'loaddata/load*.m'])
-
-pause
-clc
-
-disp(' Edit any of the demo?.m files to your needs') 
-eval(['ls ',ladcpdir,'process/d*.m'])
-
-pause
-clc
-
-disp('% parameter structure arrays used')
-disp(' ')
-disp('p.*   main parameter')
-disp('ps.*  inverse solution parameter')
-disp('f.* file names ')
-disp(' ')
-disp('% data structure arrays used')
-disp(' ')
-disp('d.*   raw data')
-disp('di.*  super ensemble data')
-disp('dr.*  results from inversion')
-disp('de.*  matrix data used by inversion')
-disp('ds.*  results from shear based method')
-disp('da.*  description of profile for NETCDF file save')
-more off
-
--- a/savearch.m	Thu Jul 18 04:12:24 2013 -0400
+++ b/savearch.m	Wed Sep 18 14:29:58 2013 +0000
@@ -1,13 +1,14 @@
 %======================================================================
 %                    S A V E A R C H . M 
 %                    doc: Wed Jan  7 16:51:58 2009
-%                    dlm: Fri Mar  5 15:49:49 2010
+%                    dlm: Thu Aug 15 12:17:03 2013
 %                    (c) 2009 A.M. Thurnherr
-%                    uE-Info: 180 13 NIL 0 0 72 0 2 8 NIL ofnI
+%                    uE-Info: 217 16 NIL 0 0 72 0 2 8 NIL ofnI
 %======================================================================
 
 % CHANGES BY ANT:
 %   Jan  7, 2009: - tightened use of exist()
+%   Aug 15, 2013: - adapted to new [ladcp2cdf.m]
 
 function [da]=savearch(dr,d,p,ps,f,att)
 % function [da]=savearch(dr,d,p,ps,f,att)
@@ -208,12 +209,14 @@
  eval(['save ',[f.res,'.ladcp.mat'],' da dr att'])
 end
 
-% save to netcdf file if you have NETCDF libary
-if exist('netcdf','file') & p.savecdf
- dr.tim=dr.tim-year0;
- disp([' save levelII results in LADCP cdffile: ',f.res,'.nc'])
- ladcp2cdf([f.res,'.nc'],dr,da,p,ps,f,att)
-else
- disp('install NETCDF libary to get the netcdf LADC archive output')
-end 
+% save to netcdf file if you have a modern-enough version of matlab
 
+if p.savecdf
+  if exist('ncwrite') == 2
+   dr.tim=dr.tim-year0;
+   disp([' save results in netcdf file: ',f.res,'.nc'])
+   ladcp2cdf([f.res,'.nc'],dr,da,p,ps,f,att)
+  else
+   disp('upgrade your matlab version to get the netcdf LADCP archive output')
+  end 
+end