Skip to content

Commit 6841ef0

Browse files
authored
Merge pull request #43 from CIAT-DAPA/develop
cpt add skip donmwload if files exist
2 parents 85e6d41 + cd275ba commit 6841ef0

File tree

1 file changed

+55
-45
lines changed

1 file changed

+55
-45
lines changed

src/aclimate_cpt/prediction_class.py

+55-45
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import subprocess
2424
import tempfile
2525
import csv
26+
from requests.exceptions import RequestException, Timeout
2627
from dateutil.relativedelta import relativedelta
2728
from aclimate_cpt.tools_d_r import DownloadProgressBar,DirectoryManager
2829

@@ -959,53 +960,62 @@ def best_GI(self,rutas):
959960
return best_gi[0]
960961

961962

962-
def download_gz_predictor_file(self,url, path ,timeout=300):
963-
# url = urls[0]
964-
# path = all_path_down[0]
965-
code = False
966-
while code== False:
967-
try:
968-
response = requests.get(url, stream=True, timeout=timeout)
969-
response.raise_for_status() # Check for HTTP errors
970-
971-
if(response.status_code != 200):
972-
raise Exception("Code status error")
973-
974-
# Obtener el tamaño esperado del archivo del encabezado "Content-Length"
975-
expected_size = int(response.headers.get('Content-Length', 0))
976-
977-
# Descargar el archivo y verificar el tamaño
978-
downloaded_size = 0
979-
980-
progress_bar = tqdm(total=expected_size, unit='B', unit_scale=True, desc=path.split('/')[-1], miniters=1)
981-
982-
with open(path, 'wb') as f:
983-
for chunk in response.iter_content(chunk_size=8192):
984-
downloaded_size += len(chunk)
985-
f.write(chunk)
986-
progress_bar.update(len(chunk))
987-
progress_bar.close()
988-
989-
if downloaded_size == expected_size:
990-
print(".gz file downloaded successfully")
991-
992-
# Descomprimir el archivo descargado
993-
with gzip.open(path, 'rb') as f_in:
994-
with open(path.replace('.gz',''), 'wb') as f_out:
995-
shutil.copyfileobj(f_in, f_out)
996-
os.remove(path)
997-
print(".gz file uncompressed successfully")
998-
code = True
999-
return print("Process done successfully")
963+
def download_gz_predictor_file(self, url, path ,timeout=1000, force= False):
964+
965+
if force or os.path.exists(path.replace('.gz','')) == False:
966+
967+
if os.path.exists(path.replace('.gz','')):
968+
os.remove(path.replace('.gz',''))
969+
970+
code = False
971+
while code== False:
972+
try:
973+
response = requests.get(url, stream=True, timeout=timeout)
974+
response.raise_for_status() # Check for HTTP errors
1000975

1001-
else:
1002-
print(f" Downloaded file size ({downloaded_size} bytes) doesn't match expected size ({expected_size} bytes).")
976+
if(response.status_code != 200):
977+
raise Exception("Code status error")
978+
979+
# Obtener el tamaño esperado del archivo del encabezado "Content-Length"
980+
expected_size = int(response.headers.get('Content-Length', 0))
981+
982+
# Descargar el archivo y verificar el tamaño
983+
downloaded_size = 0
984+
985+
progress_bar = tqdm(total=expected_size, unit='B', unit_scale=True, desc=path.split('/')[-1], miniters=1)
986+
987+
with open(path, 'wb') as f:
988+
for chunk in response.iter_content(chunk_size=8192):
989+
downloaded_size += len(chunk)
990+
f.write(chunk)
991+
progress_bar.update(len(chunk))
992+
progress_bar.close()
993+
994+
if downloaded_size == expected_size:
995+
print(".gz file downloaded successfully")
996+
997+
# Descomprimir el archivo descargado
998+
with gzip.open(path, 'rb') as f_in:
999+
with open(path.replace('.gz',''), 'wb') as f_out:
1000+
shutil.copyfileobj(f_in, f_out)
1001+
os.remove(path)
1002+
print(".gz file uncompressed successfully")
1003+
code = True
1004+
return print("Process done successfully")
1005+
1006+
else:
1007+
print(f" Downloaded file size ({downloaded_size} bytes) doesn't match expected size ({expected_size} bytes).")
1008+
os.remove(path)
1009+
except (RequestException, IOError, Timeout) as e:
1010+
if isinstance(e, Timeout):
1011+
print(f"Download timed out: {e}. Retrying...")
1012+
else:
1013+
print(f"Download failed: {e}")
10031014
os.remove(path)
1004-
except (requests.exceptions.RequestException, IOError) as e:
1005-
print(f"Download failed: {e}")
1006-
os.remove(path)
1007-
1008-
1015+
else:
1016+
print("\tFile already downloaded!",path)
1017+
1018+
10091019
return False
10101020

10111021

0 commit comments

Comments
 (0)