clear variables;
jcm_root = getenv('JCMROOT'); % change this to match your
                              % installation or set environment variable
addpath(fullfile(jcm_root, 'ThirdPartySupport', 'Matlab'));
jcmwave_startup();


% create test surface to discretize, here: builtin function sombrero
N=20;
N = N+mod(N+1,2); % make N odd

% [X,Y,Z] = peaks(N);  
[X,Y,Z] = sombrero(N); 
surf(X,Y,Z)

% make surface periodic by averaging the values 
temp = Z(end,:)+Z(1,:);
Z(1,:)=temp/2.0;
Z(end,:)=temp/2.0;
temp = Z(:,end)+Z(:,1);
Z(:,1)=temp/2.0;
Z(:,end)=temp/2.0;

% create mesh of xy coordinates
tri = delaunay(X(:),Y(:));
trisurf(tri,X(:),Y(:),Z(:))

% rescale mesh to fit into the layout
x = X(:)/(max(X(:)) - min(X(:)))*10;
y = Y(:)/(max(Y(:)) - min(Y(:)))*10;
z = Z(:)/(max(Z(:)) - min(Z(:)))*2;;

keys.points = [x y z];
keys.triangles = tri;

% execute pattern substitution, generate and view grid
jcmwave_geo('.',keys,'jcmt_pattern','triangulation')
jcmwave_view('grid.jcm')
 