From d5add5df81d186405a58b6b6cf33f70778ff9beb Mon Sep 17 00:00:00 2001 From: Ryoko Araki <52061672+RY4GIT@users.noreply.github.com> Date: Fri, 10 Feb 2023 13:31:43 -0800 Subject: [PATCH] Accomodate multiple zip file downloads --- .../Customize and Access NSIDC Data.ipynb | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/notebooks/Customize and Access NSIDC Data.ipynb b/notebooks/Customize and Access NSIDC Data.ipynb index 6405330..57c3a6a 100755 --- a/notebooks/Customize and Access NSIDC Data.ipynb +++ b/notebooks/Customize and Access NSIDC Data.ipynb @@ -685,14 +685,37 @@ "\n", " # Download zipped order if status is complete or complete_with_errors\n", " if status == 'complete' or status == 'complete_with_errors':\n", - " downloadURL = 'https://n5eil02u.ecs.nsidc.org/esir/' + orderID + '.zip'\n", - " print('Zip download URL: ', downloadURL)\n", - " print('Beginning download of zipped output...')\n", - " zip_response = session.get(downloadURL)\n", - " # Raise bad request: Loop will stop for bad response code.\n", - " zip_response.raise_for_status()\n", - " with zipfile.ZipFile(io.BytesIO(zip_response.content)) as z:\n", - " z.extractall(path)\n", + "\n", + " # When the request size is large, zip files will be divided into max 4 files\n", + " multiple_zip_files_flag = 1\n", + " for nzip in range(1,5):\n", + " try:\n", + " downloadURL = 'https://n5eil02u.ecs.nsidc.org/esir/' + orderID + '.zip?' + str(nzip)\n", + " print('Zip download URL: ', downloadURL)\n", + " print('Beginning download of zipped output...')\n", + " zip_response = session.get(downloadURL)\n", + " # Raise bad request: Loop will stop for bad response code.\n", + " zip_response.raise_for_status()\n", + " with zipfile.ZipFile(io.BytesIO(zip_response.content)) as z:\n", + " z.extractall(path)\n", + " except:\n", + " multiple_zip_files_flag = 0\n", + " continue\n", + " \n", + " # When the request size is small, only one zip file will be created\n", + " if multiple_zip_files_flag == 0:\n", + " try: \n", + " downloadURL = 'https://n5eil02u.ecs.nsidc.org/esir/' + orderID + '.zip'\n", + " print('Zip download URL: ', downloadURL)\n", + " print('Beginning download of zipped output...')\n", + " zip_response = session.get(downloadURL)\n", + " # Raise bad request: Loop will stop for bad response code.\n", + " zip_response.raise_for_status()\n", + " with zipfile.ZipFile(io.BytesIO(zip_response.content)) as z:\n", + " z.extractall(path)\n", + " except:\n", + " print('Zip download failed.')\n", + " \n", " print('Data request', page_val, 'is complete.')\n", " else: print('Request failed.')\n", " \n",