Obtaining the sum of several elements listed in a geojson

66 Views Asked by At

I apologize if this question has been asked or answered elsewhere before. I couldn't locate it.

I'm still relatively new to python and I'm currently setting up a script for downloading files from the ESA sentinel hub via the sentinelsat module.

Now it is working, but I'd like for it to have a way of printing the total file size involved. I already have it printing the name of all the files and their individual data sizes. I also have the total number of files listed. Now I just need the individual data sizes summed into 1 value..

So here is my code snippet:

print("Listing file name and size:")
print("")
for n in range(0, np.size(bu_images_json["features"])):
    print("Image name: ",json.dumps(bu_images_json["features"][n]["properties"]["title"]))
    print("File size: ",json.dumps(bu_images_json["features"][n]["properties"]["size"]))
print("Found", n+1, "files availible for download")
print("Total amount to download")

And it looks something like this

Image name:  "S2A_OPER_PRD_MSIL1C_PDMC_20161009T061329_R010_V20160727T140022_20160727T140246"
File size:  "5.02 GB"
Image name:  "S2A_OPER_PRD_MSIL1C_PDMC_20161009T060351_R139_V20160726T142942_20160726T143122"
File size:  "5.99 GB"
Image name:  "S2A_OPER_PRD_MSIL1C_PDMC_20160720T213054_R053_V20160720T141008_20160720T141008"
File size:  "5.65 GB"
Found 131 files availible for download
Total amount to download

If anyone is aware of any github page or the likes, of which anyone has played around with and expanded the sentinelsat module - Then I'd love to have a link as well.

Thank you for your time.

1

There are 1 best solutions below

10
On BEST ANSWER
from re import sub
print("Listing file name and size:")
total = 0
print("")
for n in range(0, np.size(bu_images_json["features"])):
    print("Image name: ",json.dumps(bu_images_json["features"][n]["properties"]["title"]))
    print("File size: ",json.dumps(bu_images_json["features"][n]["properties"]["size"]))
    total += float(sub('[^0-9.]','',json.dumps(bu_images_json["features"][n]["properties"]["size"])))
print("Found", n+1, "files availible for download")
print("Total amount to download: ",total)