earthlab / earth-analytics-intermediate-earth-data-science-textbook

Other
11 stars 9 forks source link

Python examples: pathlib & avoid chdir #10

Open snowman2 opened 2 years ago

snowman2 commented 2 years ago

https://www.earthdatascience.org/courses/use-data-open-source-python/multispectral-remote-sensing/intro-naip/

I think it is better to build your path instead of os.chdir to prevent confusion. This is my opinion, so feel free to ignore if you disagree.

Also, the examples could benefit from using pathlib.

lwasser commented 2 years ago

sure @snowman2 i think i follow. The lessons in our textbook are focused on newer-ish users - this is an intermediate book. The idea behind this:

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics', 'data'))

was that we'd have then always save data in their home/earth-analytics/data directory and then call the data from there. Are you suggesting rather than changing the directory we simply build paths like this:

home_path - os.path.join(et.io.HOME, 'earth-analytics', 'data')

OR using pathlib instead could work to generate a path to their home directory potentially. and then calling data from that explicit path?

# or do this with object oriented pathlib rather than just os
data_path = os.path.join(home_path, path-to-data-here)

thank you. i want to clarify because all of our lessons use this approach of changing the directory so it would be a big overhaul BUT i think i understand your point - changing the working dir makes it work on all machines but could be problematic in some scenarios.

Any clarification is helpful here. i appreciate it!

snowman2 commented 2 years ago

Thanks for asking clarification :+1: . I am suggesting to do:

from pathlib import Path

work_dir = Path(et.io.HOME, 'earth-analytics', 'data')

And then when writing files to disk:

rds.rio.to_raster(str(work_dir / "filename.tif"))