function result_status = monitored_fields_data(JSONString)

pkg load image
JSONSentinel = JSONString
B4URL = JSONSentinel.Band4URI
TileCloudCover = str2double(JSONSentinel.cloud_cover)
FieldArea = JSONSentinel.FieldArea
if FieldArea < 50
	pixelgap = 300
elseif FieldArea > 50 & FieldArea < 300
	pixelgap = 600
elseif FieldArea > 300 & FieldArea < 1200
	pixelgap = 800
else
	pixelgap = 900
end
UID = JSONSentinel.UID
if(TileCloudCover>99.9)
		result_status = strcat(UID,"failed2")
		result_status2 = 'Too much cloud in tile image'
		return
end

%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,"B04.jp2","B05.jp2")
B2URL = strrep(B4URL,"B04.jp2","B02.jp2")
B3URL = strrep(B4URL,"B04.jp2","B03.jp2")
PreviousDay = str2num(JSONSentinel.PreviousDay)
ImageDate = str2num(JSONSentinel.LatestDay)
MGRS = JSONSentinel.MGRS
B4FileName = strcat('TileB4_',MGRS);
B4FileName = strcat(B4FileName,'.jp2');

B8FileName = strcat('TileB8_',MGRS);
B8FileName = strcat(B8FileName,'.jp2');

TCIFileName = strcat('TileTCI_',MGRS);
TCIFileName = strcat(TCIFileName,'.jp2');

B8AFileName = strcat('TileB8A_',MGRS);
B8AFileName = strcat(B8AFileName,'.jp2');

B11FileName = strcat('TileB11_',MGRS);
B11FileName = strcat(B11FileName,'.jp2');

B5FileName = strcat('TileB5_',MGRS);
B5FileName = strcat(B5FileName,'.jp2');

B2FileName = strcat('TileB2_',MGRS);
B2FileName = strcat(B2FileName,'.jp2');

B3FileName = strcat('TileB3_',MGRS);
B3FileName = strcat(B3FileName,'.jp2');

sizeB4 = [10980 10980];
format long;
red = 130;
green = 0;
stepsize = 25;
mat_step =1;
c_map = zeros(31,3);
while red < 255
	red = red+stepsize;
	if red>255
		red = 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 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
while green>155
	green = green-stepsize;
	if green < 155
		green = 155;
	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
mat_step
index_range = -1:0.025:1;
size_i = size(index_range)
c_map2 = zeros(size_i(2),3);
for i = 1:size_i(2)
	ind_i = index_range(i);
	if ind_i<=-0.5
		c_map2(i,1) = 13;
		c_map2(i,2) = 13;
		c_map2(i,3) = 13;
	elseif ind_i>-0.5 & ind_i <= -0.2
		c_map2(i,1) = 191;
                c_map2(i,2) = 191;
                c_map2(i,3) = 191;
	elseif ind_i>-0.2 & ind_i <= -0.1
                c_map2(i,1) = 219;
                c_map2(i,2) = 219;
                c_map2(i,3) = 219;
	elseif ind_i>-0.1 & ind_i <= 0
                c_map2(i,1) = 235;
                c_map2(i,2) = 235;
                c_map2(i,3) = 235;
	elseif ind_i>0 & ind_i <= 0.025
                c_map2(i,1) = 254;
                c_map2(i,2) = 250;
                c_map2(i,3) = 202;
	elseif ind_i>0.025 & ind_i <= 0.05
                c_map2(i,1) = 238;
                c_map2(i,2) = 232;
                c_map2(i,3) = 182;
	elseif ind_i>0.05 & ind_i <= 0.075
                c_map2(i,1) = 222;
                c_map2(i,2) = 218;
                c_map2(i,3) = 157;
	elseif ind_i>0.075 & ind_i <= 0.1
                c_map2(i,1) = 205;
                c_map2(i,2) = 200;
                c_map2(i,3) = 132;
	elseif ind_i>0.1 & ind_i <= 0.125
                c_map2(i,1) = 191;
                c_map2(i,2) = 183;
                c_map2(i,3) = 108;
	elseif ind_i>0.125 & ind_i <= 0.15
                c_map2(i,1) = 175;
                c_map2(i,2) = 195;
                c_map2(i,3) = 98;
	elseif ind_i>0.15 & ind_i <= 0.175
                c_map2(i,1) = 164;
                c_map2(i,2) = 205;
                c_map2(i,3) = 87;
	elseif ind_i>0.175 & ind_i <= 0.2
                c_map2(i,1) = 127;
                c_map2(i,2) = 179;
                c_map2(i,3) = 71;
	elseif ind_i>0.2 & ind_i <= 0.25
                c_map2(i,1) = 111;
                c_map2(i,2) = 161;
                c_map2(i,3) = 64;
	elseif ind_i>0.25 & ind_i <= 0.3
                c_map2(i,1) = 96;
                c_map2(i,2) = 150;
                c_map2(i,3) = 54;
	elseif ind_i>0.3 & ind_i <= 0.35
                c_map2(i,1) = 79;
                c_map2(i,2) = 138;
                c_map2(i,3) = 46;
	elseif ind_i>0.35 & ind_i <= 0.4
                c_map2(i,1) = 65;
                c_map2(i,2) = 125;
                c_map2(i,3) = 37;
	elseif ind_i>0.4 & ind_i <= 0.45
                c_map2(i,1) = 48;
                c_map2(i,2) = 110;
                c_map2(i,3) = 27;
	elseif ind_i>0.45 & ind_i <= 0.5
                c_map2(i,1) = 34;
                c_map2(i,2) = 96;
                c_map2(i,3) = 19;
	elseif ind_i>0.5 & ind_i <= 0.55
                c_map2(i,1) = 15;
                c_map2(i,2) = 83;
                c_map2(i,3) = 105;
	elseif ind_i>0.55
                c_map2(i,1) = 2;
                c_map2(i,2) = 69;
                c_map2(i,3) = 0;
	else
		c_map2(i,1) = 254;
                c_map2(i,2) = 250;
                c_map2(i,3) = 202;
	end
end
c_map2 = c_map2/255;
%c_map2

%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

if FieldMaxLat < FieldMinLat
	temp = FieldMaxLat;
	FieldMaxLat = FieldMinLat;
	FieldMinLat = temp;
end

if FieldMaxLong < FieldMinLong
	temp = FieldMaxLong;
	FieldMaxLong = FieldMinLong;
	FieldMinLong = temp;
end


LatsPerPixel = (TileMaxLat - TileMinLat)/sizeB4(1)
LongsPerPixel = (TileMaxLong - TileMinLong)/sizeB4(1)
    PixelsPerLat = abs(sizeB4(1)/(TileMaxLat - TileMinLat));
PixelsPerLong = abs(sizeB4(2)/(TileMaxLong - TileMinLong));
%PixelsPerLong = PixelsPerLat;

StartPixelLat = PixelsPerLat*(1-abs(FieldMaxLat-TileMinLat));
StartPixelLat = round(abs(StartPixelLat))
EndPixelLat = PixelsPerLat*(1-abs(TileMinLat- FieldMinLat));
EndPixelLat =  round(abs(EndPixelLat))
if StartPixelLat >7000
	StartPixelLat = round(PixelsPerLat*(abs(FieldMaxLat-TileMaxLat)))
	EndPixelLat = round(PixelsPerLat*(abs(FieldMinLat-TileMaxLat)))
end
if StartPixelLat > EndPixelLat
        temp = EndPixelLat;
        EndPixelLat = StartPixelLat;
        StartPixelLat = temp;
end

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

if EndPixelLat <sizeB4(1)-pixelgap
        EndPixelLat = EndPixelLat + pixelgap
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 > pixelgap
        StartPixelLong = StartPixelLong - pixelgap
else
        StartPixelLong =1
end
if EndPixelLong < sizeB4(1) - pixelgap
        EndPixelLong = EndPixelLong+pixelgap
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
%change this line after finish 11-sep-2019
%ImageDate = PreviousDay
if PreviousDay==ImageDate
	%do nothing
	pgd = 0
else
	
	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);
		
		wait_t = randi(10)
		pause(wait_t)

		urlwrite(B2URL,B2FileName);
		wait_t = randi(10)
		pause(wait_t)

		urlwrite(B3URL,B3FileName);
	catch
		result_status2 = 'failed due to HTTP Error'
		result_status = strcat(UID,"failed4")
		return
	end
end
%read images
%try
try
	B4 = imread(B4FileName);
catch
	try
		urlwrite(B4URL,B4FileName);
		B4 = imread(B4FileName);
	catch
		result_status2 = 'failed due to HTTP Error'
		result_status = strcat(UID,"failed4")
		return
	end
end

try
	B5 = imread(B5FileName);
catch
	try
		urlwrite(B5URL,B5FileName);
		B5 = imread(B5FileName);
	catch
		result_status2 = 'failed due to HTTP Error'
		result_status = strcat(UID,"failed4")
		return
	end
end

try
	B2 = imread(B2FileName);
catch
	try
		urlwrite(B2URL,B2FileName);
		B2 = imread(B2FileName);
	catch
		result_status2 = 'failed due to HTTP Error'
		result_status = strcat(UID,"failed4")
		return
	end
end

try
	B3 = imread(B3FileName);
catch
	try
		urlwrite(B3URL,B3FileName);
		B3 = imread(B3FileName);
	catch
		result_status2 = 'failed due to HTTP Error'
		result_status = strcat(UID,"failed4")
		return
	end
end


try
	B8 = imread(B8FileName);
catch
	try
		urlwrite(B8URL,B8FileName);
		B8 = imread(B8FileName);
	catch
		result_status2 = 'failed due to HTTP Error'
		result_status = strcat(UID,"failed4")
		return
	end
end

try
	TCI = imread(TCIFileName);
catch
	try
		urlwrite(TCIURL,TCIFileName);
		TCI = imread(TCIFileName);
	catch
		result_status2 = 'failed due to HTTP Error'
		result_status = strcat(UID,"failed4")
		return
	end
end

try
	B8A = imread(B8AFileName);
catch
	try
		urlwrite(B8AURL,B8AFileName);
		B8A = imread(B8AFileName);
	catch
		result_status2 = 'failed due to HTTP Error'
		result_status = strcat(UID,"failed4")
		return
	end
end

try
	B11 = imread(B11FileName);
catch
	try
		urlwrite(B11URL,B11FileName);
		B11 = imread(B11FileName);
	catch
		result_status2 = 'failed due to HTTP Error'
		result_status = strcat(UID,"failed4")
		return
	end
end
%catch
%        result_status2 = 'failed due to image decode Error'
%        result_status = strcat(UID,"failed0")
%        return
%end
%clip larger images


topleftpix = TCI(1,1,1)
bottomleftpix = TCI(sizeB4(1),1,1)
toprightpix = TCI(1,sizeB4(1),1)
bottomrightpix = TCI(sizeB4(1),sizeB4(1),1)
if topleftpix==0 & bottomleftpix==0
	for ww = 1:sizeB4(1)
		if TCI(sizeB4(1),ww,1)==0
			aqqw = 1;
		else
			TileStartPixel = ww
			TileEndPixel = sizeB4(1)
			TileLatStartPixel = 1
			TileLatEndPixel = sizeB4(1)
			break;
		end
	end
elseif toprightpix==0 & bottomrightpix==0
	for ww = 1:sizeB4(1)
		if TCI(1,ww,1)==0
			TileStartPixel = 1
			TileEndPixel = ww
			TileLatStartPixel = 1
		TileLatEndPixel = sizeB4(1)
			break;
		else
			aqqw =1;
		end
	end

else
	TileStartPixel = 1
	TileEndPixel = sizeB4(1)
	TileLatStartPixel = 1
	TileLatEndPixel = sizeB4(1)
	
end
if toprightpix >0 & bottomrightpix==0
	for ww = 1:sizeB4(1)
		if TCI(ww,1,1)==0
			TileLatStartPixel = 1
			TileLatEndPixel = ww
			break;
		else
			aqqw =1;
		end
	end
end
TileLonPixelLength = TileEndPixel-TileStartPixel
TileLatPixelLength = TileLatEndPixel - TileLatStartPixel
PixelsPerLong = TileLonPixelLength/(TileMaxLong-TileMinLong)
PixelsPerLat = TileLatPixelLength/(TileMaxLat-TileMinLat)
if topleftpix==0 & bottomleftpix==0
	StartPixelLong = PixelsPerLong*(abs(TileMinLong - FieldMinLong));
	StartPixelLong = 10980-round(TileLonPixelLength-abs(StartPixelLong))
	EndPixelLong = PixelsPerLong*abs(TileMinLong - FieldMaxLong);
	EndPixelLong = 10980-round(TileLonPixelLength-abs(EndPixelLong))
else
	StartPixelLong = PixelsPerLong*(abs(TileMinLong - FieldMinLong));
	StartPixelLong = round(abs(StartPixelLong))
	EndPixelLong = PixelsPerLong*abs(TileMinLong - FieldMaxLong);
	EndPixelLong = round(abs(EndPixelLong))
end
if toprightpix>0 & bottomrightpix==0
	StartPixelLat = 10980*(abs(TileMaxLat - FieldMinLat));
	StartPixelLat = round(abs(StartPixelLat))
	EndPixelLat = 10980*abs(TileMaxLat - FieldMaxLat);
	EndPixelLat = round(abs(EndPixelLat))
	

	if StartPixelLat > EndPixelLat
        temp = EndPixelLat;
        EndPixelLat = StartPixelLat;
        StartPixelLat = temp;
	end
	if StartPixelLat > pixelgap
		StartPixelLat = StartPixelLat - pixelgap
	else
		StartPixelLat =1
	end
	if EndPixelLat < sizeB4(1) - pixelgap
		EndPixelLat = EndPixelLat+pixelgap
	else
		EndPixelat = sizeB4(1)
	end

end
if StartPixelLong > EndPixelLong
        temp = EndPixelLong;
        EndPixelLong = StartPixelLong;
        StartPixelLong = temp;
end
if StartPixelLong > pixelgap
        StartPixelLong = StartPixelLong - pixelgap
else
        StartPixelLong =1
end
if EndPixelLong < sizeB4(1) - pixelgap
        EndPixelLong = EndPixelLong+pixelgap
else
        EndPixelLong = sizeB4(1)
end

	clip = [StartPixelLat EndPixelLat StartPixelLong EndPixelLong];
	clip_tci = clip;
	
	TCI = TCI(clip(1):clip(2),clip(3):clip(4),:);
	TCIgray = rgb2gray(TCI);
	sizegray = size(TCIgray);
	graycount = 0;
	for i = 1:sizegray(1)
		for j = 1:sizegray(2)
			if TCIgray(i,j) >200
				graycount=graycount+1;
			end
			
		end
	end
	
	graycount
	pixelcount = sizegray(1)*sizegray(2)
	%if(graycount>0.7*pixelcount)
	%	result_status = strcat(UID,"failed1")
	%		result_status2 = 'Too much cloud in tci image'
	%	return
	%end
	
	B4 = B4(clip(1):clip(2),clip(3):clip(4));
	B8 = B8(clip(1):clip(2),clip(3):clip(4));

	B2 = B2(clip(1):clip(2),clip(3):clip(4));
	B3 = B3(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));
PixelsPerLat = PixelsPerLat/2
PixelsPerLong = PixelsPerLong/2;
%StartPixelLat = PixelsPerLat*(1-abs(FieldMaxLat-TileMinLat));
%StartPixelLat = round(abs(StartPixelLat))
%EndPixelLat = PixelsPerLat*(1-abs(TileMinLat- FieldMinLat));
%EndPixelLat =  round(abs(EndPixelLat))
StartPixelLat = round(StartPixelLat/2);
EndPixelLat = round(EndPixelLat/2);
if StartPixelLat > EndPixelLat
        temp = EndPixelLat;
        EndPixelLat = StartPixelLat;
        StartPixelLat = temp;
end
if StartPixelLat > pixelgap/2
        StartPixelLat = StartPixelLat - pixelgap/2
else
        StartPixelLat = 1
end
if EndPixelLat <sizeB4(1)-pixelgap/2
        EndPixelLat = EndPixelLat + pixelgap/2
else
        EndPixelLat = sizeB4(1)
end
if topleftpix==0 & bottomleftpix==0
	StartPixelLong = PixelsPerLong*(abs(TileMinLong - FieldMinLong));
	StartPixelLong = 5490-round(TileLonPixelLength/2-abs(StartPixelLong))
	EndPixelLong = PixelsPerLong*abs(TileMinLong - FieldMaxLong);
	EndPixelLong = 5490-round(TileLonPixelLength/2-abs(EndPixelLong))
else
	StartPixelLong = PixelsPerLong*(abs(TileMinLong - FieldMinLong));
	StartPixelLong = round(abs(StartPixelLong))
	EndPixelLong = PixelsPerLong*abs(TileMinLong - FieldMaxLong);
	EndPixelLong = round(abs(EndPixelLong))
end

if StartPixelLong > EndPixelLong
        temp = EndPixelLong;
        EndPixelLong = StartPixelLong;
        StartPixelLong = temp;
end
if StartPixelLong > pixelgap/2
        StartPixelLong = StartPixelLong - pixelgap/2
else
        StartPixelLong = 1
end
if EndPixelLong < sizeB4(1) - pixelgap/2
        EndPixelLong = EndPixelLong+pixelgap/2
else
        EndPixelLong = sizeB4(1)
end
LatLength = EndPixelLat - StartPixelLat
LongLength = EndPixelLong - StartPixelLong


TileLonPixelLength = TileEndPixel-TileStartPixel
PixelsPerLong = 0.5*TileLonPixelLength/(TileMaxLong-TileMinLong)

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 > pixelgap/2
        StartPixelLong = StartPixelLong - pixelgap/2
else
        StartPixelLong = 1
end
if EndPixelLong < sizeB4(1) - pixelgap/2
        EndPixelLong = EndPixelLong+pixelgap/2
else
        EndPixelLong = sizeB4(1)
end

%clip smaller images
%clip = [StartPixelLat EndPixelLat StartPixelLong EndPixelLong];
	clip = round(clip/2);
	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 = imresize(B8A,[abs(clip(2)-clip(1)+1) abs(clip(4)-clip(3)+1)]);
	B8A = single(B8A);
	%B11 = imresize(B11,[abs(clip(2)-clip(1)+1) abs(clip(4)-clip(3)+1)]);
	B11 = single(B11);
	B5 = single(B5);
	B2 = single(B2);
	B3 = single(B3);
	
	%calculate NDVI, NDRE, NDWI
	NDVI = (B8 - B4)./(B8 + B4);
	NDRE = (B8NDRE-B5)./(B8NDRE+B5);
	NDWI = (B11 - B8A)./(B8A + B11);
	VARI = (B3-B4)./(B3+B4-B2);
	sB11 = size(B11)
	
	%check if there is too much cloud
	sN = size(NDVI);
	
	negcount = 0;
	negcount_cloud = 0;
	cloud_count = 0;
	for i = 1:sN(1)
		for j = 1:sN(2)
			if NDVI(i,j) < 0
				negcount=negcount+1;
			end
			
		end
	end
	
	negcount
	#negcount_cloud = negcount_cloud*4
	pixelcount
	negcount_cloud = 0;
	
	%if(negcount>pixelcount*0.7 | negcount_cloud>pixelcount*0.7)
	%	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.7)
		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;
colormap(c_map)
imagesc(NDVI_new);
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)
aa_new = aa(startx:endx, starty:endy,:);
imwrite(aa_new,ndviname);


%create colormap2 NDVI
colormap(c_map2)
imagesc(NDVI_new);
axis square tight
axis off
                ndviname = strcat(UID,"_ndvi_cmap2")
                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)
aa_new = aa(startx:endx, starty:endy,:);
imwrite(aa_new,ndviname);




%create color map VARI image
VARI_new = VARI;
colormap(c_map)
imagesc(VARI_new);
axis square tight		
axis off
		variname = strcat(UID,"_vari")
		saveas(1,variname,'png')
%read VARI image, crop, resize and mask on MVARI
variname=strcat(variname,".png");
aa = imread(variname);
size_aa = size(aa)
sizeVARI = size(VARI)
aa_new = aa(startx:endx, starty:endy,:);
imwrite(aa_new,variname);



%create color map 2 VARI image
VARI_new = VARI;
colormap(c_map2)
imagesc(VARI_new);
axis square tight
axis off
                variname = strcat(UID,"_vari_cmap2")
                saveas(1,variname,'png')
%read VARI image, crop, resize and mask on MVARI
variname=strcat(variname,".png");
aa = imread(variname);
size_aa = size(aa)
sizeVARI = size(VARI)
aa_new = aa(startx:endx, starty:endy,:);
imwrite(aa_new,variname);



%create colormap NDRE image
NDRE_new = (1/(max(max(NDRE))))*NDRE;
%NDRE_new = imcomplement(NDRE);
%ndre_arr_name = strcat(UID,"_ndre_arr.png");
%imwrite(NDRE_new,ndre_arr_name);
colormap(c_map)
imagesc(NDRE_new);
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,:);

imwrite(aa_new,i_name);



%create colormap2 NDRE image
NDRE_new = (1/(max(max(NDRE))))*NDRE;
%NDRE_new = imcomplement(NDRE);
%ndre_arr_name = strcat(UID,"_ndre_arr.png");
%imwrite(NDRE_new,ndre_arr_name);
colormap(c_map2)
imagesc(NDRE_new);
axis square tight
axis off
                NDRE_name = strcat(UID,"_ndre_cmap2")
                saveas(1,NDRE_name,'png')
%read NDRE image, crop, resize and mask on MNDRE
i_name = strcat(UID,"_ndre_cmap2.png");
aa = imread(i_name);
aa_new = aa(startx:endx, starty:endy,:);

imwrite(aa_new,i_name);




NDWI_new = (1/max(max(NDWI)))*NDWI;
NDWI_new = imcomplement(NDWI);
%ndwi_arr_name = strcat(UID,"_ndwi_arr.png");
%imwrite(NDWI_new,ndwi_arr_name);
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,:);

imwrite(aa_new,i_name);


NDWI_new = (1/max(max(NDWI)))*NDWI;
NDWI_new = imcomplement(NDWI);
%ndwi_arr_name = strcat(UID,"_ndwi_arr.png");
%imwrite(NDWI_new,ndwi_arr_name);
colormap(c_map2)
imagesc(NDWI_new);
axis square tight
axis off
                imagename =strcat(UID,"_ndwi_cmap2");
                saveas(1,imagename,'png')

%read NDRE image, crop, resize and mask on MNDRE
i_name =strcat(UID,'_ndwi_cmap2.png');
aa = imread(i_name);
aa_new = aa(startx:endx, starty:endy,:);

imwrite(aa_new,i_name);


result_status = strcat(UID,"successful")
end
end
