function result_status = getETCI(JSONString,JSONCurDate,prox_host)

pkg load image
JSONDate = JSONString;

B4URL = JSONCurDate.Band4URI;
SensingDate = JSONCurDate.sensing_date;

TCIURL = strrep(B4URL,"B04.jp2","TCI.jp2");
%proxy = p_prox
TCIFileName = 'TileTCI.jp2';
%pause(5);
%prox_https=strcat("https://",prox_host)

%putenv ('https_proxy', prox_https)
try
urlwrite(TCIURL,TCIFileName);
catch
UID = JSONDate.UID;
result_status = strcat(UID,"failed0")
return
end

%urlwrite(TCIURL,TCIFileName);
try
TCI = imread('TileTCI.jp2');
catch
result_status = strcat(UID,"failed1")
                result_status2 = 'Too much black in image'
        return
end
sizeB4 = size(TCI);
format long;
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)


    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 = 0;
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 = 0;
end
if EndPixelLong < sizeB4(1) - 150
        EndPixelLong = EndPixelLong+150;
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
%urlwrite(TCIURL,TCIFileName);

%TCI = imread('TileTCI.jp2');

        clip = [StartPixelLat EndPixelLat StartPixelLong EndPixelLong];

        TCI = TCI(clip(1):clip(2),clip(3):clip(4),:);
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;
	try
a = rgb2ntsc(img);
catch
UID = JSONDate.UID;
result_status = strcat(UID,"failed0")
return
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_min=rgb2ntsc(v_min);
    %v_max=rgb2ntsc(v_max);
        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'
        return
end
img=(img-v_min(1))/(v_max(1)-v_min(1));

        TCIFilename = strcat(UID,"ETCI.png");
        TCIFilename = strcat(SensingDate,TCIFilename);
imwrite(uint8(img.*255),TCIFilename);

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

end

end
