From 0891b7880350c00052d1a729f3595baed5712aea Mon Sep 17 00:00:00 2001 From: Rose Awen Brindle Date: Fri, 11 Mar 2022 13:31:35 +0000 Subject: [PATCH] progressbar docstrings --- .gitignore | 2 ++ LASCO_imgdownload.py | 63 +++++++++++++++++++++++++++++++------------- README.md | 4 +-- progressbar.py | 26 ++++++++++++++++++ 4 files changed, 75 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index af57b99..8f69861 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ *.png +*.zip + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/LASCO_imgdownload.py b/LASCO_imgdownload.py index fe79c00..6e081a0 100644 --- a/LASCO_imgdownload.py +++ b/LASCO_imgdownload.py @@ -1,9 +1,21 @@ """ SOHO LASCO image downloader ----------------------------- -download images from the LASCO coronagraphs for CME analysis +Download all LASCO coronagraph images for a given day. +Images are downloaded to the 'download' folder in the same location as this file. -Made with love by ROSE during March 2022 +Usage: + positional arguments: + int year in yyyy format + int month in mm format + int day in dd format to be downloaded + + optional arguments: + -h, --help show this help message and exit + -t str [str ...], --imgtype str [str ...] + type of image to retrieve, defaults to 2aia193, first digit denotes which LASCO coronagraph + +Made with love by ROSE, March 2022 """ from fileinput import filename @@ -16,22 +28,24 @@ # downloads to the folder 'download' in the same location as this file -# CHANGE THESE VARS TO GET A DIFFERENT DAY -year = 2021 -month = 7 -day = 15 +lascoURL = "https://cdaw.gsfc.nasa.gov/images/soho/lasco" +imgtypes = ["2aia193", "2rdf", "2rdf_aia193rdf", "2rdf_cme", "3rdf", "3rdf_cme", "2eit"] +def getDayImgs(y, m, d, type="2aia193"): + """ + Downloads all of one type of images for a given day from LASCO Catalog -lascoURL = "https://cdaw.gsfc.nasa.gov/images/soho/lasco" + Args: + y: year + m: month + d: day + type: defaults to 2_aia193, options are 2aia193, 2rdf, 2rdf_aia193rdf, 2rdf_cme, 3rdf, 3rdf_cme + """ + if (not type in imgtypes): + print("Unexpected image type request, defaulting") + type = "2aia193" -def getDayImgs(y, m, d, type="2aia193"): - """Downloads all of one type of images for a given day from LASCO Catalog - PARAMS: - y - year - m - month - d - day - type - defaults to 2_aia193, options are 2aia193, 2rdf, 2rdf_aia193rdf, 2rdf_cme, 3rdf, 3rdf_cme""" dayURL = lascoURL + "/" + str(y) + "/" + f'{m:02d}' + "/" + f'{d:02d}' # get image adresses @@ -39,12 +53,19 @@ def getDayImgs(y, m, d, type="2aia193"): soup = BeautifulSoup(page.text, 'html.parser') links = soup.find_all('a') print("Reading all imgs at: " + dayURL) + print("Looking for image type lasc" + type) todownload = [] for a in soup.find_all('a', href=True): - if (type in a.text): + search = type + ".png" + if (search in a.text): todownload.append(dayURL + "/" + a.text) + + if (len(todownload) == 0): + print("\nWARNING - No images found for this image type") + else: + print("Found " + str(len(todownload)) + " images, begining downloads") # download images @@ -53,6 +74,12 @@ def getDayImgs(y, m, d, type="2aia193"): def getImg(url): + """ + Dowloads a single image from the web with a console progress bar + + Args: + url: web url of image to be downloaded + """ file_name = "download/" + url.split('/')[-1] u = urllib2.urlopen(url) f = open(file_name, 'wb') @@ -89,9 +116,9 @@ def getImg(url): nargs='+', help='day in dd format to be downloaded') parser.add_argument( - '-t', '--imgtype', metavar='str', type=str, choices=["2aia193", "2rdf", "2rdf_aia193rdf", "2rdf_cme", "3rdf", "3rdf_cme"], - default="2aia193", nargs='+', help='type of image to retrieve, defaults to 2aia193, first digit denotes which LASCO coronagraph') + '-t', '--imgtype', metavar='str', type=str, choices=imgtypes, + default="2aia193", const="2aia193", nargs='?', help='type of image to retrieve, defaults to 2aia193, first digit denotes which LASCO coronagraph') args = parser.parse_args() - getDayImgs(year, month, day) \ No newline at end of file + getDayImgs(args.year[0], args.month[0], args.day[0], type=args.imgtype) \ No newline at end of file diff --git a/README.md b/README.md index 3429b78..5b31bf7 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ Download all LASCO coronagraph images for a given day Images are collected from the LASCO CME Catalog dataset. Thanks to the Heliophysics Science Division at NASA Goddard Space Flight Center for providing this data to the public. -Created by Rose Awen Brindle, 2022. +Created by Rose Awen Brindle, 2022. Released under GNU GENERAL PUBLIC LICENSE Version 3. -For more information: contactme@rosebrindle.dev +For more information: contactme@rose-brindle.dev ## Usage Call LASCO_imgdownload.py from the command line using the following command line arguments. Downloaded data is placed in the 'download' folder at LASCO_imgdownload.py's location. NOTE you may need to create this folder yourself before use. diff --git a/progressbar.py b/progressbar.py index 98b2022..1349ac0 100644 --- a/progressbar.py +++ b/progressbar.py @@ -1,12 +1,37 @@ +""" + Progress Bar +------------- +Provides console progress bar functionality. + +Usage: + Call start_progress(title) immediatley before action + Call progress(x) after every step + Call end_progress() after action is complete + +Based on code from https://stackoverflow.com/a/6169274 +""" + import sys def start_progress(title): + """ + Prints beggining of a console progress bar. + + Args: + title: string, name of progress bar + """ global progress_x sys.stdout.write(title + ": [" + "-"*40 + "]" + chr(8)*41) sys.stdout.flush() progress_x = 0 def progress(x): + """ + Updates a console progress bar with new progress. + + Args: + x: percentage of total progress made + """ global progress_x x = int(x * 40 // 100) sys.stdout.write("#" * (x - progress_x)) @@ -14,5 +39,6 @@ def progress(x): progress_x = x def end_progress(): + """Prints end of progress bar after action completion""" sys.stdout.write("#" * (40 - progress_x) + "] DONE\n") sys.stdout.flush() \ No newline at end of file