function cc= crop_images(uid,xstart,xend,ystart,yend,JSONString)

pkg load image
JSONSentinel = JSONString;

FieldArea = JSONSentinel.FieldArea
FieldLatLength = JSONSentinel.FieldMaxLat - JSONSentinel.FieldMinLat;
FieldLongLength = JSONSentinel.FieldMaxLong-JSONSentinel.FieldMinLong;
MLatLength = 69172*FieldLatLength;
ODL = cos((JSONSentinel.FieldMaxLat)*0.0174533)*69172;
MLongLength = FieldLongLength*ODL;
PixelLatLength = abs(round(MLatLength/10))
PixelLongLength = abs(round(MLongLength/10))
if PixelLatLength > PixelLongLength
	smallside = PixelLatLength
else
	smallside = PixelLongLength
end
smallside = smallside*2
format long;

sizeB4 = [10980 10980];


filenametci = strcat(uid,"_TCI.png");
tci = imread(filenametci);
filenameetci = strcat(uid,"_ETCI.png");
etci = imread(filenameetci);

filenamendvi = strcat(uid,"_ndvi.png");
ndvi = imread(filenamendvi);
filenamendwi = strcat(uid,"_ndwi.png");
ndwi = imread(filenamendwi);
filenamendre = strcat(uid,"_ndre.png");
ndre = imread(filenamendre);

filenamevari = strcat(uid,"_vari.png");
vari = imread(filenamevari);

filenamendre_cmap2 = strcat(uid,"_ndre_cmap2.png");
ndre_cmap2 = imread(filenamendre_cmap2);

filenamendvi_cmap2 = strcat(uid,"_ndvi_cmap2.png");
ndvi_cmap2 = imread(filenamendvi_cmap2);

filenamendwi_cmap2 = strcat(uid,"_ndwi_cmap2.png");
ndwi_cmap2 = imread(filenamendwi_cmap2);

filenamevari_cmap2 = strcat(uid,"_vari_cmap2.png");
vari_cmap2 = imread(filenamevari_cmap2);

sizeTCI = size(tci);
%xstart = round(sizeTCI(1)/4);
%xend = sizeTCI(1)-round(sizeTCI(1)/4);
%ystart = round(sizeTCI(2)/4);
%yend = sizeTCI(2)-round(sizeTCI(2)/4);
xsize = xend-xstart
ysize = yend-ystart
c_x = xsize/2
c_y = ysize/2
xstart = round(c_x-0.9*smallside)
xend = round(c_x+0.9*smallside)
ystart = round(c_y-0.9*smallside)
yend = round(c_y+0.9*smallside)
xstart
xend
ystart
yend
try
if xstart<=0
	xstart = 1;
end
if ystart<=0
	ystart=1;
end
aee = 0
tci = tci(xstart:xend,ystart:yend,:);
aee = 1

TCIgray = rgb2gray(tci);
sizegray = size(TCIgray);
graycount = 0;
for i = 1:sizegray(1)
	for j = 1:sizegray(2)
		if TCIgray(i,j) >180
			graycount=graycount+1;
		end
		
	end
end

graycount
pixelcount = sizegray(1)*sizegray(2)
if(graycount>0.2*pixelcount)
	cc = 2
	result_status2 = 'Too much cloud in cropped tci image'
	return
end
	
etci = etci(xstart:xend,ystart:yend,:);
ndvi = ndvi(xstart:xend,ystart:yend,:);

vari = vari(xstart:xend,ystart:yend,:);

ndvi_cmap2 = ndvi_cmap2(xstart:xend,ystart:yend,:);
vari_cmap2 = vari_cmap2(xstart:xend,ystart:yend,:);
imwrite(tci,filenametci);
imwrite(etci,filenameetci);
imwrite(ndvi,filenamendvi);

imwrite(vari,filenamevari);


imwrite(ndvi_cmap2,filenamendvi_cmap2);
imwrite(vari_cmap2,filenamevari_cmap2);

%xstart = round(xstart/2)-1;
%xend = round(xend/2)-1;
%ystart = round(ystart/2)-1;
%yend = round(yend/2)-1;
ndwi = ndwi(xstart:xend,ystart:yend,:);
ndwi_cmap2 = ndwi_cmap2(xstart:xend,ystart:yend,:);

ndre = ndre(xstart:xend,ystart:yend,:);
ndre_cmap2 = ndre_cmap2(xstart:xend,ystart:yend,:);

imwrite(ndwi,filenamendwi);
imwrite(ndwi_cmap2,filenamendwi_cmap2);

imwrite(ndre_cmap2,filenamendre_cmap2);
imwrite(ndre,filenamendre);


cc = 1;

catch
	ss = 'crop failed'
	cc = 0;
	cc = 2
	result_status2 = 'Too much cloud in cropped tci image'
	return
end
end


