#!/usr/bin/env perl

#################################################
#         GLAD Landsat ARD Tools, v. 2.0        #
# Global Land Analysis and Discovery (GLAD) Lab #
#            University of Maryland             #
#         https://glad.umd.edu/ard/home         #
#    Peter Potapov (potapov.peter@gmail.com)    #
#################################################

#################################################
# Usage: >perl C:/GLAD_Tools/download_V2.pl <tile list file> <start interval> <end interval> <output folder>
# Example: >perl C:/GLAD_Tools/download_V2.pl tiles.txt 875 897 D:/ARD
#################################################

### Multi-threads setup
use threads; $threads=5; use threads::shared; my @tasklist:shared; my @log:shared;

### Get input parameters
$username="glad"; $password="ardpas";
$filelist=$ARGV[0]; if ($filelist eq "" or !-e $filelist) {die "list of tiles not found";}
$start=$ARGV[1]; if ($start eq "") {die "wrong call: start date";}
$end=$ARGV[2]; if ($end eq "" or $end<$start) {die "wrong call: end date";}
$outfolder=$ARGV[3]; $outfolder=~s/\ //g; $outfolder=~s/\\/\//g; if ($outfolder eq "") {die "wrong output folder name";}
if (!-d "$outfolder") {
	(@path)=split('/',$outfolder); $temp_path=$path[0];
	for ($i=1; $i<=$#path; $i++){
	$temp_path=$temp_path."/".$path[$i];
	if (!-d "$temp_path"){mkdir "$temp_path";}
	}
	if (!-d "$outfolder") {die "cannot create $outfolder";}
}
$winfolder=$outfolder; $winfolder =~ s/\//\\/g;

### Get list of tiles and composites
@tasklist=();
open (DAT, "$filelist") or die "list of tiles not found"; @templist=<DAT>; close (DAT); 
for $tile (@templist){ chomp ($tile); $tile=~s/\ //g; if ($tile ne ""){push (@tilelist,$tile);}}
for $tile (@tilelist){
for ($i=$start; $i<=$end; $i++){
push (@tasklist, "$tile,$i");
}}

### Threading
@log=(); if (-e "log.txt"){system "del log.txt";}
for($threadID=1;$threadID<=$threads;$threadID++){push @ClassThreads, threads->create(\&process);} 
foreach $thread (@ClassThreads)  {$thread->join();} @ClassThreads=();

$num=@log;
if ($num>0){
	print "Download failure. Check log.txt\n";
	open (OUT, ">log.txt");
	for $line (@log) {print OUT "$line\n";}
	close (OUT);
}
else {print "Download complete\n";}

### Downloading
sub process {
while ($line = shift(@tasklist)){
	if ($line eq ""){die;} if (-e "stop.txt"){return;}
	($tile,$i)=split('\,',$line);  ($long,$lat)=split('_',$tile);
	if (!-d "$outfolder/$tile") {mkdir "$outfolder/$tile";}
	print "downloading $tile $i\n";
	#get size
	system "curl -u glad:ardpas --head https://glad.umd.edu/dataset/glad_ard2/$lat/$tile/$i.tif >temp_$tile\_$i.txt  2>&1";
	if (!-e "temp_$tile\_$i.txt"){print "Tile $tile Interval $i not found or no connection to ARD server (1)\n"; push (@log, "$tile\t$i\tno connection");}
	else {
		open (DAT, "temp_$tile\_$i.txt"); @temp=<DAT>; close (DAT); $checksize=0; system "del temp_$tile\_$i.txt";
		for $str (@temp){ $index = index ($str, "Content-Length"); if ($index>=0){chomp ($str); ($t,$checksize)=split('\ +',$str);}}
		if ($checksize==0){print "Tile $tile Interval $i not found or no connection to ARD server (2)\n"; push (@log, "$tile\t$i\tno connection");}	
		else {$test=1;}
	}
	if ($test==1){
		if (-e "$outfolder/$tile/$i.tif"){
			$size = (-s "$outfolder/$tile/$i.tif");
			if ($size == $checksize){print "Tile $tile Interval $i already downloaded\n"; $test=2;}
			else {print "$size == $checksize\n";}
		}
		if ($test==1){
			system "curl -u $username:$password -X GET https://glad.umd.edu/dataset/glad_ard2/$lat/$tile/$i.tif -o $outfolder/$tile/$i.tif >nul 2>&1";
			$size = (-s "$outfolder/$tile/$i.tif");
			if ($size == $checksize){print "Tile $tile Interval $i downloaded\n";}
			else {print "Tile $tile Interval $i download failure\n"; push (@log, "$tile\t$i\tdownload failure");}
		}
	}
}
}



