         function view_cdf_m(jbeg,jend,interval,lev)
%        *****************************************************************
%        program view_cdf_m(jbeg,jend,interval,lev)
%        *
%        *  jbeg - beginning day of the viewing (1:2151);
%        *  jend - ending day of the viewing (1:2151);
%        *  attention!!!   jend > jbeg ! 
%        *  interval - time interval (in days);
%        *  lev - depth level number;
%        *  example:  view_cdf_m(25,2000,10,1)
%        *
%        ******************************************************************
% 
%     DEFINE ERROR STRINGS
err4=str2mat('You must specify 4 input arguments:',...
      'jbeg - the beginning day of the viewing period (1:2151)',...
      'jend - the ending day of the viewing period (1:2151)',...
      'interval - time interval (in days)',...
      'lev - depth level number (1:6)',...
      'please specify all of them');

err3=str2mat('You must specify 4 input arguments:',...
      'jbeg - the beginning day of the viewing period (1:2151)',...
      'jend - the ending day of the viewing period (1:2151)',...
      'interval - time interval (in days)',...
      'lev - depth level number (1:6)',...
      'please specify 3 more parameters');

err2=str2mat('You must specify 4 input arguments; ',...
      'jbeg - the beginning day of the viewing  period (1:2151)',...
      'jend - the ending day of the viewing  period (1:2151)',...
      'interval - time interval (in days)',...
      'lev - depth level number (1:6)',...
      'please specify 2 more parameters');

err1=str2mat('You must specify 4 input arguments; ',...
      'jbeg - the beginning day of the viewing  period (1:2151)',...
      'jend - the ending day of the viewing  period (1:2151)',...
      'interval - time interval (in days)',...
      'lev - depth level number (1:6)',...
      'please specify 1 more parameter');
       
if nargin < 1
   disp('??? Error using ==> view_cdf ');
   disp(' ');
   disp(err4);
   return
end
if nargin < 2
   disp('??? Error using ==> view_cdf ');
   disp(' ');
   disp(err3);
   return
end
if nargin < 3
   disp('??? Error using ==> view_cdf ');
   disp(' ');
   disp(err2);
   return
end
if nargin < 4
   disp('??? Error using ==> view_cdf ');
   disp(' ');
   disp(err1);
   return
end

   disp(' ');

netcdfhome = '/whoinet/bernard/usr/people/valery/netcdf/';

eval(['addpath ' netcdfhome]);
eval(['addpath ' netcdfhome 'ncfiles']);
eval(['addpath ' netcdfhome 'nctype']);
eval(['addpath ' netcdfhome 'ncutility']);

%  program view_cdf.m     
%  view content of the netCDF file: predicted streamfunction  field, 
%  referenced SLA field (OA output), and pridicted-referenced difference.
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
%                                                                      $
%                      PROGRAM VIEW_CDF                                $
%                                                                      $
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
% set up input parameters:
% BATS site coordinates
       xbats=-64.17;
       ybats=31.74;
% set julian day, model day, the beginning and the ending days of the movie  
       jul=datenum(1992,10,27);
       t=jul-datenum(1992,1,1)+(jbeg-1);
       disp('your choice:')
       disp('  ')
       aa=['jbeg = ',num2str(jbeg),', or '];
       disp(aa);
       mm=jul+(jbeg-1);
       aa=datestr(mm,1);
       disp('  ');
       disp(aa);
       disp('  ');
       aa=['jend = ',num2str(jend),', or '];
       disp(aa);
       mm=jul+(jend-1);
       aa=datestr(mm,1);
       disp('  ')
       disp(aa);
       disp('  ');
       aa=['interval = ',num2str(interval),' day(s) '];
       disp(aa);
       disp('  ');
       aa=['depth level = ',num2str(lev)];
       disp(aa);
       disp('  ');
       disp('Press "Enter" to start the viewing');
       input(' ')
%       return
% set colormap
       colormap(jet(14));
% set caxis limits
       cval1=[-70:5:70];
       cval2=[-56:2:56];
% ------------------------------------------------------------%
% ------------------------------------------------------------%
%          OPEN SOURCE (CDF) FILE                             %
% ------------------------------------------------------------%
% ------------------------------------------------------------% 
        nc = netcdf('tp2_m.nc', 'nowrite');
        description = nc.description(:)                      
        authors=nc.authors(:)
        date=nc.date(:)                      
%  load source files: computed  streamfunction
%  and vorticity (nondimensional)
       disp('************************************** ');
       disp('Loading 3-D streamfunction...');
       disp(' ');
       disp('Wait 1-3 minutes...')
       disp(' ');
       disp('************************************** ');
       disp(' ');
       disp(' ');
       
       a=nc{'streamf_sim'}(:);
%  get predicted and observed streamfunction (m^2/sec ** 10^-3)
%  get predicted and observed vorticity (1/sec ** 10^6)
       disp('Loading 3-D vorticity...');
       disp(' ');
       disp('Wait 1-3 minutes...')
       disp(' ');
       disp('************************************** ');
       disp(' ');
       disp(' ');
       disp(' ');

       c=nc{'vort_sim'}(:);
       disp('Done');
       disp(' ');
% get coordinates (rotated 64 degrees cckw)
       xi=nc{'lon'}(:);
       yi=nc{'lat'}(:);
% get time
       time=nc{'time'}(:);
%get streamfunction levels
       depth=nc{'depth'}(:);
%get vorticity levels
       nc = close(nc);
% start drawing fields
       for j=[jbeg:interval:jend];
       as=a(:,:,lev,j);
       cs=c(:,:,lev,j);
% draw predicted SLA
       subplot(1,2,1)
       contourf3(xi,yi,as,cval1);
       hold on
       plot(xbats,ybats,'o','MarkerSize',10,'Markerfacecolor',...
       'w','Markeredgecolor','r');
       hold off
       text(xbats,ybats,['    BATS '],'HorizontalAlignment','left',...
       'color','k','FontWeight','bold','FontSize',10);

       axis;
       lonmax=ans(2);
       lonmin=ans(1);
       latmax=ans(4);
       latmin=ans(3);
       weight=111*cos(pi*(latmax+latmin)*0.5/180);
       xc=(lonmax-lonmin)*weight;
       yc=(latmax-latmin)*111;
       relyx=yc/xc;
 
       set(gca,'PlotBoxAspectRatio',[1 relyx*1 1]);
       caxis([-35 35]);
       hh=text(-61.9,34.75,[time(j,:)],'color','b','FontWeight','bold','FontSize',10);
       hh1=text(-62.9,33.75,['z=',num2str(depth(lev)),' m'],'color',...
       'k','FontSize',10);
     
       h1=colorbar('horiz');
       set(h1,'Position',[0.1300    0.1800    0.3270    0.0311])
       set(h1,'FontSize',10);
       set(h1,'xtick',[-35:5:35]);
       set(h1,'xTickLabel','|-30||-20||-10||0||10||20||30|'); 
       set(h1,'ticklength',[0.05 0.025]);
       ylabel('Latitude (deg N)','FontSize',11);
       xlabel('Longitude (deg E)','FontSize',11);
       title(['Predicted \Psi (m^2/sec * 10^-^3)'],'FontSize',11);
       grid on
       box on
% draw predicted vorticity
       subplot(1,2,2)
       contourf3(xi,yi,cs,cval2);
       hold on;
       plot(xbats,ybats,'o','MarkerSize',10,'Markerfacecolor',...
       'w','Markeredgecolor','r');
        hold off
       text(xbats,ybats,['    BATS '],'HorizontalAlignment','left',...
       'FontWeight','bold','FontSize',10);
       set(gca,'PlotBoxAspectRatio',[1 relyx*1 1]);
       caxis([-14 14]);
       hh2=text(-62.9,33.75,['z=',num2str(depth(lev)),' m'],'color',...
       'k','FontSize',10);

       h3=colorbar('horiz');
       set(h3,'Position',[0.5780    0.1800    0.3270    0.0311])
       set(h3,'xtick',[-14:2:14]);
       set(h3,'xticklabel','|-12|| -8 ||-4 ||0|| 4 ||8|| 12|');
       set(h3,'ticklength',[0.05 0.025]);
       xlabel('Longitude (deg E)','FontSize',11);
       ylabel('Latitude (deg N)','FontSize',11);
       title(['Predicted \zeta (1/sec * 10^6)'],'color','k',...
       'FontSize',11);
       grid on
       box on
      
       
       t=t+interval;
       jul=jul+interval;
       input('Press "Enter" to see the next plot');
       delete(hh)
       end
