function imwrite_big(img,filepath,append_tag)
%write BigTiff files
if ~exist('append_tag','var')
    append_tag=0;
end
if append_tag==0
    tiff_obj=Tiff(filepath,'w8');
    tagstruct.ImageLength=size(img,1);
    tagstruct.ImageWidth=size(img,2);
    tagstruct.Photometric=Tiff.Photometric.MinIsBlack;
    tagstruct.BitsPerSample=16;
    tagstruct.SamplesPerPixel=1;
    tagstruct.PlanarConfiguration=Tiff.PlanarConfiguration.Chunky;
    setTag(tiff_obj,tagstruct);
    write(tiff_obj,img);
    close(tiff_obj)
else
    tiff_obj=Tiff(filepath,'a');
    tagstruct.ImageLength=size(img,1);
    tagstruct.ImageWidth=size(img,2);
    tagstruct.Photometric=Tiff.Photometric.MinIsBlack;
    tagstruct.BitsPerSample=16;
    tagstruct.SamplesPerPixel=1;
    tagstruct.PlanarConfiguration=Tiff.PlanarConfiguration.Chunky;
    setTag(tiff_obj,tagstruct);
    write(tiff_obj,img);
    close(tiff_obj);
end


%%
close(tiff_obj)
end