function result_status = getNDVI(JSONString,JSONCurDate,prox_host)

pkg load image
JSONDate = JSONString;
B4URL = JSONCurDate.Band4URI;
B8URL = strrep(B4URL,"B04.jp2","B08.jp2");
SensingDate = JSONCurDate.sensing_date;
%bandtag =strcat(band,".jp2")
%B3URL = strrep(B4URL,"B04.jp2",bandtag)


%B3FileName = strcat("Tile",bandtag)
B4FileName = 'TileB04.jp2';
B8FileName = 'TileB08.jp2';
%prox_https=strcat("https://",prox_host)

%putenv ('https_proxy', prox_https)
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
asdq =round(255*c_map)

try
urlwrite(B4URL,B4FileName);
catch
UID = JSONDate.UID;
result_status = strcat(UID,"failed0")
return
end



try
urlwrite(B8URL,B8FileName);
catch
UID = JSONDate.UID;
result_status = strcat(UID,"failed0")
return
end

%pause(5);
%urlwrite(B4URL,B4FileName);
%pause(5);
%urlwrite(B8URL,B8FileName);
%urlwrite(TCIURL,TCIFileName);
try
B4 = imread('TileB04.jp2');

B8 = imread('TileB08.jp2');
catch
        result_status = strcat(UID,"failed1")
                result_status2 = 'Too much black in image'
        return
end
%urlwrite(B3URL,B3FileName);

%B3 = imread(B3FileName);
sizeB4 = size(B4);

TileMaxLat =JSONCurDate.north_lat
TileMinLat =JSONCurDate.south_lat
TileMaxLong = JSONCurDate.east_lon
TileMinLong =JSONCurDate.west_lon


FieldMaxLat = JSONDate.FieldMaxLat
FieldMinLat = JSONDate.FieldMinLat
FieldMaxLong =JSONDate.FieldMaxLong
FieldMinLong =JSONDate.FieldMinLong

UID = JSONDate.UID;

LatsPerPixel = (TileMaxLat - TileMinLat)/sizeB4(1)
LongsPerPixel = (TileMaxLong - TileMinLong)/sizeB4(1)
NewFieldMaxLat = FieldMaxLat+50*LatsPerPixel
NewFieldMinLat = FieldMinLat-50*LatsPerPixel

    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 > 50
        StartPixelLat = StartPixelLat - 50;
else
        StartPixelLat = 0;
end

if EndPixelLat <sizeB4(1)-50
        EndPixelLat = EndPixelLat + 50;
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 > 50
        StartPixelLong = StartPixelLong - 50;
else
        StartPixelLong = 0;
end
if EndPixelLong < sizeB4(1) - 50
        EndPixelLong = EndPixelLong+50;
else
        EndPixelLong = sizeB4(1);
end


TCI = B4;
topleftpix = TCI(1,1)
bottomleftpix = TCI(sizeB4(1),1)
toprightpix = TCI(1,sizeB4(1))
bottomrightpix = TCI(sizeB4(1),sizeB4(1))
if topleftpix==0 & bottomleftpix==0
	for ww = 1:sizeB4(1)
		if TCI(sizeB4(1),ww)==0
			aqqw = 1;
		else
			TileStartPixel = ww
			TileEndPixel = sizeB4(1)
			break;
		end
	end
elseif toprightpix==0 & bottomrightpix==0
	for ww = 1:sizeB4(1)
		if TCI(1,ww)==0
			TileStartPixel = 1
			TileEndPixel = ww
			break;
		else
			aqqw =1;
		end
	end
else
	TileStartPixel = 1
	TileEndPixel = sizeB4(1)
end
pixelgap = 50;
TileLonPixelLength = TileEndPixel-TileStartPixel
PixelsPerLong = TileLonPixelLength/(TileMaxLong-TileMinLong)
LongsPerPixel = 1/PixelsPerLong;
NewFieldMaxLong = FieldMaxLong+50*LongsPerPixel
NewFieldMinLong = FieldMinLong-50*LongsPerPixel

FieldBounds = [NewFieldMinLat NewFieldMaxLat NewFieldMinLong NewFieldMaxLong]
csvwrite('FieldBounds.csv',FieldBounds')

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

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


clip = [StartPixelLat EndPixelLat StartPixelLong EndPixelLong];

       % B3 = B3(clip(1):clip(2),clip(3):clip(4));
try
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),:);
catch
result_status = strcat(UID,"failed1")
                result_status2 = 'Too much black in image'
	return
end
        B4 = single(B4);
        B8 = single(B8);


        Numerator = (B8 - B4);
        Denominator = (B8 + B4);
        NDVI = Numerator./Denominator;
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'
	else
NDVI_new = NDVI;


colormap(c_map)
imagesc(NDVI_new, [0 1]);

axis square tight
axis off
                %axis square tight, axis off
                imagename = strcat(SensingDate,UID);
		imagename = strcat(imagename,"NDVI.png");
                saveas(1,imagename,'png')

i_name=imagename;
aa = imread(i_name);

%aa = single(aa);
size_aa = size(aa)
sizeN = size(NDVI)
startaaheight=27;
endaaheight = 900-25;
startaawidth=182;
endaawidth=1200-188;
%if sizeN(1) > size_aa(1)
	aa = aa(startaaheight:endaaheight,startaawidth:endaawidth,:);
    %resize_factor_width = sizeN(1)/size_aa(1)
%	resize_factor_height = sizeN(2)/size_aa(2)
    #aa = imresize(aa,[sizeN(1) sizeN(2)]);
   % size_aa = size(aa)
%end
imwrite(aa,i_name);
result_status  = strcat(UID,"successful")
end

end

end
