Skip to content

Commit 88c3c2f

Browse files
Merge pull request #9 from gregory-halverson-jpl/main
fixing download
2 parents e015828 + e6932ab commit 88c3c2f

File tree

7 files changed

+203
-4
lines changed

7 files changed

+203
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pip install gedi-canopy-height
4545
Import this package as `gedi_canopy_height` with under-scores.
4646

4747
```python
48-
import gedi_canopy_height
48+
from gedi_canopy_height import load_canopy_height
4949
```
5050

5151
## References

Using wget package.ipynb

Lines changed: 143 additions & 0 deletions
Large diffs are not rendered by default.

gedi_canopy_height/download.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import requests
3+
import posixpath
4+
from tqdm.notebook import tqdm
5+
6+
def download(URL, filename):
7+
# Get the current file size if it exists
8+
existing_file_size = os.path.getsize(filename) if os.path.exists(filename) else 0
9+
# Request the file details using Range header if a partial file exists
10+
headers = {'Range': f'bytes={existing_file_size}-'} if existing_file_size else {}
11+
response = requests.get(URL, headers=headers, stream=True)
12+
13+
# Check for resumable support (206 Partial Content response)
14+
if response.status_code not in (200, 206):
15+
raise Exception(f"Server does not support resuming or returned an error: {response.status_code}")
16+
17+
# Determine total size based on Content-Range or Content-Length
18+
content_range = response.headers.get('Content-Range')
19+
if content_range:
20+
total_size = int(content_range.split('/')[-1])
21+
else:
22+
total_size = int(response.headers.get('content-length', 0)) + existing_file_size
23+
24+
# Set mode to append if resuming, otherwise write
25+
mode = 'ab' if existing_file_size else 'wb'
26+
27+
# Start download
28+
with open(filename, mode) as f, tqdm(
29+
initial=existing_file_size,
30+
total=total_size,
31+
unit='B',
32+
unit_scale=True,
33+
unit_divisor=1024,
34+
desc=f"Downloading {posixpath.basename(URL)}"
35+
) as bar:
36+
for chunk in response.iter_content(chunk_size=8192):
37+
if chunk:
38+
f.write(chunk)
39+
bar.update(len(chunk))
40+
41+
print(f"Download completed: {filename}")

gedi_canopy_height/gedi_canopy_height.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import rasters as rt
1313
from rasters import RasterGeometry, Raster
1414

15+
from .download import download
16+
1517
GEDI_DOWNLOAD_DIRECTORY = join("~", "data", "gedi-canopy-height")
1618

1719
CANOPY_COLORMAP = LinearSegmentedColormap.from_list(
@@ -56,9 +58,10 @@ def download_file(self, URL: str, filename: str) -> str:
5658
directory = dirname(filename_absolute)
5759
makedirs(directory, exist_ok=True)
5860
partial_filename = f"{filename_absolute}.download"
59-
command = f'wget -c -O "{partial_filename}" "{URL}"'
61+
# command = f'wget -c -O "{partial_filename}" "{URL}"'
6062
download_start = perf_counter()
6163
# system(command)
64+
download(URL, partial_filename)
6265
download_end = perf_counter()
6366
download_duration = download_end - download_start
6467
self.logger.info(f"completed download in {download_duration:0.2f} seconds: {filename}")

gedi_canopy_height/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.1
1+
1.2.0

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools>=60", "setuptools-scm>=8.0", "wheel"]
33

44
[project]
55
name = "gedi-canopy-height"
6-
version = "1.1.1"
6+
version = "1.2.0"
77
description = "generates rasters of canopy height from the Global Ecosystem Dynamics Investigation (GEDI) mission"
88
readme = "README.md"
99
authors = [

wget-log

Lines changed: 12 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)