function [outcount,expectedcount] = WriteDMCFile(fwriteid,m,data);
%function [outcount,expectedcount = WriteDMCFile(fwriteid,m,data);

%definisions note must be same as in ReadDMCFile
BLOCKTYPEPARAM = 1001;
BLOCKTYPEFIBRE = 1002;
BLOCKTYPEFIBREISO = 1003;
BLOCKTYPEFIBREISO2 = 1004;
BLOCKTYPESPHSHELL = 1005;
BLOCKTYPEREFBOUNDARY = 1006;
BLOCKTYPEOUTBOUNDARY = 1007;
BLOCKTYPETRAJ = 1008;
BLOCKTYPEGRID = 1009;
BLOCKTYPECYLSHELLGRID = 1010;

%initialisations
outcount = 0;

%header
outcount = outcount+fwrite(fwriteid,data.NumPhotonsSoFar,'double');
outcount = outcount+fwrite(fwriteid,data.flops,'double');
expectedcount=2;
%parameters
outcount = outcount+fwrite(fwriteid,BLOCKTYPEPARAM,'double');
outcount = outcount+fwrite(fwriteid,size(fieldnames(m),1),'double'); %size of model stucture
outcount = outcount+writemodelparams(fwriteid,m);
expectedcount=expectedcount+2+size(fieldnames(m),1);
%other blocks
outcount = outcount+fwrite(fwriteid,BLOCKTYPEFIBRE,'double');
outcount = outcount+fwrite(fwriteid,size(data.CFibre,1),'double'); %size of fibre stucture
outcount = outcount+fwrite(fwriteid,data.CFibre,'double');
expectedcount=expectedcount+2+size(data.CFibre,1);
outcount = outcount+fwrite(fwriteid,BLOCKTYPEFIBREISO,'double');
outcount = outcount+fwrite(fwriteid,size(data.CFibreiso,1),'double'); %size of fibre stucture
outcount = outcount+fwrite(fwriteid,data.CFibreiso,'double');
expectedcount=expectedcount+2+size(data.CFibreiso,1);
outcount = outcount+fwrite(fwriteid,BLOCKTYPEFIBREISO2,'double');
outcount = outcount+fwrite(fwriteid,size(data.CFibreiso2,1),'double'); %size of fibre stucture
outcount = outcount+fwrite(fwriteid,data.CFibreiso2,'double');
expectedcount=expectedcount+2+size(data.CFibreiso2,1);
outcount = outcount+fwrite(fwriteid,BLOCKTYPESPHSHELL,'double');
outcount = outcount+fwrite(fwriteid,size(data.CSph,1),'double'); %size of fibre stucture
outcount = outcount+fwrite(fwriteid,data.CSph,'double');
expectedcount=expectedcount+2+size(data.CSph,1);
if isfield(data,'CGrid')
   outcount = outcount+fwrite(fwriteid,BLOCKTYPEGRID,'double');
   outcount = outcount+fwrite(fwriteid,size(data.CGrid,1)*size(data.CGrid,2),'double'); 
   outcount = outcount+fwrite(fwriteid,data.CGrid,'double');
   expectedcount=expectedcount+2+size(data.CGrid,1)*size(data.CGrid,2);
end
if isfield(data,'CCylGrid')
   outcount = outcount+fwrite(fwriteid,BLOCKTYPECYLSHELLGRID,'double');
   outcount = outcount+fwrite(fwriteid,size(data.CCylGrid,1)*size(data.CCylGrid,2),'double'); 
   outcount = outcount+fwrite(fwriteid,data.CCylGrid,'double');
   expectedcount=expectedcount+2+size(data.CCylGrid,1)*size(data.CCylGrid,2);
end
%boundary incurances
outcount = outcount+fwrite(fwriteid,BLOCKTYPEREFBOUNDARY,'double');
outcount = outcount+fwrite(fwriteid,size(data.SurfaceR,1)*size(data.SurfaceR,2),'double'); %size of fibre stucture
outcount = outcount+fwrite(fwriteid,data.SurfaceR,'double');
expectedcount=expectedcount+2+size(data.SurfaceR,1)*size(data.SurfaceR,2);
outcount = outcount+fwrite(fwriteid,BLOCKTYPEOUTBOUNDARY,'double');
outcount = outcount+fwrite(fwriteid,size(data.SurfaceO,1)*size(data.SurfaceO,2),'double'); %size of fibre stucture
outcount = outcount+fwrite(fwriteid,data.SurfaceO,'double');
expectedcount=expectedcount+2+size(data.SurfaceO,1)*size(data.SurfaceO,2);
%trajectories
for i = 1:size(data.Field,2)
   outcount = outcount+fwrite(fwriteid,BLOCKTYPETRAJ,'double');
   outcount = outcount+fwrite(fwriteid,size(data.Field{i},1)*size(data.Field{i},2),'double'); %size of fibre stucture
   outcount = outcount+fwrite(fwriteid,data.Field{i},'double');
   expectedcount=expectedcount+2+size(data.Field{i},1)*size(data.Field{i},2);
end


      