-
Notifications
You must be signed in to change notification settings - Fork 256
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
Ref/remote download #598
Ref/remote download #598
Changes from 2 commits
97e4488
102d76f
e5c149f
9d4172e
88348c2
cc27951
2818916
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from SimPEG import Mesh | ||
import time as tm | ||
import re | ||
import warnings | ||
|
||
|
||
def read_GOCAD_ts(tsfile): | ||
|
@@ -146,35 +147,72 @@ def surface2inds(vrtx, trgl, mesh, boundaries=True, internal=True): | |
return insideGrid | ||
|
||
|
||
def remoteDownload(url, remoteFiles, basePath=None): | ||
def remoteDownload( | ||
url, remoteFiles, path='.', directory='SimPEGtemp', rm_previous=False | ||
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. |
||
): | ||
""" | ||
Function to download all files stored in a cloud directory | ||
var: url ("http:\\...") | ||
list: List of file names to download | ||
|
||
:param str url: url of the storage bucket ("http://...") | ||
:param list remoteFiles: List of file names to download from the storate bucket | ||
:param str path: path to where the directory is created and files downloaded (default is the current directory) | ||
:param str directory: name of the directory to be created and have content downloaded to | ||
:param bool rm_previous: remove file and contents if a directory with the specified name already exists | ||
""" | ||
|
||
# Download from cloud | ||
import urllib | ||
import shutil | ||
import os | ||
import sys | ||
|
||
def rename_path(downloadpath): | ||
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. if we start to make heavy use of this function, I can see a number of scenarios where we do not necessarily want to over-write the contents of |
||
splitfullpath = downloadpath.split(os.path.sep) | ||
curdir = splitfullpath[-1] | ||
splitdir = curdir.split('(') | ||
rootdir = splitdir[0] | ||
|
||
# add (num) to the end of the filename | ||
if len(splitdir) == 1: | ||
num = 1 | ||
else: | ||
num = int(splitdir[-1][:-1]) | ||
num += 1 | ||
|
||
return os.path.sep.join( | ||
splitfullpath[:-1] + [rootdir + '({})'.format(num)] | ||
) | ||
|
||
# grab the correct url retriever | ||
if sys.version_info < (3,): | ||
urlretrieve = urllib.urlretrieve | ||
else: | ||
urlretrieve = urllib.request.urlretrieve | ||
|
||
if basePath is None: | ||
basePath = os.curdir+os.path.sep+'SimPEGtemp'+os.path.sep | ||
|
||
if os.path.exists(basePath): | ||
shutil.rmtree(basePath) | ||
|
||
os.makedirs(basePath) | ||
|
||
print("Download files from URL...") | ||
# ensure we are working with absolute paths | ||
path = os.path.abspath(path) | ||
downloadpath = os.path.sep.join([path]+[directory]) | ||
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.
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. Yeah, this is likely confusing. Thoughts on alternatives? |
||
# check if the directory already exists | ||
while os.path.exists(downloadpath): | ||
if rm_previous is True: | ||
print("removing previous contents of {}".format(downloadpath)) | ||
shutil.rmtree(downloadpath) | ||
elif rm_previous is False: | ||
downloadpath = rename_path(downloadpath) | ||
print( | ||
"directory already exists, new downloads will be in {}".format( | ||
downloadpath | ||
) | ||
) | ||
|
||
# create the directory | ||
os.makedirs(downloadpath+os.path.sep) | ||
|
||
# download files | ||
print("Download files from {}...".format(url)) | ||
for file in remoteFiles: | ||
print("Retrieving: " + file) | ||
urlretrieve(url + file, basePath+file) | ||
print(" Retrieving: " + file) | ||
urlretrieve(url + file, os.path.sep.join([downloadpath]+[file])) | ||
|
||
print("Download completed!") | ||
return basePath | ||
return downloadpath |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,14 +25,18 @@ def run(plotIt=True, cleanAfterRun=True): | |
|
||
# Start by downloading files from the remote repository | ||
url = "https://storage.googleapis.com/simpeg/Chile_GRAV_4_Miller/" | ||
cloudfiles = ['LdM_grav_obs.grv', 'LdM_mesh.mesh', | ||
'LdM_topo.topo', 'LdM_input_file.inp'] | ||
|
||
basePath = os.path.sep.join(os.path.abspath(os.getenv('HOME')).split | ||
(os.path.sep)+['Downloads']+['SimPEGtemp']) | ||
basePath = os.path.abspath(remoteDownload(url, | ||
cloudfiles, | ||
basePath=basePath+os.path.sep)) | ||
cloudfiles = [ | ||
'LdM_grav_obs.grv', 'LdM_mesh.mesh', | ||
'LdM_topo.topo', 'LdM_input_file.inp' | ||
] | ||
|
||
# Download to Downloads/SimPEGtemp | ||
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. Here we are not specifying where things get put? I think they should just go directly into Can we just rename this to file_names = download([url1, url2], folder='~/Downloads/mag_stuff', overwrite=True)
# or
file_name = download(url1)
# where
assert isinstance(file_names, list)
assert len(file_names) == 2
assert isinstance(file_name, str) I think that the default should the the pwd of where the python file is run from, appending a 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. This also would mean that you don't have to reconstruct the file name after the fact, you just loop through the list: with open(file_name, 'r') as f:
f.read()
with open(file_names[0], 'r') as f2:
f2.read() 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.
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. I think that it could be both? You can call folder='here'
folder='or/here'
folder='/Users/rowan/downloads/or/even/here' I see all of these as folders? 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. ok, fair enough, folder can work |
||
basePath = remoteDownload( | ||
url, cloudfiles, | ||
path=os.path.sep.join([os.getenv('HOME'), 'Downloads']), | ||
rm_previous=True | ||
) | ||
|
||
input_file = basePath + os.path.sep + 'LdM_input_file.inp' | ||
# %% User input | ||
# Plotting parameters, max and min densities in g/cc | ||
|
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.
this is just white-space