function [result_status,ndvi_array,ndre_array,ndwi_array] = monitored_fields_data(JSONString)

pkg load image
JSONSentinel = JSONString
B4URL = JSONSentinel.Band4URI
ndvi_array = 0;
ndre_array = 0;
ndwi_array = 0;
%create URLs
B8URL = strrep(B4URL,"B04.jp2","B08.jp2")
B11URL = strrep(B4URL,"B04.jp2","B11.jp2")
B8AURL = strrep(B4URL,"B04.jp2","B8A.jp2")
TCIURL = strrep(B4URL,"B04.jp2","TCI.jp2")
B5URL = strrep(B4URL,"B05.jp2","TCI.jp2")
B4FileName = 'TileB4.jp2';
B8FileName = 'TileB8.jp2';
TCIFileName = 'TileTCI.jp2';
B8AFileName = 'TileB8A.jp2';
B11FileName = 'TileB11.jp2';
B5FileName = 'TileB5.jp2';
sizeB4 = [10980 10980];
format long;
red = 255;
green = 0;
stepsize = 25;
mat_step =1;
c_map = zeros(20,3);
while green<255
	green = green+stepsize
	if green > 255
		green = 255;
	end
	r_color = red/255;
	g_color = green/255;
	c_map(mat_step,1) = r_color;
	c_map(mat_step,2) = g_color;
	c_map(mat_step,3) = 0;
	mat_step = mat_step+1;
end
while red >0
	red = red-stepsize;
	if red<0
		red = 0;
	end
	r_color = red/255;
	g_color = green/255;
	c_map(mat_step,1) = r_color;
	c_map(mat_step,2) = g_color;
	c_map(mat_step,3) = 0;
	mat_step = mat_step+1;
end
%c_map

%finding field bounding coordinates
TileMaxLat =JSONSentinel.TileMaxLat
TileMinLat =JSONSentinel.TileMinLat
TileMaxLong = JSONSentinel.TileMaxLong
TileMinLong =JSONSentinel.TileMinLong
FieldMaxLat = JSONSentinel.FieldMaxLat
FieldMinLat = JSONSentinel.FieldMinLat
FieldMaxLong =JSONSentinel.FieldMaxLong
FieldMinLong =JSONSentinel.FieldMinLong
UID = JSONSentinel.UID
LatsPerPixel = (TileMaxLat - TileMinLat)/sizeB4(1)
LongsPerPixel = (TileMaxLong - TileMinLong)/sizeB4(1)
    PixelsPerLat = abs(sizeB4(1)/(TileMaxLat - TileMinLat));
PixelsPerLong = abs(sizeB4(2)/(TileMaxLong - TileMinLong));
StartPixelLat = PixelsPerLat*(1-abs(FieldMaxLat-TileMinLat));
StartPixelLat = round(abs(StartPixelLat))
EndPixelLat = PixelsPerLat*(1-abs(TileMinLat- FieldMinLat));
EndPixelLat =  round(abs(EndPixelLat))
if StartPixelLat > EndPixelLat
        temp = EndPixelLat;
        EndPixelLat = StartPixelLat;
        StartPixelLat = temp;
end

if StartPixelLat > 300
        StartPixelLat = StartPixelLat - 300
else
        StartPixelLat = 1
end

if EndPixelLat <sizeB4(1)-300
        EndPixelLat = EndPixelLat + 300
else
        EndPixelLat = sizeB4(1)
end
StartPixelLong = PixelsPerLong*(abs(TileMinLong - FieldMinLong));
StartPixelLong = round(abs(StartPixelLong))
EndPixelLong = PixelsPerLong*abs(TileMinLong - FieldMaxLong);
EndPixelLong = round(abs(EndPixelLong))
if StartPixelLong > EndPixelLong
        temp = EndPixelLong;
        EndPixelLong = StartPixelLong;
        StartPixelLong = temp;
end
if StartPixelLong > 300
        StartPixelLong = StartPixelLong - 300
else
        StartPixelLong =1
end
if EndPixelLong < sizeB4(1) - 300
        EndPixelLong = EndPixelLong+300
else
        EndPixelLong = sizeB4(1)
end


LatLength = EndPixelLat - StartPixelLat;
LongLength = EndPixelLong - StartPixelLong;
LatNDVI = LatLength
LongNDVI = LongLength
if StartPixelLat > sizeB4(1) | EndPixelLat > sizeB4(1) | StartPixelLong > sizeB4(1) | EndPixelLong > sizeB4(1)
	result_status2 = 'failed due to unbound start end'
	result_status = strcat(UID,"failed0")
else

%download images
wait_t = randi(10)
pause(wait_t)
try
	urlwrite(B4URL,B4FileName);

wait_t = randi(10)
pause(wait_t)

urlwrite(B8URL,B8FileName);

wait_t = randi(10)
pause(wait_t)

urlwrite(TCIURL,TCIFileName);

wait_t = randi(10)
pause(wait_t)

urlwrite(B11URL,B11FileName);

wait_t = randi(10)
pause(wait_t)

urlwrite(B8AURL,B8AFileName);
wait_t = randi(10)
pause(wait_t)

urlwrite(B5URL,B5FileName);
catch
        result_status2 = 'failed due to HTTP Error'
        result_status = strcat(UID,"failed4")
        return
end
%read images
try
B4 = imread('TileB4.jp2');
B5 = imread('TileB5.jp2');

B8 = imread('TileB8.jp2');
TCI = imread('TileTCI.jp2');
B8A = imread('TileB8A.jp2');
B11 = imread('TileB11.jp2');
catch
        result_status2 = 'failed due to image decode Error'
        result_status = strcat(UID,"failed0")
        return
end
%clip larger images
	clip = [StartPixelLat EndPixelLat StartPixelLong EndPixelLong];
	clip_tci = clip;
	B4 = B4(clip(1):clip(2),clip(3):clip(4));
	B8 = B8(clip(1):clip(2),clip(3):clip(4));
	TCI = TCI(clip(1):clip(2),clip(3):clip(4),:);

%find field coordinates for smaller resolution images
sizeB4 = [5490 5490];
rr = 11
LatsPerPixel = (TileMaxLat - TileMinLat)/sizeB4(1)
LongsPerPixel = (TileMaxLong - TileMinLong)/sizeB4(1)
    PixelsPerLat = abs(sizeB4(1)/(TileMaxLat - TileMinLat));
PixelsPerLong = abs(sizeB4(2)/(TileMaxLong - TileMinLong));
StartPixelLat = PixelsPerLat*(1-abs(FieldMaxLat-TileMinLat));
StartPixelLat = round(abs(StartPixelLat))
EndPixelLat = PixelsPerLat*(1-abs(TileMinLat- FieldMinLat));
EndPixelLat =  round(abs(EndPixelLat))
if StartPixelLat > EndPixelLat
        temp = EndPixelLat;
        EndPixelLat = StartPixelLat;
        StartPixelLat = temp;
end
if StartPixelLat > 150
        StartPixelLat = StartPixelLat - 150
else
        StartPixelLat = 1
end
if EndPixelLat <sizeB4(1)-150
        EndPixelLat = EndPixelLat + 150
else
        EndPixelLat = sizeB4(1)
end
StartPixelLong = PixelsPerLong*(abs(TileMinLong - FieldMinLong));
StartPixelLong = round(abs(StartPixelLong))
EndPixelLong = PixelsPerLong*abs(TileMinLong - FieldMaxLong);
EndPixelLong = round(abs(EndPixelLong))
if StartPixelLong > EndPixelLong
        temp = EndPixelLong;
        EndPixelLong = StartPixelLong;
        StartPixelLong = temp;
end
if StartPixelLong > 150
        StartPixelLong = StartPixelLong - 150
else
        StartPixelLong = 1
end
if EndPixelLong < sizeB4(1) - 150
        EndPixelLong = EndPixelLong+150
else
        EndPixelLong = sizeB4(1)
end
LatLength = EndPixelLat - StartPixelLat
LongLength = EndPixelLong - StartPixelLong

%clip smaller images
clip = [StartPixelLat EndPixelLat StartPixelLong EndPixelLong];
	B8A = B8A(clip(1):clip(2),clip(3):clip(4),:);
	B11 = B11(clip(1):clip(2),clip(3):clip(4),:);
	B5 = B5(clip(1):clip(2),clip(3):clip(4),:);
	rr = 12
	B8NDRE = B8;
	B4F_name = strcat(UID,"_B4.png");
	%imwrite(B4,B4F_name);
	B8NDRE = imresize(B8NDRE,[abs(clip(2)-clip(1)+1) abs(clip(4)-clip(3)+1)]);
	B8NDRE = single(B8NDRE);
	B4 = single(B4);
	B8 = single(B8);
	B8A = single(B8A);
	B11 = single(B11);
	B5 = single(B5);
	%calculate NDVI, NDRE, NDWI
	NDVI = (B8 - B4)./(B8 + B4);
	NDRE = (B8NDRE-B5)./(B8NDRE+B5);
	NDWI = (B11 - B8A)./(B8A + B11);
	ndvi_array = NDVI;
	ndre_array = NDRE;
	ndwi_array = NDWI;
	%check if there is too much cloud
	sN = size(NDVI);
	pixelcount = sN(1)*sN(2);
	negcount = 0;
	cloud_count = 0;
	for i = 1:sN(1)
		for j = 1:sN(2)
			if NDVI(i,j) < 0
				negcount=negcount+1;
			end
			%if B4(i,j)>0.65
			%	cloud_count=cloud_count+1;
			%end
		end
	end
	negcount
	pixelcount
	
	if(negcount>pixelcount*0.2 | cloud_count>pixelcount*0.5)
		result_status = strcat(UID,"failed1")
		result_status2 = 'Too much cloud in image'
		return
	end
	%check if the image has large black portion
	SizeNewB4 = size(B4);
	IsNaNMat = isnan(NDVI);
	IsNaNCount =sum(sum(IsNaNMat))
	if(IsNaNCount > SizeNewB4(1)*SizeNewB4(2)*0.5)
		result_status = strcat(UID,"failed1")
		result_status2 = 'Too much black in image'
		return
	else	

	%create ETCI image
		my_limit=0.5;
low_limit=0.008;
up_limit=0.992;
%----------------------------------------------------------------------
img=TCI;
[m1 n1 r1]=size(img);
%----------------------------------------------------------------------
if r1==3
    my_limit2=0.04;my_limit3=-0.04;
    a=rgb2ntsc(img);
    mean_adjustment=my_limit2-mean(mean(a(:,:,2)));
    a(:,:,2)=a(:,:,2)+mean_adjustment*(0.596-a(:,:,2));
    mean_adjustment=my_limit3-mean(mean(a(:,:,3)));
    a(:,:,3)=a(:,:,3)+mean_adjustment*(0.523-a(:,:,3));
else
    a=double(img)./255;
end
%----------------------------------------------------------------------
mean_adjustment=my_limit-mean(mean(a(:,:,1)));
a(:,:,1)=a(:,:,1)+mean_adjustment*(1-a(:,:,1));
if r1==3
    a=ntsc2rgb(a);
end
%----------------------------------------------------------------------
img=a.*255;
%--------------------calculation of vmin and vmax----------------------
for k=1:r1
    arr=sort(reshape(img(:,:,k),m1*n1,1));
    v_min(k)=arr(ceil(low_limit*m1*n1));
    v_max(k)=arr(ceil(up_limit*m1*n1));
end
%----------------------------------------------------------------------
if r1==3

trans = [ 0.299,  0.596,  0.211; 0.587, -0.274, -0.523; 0.114, -0.322,  0.312 ];

  v_min = v_min*trans;
	v_max = v_max*trans;
end
%----------------------------------------------------------------------
if abs(v_max(1)-v_min(1))<1e-3
	result_status = strcat(UID,"failed1")
                result_status2 = 'Too much black in image vmin vmax'
	return
end
%v_min
img=(img-v_min(1))/(v_max(1)-v_min(1));

startx=25;
endx = 900-25;
starty=181;
endy=1200-188;
TCIFilename = strcat(UID,"_ETCI.png");
%save ETCI image
imwrite(uint8(img.*255),TCIFilename);
TCIFilenameL = strcat(UID,"_ETCI_large.png");
%save ETCI image
imwrite(uint8(img.*255),TCIFilenameL);
%save TCI image
TCIFilename2 = strcat(UID,"_TCI.png");
imwrite(TCI,TCIFilename2);
TCIFilename2L = strcat(UID,"_TCI_large.png");
imwrite(TCI,TCIFilename2L);
%create color map NDVI image
NDVI_new = NDVI;
%NDVI_new = imcomplement(NDVI);
colormap(c_map)
imagesc(NDVI_new, [0 1]);
axis square tight		
axis off
		ndviname = strcat(UID,"_ndvi")
		saveas(1,ndviname,'png')
%read NDVI image, crop, resize and mask on MNDVI
ndviname=strcat(ndviname,".png");
aa = imread(ndviname);
size_aa = size(aa)
sizeNDVI = size(NDVI)
%min(min(aa))
%max(max(aa))
aa_new = aa(startx:endx, starty:endy,:);
try
aa_new = imresize(aa_new,[sizeNDVI(1) sizeNDVI(2)]);
catch
result_status = strcat(UID,"failed1")
end
imwrite(aa_new,ndviname);
ndvinameL = strcat(UID,"_ndvi_large.png");
imwrite(aa_new,ndvinameL);
aa_new = imread(ndviname);
%aa_new = single(aa_new);
%ndvi_img = single(img);
Rnew = zeros(sizeNDVI(1),sizeNDVI(2),3);
for i = 1:sizeNDVI(1)
    for j = 1:sizeNDVI(2)
        if NDVI(i,j) < 0.4 && NDVI(i,j)>0.05
            Rnew(i,j,:) = aa_new(i,j,:);
        else
            Rnew(i,j,:) = img(i,j,:);
        end
    end
end
image_name = strcat(UID,"_mndvi.png")
imwrite(Rnew,image_name);
image_nameL = strcat(UID,"_mndvi_large.png")
imwrite(Rnew,image_nameL);

%create colormap NDRE image
NDRE_new = (1/(max(max(NDRE))))*NDRE;
%NDRE_new = imcomplement(NDRE);
colormap(c_map)
imagesc(NDRE_new, [0 1]);
axis square tight		
axis off
		NDRE_name = strcat(UID,"_ndre")
		saveas(1,NDRE_name,'png')
%read NDRE image, crop, resize and mask on MNDRE
i_name = strcat(UID,"_ndre.png");
aa = imread(i_name);
aa_new = aa(startx:endx, starty:endy,:);
sizeNDRE = size(NDRE)
try
aa_new = imresize(aa_new,[sizeNDRE(1) sizeNDRE(2)]);
catch
result_status = strcat(UID,"failed1")
end
imwrite(aa_new,i_name);
i_nameL = strcat(UID,"_ndre_large.png");
imwrite(aa_new,i_nameL);
aa_new = single(aa_new);
size_aa = size(aa_new)
AlphaMatndre = zeros(sizeNDRE(1),sizeNDRE(2));
Rnew = zeros(sizeNDRE(1),sizeNDRE(2),3);
sizeaanew=size(aa_new)
for i = 1:sizeNDRE(1)
    for j = 1:sizeNDRE(2)
        if NDRE(i,j) < 0.4 && NDRE(i,j)>0.05
            Rnew(i,j,:) = aa_new(i,j,:);
        else
	AlphaMatndre(i,j)=1;
        end
    end
end
sizeRnewndre = size(Rnew)
i_namendre = strcat(UID,"_mndre.png");
imwrite(Rnew,i_namendre,'Alpha',AlphaMatndre);
i_namendreL = strcat(UID,"_mndre_large.png");
imwrite(Rnew,i_namendreL,'Alpha',AlphaMatndre);
%create colormap NDWI image

NDWI_new = (1/(max(max(NDWI))))*NDWI;
NDWI_new = imcomplement(NDWI);
colormap(c_map)
imagesc(NDWI_new);
axis square tight		
axis off
		imagename =strcat(UID,"_ndwi");
		saveas(1,imagename,'png')

%read NDRE image, crop, resize and mask on MNDRE	
i_name =strcat(UID,'_ndwi.png');
aa = imread(i_name);
aa_new = aa(startx:endx, starty:endy,:);
try
aa_new = imresize(aa_new,[sizeNDRE(1) sizeNDRE(2)]);
catch
result_status = strcat(UID,"failed1")
end
imwrite(aa_new,i_name);
i_nameL = strcat(UID,'_ndwi_large.png');
imwrite(aa_new,i_nameL);
aaaa = 'came here'
aa_new = single(aa_new);
sizeN = size(NDWI)
Rnew = zeros(sizeNDRE(1),sizeNDRE(2),3);
AlphaMat = zeros(sizeNDRE(1),sizeNDRE(2));
for i = 1:sizeNDRE(1)
    for j = 1:sizeNDRE(2)
        if NDWI(i,j) < 0.4 && NDWI(i,j)>0.05
            Rnew(i,j,:) = aa_new(i,j,:);
        else
          
	AlphaMat(i,j)=1;
        end
    end
end
i_mndwi = strcat(UID,"_mndwi.png");
imwrite(Rnew,i_mndwi,'Alpha',AlphaMat);
i_mndwiL = strcat(UID,"_mndwi_large.png");
imwrite(Rnew,i_mndwiL,'Alpha',AlphaMat);
result_status = strcat(UID,"successful")
end
end
