function result_status = monitored_field2022(JSONString)

pkg load image
JSONSentinel = JSONString
try
TileCloudCover = str2double(JSONSentinel.cloud_cover)
catch
TileCloudCover = 0.0;
end
FieldArea = JSONSentinel.FieldArea
StartPixelLat = JSONSentinel.StartPixelLat
EndPixelLat = JSONSentinel.EndPixelLat
StartPixelLong = JSONSentinel.StartPixelLong
EndPixelLong = JSONSentinel.EndPixelLong
FieldArea = JSONSentinel.FieldArea
FieldID = JSONSentinel.FieldID
PlantDistance = JSONSentinel.PlantDistance
if EndPixelLong > 10980
	EndPixelLong = 10979;
end
UID = JSONSentinel.UID

PreviousDay = str2num(JSONSentinel.PreviousDay)
ImageDate = str2num(JSONSentinel.LatestDay)
MGRS = JSONSentinel.MGRS
mainPath = strcat(UID, "/");
mainPath = strcat(mainPath,FieldID);
B4FileName = strcat(mainPath,'/B04.png');

B8FileName = strcat(mainPath,'/B08.png');
TCIFileName = strcat(mainPath,'/TCI.png');

B12FileName = strcat(mainPath,'/B12.png');
B11FileName = strcat(mainPath,'/B11.png');
B5FileName = strcat(mainPath,'/B05.png');

B2FileName = strcat(mainPath,'/B02.png');

B3FileName = strcat(mainPath,'/B03.png');

RVIFileName = strcat(mainPath,'/rvi.png');

RSMFileName = strcat(mainPath,'/rsm.png');

LAIFileName = strcat(mainPath,'/lai.png');

ImageDate = JSONSentinel.LatestDay

sizeB4 = [10980 10980];
format long;

try
	B4 = imread(B4FileName);
    qqq = 1

	B5 = imread(B5FileName);
    qqq = 2

	B2 = imread(B2FileName);
    qqq = 3
	B3 = imread(B3FileName);
    qqq = 4
	B8 = imread(B8FileName);
    qqq = 5
	TCI = imread(TCIFileName);
    qqq = 6
	B12 = imread(B12FileName);
    qqq = 7
	B11 = imread(B11FileName);

    qqq = 8
catch
	
	result_status = strcat(UID,"failed3")
	return
	end

try
	RVI = imread(RVIFileName);
	RVI = rgb2gray(RVI);
	RVI = single(RVI)/255;
catch
	aaaa = 1
end

try
	RSM = imread(RSMFileName);
	RSM = rgb2gray(RSM);
	RSM = single(RSM)/255;
catch
	aaaa = 1
end

try
	LAI_2 = imread(LAIFileName);
	LAI_2 = rgb2gray(LAI_2);
	LAI_2 = single(LAI_2);
	
catch
	aaaa = 1
end

sizeImg = size(B8);

%%resize the images
B4 = imresize(B4, [sizeImg(1), sizeImg(2)]);
B5 = imresize(B5, [sizeImg(1), sizeImg(2)]);
B2 = imresize(B2, [sizeImg(1), sizeImg(2)]);
B3 = imresize(B3, [sizeImg(1), sizeImg(2)]);
B8 = imresize(B8, [sizeImg(1), sizeImg(2)]);
B12 = imresize(B12, [sizeImg(1), sizeImg(2)]);
B11 = imresize(B11, [sizeImg(1), sizeImg(2)]);
TCI = imresize(TCI, [sizeImg(1), sizeImg(2)]);
sizeB4 = [10980 10980];
wq = 1

sizeB4 = [5490 5490];
rr = 11
StartPixelLat = round(StartPixelLat/2);
EndPixelLat = round(EndPixelLat/2);

	rr = 12
	B8NDRE = B8;
	B4BSI = B4;
	B12BSI = B12;	
	B8NDRE = single(B8NDRE);
       B4BSI = single(B4BSI);
        B12BSI = single(B12BSI);
	B4 = single(B4);
	B8 = single(B8);
	
	B12 = single(B12);
	B5 = single(B5);
	B2 = single(B2);
	B3 = single(B3);
	B11 = single(B11);


	NDVI = (B8 - B4)./(B8 + B4);
	minN = min(min(NDVI));
	maxN = max(max(NDVI))
	AVI = (B8.*(4096-B4).*(B8-B4)).^(1/3);
	AVI = AVI/4096;
	BSI = (B8 +B3 +B4)./(B8+B3-B4);
	sizeBSI = size(BSI);
	for ii =1:sizeBSI(1)
		for jj=1:sizeBSI(2)
			if BSI(ii,jj) > 2.5
			BSI(ii,jj) = 2.5;
			end
		end
	end
	BSI = BSI/2.5;
	SI = ((4096-B2).*(4096-B3).*(4096-B4)).^(1/3);
	SI = SI/4096;

	NDRE = (B8NDRE-B5)./(B8NDRE+B5);
	NDWI = (B8NDRE - B12)./(B8NDRE + B12);
	NDMI = (B8NDRE - B11)./(B8NDRE + B11);
weas = 'ndmi minmax'
	min(min(NDMI))
	max(max(NDMI))
	NumVARI = B3-B4;
	DenVARI = (B3+B4-B2);
	VARI = NumVARI./DenVARI;
	EVI = (2.5*(B8-B4))./((B8+6*B4-7.5*B2)+10000);
	sizeVARI = size(VARI);
	sizeNDRE = size(NDRE);
	minNDVI = -1;
	maxNDVI = 1;
	SAVI = (B8-B4)./(B8+B4+0.428*4096)*(1+0.428);
	SAVI = 1.5*(B8-B4)./(B8+B4+0.5);
	LAI = zeros(sizeVARI(1), sizeVARI(2));
	SOC = zeros(sizeVARI(1), sizeVARI(2));
	VSSI = 2*B3 - 5*(B4+B8);
	for ii = 1:sizeVARI(1)
		for jj = 1:sizeVARI(2)
			NDVIij = NDVI(ii,jj);
			Fc = 1- (maxNDVI - NDVIij)/(maxNDVI - minNDVI);
			LAI = -2*log(1 - Fc);
			SOC_pc_ij = 100*(LAI - 0.0411)/0.0087;
			SOCij= SOC_pc_ij*0.004536*100/(350*9*20);
			SOC(ii,jj) = SOCij;
		end
	end
	SOC = single(SOC);
	for ii = 1:sizeNDRE(1)
		for jj = 1:sizeNDRE(2)			
			if NDRE(ii,jj)>=1
				NDRE(ii,jj)=1;
			elseif NDRE(ii,jj)<-1
				NDRE(ii,jj)=-1;
			end
			if NDWI(ii,jj)>=1
				NDWI(ii,jj)=1;
			elseif NDWI(ii,jj)<-1
				NDWI(ii,jj)=-1;
			end
			if NDMI(ii,jj)>=0.8
				NDMI(ii,jj)=0.8;
			elseif NDMI(ii,jj)<-0.8
				NDMI(ii,jj)=-0.8;
			end
		end
	end
	for ii = 1:sizeVARI(1)
		for jj = 1:sizeVARI(2)
			a_mat = max([-1 VARI(ii,jj)]);
			VARI(ii,jj) = min([1 a_mat]);
			
		end
	end
	sB12 = size(B12)
	xlsFileName = strcat(mainPath, "/IndexValues_ndvi.csv");
	csvwrite(xlsFileName, NDVI);

	sN = size(NDVI);	
	
	SizeNewB4 = size(B4);
	IsNaNMat = isnan(NDVI);
TCIFilename2 = strcat(mainPath,"/TCI.png");

imwrite(TCI,TCIFilename2);	%sar image
try
filename = strcat(mainPath,"/evapo.png");
EVAPO = imread(filename);
EVAPO_new = imresize(EVAPO,[sizeVARI(1),sizeVARI(2)]);
imwrite(EVAPO_new,filename);
catch
        akkk = 1;
end


%% rvi snippet was here


	IsNaNCount =sum(sum(IsNaNMat))
	if(IsNaNCount > SizeNewB4(1)*SizeNewB4(2)*1)
		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 = img;
try
    a=rgb2ntsc(img);
catch
	img = im2double(img);
	a=rgb2ntsc(img);
end
    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

img=(img-v_min(1))/(v_max(1)-v_min(1));

startx=25;
endx = 900-25;
starty=181;
endy=1200-188;
TCIFilename = strcat(mainPath,"/ETCI.png");
%save ETCI image
imwrite(uint8(img.*255),TCIFilename);
TCIFilenameL = strcat(mainPath,"/ETCI_large.png");
%save ETCI image
imwrite(uint8(img.*255),TCIFilenameL);
%save TCI image
TCIFilename2 = strcat(mainPath,"/TCI.png");

imwrite(TCI,TCIFilename2);
TCIFilename2L = strcat(mainPath,"/TCI_large.png");
imwrite(TCI,TCIFilename2L);
%create color map NDVI image
filenametci = strcat(mainPath,"/ETCI.png");
tci = imread(filenametci);
try
TCIgray = rgb2gray(tci);
catch
        result_status = strcat(UID,"failed1")
        return
        end
badveg = [247 136 90];
vbadveg = [229 25 26];
badwater = [180 145 200];
vbadwater = [60 19 97];
badcloud = [255 255 255];
bothbad = [145 16 44];
otherregions = [17 167 95];
sizeNDVI = size(NDVI);


BI = B8NDRE./B5 - 1;
BI_expanded = zeros(sizeNDVI(1), sizeNDVI(2));
NDRE_expanded= zeros(sizeNDVI(1), sizeNDVI(2));
NDWI_expanded = zeros(sizeNDVI(1), sizeNDVI(2));
NDMI_expanded = zeros(sizeNDVI(1),sizeNDVI(2));

delta = 0;
if PlantDistance > 1
	if PlantDistance < 3
		delta = 0.1;
	end
end

if PlantDistance > 3
	if PlantDistance < 6
		delta = 0.2;
	end
end

if PlantDistance > 6
	delta = 0.3;
end




hybridImage = zeros(sizeVARI(1), sizeVARI(2), 3);
hybridBlindImage = hybridImage;
for ii = 1:sizeVARI(1)
                for jj = 1:sizeVARI(2)
                        NDVIij = NDVI(ii,jj);
                        EVIij = EVI(ii,jj);
			NDREij = NDRE_expanded(ii,jj);
			NDWIij = NDWI_expanded(ii,jj);
			NDVIij = NDVIij + delta;
			EVIij = EVIij + delta;
			NDREij = NDREij + delta;
			NDWIij = NDWIij + delta;
			if NDVIij > 0.9
				NDVIij = NDVIij;
			end
			if EVIij > 0.9
				EVIij = EVIij;
			end
			if NDREij > 0.9
				NDREij = NDREij;
			end
			if NDWIij > 0.9
				NDWIij = NDWIij;
			end
			NDVI(ii,jj) = NDVIij;
			EVI(ii,jj) = EVIij;
			if TCIgray(ii,jj) >170
				CloudFlag = 1;
			else
				CloudFlag = 0;
			end
			hybridImage(ii,jj,:) = otherregions;
			hybridBlindImage(ii,jj,:) = [111,111,111];
			if CloudFlag ==1
				hybridImage(ii,jj,:) = badcloud;
				hybridBlindImage(ii,jj,:) = [0,0,0];
			else
				if NDVIij< 0.4 
					if NDVIij > 0.3
					hybridImage(ii,jj,:) = badveg;
					hybridBlindImage(ii,jj,:) = [0,0,0];
				end
				end
				if NDVIij < 0.3
					hybridImage(ii,jj,:) = vbadveg;
					hybridBlindImage(ii,jj,:) = [0,0,0];
				end
				if NDWIij< 0.4
					if NDVIij > 0.3
                                        hybridImage(ii,jj,:) = badwater;
										hybridBlindImage(ii,jj,:) = [255,255,255];
                                end
				end
                                if NDWIij < 0.3
                                        hybridImage(ii,jj,:) = vbadwater;
										hybridBlindImage(ii,jj,:) = [255,255,255];
                                end
				if NDVIij < 0.4
					if NDWIij < 0.4
					hybridImage(ii,jj,:) = bothbad;
					hybridBlindImage(ii,jj,:) = [0,0,0];
				end
				end
			end
                end
        end


filename = strcat(mainPath,"/hybrid");
filename=strcat(filename,".png");
imwrite(uint8(hybridImage),filename);

filename = strcat(mainPath,"/hybrid_blind");
filename=strcat(filename,".png");
imwrite(uint8(hybridBlindImage),filename);

index_new = NDVI;
imgCMP = gen_map1_img(index_new);
filename = strcat(mainPath,"/ndvi")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
filename = strcat(mainPath,"/ndvi_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);

try

index_new = RVI;
xlsFileName = strcat(mainPath, "/IndexValues_rvi.csv");
csvwrite(xlsFileName, RVI);


index_new = RSM;
xlsFileName = strcat(mainPath, "/IndexValues_rsm.csv");
csvwrite(xlsFileName, RSM);

index_new = LAI_2;
xlsFileName = strcat(mainPath, "/IndexValues_lai.csv");
csvwrite(xlsFileName, LAI_2);

catch
awqa = 1;
end
index_new = VSSI;
xlsFileName = strcat(mainPath, "/IndexValues_vssi.csv");
csvwrite(xlsFileName, VSSI);
imgCMP = gen_map1_img(index_new);
filename = strcat(mainPath,"/vssi")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
filename = strcat(mainPath,"/vssi_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);

index_new = SOC;
xlsFileName = strcat(mainPath, "/IndexValues_soc.csv");
csvwrite(xlsFileName, SOC);
imgCMP = gen_map1_soc(index_new);
filename = strcat(mainPath,"/soc")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_soc(index_new);
filename = strcat(mainPath,"/soc_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);



%create color map EVI image
xlsFileName = strcat(mainPath, "/IndexValues_evi.csv");
csvwrite(xlsFileName, EVI);

index_new = EVI;
imgCMP = gen_map1_img(index_new);
filename = strcat(mainPath,"/evi")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
filename = strcat(mainPath,"/evi_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);

xlsFileName = strcat(mainPath, "/IndexValues_vari.csv");
       csvwrite(xlsFileName,VARI);
index_new = VARI;
imgCMP = gen_map3_img(index_new);
filename = strcat(mainPath,"/vari")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map4_img(index_new);
filename = strcat(mainPath,"/vari_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
       

%create colormap NDRE image
xlsFileName = strcat(mainPath, "/IndexValues_ndre.csv");
        csvwrite(xlsFileName, NDRE);
		xlsFileName = strcat(mainPath, "/IndexValues_avi.csv");
 csvwrite(xlsFileName, AVI);
index_new = AVI;
imgCMP = gen_map1_img(index_new);
filename = strcat(mainPath,"/avi")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
filename = strcat(mainPath,"/avi_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);

xlsFileName = strcat(mainPath, "/IndexValues_bsi.csv");
 csvwrite(xlsFileName, BSI);
index_new = BSI;
imgCMP = gen_map1_img(index_new);
filename = strcat(mainPath,"/bsi")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
filename = strcat(mainPath,"/bsi_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);


xlsFileName = strcat(mainPath, "/IndexValues_si.csv");
 csvwrite(xlsFileName, SI);
index_new = SI;
imgCMP = gen_map1_img(index_new);
filename = strcat(mainPath,"/si")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
filename = strcat(mainPath,"/si_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);

xlsFileName = strcat(mainPath, "/IndexValues_savi.csv");
 csvwrite(xlsFileName,SAVI);
index_new = SAVI;
imgCMP = gen_map1_img(index_new);
imgCMP = resize_index_image(imgCMP, size(NDVI));
filename = strcat(mainPath,"/savi")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
filename = strcat(mainPath,"/savi_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);


index_new = NDRE;
imgCMP = gen_map1_img(index_new);
imgCMP = resize_index_image(imgCMP, size(NDVI));
filename = strcat(mainPath,"/ndre")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
imgCMP = resize_index_image(imgCMP, size(NDVI));
filename = strcat(mainPath,"/ndre_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);



try
xlsFileName = strcat(mainPath, "/IndexValues_ndmi.csv");
	csvwrite(xlsFileName, NDMI);
catch
	asdfd = 1;
end

index_new = NDMI;
imgCMP = gen_map1_img(index_new);
imgCMP = resize_index_image(imgCMP, size(NDVI));
filename = strcat(mainPath,"/ndmi")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
imgCMP = resize_index_image(imgCMP, size(NDVI));
filename = strcat(mainPath,"/ndmi_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);



NDWI_new = NDWI;
xlsFileName = strcat(mainPath, "/IndexValues_ndwi.csv");
        csvwrite(xlsFileName, NDWI);
index_new = NDWI;
imgCMP = gen_map1_img(index_new);
imgCMP = resize_index_image(imgCMP, size(NDVI));
filename = strcat(mainPath,"/ndwi")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);
imgCMP = gen_map2_img(index_new);
imgCMP = resize_index_image(imgCMP, size(NDVI));
filename = strcat(mainPath,"/ndwi_cmap2")
filename=strcat(filename,".png");
imwrite(uint8(imgCMP),filename);

result_status = strcat(UID,"successful")
end
