function redo_webfigs(cruise_path, varargin)
% redo_webfigs(cruise_path, varargin)
% remakes all the figures of a specified type (see argument list)
% 
% cruise_path is the full or relative path to the web directory, for
%    example '/home/moli20/htdocs/ron_brown/rb0003'
%
%  figure type       values          comment
%   'contour'         0 or 1         1: redo all contour figures, 0: do not
%   'vector'          0 or 1         1: redo all vector figures, 0: do not
% 
% usage ex. 1): redo all plots after regenerating new "c/v_sect_xy/uv.mat" files
%         from /home/moli20/htdocs/ron_brown/
%         >> redo_webfigs('rb0003')
%         
% usage ex. 2): redo all vector plots with bold text, and TSG temperature data:
%         from /home/moli20/htdocs/ron_brown/
%         >> redo_webfigs('rb0003', 'contour', 0, 'bold', 1,...
%              'mv_cdatafile', '/home/moll5/ron_brown/rb0003/edit/a0003tsg.tem')
%
%  ---View bottom of file for lists of changable parameters----

%NOTE: all .eps &  .png are redone. Html files do not need redoing.  
if nargin == 0, eval('help redo_webfigs'); return; end;

%set up the defaults in a structure
cfg.contour = 1;
cfg.vector = 1;

%replace values in cfg with values from varargin
cfg = fillstruct(cfg, varargin, 1);
pdir = pwd;
cd(cruise_path)

if (cfg.contour)
   disp('Regenerating contour plots')
   disp('  Retrieving contour data')
   loaddata(1)
   disp('  Processing contour .eps/.png files')
   repeatcsections(cfg)
else
   disp('not regenerating contour figures')
end

if (cfg.vector)
   disp('Regenerating vector plots')
   disp('  Retrieving vector data')
   loaddata(2)
   disp('  Processing vector .eps/.png files')
   repeatvsections(cfg)
else
   disp('not regenerating vector figures')
end
cd(pdir)

%%%%%%%%%%%

function loaddata(kindof)

global XYDATA
global UVDATA

% Load data
loaderrorflag = 0;
while loaderrorflag == 0
   if kindof == 1  % contours
      fnameuv = 'c_sect_uv.mat';
      fnamexy = 'c_sect_xy.mat';
   else               % vectors
      fnameuv = 'v_sect_uv.mat';
      fnamexy = 'v_sect_xy.mat';
   end
   if ~exist(fnameuv, 'file') | ~exist(fnamexy, 'file')
      fnameuv = input('Enter full path/name to uv.mat file (with quotes):  ');
      fnameuv = input('Enter full path/name to uv.mat file (with quotes):  ');
   end  
   eval('UVDATA = load(fnameuv);', 'loaderrorflag = 1;');
   eval('XYDATA = load(fnamexy);', 'loaderrorflag = 1;');
   if loaderrorflag
      msg = sprintf('\nError trying to load %s \nor %s ', fnameuv, fnamexy);
      noticeOK(msg)
      pause(5) % otherwise rips too fast
      loaderrorflag = 0;
   else
      break
   end
end

preparedata

%____________________________ CONTOURS
%%%%%%%%%%%%
function repeatcsections(cfg)

global XYDATA
global UVDATA
global CONTFIG

% Retrieve sectinfo data
if ~exist('sectinfo.mat', 'file')
   msg = sprintf('\nFatal Error: no sectinfo.mat ');
   noticeOK(msg)
   return
else
   load sectinfo
end

for secti = 1: length(sect_info) %%%% repeat sections
    % 1) Retrieve specs saved in webcspecs_*.mat
    webfile = sprintf('webcspecs_%s.mat', sect_info(secti).name);
    if exist(webfile, 'file'), 
       load(webfile);
    else
       msg = sprintf('No contour spec file for Section %s', sect_info(secti).name);
       disp(msg) 
       continue
    end;
    % 2) Overwrite with any specs given with varargin
    fn = fieldnames(cfg);
    for item = 1: length(fn)
       fldnm = char(fn(item));
       evalstr = sprintf('CONTFIG.%s = cfg.%s;', fldnm, fldnm);
       eval(evalstr)
    end

    CONTFIG.ii = find(XYDATA.dday >= min(CONTFIG.DDAY) & XYDATA.dday <= max(CONTFIG.DDAY));
    XYDATA.X=XYDATA.x(CONTFIG.ii);
    XYDATA.Y=XYDATA.y(CONTFIG.ii);
    XYDATA.DDAY=XYDATA.dday(CONTFIG.ii);
    
    gcontour
    pfname = sprintf('cont_%s.eps', sect_info(secti).name);
    orient tall; 
    print( '-depsc2', pfname);
    close(gcf)
    fnbase = sprintf('cont_%s', sect_info(secti).name);  
    gsconvert(fnbase, 'eps', 'png')  % will choke if includes long path name
 end

 
% __________________________ VECTORS       
%%%%%%%%%%

function repeatvsections(cfg)

global XYDATA
global UVDATA
global VECFIG

% Retrieve sectinfo data
if ~exist('sectinfo.mat', 'file')
   msg = sprintf('\nFatal Error: no sectinfo.mat ');
   noticeOK(msg)
   return
else
   load sectinfo
end

for secti = 1: length(sect_info) %%%% repeat sections
    % 1) Retrieve specs saved in webcspecs_*.mat
    webfile = sprintf('webvspecs_%s.mat', sect_info(secti).name);
    if exist(webfile, 'file'), 
       load(webfile);
    else
       msg = sprintf('No vector spec file for Section %s', sect_info(secti).name);
       disp(msg) 
       continue
    end;
    % 2) Overwrite with any specs given with varargin
    fn = fieldnames(cfg);
    for item = 1: length(fn)
       fldnm = char(fn(item));
       evalstr = sprintf('VECFIG.%s = cfg.%s;', fldnm, fldnm);
       eval(evalstr)
    end
    
    VECFIG.ii = find(XYDATA.dday >= min(VECFIG.DDAY) & XYDATA.dday <= max(VECFIG.DDAY));
    
    XYDATA.X=XYDATA.x(VECFIG.ii);
    XYDATA.Y=XYDATA.y(VECFIG.ii);
    XYDATA.DDAY=XYDATA.dday(VECFIG.ii);
    UVDATA.U=UVDATA.u(:,VECFIG.ii);
    UVDATA.V=UVDATA.v(:,VECFIG.ii);
    
    pd1val = VECFIG.vectorlevel(1);
    pd2val = VECFIG.vectorlevel(2);
    if pd1val == pd2val  % one level only
       UVDATA.UU=UVDATA.U(VECFIG.vectorlevel(1),:)';
       UVDATA.VV=UVDATA.V(VECFIG.vectorlevel(1),:)';
    else  % multiple levels - needs weighted average
       ref = min(diff(XYDATA.Z));
       for k = 1:length(XYDATA.Z)-1
          wt(k) = (XYDATA.Z(k+1)-XYDATA.Z(k))/ref;
       end
       totwt = sum(wt(pd1val:pd2val));
       UT = zeros(1,length(UVDATA.U(1,:))); 
       VT = UT; 
       for k = pd1val:pd2val
          UT(1,:) = UT(1,:) + UVDATA.U(k,:).*wt(k);
          VT(1,:) = VT(1,:) + UVDATA.V(k,:).*wt(k);
       end
       UVDATA.UU=UT(1,:)./totwt;
       UVDATA.VV=VT(1,:)./totwt;
       UVDATA.UU=UVDATA.UU';
       UVDATA.VV=UVDATA.VV';
    end
    
    TT=-99+0*XYDATA.X;
    tt = [];  % try to fill this with temperature data
    
    if VECFIG.usecdata & ~isempty(VECFIG.mv_cdatafile)
       ttfile = VECFIG.mv_cdatafile;
       tt = read_asc(ttfile);        % col 1 = ddates; col 2 = temperatures
       ttout = zeros(size(XYDATA.dday));   % already badi NaN'ed
       trouble = 0;
       for lap = 1:length(XYDATA.dday)
          index = find(tt(:,1) >= XYDATA.dday(lap));  % == off in 4th decimal usu.
          eval('ttout(lap) = tt(index(1),2);', 'trouble = 1;')
          if trouble == 1
             marks = findstr(ttfile,'/');
             shortname = ttfile(marks(end-2):end);
             okstr = sprintf('\nTemperature file error. \n\nEdit: %s\n\nProbably not enough dday time', shortname); 
             noticeOK(okstr)
             return
          end
       end
       if isempty(tt)
          msg = sprintf('Cdata file unsuccessfully read:\nfile must be >= 2 cols.\n');
          msg = sprintf('%s(date  data(e.g. temps).', msg);
          noticeOK(msg);
          return
       else
          TT = ttout;
       end
    else
       TT = VECFIG.mv_color;
    end
    VECFIG.TT = TT;
    
    vv = num2str(VECFIG.lonrange);
    VECFIG.lonrange = str2num(vv); % do not delete this strange couplet
    
    gvector
    pfname = sprintf('vect_%s.eps', sect_info(secti).name);
    orient tall; 
    pause(1)
    print( '-depsc2', pfname);
    pause(2)
    close(gcf)
    fnbase = sprintf('vect_%s', sect_info(secti).name);  
    gsconvert(fnbase, 'eps', 'png')  
 end
 
 %_____________  LISTS OF POTENTIAL PARAMETERS
 
% CONTOURS------
% depthstr: '[ 0  400 ]'
% bold: 1
% custlevstr: [-200:20:200]
% boldlevs: '0'
% outline: 0
% disable: 0
% title:  RB0003, Section A; 04/13-04/17, 2000
% latrange: [45 58]
% lonrange: [225 241]
% blkwht: 0
% reftype: 2
		% reftype's complete code:
		%          1 both, long, where both = vect & cont plots
		%          2 both, lat
		%          3 both, time
		%          4 vec only
		%          5 cont only, long
		%          6 cont only, lat
		%          7 cont only, time

% VECTORS------
% title:  RB0003, Section A; 25-75 m; 04/13-04/17, 2000
% vec_scale: 0.3333
% usecdata: 1
% cdata_mm: -99    (represents auto, otherwise [0  15] e.g.)
% mv_cdatafile: /home/moli4/datasets/ron_brown/rb0003/edit/a0003.tem
% m_coast_str: m_coast('patch',[0.6  0.5  0.0]);
% basintype: 1
% basinresolution: 1
% bold: 0
% skiptitle: 0
% blkwht: 0
% veclevelmeters: {'25'; '75'}
% vectorlevel: 1
% mv_color: 'k'
% mv_edgecolor: 'k'
% mv_headangle: 60
% mv_headwidth: NaN
% mv_shaftwidth: 1
% mv_headlength: 5
% mv_centered: 0
% mv_suppdatafile: []
% mv_supplementary: 0
% mv_colorsupp: 'none'
% mv_edgecolorsupp: 'red'
% circlescale: 10
% circlescalesupp: '20'



