function nc_multibeam_from_asc_test(varargin)
%NC_MULTIBEAM_FROM_ASC_TEST   unit test for nc_multibeam_from_asc
%
%see also: arcgis, nc_multibeam, ArcGisRead_test

MTestCategory.UserInput;

   OPT1.checktime = 1;
   OPT1.timefmt   = 'yyyymmdd';
   randomstr      = filename(tempname); % leave out earlier asc files

   basename  = fullfile(fileparts(mfilename('fullpath')),['a',datestr(now+OPT1.checktime,OPT1.timefmt),randomstr]); % later time first in alphabet: check for increasing time in netcdf file
   basename1 = fullfile(fileparts(mfilename('fullpath')),['b',datestr(now               ,OPT1.timefmt),randomstr]);

%% create an ascii ascgrid file (same as tst for arc_asc_read)



   %      Input: nx=5, ny=4, note partial overlap of data in middle
   %           +-------------+ LL
   %                 +-------------+ UR
   %      +  .  .  .  .  .  .  .  .  .  .  .  .
   %      |  .  .  .  .  .  .  .  .  .  .  .  .
   %      |  .  .  . 36 37 38 39 40  .  .  .  .
   %      |  .  .  . 31 32 33 34 35  .  .  .  .
   %     _|  . 16 17|26 20 28|29 30  .  .  .  . NOTE partial overlap    18 19 20 -> 26 20 28
   %      |  . 11 12|21 22 23|24 25  .  .  .  . NOTE partial overlap    13 14 15 -> 21 22 23
   %      |  .  6  0  8  9 10  .  .  .  .  .  .
   %      |  .  1  2  3  4  5  .  .  .  .  .  .
   %      |  .  .  .  .  .  .  .  .  .  .  .  .
   %      +------------------+------------------+
   %
   %      Output: nx=6, ny=5

   fname = [basename,'LL.asc']; % later time first in alphabet: check for increasing time in netcdf file
   fid = fopen(fname,'w');
   fprintf(fid,'%s\n','ncols        5'); % nx
   fprintf(fid,'%s\n','nrows        4'); % ny
   fprintf(fid,'%s\n','xllcorner    0'); % lower left is (0,0), so ...
   fprintf(fid,'%s\n','yllcorner    0'); % first data point is at located at ...
   fprintf(fid,'%s\n','cellsize     1'); % (0.5 ,0.5)
   fprintf(fid,'%s\n','nodata_value 0');
   fprintf(fid,'%s\n','16 17 18 19 20');
   fprintf(fid,'%s\n','11 12 13 14 15');
   fprintf(fid,'%s\n',' 6  0  8  9 10'); % note one nodatavalue
   fprintf(fid,'%s\n',' 1  2  3  4  5');
   fclose (fid);

   fname = [basename1,'UR.asc'];
   fid = fopen(fname,'w');
   fprintf(fid,'%s\n','ncols        5'); % nx
   fprintf(fid,'%s\n','nrows        4'); % ny
   fprintf(fid,'%s\n','xllcorner    2'); % lower left is (0,0), so ...
   fprintf(fid,'%s\n','yllcorner    2'); % first data point is at located at ...
   fprintf(fid,'%s\n','cellsize     1'); % (0.5 ,0.5)
   fprintf(fid,'%s\n','nodata_value 0');
   fprintf(fid,'%s\n','36 37 38 39 40');
   fprintf(fid,'%s\n','31 32 33 34 35');
   fprintf(fid,'%s\n','26 20 28 29 30'); % note one nodatavalue
   fprintf(fid,'%s\n','21 22 23 24 25');
   fclose (fid);

%% create

   OPT.make                = true;

   OPT.basepath_local      = fileparts(basename);
   OPT.basepath_network    = fileparts(basename);
   OPT.basepath_opendap    = fileparts(basename);
   OPT.raw_path            = fileparts(basename);
   OPT.raw_extension       = ['*',randomstr,'*.asc'];
   OPT.netcdf_path         = '';
   OPT.cache_path          = fullfile(tempdir,'nc_asc');
   OPT.zip                 = false;         % are the files zipped?
   OPT.zip_extension       = '*.zip';       % zip file extension

   OPT.datatype            = 'multibeam';
   OPT.EPSGcode            = 28992;
   OPT.dateFcn             = @(s) datenum(monthstr_mmm_dutch2eng(s(1:8)),'yyyy mmm'); % how to extract the date from the filename

   OPT.mapsizex            = 5+1;             % size of fixed map in x-direction
   OPT.mapsizey            = 4+1;             % size of fixed map in y-direction
   OPT.gridsizex           = 1;               % x grid resolution
   OPT.gridsizey           = 1;               % y grid resolution
   OPT.xoffset             = -OPT.gridsizex/2; % zero point of x grid (x of data points, not of pixels corners)
   OPT.yoffset             = -OPT.gridsizey/2; % zero point of y grid (x of data points, not of pixels corners)
   OPT.dateFcn             = @(s)datenum(s(2:length(OPT1.timefmt)+1),OPT1.timefmt);

   ncfiles = nc_multibeam_from_asc(OPT);

   nc_dump(ncfiles{3})

%% load

   for i=1:length(ncfiles)
   D(i) = nc2struct(ncfiles{i});
   end

   D().z

%% check

   assert(isequal(D(1).x',[0:+1:5]-0.5));
   assert(isequal(D(2).x',[0:+1:5]-0.5));
   assert(isequal(D(3).x',[0:+1:5]+5.5));
   assert(isequal(D(4).x',[0:+1:5]+5.5));

   assert(isequal(D(1).y',[4:-1:0]-0.5));
   assert(isequal(D(2).y',[4:-1:0]+4.5));
   assert(isequal(D(3).y',[4:-1:0]-0.5));
   assert(isequal(D(4).y',[4:-1:0]+4.5));

   if OPT1.checktime ==1


   a1(1,:,:)= [   NaN   NaN   NaN    26    20    28 % NOTE partial overlap    18 19 20 -> 26 20 28, last file in alphabetical order WON
                  NaN   NaN   NaN    21    22    23 % NOTE partial overlap    13 14 15 -> 21 22 23, last file in alphabetical order WON
                  NaN   NaN   NaN   NaN   NaN   NaN
                  NaN   NaN   NaN   NaN   NaN   NaN
                  NaN   NaN   NaN   NaN   NaN   NaN];

   a1(2,:,:)= [   NaN    16    17    18    19    20 % NOTE partial overlap    18 19 20 -> 26 20 28, last file in alphabetical order WON
                  NaN    11    12    13    14    15 % NOTE partial overlap    13 14 15 -> 21 22 23, last file in alphabetical order WON
                  NaN     6   NaN     8     9    10
                  NaN     1     2     3     4     5
                  NaN   NaN   NaN   NaN   NaN   NaN];

   assert(all(diff(D(1).time) > 0));

   else

   a1= [   NaN    16    17    26    20    28 % NOTE partial overlap    18 19 20 -> 26 20 28, last file in alphabetical order WON
           NaN    11    12    21    22    23 % NOTE partial overlap    13 14 15 -> 21 22 23, last file in alphabetical order WON
           NaN     6   NaN     8     9    10
           NaN     1     2     3     4     5
           NaN   NaN   NaN   NaN   NaN   NaN];

   end

   a2= [   NaN   NaN   NaN   NaN   NaN   NaN
           NaN   NaN   NaN   NaN   NaN   NaN
           NaN   NaN   NaN   NaN   NaN   NaN
           NaN   NaN   NaN    36    37    38
           NaN   NaN   NaN    31    32    33];

   a3= [    29    30   NaN   NaN   NaN   NaN
            24    25   NaN   NaN   NaN   NaN
           NaN   NaN   NaN   NaN   NaN   NaN
           NaN   NaN   NaN   NaN   NaN   NaN
           NaN   NaN   NaN   NaN   NaN   NaN];

   a4= [   NaN   NaN   NaN   NaN   NaN   NaN
           NaN   NaN   NaN   NaN   NaN   NaN
           NaN   NaN   NaN   NaN   NaN   NaN
            39    40   NaN   NaN   NaN   NaN
            34    35   NaN   NaN   NaN   NaN];

   assert(isequalwithequalnans(D(1).z,a1));
   assert(isequalwithequalnans(D(2).z,a2));
   assert(isequalwithequalnans(D(3).z,a3));
   assert(isequalwithequalnans(D(4).z,a4));
