function [Q, Qval]=chunk(section, mask);

chunksize=size(section);
chunksize=chunksize(1);

maxval=[2000 500 300 200 200 200 200 200
500 300 200 200 200 200 200 200
300 200 200 200 200 200 200 200
200 200 200 200 200 200 200 200
200 200 200 200 200 200 200 200
200 200 200 200 200 200 200 200
200 200 200 200 200 200 200 200
200 200 200 200 200 200 200 200];

zmask=find(mask==0);
mask(zmask)=NaN;
levels=2.^mask;
boundstepsize=2*maxval./levels;
midpoint=round(boundstepsize/2);

if isempty(find( or( section>maxval,section<-maxval ) ==1))  == 0
   top=find(section>maxval);
   bottom=find(section<-maxval);
   section(top)=maxval(top);
   section(bottom)=maxval(bottom);
end

lower=floor(section./boundstepsize);

shift=lower+0.5*levels;

lowernum=lower.*boundstepsize;

qval = lowernum + midpoint;

Q=shift;
Qval=qval;

