% make bathymetry file "topog" for global model configuration

% load and plot mitgcm bathymetry file
% file created by Alistair for integrating a 4-degree global configuration
clear all, clf reset, cd /if7/dimitri/mitgcm/matlab
plot_2D('/if7/dimitri/mitgcm/UV/exe/topog',90,40)

% load tong lee's bathymetry files and save in matlab format
clear all, clf reset, cd /if7/dimitri/mitgcm/matlab

fid=fopen('/if7/medea/tlee/grid/z.grid','r'); % z-grid depths
tmp=fgetl(fid);                      % read and discard file header
mdpt=fscanf(fid,'%f',[2 inf])';
tmp=fclose(fid);
mdpt=mdpt(:,2);

fid=fopen('/if7/medea/tlee/grid/cgrid.dat','r'); % bathymetry files
tmp=fgetl(fid); tmp=fgetl(fid);      % read and discard file header
topog=fscanf(fid,'%f',[4 inf])';
tmp=fclose(fid);
lon=topog(:,1);                      % longitude east (deg)
lat=topog(:,2);                      % latitude north (deg)
mtopog=topog(:,3);                   % model depth in (m)
etopo5=topog(:,4);                   % ETOPO5 bined bathymetry (m)
topog=topog(:,3);                    % model depth level

for i=1:length(mdpt)                 % convert level to depth
  mtopog(find(mtopog==i))=mdpt(i)-.01;
end, clear tmp fid i

lon=reshape(lon,224,360); lon=lon(1,:);
lat=reshape(lat,224,360); lat=lat(:,1);
mtopog=reshape(mtopog,224,360);
etopo5=reshape(etopo5,224,360);
topog=reshape(topog,224,360);
save cgrid                           % bathymetry file in matlab format

% $$$ fid=fopen('topog.int','wt');         % integer bathymetry level file
% $$$ tmp=fprintf(fid,'%3i',topog);
% $$$ tmp=fclose(fid); clear fid tmp
% $$$ contour(lon,lat,topog,0:5)

% save bathymetry file in binary format
fid=fopen('topog','w','b');
tmp=fwrite(fid,length(mtopog(:))*4,'integer*8'); % fortran record length
tmp=fwrite(fid,mtopog','real*4');
tmp=fwrite(fid,length(mtopog(:))*4,'integer*8'); % fortran record length
tmp=fclose(fid);
plot_2D('topog',lon,lat)

