function [] = WriteMGEN(filenm,title,depth_offset,RunConditions,BaseEl,SE)

%Number of points for each element type
NumPts(1) = 4;  
NumPts(2) = 5; 
NumPts(3) = 8;  
NumPts(4) = 9; 

fid = fopen([filenm '.mgen'],'w');
fprintf(fid,'Title\n%s\n\n',title);

fprintf(fid,'Number of superelements\n%d\n\n',length(SE));

fprintf(fid,'Element types\n');
for i = 1:length(SE)
    fprintf(fid,'%d   %d   %f   %f   %f\n',...
        BaseEl{SE{i}.BaseElNum}.Type, ...
        BaseEl{SE{i}.BaseElNum}.CoordSys, ...
        SE{i}.Offset(1),SE{i}.Offset(2),SE{i}.Offset(3)+depth_offset);
end
fprintf(fid,'\n');
    
fprintf(fid,'Boundary points\n');
for i = 1:length(SE)
    if(SE{i}.Normal == 1)
      pt_order = 1:NumPts(BaseEl{SE{i}.BaseElNum}.Type);
    else
      pt_order = NumPts(BaseEl{SE{i}.BaseElNum}.Type):-1:1;
    end
   for j = pt_order
     fprintf(fid,'%f %f %f\n',BaseEl{SE{i}.BaseElNum}.x(j),BaseEl{SE{i}.BaseElNum}.y(j),BaseEl{SE{i}.BaseElNum}.z(j));
   end
       fprintf(fid,'\n');
end
   
fprintf(fid,'Divisions\n');
for i = 1:length(SE)
   fprintf(fid,'%d %d\n',SE{i}.DivX,SE{i}.DivY);
end
fprintf(fid,'\n');

fprintf(fid,'Number of runs\n%d\n\n',length(RunConditions.WaveNumbers));
fprintf(fid,'Wave numbers for each run\n');
for k = 1:length(RunConditions.WaveNumbers)
    fprintf(fid,'%f\n',RunConditions.WaveNumbers(k));
end
fprintf(fid,'\n');

fprintf(fid,'Run Conditions\n');
fprintf(fid,'%f  ',RunConditions.depth);
fprintf(fid,'%f  ',RunConditions.WaveAmp);
fprintf(fid,'%f  ',RunConditions.Angle);
fprintf(fid,'%d  ',RunConditions.ISym);
fprintf(fid,'%d  ',RunConditions.IType);
fprintf(fid,'%d   0\n',RunConditions.IPrint);

fclose(fid);

return
