function result_status = getTCI(JSONString,JSONCurDate,prox_host)

pkg load image
JSONDate = JSONString;

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

TCIURL = strrep(B4URL,"B04.jp2","TCI.jp2");

TCIFileName = 'TileTCI.jp2';
%java.lang.System.setProperty('http.proxyHost',prox_host)
%java.lang.System.setProperty('http.proxyPort',prox_port)
%pause(5);
%prox_https=strcat("https://",prox_host)

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

end
wqw = 'cametci'
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 > 100
        StartPixelLat = StartPixelLat - 100;
else
        StartPixelLat = 0;
end

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

        TCIFilename = strcat(UID,"TCI.png");
	TCIFilename = strcat(SensingDate,TCIFilename);
imwrite(TCI,TCIFilename);

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

end

end


