function tp2cdf_d

% tp2cdf.m -- "NetCDF Toolbox for Matlab-5" code.
% tp2cdf (no argument) is a code  that builds cdf file, lists
% itself, then displays its variables.

%type(mfilename)

%help(mfilename)
 
% ---------------------------- DEFINE THE FILE --------------------------- %
addpath /usr/people/valery/netcdf/
addpath /usr/people/valery/netcdf/ncfiles
addpath /usr/people/valery/netcdf/nctype
addpath /usr/people/valery/netcdf/ncutility

ncquiet                                              

nc = netcdf('tp2cdf_d.nc', 'clobber');                

nc.description = 'The output of the QG model configured in T/P diamond'; 
nc.authors = 'D. McGillicuddy & V. Kosnyrev';
nc.date = '19 December, 2000';

nc('x2') = 31;
nc('y2') = 25;
nc('leber') = 140;
nc('wiber') = 2;
nc('leday') = 2151;
nc('lebnd') = 108;
nc('lealt_1') = 119117;
nc('lealt_2') = 175528;
nc('lealt_3') = 89993;
nc('wialt') = 5;
nc('leday6') = 6*2151;
nc('wid') = 4;
nc('ledh') =  118;
nc('leof') = 6;
nc('weof') = 1;
% ------------------------
nc('x2') = 'x2';
nc('y2') = 'y2';
nc('leof') = 'leof';
nc('weof') = 'weof';
nc('leber') = 'leber';
nc('wiber') = 'wiber';
nc('leday') = 'leday';
nc('lebnd') = 'lebnd';
nc('lealt_1') = 'lealt_1';
nc('lealt_2') = 'lealt_2';
nc('lealt_3') = 'lealt_3';
nc('wialt') = 'wialt';
nc('leday6') = 'leday6';
nc('wid') = 'wid';
nc('ledh') = 'ledh';
% ------------------------
nc{'lat'} = {'y2','x2'};                      
nc{'lon'} = {'y2','x2'};

nc{'lat'} = 'latitude';                        
nc{'lon'} = 'longitude';

nc{'alt_1'}= {'lealt_1','wialt'};
nc{'alt_2'}= {'lealt_2','wialt'};
nc{'alt_3'}= {'lealt_3','wialt'};

nc{'alt_1'}= 'alt1';
nc{'alt_2'}= 'alt2';
nc{'alt_3'}= 'alt3';

nc{'streamf_sim'} = {'y2','x2','leday'};
nc{'streamf_ref'} = {'y2','x2','leday'};

nc{'streamf_sim'} = 'simf';
nc{'streamf_ref'} = 'ref';

nc{'bndvort'} = {'leday6','lebnd'};
nc{'bndvort'} = 'bndvort';

nc{'dynheight'} = {'ledh','wid'};
nc{'dynheight'} = 'dynh';

nc{'eof'} = {'leof','weof'};
nc{'eof'} = 'eof';

nc{'ber'} = {'leber','wiber'};
nc{'ber'} = 'ber';


nc{'streamf_sim'}.units = 'QG model, nondim';
nc{'streamf_rem'}.units = 'QG model, nondim';
nc{'lat'}.units = 'degrees';                    
nc{'lon'}.units = 'degrees';
nc{'leday'}.units = 'days';
nc{'alt_1'}.units = 'day (1), latitude (2), longitude (3), SLA (mm) (4), flag (5)';
nc{'alt_2'}.units = 'day (1), latitude (2), longitude (3), SLA (mm) (4), flag (5)'; 
nc{'alt_3'}.units = 'day (1), latitude (2), longitude (3), SLA (mm) (4), flag (5)'; 
nc{'dynheight'}.units = 'cruise ID (1), yymmdd (2), day since 1 January, 1992 (3), dh (mm) (4)';
nc{'eof'}.inits = 'nondimensional';
nc{'ber'}.inits = 'longitude (1), latitude (2)';
% ---------------------------- STORE THE DATA ---------------------------- %
leday = 2151;
lebnd= 108;
lealt_1 = 119117;
lealt_2 = 175528;
lealt_3 = 89993;
wialt = 5;
leday6 = 6*2151;
wid= 4;
ledh =  118;
leof = 6;
weof=1;
leber=140;
wiber=2;
x2=31;
y2=25;
% ---------------------------- LOAD DATA ----------------------------------%


% prepare large scale grid  
      [x3,y3]=meshgrid(.85:.57627:34.85,1:.68085:33);
%transform grid to the geographycal coordinate system 
      x3=-70+(x3-.85)*10/34;
      y3=28.25+(y3-1)*8/32;
% BATS site
      xb=-64.17;
      yb=31.74;
% rotation angle
      f=pi*64.0/180;
% rotated coordinate system origin
      yn=10.2728;
      xn=23.4591;
% build meshgrid
      xi=ones(25,31);
      yi=ones(25,31);
      for k=1:1:25
      for l=1:1:31
      xi(k,l)=(xn)+(-(k-1)*sin(f)+(l-1)*cos(f))*0.366;
      yi(k,l)=(yn)+((k-1)*cos(f)+(l-1)*sin(f))*0.366;
      end
      end
% T/P meshgrid     
      longitude = -70+(xi-.85)*10/34;
      latitude=28.25+(yi-1)*8/32;
% load T/P & ERS data
      load /whoinet/deltorchio/p2/valery/TPD_1.0/data/tp12ers1c;
alt1=tp12ers1c(1:lealt_1,:);
clear tp12ers1c;
      load /whoinet/deltorchio/p2/valery/TPD_1.0/data/tp36ers2a13_1;
alt2 = tp36ers2a13_1(1:lealt_2,:);
clear tp36ers2a13_1;
      load /whoinet/deltorchio/p2/valery/TPD_1.0/data/tp36ers2a13_2;
alt3 = tp36ers2a13_2(1:lealt_3,:);
clear tp36ers2a13_2;
% load simulated & referenced streamfunction
      load /whoinet/deltorchio/p2/valery/TPD_1.0/data/dh3a15b5f.vbc;
for i=[1:leday]
simf(:,:,i)=reshape(dh3a15b5f(i,:),31,25)';
end
clear dh3a15b5f;
      load /whoinet/deltorchio/p2/valery/TPD_1.0/data/dh3a15b5f.vbr;
for i=[1:leday]
ref(:,:,i)=reshape(dh3a15b5f(i,:),31,25)';
end
clear dh3a15b5f;
% load alongboundary vorticity
      load /whoinet/deltorchio/p2/valery/TPD_1.0/data/dh3v1015.dat;  
bndvort=dh3v1015(1:leday6,:);
% load dynamic height
      load /whoinet/deltorchio/p2/valery/TPD_1.0/data/dynh20.dat;
dynh=dynh20(1:ledh,:);
% load eof
      load /whoinet/deltorchio/p2/valery/TPD_1.0/data/eof_dh3.dat;
eof=eof_dh3(1:leof,:);
% load bermuda
      load /whoinet/deltorchio/p2/valery/TPD_1.0/data/berm.dat;
ber=berm(1:leber,:);


nc{'streamf_sim'}(:) = simf;
nc{'streamf_ref'}(:) = ref;
nc{'bndvort'}(:) = bndvort;
nc{'lat'}(:) = latitude;                       
nc{'lon'}(:) = longitude;
nc{'dynheight'}(:) = dynh;
nc{'eof'}(:) = eof;
nc{'ber'}(:) = ber;
nc{'alt_1'}(:) =alt1;
nc{'alt_2'}(:) =alt2;
nc{'alt_3'}(:) =alt3;

nc = close(nc);                                      % Close the file.

% ---------------------------- RECALL THE DATA --------------------------- %

nc = netcdf('tp2cdf_d.nc', 'nowrite');                 % Open NetCDF file.
description = nc.description(:)                      % Global attribute.
variables = var(nc);                                 % Get variable data.
for i = 1:length(variables)
   disp([name(variables{i}) ' =']), disp(' ')
%   disp(variables{i}(:))
end
nc = close(nc);                                      % Close the file.

% --------------------------------- DONE --------------------------------- %
