function plot_3D(fname,lon,lat,dpt,level)

% function plot_3D(fname,lon,lat,dpt,level)
% reads file fname in mitgcm format and contour plots it
% lon is either a vector of longitudes or a scalar length
% lat is either a vector of latitudes or a scalar length
% dpt is either a vector of depths or a scalar length
% level is the level to be plotted

if nargin ~= 5, error('function plot_3D requires 5 arguments'), end
if length(lon) == 1, lon=1:lon; end
if length(lat) == 1, lat=1:lat; end
if length(dpt) == 1, dpt=1:dpt; end
nlon=length(lon); nlat=length(lat); ndpt=length(dpt);

eval(['fid=fopen(''' fname ''',''r'',''b'');'])
tmp=fread(fid,1,'integer*8');        % fortran record length in bytes
if tmp ~= nlon*nlat*ndpt*4, error('bad fortran record'), end

fld=fread(fid,[nlon*nlat,ndpt],'real*4');
tmp=fread(fid,1,'integer*8');        % fortran record length in bytes
if tmp ~= nlon*nlat*ndpt*4, error('bad fortran record'), end
tmp=fclose(fid);

fld=reshape(fld,nlon,nlat,ndpt);
contourf(lon,lat,squeeze(fld(:,:,level))')
colorbar('horiz')
