2

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.

Mars
  • 341
  • 1
  • 3
  • 12

1 Answers1

3
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)
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Thank you for your answer :) However I get this error: ValueError: could not convert string to float: '"193.48' – Mars Aug 13 '18 at 16:13
  • @Kongie I just edited it. There was a missing parenthesis. Now try it again – Onyambu Aug 13 '18 at 16:14
  • I found the missing parenthesis. However still getting that error :o – Mars Aug 13 '18 at 16:15
  • Image name: "S2A_MSIL1C_20160905T140012_N0204_R010_T24WWU_20160905T140241" File size: "193.48 MB" Traceback (most recent call last): File "", line 36, in total += float(sub(r'(?<=\d)\s.*','',json.dumps(bu_images_json["features"][n]["properties"]["size"]))) ValueError: could not convert string to float: '"193.48' – Mars Aug 13 '18 at 16:15
  • change `r'(?<=\d)\s.*'` to `[^0-9.]` and try again – Onyambu Aug 13 '18 at 16:16
  • total += float([^0-9.],'',json.dumps(bu_images_json["features"][n]["properties"]["size"]))) ^ SyntaxError: invalid syntax – Mars Aug 13 '18 at 16:20
  • 1
    no, you were supposed to replace the `r'(?<=\d)\s.*'` part only ie: `float(sub('[^0-9.]','',json.dumps(b...` – Onyambu Aug 13 '18 at 16:23
  • 1
    `float(sub('[^0-9.]','',json.dumps(bu_images_json["features"][n]["properties"]["size"])))` – Onyambu Aug 13 '18 at 16:24
  • Now I have you here, is there an easy way to shorten down how many decimals that are given in the print ;) – Mars Aug 13 '18 at 16:25
  • 1
    just use `round(total, 2)` for 2 decimals, 3 for 3decimals etc – Onyambu Aug 13 '18 at 16:26