def download(username, password, date_start, date_end, earthData_name, fileList): if not os.path.exists('./' + earthData_name): os.mkdir('./' + earthData_name) saveDir = './' + earthData_name # Set local directory to download to pathNetrc = os.path.join(os.path.expanduser("~"), '.netrc') if os.path.exists(pathNetrc): os.remove(pathNetrc) netrcFile = [ 'machine urs.earthdata.nasa.gov', 'login ' + username, 'password ' + password ] with open('.netrc', 'w') as f: for item in netrcFile: f.write("%s\n" % item) shutil.copy('.netrc', os.path.expanduser("~")) #fileList = DownloadList(date_start , date_end,earthData_name) fileList = sorted(fileList) #for i in fileList: # print(i) # -----------------------------------------DOWNLOAD FILE(S)-------------------------------------- # # Loop through and download all files to the directory specified above, and keeping same filenames count = 0 for f in fileList: #print(' ',str((count+1)/len(fileList)*100)[:5] + ' % Completed') date_of_file = f.split('/')[5].replace('.', '-') path = os.path.join(saveDir, date_of_file) if not os.path.exists(path): os.mkdir(path) saveName = os.path.join(path, f.split('/')[-1].strip()) if os.path.exists(saveName): try: if not earthData_name == 'IMERG': f = SD(saveName, SDC.READ) f.end() else: f = Dataset(saveName, 'r') f.close() count += 1 continue except: print('Damgeged file encountered, redownloading...') # Create and submit request and download file file_downloaded = 0 while file_downloaded == 0: try: with requests.get(f.strip(), stream=True) as response: if response.status_code != 200: print( "Verify that your username and password are correct" ) else: response.raw.decode_content = True content = response.raw with open(saveName, 'wb') as d: while True: chunk = content.read(16 * 1024) if not chunk: break d.write(chunk) print('Downloaded file: {}'.format(saveName)) file_downloaded = 1 except: print("Connection refused by the server..") print("Let me sleep for 5 seconds") print("ZZzzzz...") time.sleep(10) print("Was a nice sleep, now let me continue...") continue count += 1