  function tp2cdf_m
%      *   program tp2cdf_m.m
%      *   tp2cdf_m -- "NetCDF Toolbox for Matlab-5" code.
%      *   tp2cdf_m (no argument) is a code  that builds cdf file, lists
%      *   itself, then displays its variables.
 
% ---------------------------- 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
addpath /whoinet/deltorchio/p2/valery/TP_netCDF

ncquiet                                              

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

nc.description = 'The output of the QG model configured in T/P diamond'; 
nc.authors = 'D. McGillicuddy & V. Kosnyrev';
nc.date = '27 June, 2001';
% define dimensions
nc('x2') = 31;
nc('y2') = 25;
nc('leday') = 2151;
nc('leof') = 6;
nc('ldate') = 11;
%  define variables
nc{'lat'} = {'y2','x2'};                      
nc{'lon'} = {'y2','x2'};

nc{'lat'}.x_axis_rotation = 'rotated 64 deg cckw';
nc{'lon'}.y_axis_rotation = 'rotated 64 deg cckw';

nc{'lat'}.units = 'degrees_east';                    
nc{'lon'}.units = 'degrees_north';

nc{'lat'}.point_spacing = 'even';
nc{'lon'}.point_spacing = 'even';

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

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

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

nc{'streamf_sim'}.units = 'meters^2/second';

nc{'streamf_sim'}.long_name = 'predicted streamfunction';

nc{'streamf_sim'}.missing_value = -1.e+34;

nc{'streamf_sim'} = 'simf';

nc{'vort_sim'} = {'y2','x2','leof','leday'};

nc{'vort_sim'}.units = 'second^-1';

nc{'vort_sim'}.long_name = 'predicted vorticity';

nc{'vort_sim'}.missing_value = -1.0e+34;

nc{'vort_sim'} = 'simv';

nc{'depth'} = {'leof'};

nc{'depth'}.units = 'meters';

nc{'depth'}.long_name = 'depth streamfunction defined';

nc{'depth'} = 'depth';

nc{'time'}=ncchar('leday','ldate');
nc{'time'}.long_name = 'date in dd-mmm-yyyy format';

nc{'time'}.units = 'days';

nc{'time'} = 'time';
% ---------------------------- LOAD DATA ----------------------------------%
% ---------------------------- STORE DATA ---------------------------- %
leday = 2151;
leof = 6;
x2=31;
y2=25;
ldate = 11;

% 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;
% 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 simulated  streamfunction

im=leday*6;

fid =fopen('dh3a15b5f_2001.vbc','r')
a=fscanf(fid,'%f',[775 im])';
fclose(fid);
for i=[1:leday]
kk=(i-1)*6;
for k=[1:leof]
kk=kk+1;
simf(:,:,k,i)=reshape(a(kk,:),31,25)';
end
end

% load simulated  vorticity
fid =fopen('dh3a15b5f_2001.voc','r')
a=fscanf(fid,'%f',[775 im])';
fclose(fid);
for i=[1:leday]
kk=(i-1)*6;
for k=[1:leof]
kk=kk+1;
simv(:,:,k,i)=reshape(a(kk,:),31,25)';
end
end

clear a

for i=[1:leday]
time(i,:) = datestr(i+299+datenum(1992,1,1),1);
end

% load depth
    depth=[ 100 400  800 1200 2000 3550];   

% load variables
nc{'lat'}(:) = latitude;
nc{'lon'}(:) = longitude;
% rescale to the dimensional units (m**2/sec ** 10^-3)
nc{'streamf_sim'}(:) = simf*1.2;
% rescale to the dimensional units (1/sec **10^6)
nc{'vort_sim'}(:) = simv*1.615;
nc{'depth'}(:) = depth;
nc{'time'}(:)  = time;

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

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

nc = netcdf('tp2_m.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})
   disp([size(variables{i})])
   disp([att(variables{i})])
   disp([dim(variables{i})])
end
nc = close(nc);                                      % Close the file.

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