jgomezdans / geog0133-practicals

Practicals for UCL's Department of Geography Geog0133: "Terrestrial Carbon: modelling and monitoring"
GNU General Public License v3.0
5 stars 3 forks source link

File is not a zip file error for request in grab_data() #6

Closed aclong closed 4 years ago

aclong commented 4 years ago

Get the error BadZipFile: File is not a zip file when loading in phenology.py.

Th response from loading in:

def grab_data(url="https://www.dropbox.com/sh/6wt85sf0ubc9e8n/AACberbeIDG39yzjppNzhHMha?dl=0"):
    fnames=["NDVI_2001.tif", "NDVI_2002.tif", "NDVI_2003.tif", 
            "NDVI_2004.tif", "NDVI_2005.tif", "NDVI_2006.tif", 
            "NDVI_2007.tif", "NDVI_2008.tif", "NDVI_2009.tif",
            "NDVI_2010.tif", "NDVI_2011.tif", "temp_2m.tif"]
    print(f"{time.asctime():s} -> Please wait while I get hold of the data")

    if not os.path.exists("./data/"):
        os.mkdir("./data")
    r = requests.get(url, stream=True)

    if r.status_code == 200:

        with open("./temporal.zip", 'wb') as f:

            for chunk in r:

                f.write(chunk)

        zipper = zipfile.ZipFile("./temporal.zip")

        zipper.extractall("./data/")

        print(f"{time.asctime():s} -> Successfully downloaded data!")

    raise IOError("SOmething happened")

It looks like the file created here:

with open("./temporal.zip", 'wb') as f:

            for chunk in r:

                f.write(chunk)

is actually in .html format

aclong commented 4 years ago

editing the url link to be an automatic download fixes this issue.

https://www.dropbox.com/sh/6wt85sf0ubc9e8n/AACberbeIDG39yzjppNzhHMha?dl=1

instead of

https://www.dropbox.com/sh/6wt85sf0ubc9e8n/AACberbeIDG39yzjppNzhHMha?dl=0

Also the raise error line doesn't appear to do anything so I removed it

jgomezdans commented 4 years ago

Your PR appears to fix this. Not closing as making sure it works on Binder too, but will close once it passes.

Thanks!

jgomezdans commented 4 years ago

Fixed