function NCP2NC(varargin)
%NCP2NC  save TNO arc grid file of NCP 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\ncp\';

%% 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/';
   OPT.email          = 'denise.maljers@tno.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           = 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          = 'sediment grain size D10 in upper seabed of Dutch continental shelf part of North Sea';
   OPT2(1).units          = 'micrometer';
   OPT2(1).varname        = 'D10';
   OPT2(1).long_name      = 'D10 sediment grain size diameter in upper seabed';
   OPT2(1).dataset        = 'dz10_juli2007';

   OPT2(2).title          = 'sediment grain size D50 in upper seabed of Dutch continental shelf part of North Sea';
   OPT2(2).units          = 'micrometer';
   OPT2(2).varname        = 'D50';
   OPT2(2).long_name      = 'D50 sediment grain size diameter in upper seabed';
   OPT2(2).dataset        = 'dz50_juli2007';

   OPT2(3).title          = 'sediment grain size D90 in upper seabed of Dutch continental shelf part of North Sea';
   OPT2(3).units          = 'micrometer';
   OPT2(3).varname        = 'D90';
   OPT2(3).long_name      = 'D90 sediment grain size diameter in upper seabed';
   OPT2(3).dataset        = 'dz90_juli2007';

   OPT2(4).title          = 'pebble percentage in upper seabed of Dutch continental shelf part of North Sea';
   OPT2(4).units          = '100*kg/kg';
   OPT2(4).varname        = 'pebbles';
   OPT2(4).long_name      = 'pebble fraction in upper seabed (by mass)'; 
   OPT2(4).dataset        = 'grind_fbr2007';

   OPT2(5).title          = 'slib percentage in upper seabed of Dutch continental shelf part of North Sea';
   OPT2(5).units          = '%'; 
   OPT2(5).varname        = 'mud';
   OPT2(5).long_name      = 'mud fraction in upper seabed (mixture between mass (sieve) and volume (laser particle sizer))';
   OPT2(5).dataset        = 'slib_juli2007';
      
%% 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
