-
Notifications
You must be signed in to change notification settings - Fork 12
Improve portability w/ hdw file download #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Wtristen
wants to merge
2
commits into
SuperDARN:develop
Choose a base branch
from
Wtristen:hdw-refactor
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -26,8 +26,11 @@ | |||||
| from typing import NamedTuple | ||||||
| from enum import Enum | ||||||
| from datetime import datetime | ||||||
| from subprocess import check_call | ||||||
| #from subprocess import check_call | ||||||
|
|
||||||
| #new imports | ||||||
| from urllib import request | ||||||
| from zipfile import ZipFile | ||||||
|
|
||||||
| def get_hdw_files(force: bool = True, version: str = None): | ||||||
| """ | ||||||
|
|
@@ -46,35 +49,42 @@ def get_hdw_files(force: bool = True, version: str = None): | |||||
| have yet to be versioned. | ||||||
| """ | ||||||
|
|
||||||
| # Path should the path where pydarn is installed | ||||||
| hdw_path = "{}/hdw/".format(os.path.dirname(pydarn.utils.__file__)) | ||||||
|
|
||||||
| # Path should the path where pydarn utils are located, joined with 'hdw'. | ||||||
| hdw_path = os.path.join(os.path.dirname(pydarn.utils.__file__), "hdw") | ||||||
| if not os.path.exists(hdw_path): | ||||||
| os.makedirs(hdw_path) | ||||||
|
|
||||||
| # TODO: implement when DSWG starts versioning hardware files | ||||||
| if version is not None: | ||||||
| raise Exception("This feature is not implemented yet") | ||||||
| raise NotImplementedError("This feature is not implemented, Versioned hardware does not exist yet.") #clarified error | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| # if there is no files in hdw folder or force is true | ||||||
| # download the hdw files | ||||||
| if len(os.listdir(hdw_path)) == 0 or force: | ||||||
| # pycurl doesn't download a zip folder easily so | ||||||
| # use the command line command | ||||||
| check_call(['curl', '-L', '-o', hdw_path+'/main.zip', | ||||||
| 'https://github.com/SuperDARN/hdw/archive/main.zip']) | ||||||
| # use unzip command because zipfile on works with files and not folders | ||||||
| # though this is possible with zipfile but this was easier for me to | ||||||
| # get it working | ||||||
| check_call(['unzip', '-d', hdw_path, hdw_path+'/main.zip']) | ||||||
| dat_files = glob.glob(hdw_path+'/hdw-main/*') | ||||||
| # shutil only moves specific files so we need to move | ||||||
| # everything one at a time | ||||||
| for hdw_file in dat_files: | ||||||
| shutil.move(hdw_file, hdw_path+os.path.basename(hdw_file)) | ||||||
| # delete the empty folder | ||||||
| os.removedirs(hdw_path+'/hdw-main/') | ||||||
|
|
||||||
| # Define URL and temporary file paths | ||||||
| url = 'https://github.com/SuperDARN/hdw/archive/main.zip' | ||||||
| zip_path = os.path.join(hdw_path, 'main.zip') | ||||||
| unzip_dir_name = 'hdw-main' # The unzipped folder will have this name | ||||||
| unzip_dir_path = os.path.join(hdw_path, unzip_dir_name) | ||||||
| try: | ||||||
| # Download the file using Python's urllib | ||||||
| request.urlretrieve(url, zip_path) | ||||||
|
|
||||||
| # Extract the zip file using Python's zipfile module | ||||||
| with ZipFile(zip_path, 'r') as zip_ref: | ||||||
| zip_ref.extractall(hdw_path) | ||||||
|
|
||||||
| # Move files from the subdirectory to the parent hdw_path | ||||||
| source_dir = os.path.join(hdw_path, unzip_dir_name) | ||||||
| files_to_move = glob.glob(os.path.join(source_dir, '*')) | ||||||
| for file_path in files_to_move: | ||||||
| shutil.move(file_path, hdw_path) | ||||||
| finally: | ||||||
| # Clean up the downloaded zip file and the empty subdirectory | ||||||
| if os.path.exists(zip_path): | ||||||
| os.remove(zip_path) | ||||||
| if os.path.exists(unzip_dir_path): | ||||||
| shutil.rmtree(unzip_dir_path, ignore_errors=True) | ||||||
|
|
||||||
| def read_hdw_file(abbrv, date: datetime = None, update: bool = False): | ||||||
| """ | ||||||
|
|
@@ -250,7 +260,6 @@ def read_hdw_file(abbrv, date: datetime = None, update: bool = False): | |||||
| except FileNotFoundError: | ||||||
| raise pydarn.radar_exceptions.HardwareFileNotFoundError(abbrv) | ||||||
|
|
||||||
|
|
||||||
| class Hemisphere(Enum): | ||||||
| """ | ||||||
| Class used to denote which hemisphere a radar is located in | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from datetime import datetime | ||
|
|
||
| # This will fail on the old code if 'curl' or 'unzip' are not installed like as default on Windows. | ||
| from pydarn.utils import superdarn_radars | ||
|
|
||
| # It should succeed with the new code, and the following should download and extract the files automatically. | ||
| print("--- Testing automatic download and read for 'wal' radar ---") | ||
| try: | ||
| # Use a recent date | ||
| wal_hdw = superdarn_radars.read_hdw_file('wal', date=datetime(2023, 1, 1)) | ||
| print(f" Station ID: {wal_hdw.stid}") #some test output | ||
| except Exception as e: | ||
| print(f"An error occurred: {e}") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.