%NCP2NC  save TNO arc grid file of NCP as netCDF file
%
%See also: NCP2KML
   
%   --------------------------------------------------------------------
%   Copyright (C) 2010 Deltares for Building with Nature
%       Gerben de Boer
%
%       g.j.deboer@deltares.nl
%
%       Deltares
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%   This library is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 of the License, or
%   (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this library.  If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://OpenEarth.nl">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute
% your own tools.

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

   rawdirectory  = [first_subdir(mfilename('fullpath'),-1),filesep,'raw',filesep];
   ncdirectory   = [first_subdir(mfilename('fullpath'),-1),filesep,'processed',filesep];

%% overall parameters

   % set meta-info   

   OPT.institution    = 'Dienst der Hydrografie, Koninklijke Marine / Hydrographical Survey, Royal Navy, the Netherlands';
   OPT.source         = '';
   OPT.history        = ['$HeadURL$'];
   OPT.references     = 'http://www.hydro.nl/';
   OPT.email          = 'info@hydro.nl';
   OPT.comment        = 'NOT MEANT FOR NAVIGATIONAL PURPOSES. The grid is an interpolated grid with 200 x 200m cells. The data consists of a mosiac of soundings gathered over a long period (so a sounding made in 1990 might be right next to a sounding made in 2005) and contain both single beam data and multibeam data of a range of data densities(however the grid always contains more sounding point per grid cell). For large parts of the NCP (especially in the zone north of the Frisian Front) no digital data was avai;ble for interpolation. IN those parts the grid has been filled with information from old minute maps (on paper), by digitizing the hand-drawn contour lines (acknowlegdements: Peter Frantsen van of Deltares-TGG.) The datum is MLLWS (mean low low waterspring). The grid contains some artifacts: some rhythmic waves are visible on the Brown Bank, which are not recognizable on a  modern Multibeam echo sounding recording: this will be an interpolation artifact.';
   OPT.version        = 'F3205/001988/BI (january 2010)';
   OPT.acknowledge    =['These data can be used freely for research purposes provided that the following source is acknowledged: ',OPT.institution];
   OPT.disclaimer     = 'This data is made available in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The dats is NOT MEANT FOR NAVIGATIONAL PURPOSES';
   
   % coordinate meta-info

   OPT.epsg           = 23031;         % 'ED50 / UTM zone 31N'
   OPT.standard_name  = '';            % required if present see: http://cf-pcmdi.llnl.gov/documents/cf-standard-names/standard-name-table/current/standard-name-table
   OPT.type           = 'single';      % for slib % and micrometers single precision is sufficient
   OPT.latitude_type  = 'single';      % for 200 m cell size single precision is sufficient
   OPT.longitude_type = 'single';      % for 200 m cell size single precision is sufficient
   
   % process info

   OPT.convertperline = 250; % fast conversion (if memory allows)

%% specific parameters

   OPT2(1).title          = 'Bathymetry Dutch Continental Shelf';
   OPT2(1).units          = 'm';
   OPT2(1).varname        = 'depth';
   OPT2(1).long_name      = 'depth of the sea floor (UTM31 ED50 projection, depth relative to MLLWS)';
   OPT2(1).dataset        = 'bathy200mgrid';

%% save as netCDF

   for jj=1:length(OPT2)   
   
   disp(['processing ',num2str(jj,'%0.4d'),': ',OPT2(jj).dataset]);
   
      % read raw data
   
     %rawname       = [rawdirectory,OPT2(jj).dataset,filesep,'rastert_',OPT2(jj).dataset(1:7),'1.txt'];
     %D             = arcgisread     (rawname,'varname',OPT2(jj).varname);
      rawname       = [rawdirectory,OPT2(jj).dataset];
    [D.x,D.y,D.(OPT2(jj).varname),M] = arc_info_binary(rawname);
      
      % add options
      
      OPT.title     = OPT2(jj).title    ;
      OPT.units     = OPT2(jj).units    ;
      OPT.varname   = OPT2(jj).varname  ;
      OPT.long_name = OPT2(jj).long_name;
      
      args          = reshape([fieldnames(OPT     )'; struct2cell(OPT     )'], 1, 2*length(fieldnames(OPT     )));
      
      ncname        = [ncdirectory,OPT2(jj).dataset '.nc'];
      cdlname       = [ncdirectory,OPT2(jj).dataset '.cdl'];
   
      nc_cf_grid_write(ncname,'x',D.x,'y',D.y,'val',D.(OPT2(jj).varname),args{:});

      fid = fopen(cdlname,'w');
         nc_dump(ncname,fid)
      fclose(fid);
      
   end   
   
%% EOF
