function DGM2NC(varargin)
%DGM2NC  save TNO arc grid file of DGM as netCDF file
%
%See also: AHN2KML, nc_cf_grid_write
   
%   --------------------------------------------------------------------
%   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];

   ncdirectory   = 'F:\opendap\thredds\tno\dgm\';

%% overall parameters

   % set meta-info   

   OPT.institution    = 'Geological Survey of the Netherlands';
   OPT.source         = 'Geological Survey of the Netherlands';
   OPT.history        = ['$HeadURL$ $Id$'];
   OPT.references     = 'http://www.tno.nl/; http://www.deltares.nl/';
   OPT.email          = 'Jan.Stafleu@deltares.nl';
   OPT.comment        = '';
   OPT.version        = '$Id$';
   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.';

   % coordinate meta-info

   OPT.epsg           = 28992;          % RD
   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          = 'Kreftenheye Formation';
   OPT2(1).units          = 'm';
   OPT2(1).varname        = 'kr_bcc';
   OPT2(1).long_name      = 'depth of Holocene Formation';
   OPT2(1).dataset        = 'kr_bcc';

   OPT2(2).title          = 'Holocene Formation';
   OPT2(2).units          = 'm';
   OPT2(2).varname        = 'hl_bcc';
   OPT2(2).long_name      = 'depth of Holocene Formation';
   OPT2(2).dataset        = 'hl_bcc';

 
%% 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,'.asc'];
      D             = arcgisread     (rawname,'varname',OPT2(jj).varname);
      
      % 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
