Skip to content

Commit

Permalink
progressbar docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
RosesHaveThorns committed Mar 11, 2022
1 parent dd8a553 commit 0891b78
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.png
*.zip

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
63 changes: 45 additions & 18 deletions LASCO_imgdownload.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,35 +28,44 @@

# 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
page = requests.get(dayURL)
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

Expand All @@ -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')
Expand Down Expand Up @@ -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)
getDayImgs(args.year[0], args.month[0], args.day[0], type=args.imgtype)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions progressbar.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
"""
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))
sys.stdout.flush()
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()

0 comments on commit 0891b78

Please sign in to comment.