clear
%  http://cf-pcmdi.llnl.gov/  <-compliances

home= '/login/jebro/';
% NETCDF
path(path,[home,'/netcdf_toolbox/mexnc']);
path(path,[home,'/netcdf_toolbox/netcdf']);
path(path,[home,'/netcdf_toolbox/netcdf/nctype']);
path(path,[home,'/netcdf_toolbox/netcdf/ncutility']);
path(path,[home,'/netcdf_toolbox/netcdf/ncsource']);


%[fid1,message]=fopen('/login/jebro/POLCOMS/jan05/bathymetry.dat','r','n')% liverpool
[fid1,message]=fopen('/login/jebro/POLCOMS/10yr_IRS_WAM/bathymetry.dat','r','n')% IRS

%IRS
nlat = 301;
nlon = 173;
lonw= -7; lone= -2.7;
lats= 51; latn= 56;

%S12
% nlat = 224;
% nlon = 198;
% lonw= -19.83333; lone= 13.;
% lats= 40.11111; latn= 64.88889;

%LB
% nlat = 474;
% nlon = 576;
% lonw= -3.99875; lone= -2.56125;
% lats= 53.1358333; latn= 53.92416663;


dx = (lone-lonw) / (nlon-1);
dy = (latn-lats) / (nlat-1);
xx= [lonw:dx:lone] ;
yy= [lats:dy:latn];

 %%%% headers etc for netcdf file  (metadata)
l=nlon;m=nlat;n=34;% from prameters.dat
clear dims vars atts
%xx=(1:l)/40-lonw;yy=(1:m)/60+lats;
lon=xx;lat=yy;

%
dims(2).name          = 'lat' ;
dims(2).standard_name = 'latitude' ;
dims(2).long_name     = 'latitude' ;
dims(2).units         = 'degrees_north' ;
dims(2).type          = 'float' ;
dims(2).length        =  m;
%
dims(1).name          = 'lon' ;
dims(1).standard_name = 'longitude' ;
dims(1).long_name     = 'longitude' ;
dims(1).units         = 'degrees_east' ;
dims(1).type          = 'float' ;
dims(1).length        =  l;
%
vars(1).name = 'lon' ;
vars(1).dims{1} = 'lon';
vars(1).type = 'float' ;
vars(1).fillval = -9999.00 ;
vars(1).long_name = 'longitude' ;
vars(1).standard_name = 'longitude' ;
vars(1).units = 'degrees_east';
%
vars(2).name = 'lat' ;
vars(2).dims{1} = 'lat';
vars(2).type = 'float' ;
vars(2).fillval = -9999.00 ;
vars(2).long_name = 'latitude' ;
vars(2).standard_name = 'latitude' ;
vars(2).units = 'degrees_north';
%
vars(3).name  = 'bathy';
vars(3).standard_name  = 'sea_floor_depth_below_sea_level'; 
vars(3).long_name  = 'Bathymetry - positive is depth below the mean tidal level';
vars(3).type  = 'float';
vars(3).fillval = -9999.00 ;
vars(3).dims{2} = 'lon';
vars(3).dims{1} = 'lat';
vars(3).units = 'm';
%

%
atts.source    = 'Bathymetric data for the 11-year POLCOMS-WAM run on the Irish Sea Model (IRS), -9999.00 is a dry land point. The Irish Sea model bathymetry is available/stored at www.BODC.ac.uk in CF-netCDF format.';
atts.author  = 'Jenny Brown';
atts.inst = 'Proudman Oceanographic Laboratory';
atts.title = '11-year POLCOMS-WAM bathymetry';
atts.conv = 'CF-1.4';

outfile=['POLCOMS_WAM_bath.nc'];
                              
nc_produce_auto(outfile,dims,vars,atts);
eval(['nc = netcdf(''' outfile ''',''write'');'])

nc{'lon'}(:)=lon;
nc{'lat'}(:)=lat;
batht=zeros(m,l);
    

%%%%end of headers for netcdf file 

%load in data
bath = zeros(1,nlon*nlat);
%IRS
int = fix(nlon*nlat / 51) ;
for i=1:int
    i1 = (i-1)*51 + 1;
    i2 = i*51;
    bath(i1:i2) = fscanf(fid1,'%e',51);
end

%LB
%int = fix(nlon*nlat / 20) ;
%for i=1:int
%    i1 = (i-1)*20 + 1;
%    i2 = i*20;
%    bath(i1:i2) = fscanf(fid1,'%e',20);
%end
i1 = i2 + 1;
i2 = ((198*224)-44350)+i1-1;
a = fix(i2-i1) + 1;
bath(i1:i2) = fscanf(fid1,'%e',a);
status = fclose(fid1)

bath1 = zeros(nlon,nlat);
for j=1:nlat
    i1 = (j-1)*nlon + 1;
    i2 = j*nlon;
    bath1(:,j) = bath(i1:i2);
end
II=find(bath1<0);bath1(II)=-9999.00;
nc{'bathy'}(:,:)=bath1';

close(nc)
 
figure(1)
bath1(II)=NaN;
pcolor(xx,yy,bath1');shading flat;colormap(jet); colorbar;hold on
caxis([0 270])






