def compile_code(): # Remove the existing mfusg directory if it exists if os.path.isdir(mfusgpth): shutil.rmtree(mfusgpth) # Download the MODFLOW-USG distribution url = 'https://water.usgs.gov/ogw/mfusg/mfusg.1_4.zip' pymake.download_and_unzip(url, pth=dstpth) # Remove extraneous source directories dlist = ['zonebudusg', 'serial'] for d in dlist: dname = os.path.join(srcpth, d) if os.path.isdir(dname): print('Removing ', dname) shutil.rmtree(os.path.join(srcpth, d)) # compile MODFLOW-USG pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=False, debug=False) assert os.path.isfile(target), 'Target does not exist.'
def download_dfn(branch, new_dfn_pth): pymake = None try: import pymake except: pass if pymake is None: msg = ( 'Error. The pymake package must be installed in order to ' 'generate the MODFLOW 6 classes. pymake can be installed using ' 'pip install pymake. Stopping.') print(msg) return mf6url = 'https://github.com/MODFLOW-USGS/modflow6/archive/{}.zip' mf6url = mf6url.format(branch) print(' Downloading MODFLOW 6 repository from {}'.format(mf6url)) with tempfile.TemporaryDirectory() as tmpdirname: pymake.download_and_unzip(mf6url, tmpdirname) downloaded_dfn_pth = os.path.join(tmpdirname, 'modflow6-{}'.format(branch)) downloaded_dfn_pth = os.path.join(downloaded_dfn_pth, 'doc', 'mf6io', 'mf6ivar', 'dfn') shutil.copytree(downloaded_dfn_pth, new_dfn_pth) return
def getmfexes(pth='.', version='', pltfrm=None): """ Get the latest MODFLOW binary executables from a github site (https://github.com/MODFLOW-USGS/executables) for the specified operating system and put them in the specified path. Parameters ---------- pth : str Location to put the executables (default is current working directory) version : str Version of the MODFLOW-USGS/executables release to use. pltfrm : str Platform that will run the executables. Valid values include mac, linux, win32 and win64. If platform is None, then routine will download the latest appropriate zipfile from the github repository based on the platform running this script. """ # Determine the platform in order to construct the zip file name pltfrm = get_platform(pltfrm) zipname = '{}.zip'.format(pltfrm) # Determine path for file download and then download and unzip url = ('https://github.com/MODFLOW-USGS/executables/' 'releases/download/{}/'.format(version)) assets = {p: url + p for p in ['mac.zip', 'linux.zip', 'win32.zip', 'win64.zip']} download_url = assets[zipname] pymake.download_and_unzip(download_url, pth) return
def download_mf6_examples(): """ Download mf6 examples and return location of folder """ # set url dirname = "mf6examples" url = "https://github.com/MODFLOW-USGS/modflow6-examples/releases/" + \ "download/current/modflow6-examples.zip" # create folder for mf6 distribution download cpth = os.getcwd() dstpth = os.path.join('temp', dirname) print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) # Download the distribution pymake.download_and_unzip(url, verify=True) # change back to original path os.chdir(cpth) # return the absolute path to the distribution mf6path = os.path.abspath(dstpth) return mf6path
def download_mf6_examples(verbose=False): """Download mf6 examples and return location of folder """ # set url url = "https://github.com/MODFLOW-USGS/modflow6-examples/releases/" + \ "download/current/modflow6-examples.zip" # create folder for mf6 distribution download cpth = os.getcwd() print('create...{}'.format(mf6_exdir)) if os.path.exists(mf6_exdir): shutil.rmtree(mf6_exdir) os.makedirs(mf6_exdir) os.chdir(mf6_exdir) # Download the distribution pymake.download_and_unzip(url, verify=True, verbose=verbose) # change back to original path os.chdir(cpth) # return the absolute path to the distribution mf6path = os.path.abspath(mf6_exdir) return mf6path
def download_mf6_distribution(): """ Download mf6 distribution and return location of folder """ # set url dirname = 'mf6.0.1' url = 'https://water.usgs.gov/ogw/modflow/{0}.zip'.format(dirname) # create folder for mf6 distribution download cpth = os.getcwd() dstpth = os.path.join('temp', 'mf6dist') print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) # Download the distribution pymake.download_and_unzip(url, verify=True) # change back to original path os.chdir(cpth) # return the absolute path to the distribution mf6path = os.path.abspath(os.path.join(dstpth, dirname)) return mf6path
def test_compile_prev(): # Compile reference version of the program from the source. # Remove the existing distribution directory if it exists dir_previous = config.dir_previous if os.path.isdir(dir_previous): print('Removing folder ' + dir_previous) shutil.rmtree(dir_previous) # Setup variables url = config.url_previous srcdir = config.srcdir_previous target = config.target_previous # Download the MODFLOW-2005 distribution pymake.download_and_unzip(url, pth=config.testdir) # compile pymake.main(srcdir, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=False, debug=False, include_subdirs=False) assert os.path.isfile(target), 'Target {} does not exist.'.format(target) return
def compile_code(): # Remove the existing mfusg directory if it exists if os.path.isdir(mp6pth): shutil.rmtree(mp6pth) # Download the MODPATH 6 distribution url = "http://water.usgs.gov/ogw/modpath/modpath.6_0_01.zip" pymake.download_and_unzip(url, pth=dstpth) fname1 = os.path.join(srcpth, 'MP6Flowdata.for') f = open(fname1, 'r') fname2 = os.path.join(srcpth, 'MP6Flowdata_mod.for') f2 = open(fname2, 'w') for line in f: line = line.replace('CD.QX2', 'CD%QX2') f2.write(line) f.close() f2.close() os.remove(fname1) # compile MODPATH 6 pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=False, debug=False) assert os.path.isfile(target), 'Target does not exist.'
def download_mf6_distribution(): """ Download mf6 distribution and return location of folder """ # set url dirname = 'mf6.0.3' url = 'https://water.usgs.gov/ogw/modflow/{0}.zip'.format(dirname) # create folder for mf6 distribution download cpth = os.getcwd() dstpth = os.path.join('temp', 'mf6dist') print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) # Download the distribution pymake.download_and_unzip(url, verify=True) # change back to original path os.chdir(cpth) # return the absolute path to the distribution mf6path = os.path.abspath(os.path.join(dstpth, dirname)) return mf6path
def test_build_triangle(keep=True): if pymake is None: return starget = 'TRIANGLE' exe_name = 'triangle' dirname = 'triangle' url = "http://www.netlib.org/voronoi/{}.zip".format(dirname) print('Determining if {} needs to be built'.format(starget)) if platform.system().lower() == 'windows': exe_name += '.exe' exe_exists = flopy.which(exe_name) if exe_exists is not None and keep: print('No need to build {}'.format(starget) + ' since it exists in the current path') return # get current directory cpth = os.getcwd() # create temporary path dstpth = os.path.join('tempbin', 'triangle') print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) pymake.download_and_unzip(url) srcdir = 'src' os.mkdir(srcdir) shutil.move('triangle.c', 'src/triangle.c') shutil.move('triangle.h', 'src/triangle.h') fct, cct = set_compiler(starget) pymake.main(srcdir, 'triangle', fct, cct) # move the file src = os.path.join('.', exe_name) dst = os.path.join(bindir, exe_name) try: shutil.move(src, dst) except: print('could not move {}'.format(exe_name)) # change back to original path os.chdir(cpth) # Clean up downloaded directory print('delete...{}'.format(dstpth)) if os.path.isdir(dstpth): shutil.rmtree(dstpth) # make sure the gridgen was built msg = '{} does not exist.'.format(os.path.relpath(dst)) assert os.path.isfile(dst), msg return
def compile_code(): # Remove the existing modpath6 directory if it exists if os.path.isdir(mp6pth): shutil.rmtree(mp6pth) # Download the MODPATH 6 distribution url = "https://water.usgs.gov/ogw/modpath/archive/modpath_v6.0.01/modpath.6_0_01.zip" pymake.download_and_unzip(url, pth=dstpth) # start of edit a few files so it can compile with gfortran # file 1 fname1 = os.path.join(srcpth, 'MP6Flowdata.for') f = open(fname1, 'r') bigline = 'CB%BALANCE = ABS(100.0*CB%QRESIDUAL/CB%QAVE)' newline = ' IF (ABS(CB%QAVE) > 0.) THEN\n' + \ ' CB%BALANCE = ABS(100.0*CB%QRESIDUAL/CB%QAVE)\n' + \ ' ELSE\n' + \ ' CB%BALANCE = 0.\n' + \ ' END IF\n' fname2 = os.path.join(srcpth, 'MP6Flowdata_mod.for') f2 = open(fname2, 'w') for line in f: line = line.replace('CD.QX2', 'CD%QX2') if bigline in line: line = newline f2.write(line) f.close() f2.close() os.remove(fname1) # file 2 fname1 = os.path.join(srcpth, 'MP6MPBAS1.for') f = open(fname1, 'r') fname2 = os.path.join(srcpth, 'MP6MPBAS1_mod.for') f2 = open(fname2, 'w') for line in f: line = line.replace('MPBASDAT(IGRID)%NCPPL=NCPPL', 'MPBASDAT(IGRID)%NCPPL=>NCPPL') f2.write(line) f.close() f2.close() os.remove(fname1) # end of edit a few files so it can compile with gfortran # compile MODPATH 6 pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=False, debug=False) assert os.path.isfile(target), 'Target does not exist.'
def getmfexes(pth='.', version='', pltfrm=None): """ Get the latest MODFLOW binary executables from a github site (https://github.com/MODFLOW-USGS/executables) for the specified operating system and put them in the specified path. Parameters ---------- pth : str Location to put the executables (default is current working directory) version : str Version of the MODFLOW-USGS/executables release to use. pltfrm : str Platform that will run the executables. Valid values include mac, linux, win32 and win64. If platform is None, then routine will download the latest appropriate zipfile from the github repository based on the platform running this script. """ # Determine the platform in order to construct the zip file name pltfrm = get_platform(pltfrm) zipname = '{}.zip'.format(pltfrm) # Determine path for file download and then download and unzip url = ('https://github.com/MODFLOW-USGS/executables/' 'releases/download/{}/'.format(version)) assets = { p: url + p for p in ['mac.zip', 'linux.zip', 'win32.zip', 'win64.zip'] } download_url = assets[zipname] pymake.download_and_unzip(download_url, pth) # additional assets if 'win' in pltfrm: zipname = 'pestppwin.zip' else: zipname = 'pestpp{}.zip'.format(pltfrm) url = ('https://github.com/jdhughes-usgs/MM2019_FloPy/' 'releases/download/1/') assets = { p: url + p for p in ['pestppmac.zip', 'pestpplinux.zip', 'pestppwin.zip'] } download_url = assets[zipname] pymake.download_and_unzip(download_url, pth) # check for stray directories listdir = os.listdir() for v in os.listdir(): if os.path.isdir(v): print('removing...{}'.format(v)) shutil.rmtree(v, ignore_errors=True) return
def getmfexes(pth='.', version='', platform=None): """ Get the latest MODFLOW binary executables from a github site (https://github.com/MODFLOW-USGS/executables) for the specified operating system and put them in the specified path. Parameters ---------- pth : str Location to put the executables (default is current working directory) version : str Version of the MODFLOW-USGS/executables release to use. platform : str Platform that will run the executables. Valid values include mac, linux, win32 and win64. If platform is None, then routine will download the latest asset from the github reposity. """ # Determine the platform in order to construct the zip file name if platform is None: if sys.platform.lower() == 'darwin': platform = 'mac' elif sys.platform.lower().startswith('linux'): platform = 'linux' elif 'win' in sys.platform.lower(): is_64bits = sys.maxsize > 2**32 if is_64bits: platform = 'win64' else: platform = 'win32' else: errmsg = ('Could not determine platform' '. sys.platform is {}'.format(sys.platform)) raise Exception(errmsg) else: assert platform in ['mac', 'linux', 'win32', 'win64'] zipname = '{}.zip'.format(platform) # Wanted to use github api, but this is timing out on travis too often #mfexes_repo_name = 'MODFLOW-USGS/executables' # assets = repo_latest_assets(mfexes_repo_name) # Determine path for file download and then download and unzip url = ('https://github.com/MODFLOW-USGS/executables/' 'releases/download/{}/'.format(version)) assets = { p: url + p for p in ['mac.zip', 'linux.zip', 'win32.zip', 'win64.zip'] } download_url = assets[zipname] pymake.download_and_unzip(download_url, pth) return
def _get_previous_version(): version = _get_version() url = "https://github.com/{}".format( github_repo ) + "/releases/download/{0}/mf{0}.zip".format(version) if not _is_dryrun(): pymake.download_and_unzip(url, pth=working_dir, verbose=True, verify=VERIFY) return version, "mf{}".format(version)
def build_target(starget, exe_name, url, dirname, srcname='src', replace_function=None, verify=True, keep=True, dble=dbleprec, include_subdirs=False): print('Determining if {} needs to be built'.format(starget)) if platform.system().lower() == 'windows': exe_name += '.exe' exe_exists = flopy.which(exe_name) if exe_exists is not None and keep: print('No need to build {}'.format(starget) + ' since it exists in the current path') return fct, cct = set_compiler(starget) # set up target target = os.path.abspath(os.path.join(bindir, exe_name)) # get current directory cpth = os.getcwd() # create temporary path dstpth = os.path.join('tempbin') print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) # Download the distribution pymake.download_and_unzip(url, verify=verify) # Set srcdir name srcdir = os.path.join(dirname, srcname) if replace_function is not None: replace_function(srcdir) # compile code print('compiling...{}'.format(os.path.relpath(target))) pymake.main(srcdir, target, fct, cct, makeclean=True, expedite=False, dryrun=False, double=dble, debug=False, include_subdirs=include_subdirs) # change back to original path os.chdir(cpth) # Clean up downloaded directory print('delete...{}'.format(dstpth)) if os.path.isdir(dstpth): shutil.rmtree(dstpth) msg = '{} does not exist.'.format(os.path.relpath(target)) assert os.path.isfile(target), msg return
def test_build_modflow(): starget = 'MODFLOW-2005' fct, cct = set_compiler() # set up target target = os.path.abspath(os.path.join(ebindir, 'mf2005dbl')) target += eext rebuild = rebuild_exe(target, starget) if not rebuild: return # get current directory cpth = os.getcwd() # create temporary path dstpth = os.path.join('tempbin') print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) # Set dir name dirname = mf2005dir srcdir = os.path.join(dirname, 'src') # Download the MODFLOW-2005 distribution url = mf2005url pymake.download_and_unzip(url) # compile code print('compiling...{}'.format(os.path.relpath(target))) pymake.main(srcdir, target, fct, cct, makeclean=True, expedite=False, dryrun=False, double=True, debug=False) msg = '{} does not exist.'.format(os.path.relpath(target)) assert os.path.isfile(target), msg # change back to original path os.chdir(cpth) # Clean up downloaded directory print('delete...{}'.format(dstpth)) if os.path.isdir(dstpth): shutil.rmtree(dstpth) return
def _get_previous_version(): version = _get_version() url = (f"https://github.com/{github_repo}" + f"/releases/download/{version}/mf{version}.zip") if not _is_dryrun(): pymake.download_and_unzip( url, pth=working_dir, verbose=True, verify=VERIFY, ) return version, f"mf{version}"
def compile_code(): # Remove the existing mfusg directory if it exists if os.path.isdir(mflgrpth): shutil.rmtree(mflgrpth) # Download the MODFLOW-LGR distribution url = "http://water.usgs.gov/ogw/modflow-lgr/modflow-lgr-v2.0.0/mflgrv2_0_00.zip" pymake.download_and_unzip(url, pth=dstpth) # compile MODFLOW-LGR pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=False, debug=False) assert os.path.isfile(target), 'Target does not exist.' return
def compile_code(): # Remove the existing swt_v4_00_05 directory if it exists if os.path.isdir(swtpth): shutil.rmtree(swtpth) # Download the SEAWAT distribution url = 'https://water.usgs.gov/ogw/seawat/swt_v4_00_05.zip' pymake.download_and_unzip(url, pth=dstpth) # Remove the parallel and serial folders from the source directory dlist = ['parallel', 'serial'] for d in dlist: dname = os.path.join(srcpth, d) if os.path.isdir(dname): print('Removing ', dname) shutil.rmtree(os.path.join(srcpth, d)) # Replace filespec with standard fortran l = " CHARACTER*20 ACCESS,FORM,ACTION(2)\n" +\ " DATA ACCESS/'STREAM'/\n" +\ " DATA FORM/'UNFORMATTED'/\n" +\ " DATA (ACTION(I),I=1,2)/'READ','READWRITE'/\n" fn = os.path.join(srcpth, 'filespec.inc') f = open(fn, 'w') f.write(l) f.close() # rename all source files to lower case so compilation doesn't # bomb on case-sensitive systems srcfiles = os.listdir(srcpth) for filename in srcfiles: src = os.path.join(srcpth, filename) dst = os.path.join(srcpth, filename.lower()) os.rename(src, dst) # compile seawat pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=True, debug=False, include_subdirs=False) assert os.path.isfile(target) is True, 'Target does not exist.' return
def compile_code(): # Remove the existing mfusg directory if it exists if os.path.isdir(mflgrpth): shutil.rmtree(mflgrpth) # Download the MODFLOW-LGR distribution url = "https://water.usgs.gov/ogw/modflow-lgr/modflow-lgr-v2.0.0/mflgrv2_0_00.zip" pymake.download_and_unzip(url, pth=dstpth) # compile MODFLOW-LGR pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=False, debug=False) assert os.path.isfile(target), 'Target does not exist.' return
def get_modflow_exes(pth='.', version='', platform=None): """ Get the latest MODFLOW binary executables from a github site (https://github.com/MODFLOW-USGS/executables) for the specified operating system and put them in the specified path. Parameters ---------- pth : str Location to put the executables (default is current working directory) version : str Version of the MODFLOW-USGS/executables release to use. platform : str Platform that will run the executables. Valid values include mac, linux, win32 and win64. If platform is None, then routine will download the latest asset from the github reposity. """ # Determine the platform in order to construct the zip file name if platform is None: if sys.platform.lower() == 'darwin': platform = 'mac' elif sys.platform.lower().startswith('linux'): platform = 'linux' elif 'win' in sys.platform.lower(): is_64bits = sys.maxsize > 2 ** 32 if is_64bits: platform = 'win64' else: platform = 'win32' else: errmsg = ('Could not determine platform' '. sys.platform is {}'.format(sys.platform)) raise Exception(errmsg) else: assert platform in ['mac', 'linux', 'win32', 'win64'] zipname = '{}.zip'.format(platform) # Determine path for file download and then download and unzip url = ('https://github.com/MODFLOW-USGS/executables/' 'releases/download/{}/'.format(version)) assets = {p: url + p for p in ['mac.zip', 'linux.zip', 'win32.zip', 'win64.zip']} download_url = assets[zipname] pymake.download_and_unzip(download_url, pth) return
def compile_code(): # Remove the existing mf6 directory if it exists if os.path.isdir(mf6pth): shutil.rmtree(mf6pth) # Download the MODFLOW 6 distribution url = 'https://water.usgs.gov/ogw/modflow/mf6.0.2.zip' pymake.download_and_unzip(url, pth=dstpth) # compile MODFLOW 6 pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=False, debug=False, include_subdirs=True) assert os.path.isfile(target), 'Target does not exist.'
def get_examples(): examples_repo = "MODFLOW-USGS/modflow6-examples" version = pymake.repo_latest_version(github_repo=examples_repo, verify=VERIFY) print(f"current examples version: {version}") url = (f"https://github.com/{examples_repo}" + f"/releases/download/{version}/modflow6-examples.zip") pth = os.path.join(working_dir, examples_dir) if not _is_dryrun(): pymake.download_and_unzip(url, pth=pth, verbose=True, verify=VERIFY) example_files = [] for root, dirs, files in os.walk(pth): fpth = os.path.join(root, "mfsim.nam") if os.path.exists(fpth): example_files.append(os.path.abspath(root)) return sorted(example_files)
def modflow_lib_path(tmp_path_factory): tmp_path = tmp_path_factory.getbasetemp() url = "https://github.com/MODFLOW-USGS/modflow6-nightly-build/releases/latest/download/" sysinfo = platform.system() if sysinfo == "Windows": url += "win64.zip" lib_path = tmp_path / "libmf6.dll" elif sysinfo == "Linux": url += "linux.zip" lib_path = tmp_path / "libmf6.so" elif sysinfo == "Darwin": url += "mac.zip" lib_path = tmp_path / "libmf6.dylib" else: raise RuntimeError(f"system not supported: {sysinfo}") pymake.download_and_unzip(url=url, pth=str(tmp_path)) return str(lib_path)
def compile_code(): # Remove the existing swt_v4_00_05 directory if it exists if os.path.isdir(swtpth): shutil.rmtree(swtpth) # Download the SEAWAT distribution url = 'http://water.usgs.gov/ogw/seawat/swt_v4_00_05.zip' pymake.download_and_unzip(url, pth=dstpth) # Remove the parallel and serial folders from the source directory dlist = ['parallel', 'serial'] for d in dlist: dname = os.path.join(srcpth, d) if os.path.isdir(dname): print('Removing ', dname) shutil.rmtree(os.path.join(srcpth, d)) # Replace filespec with standard fortran l = " CHARACTER*20 ACCESS,FORM,ACTION(2)\n" +\ " DATA ACCESS/'STREAM'/\n" +\ " DATA FORM/'UNFORMATTED'/\n" +\ " DATA (ACTION(I),I=1,2)/'READ','READWRITE'/\n" fn = os.path.join(srcpth, 'filespec.inc') f = open(fn, 'w') f.write(l) f.close() # rename all source files to lower case so compilation doesn't # bomb on case-sensitive systems srcfiles = os.listdir(srcpth) for filename in srcfiles: src = os.path.join(srcpth, filename) dst = os.path.join(srcpth, filename.lower()) os.rename(src, dst) # compile seawat pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=True, debug=False, include_subdirs=False) assert os.path.isfile(target) is True, 'Target does not exist.' return
def compile_code(): # Remove the existing mfusg directory if it exists if os.path.isdir(mfusgpth): shutil.rmtree(mfusgpth) # Download the MODFLOW-USG distribution url = 'http://water.usgs.gov/ogw/mfusg/mfusg.1_3.zip' pymake.download_and_unzip(url, pth=dstpth) # Remove extraneous source directories dlist = ['zonebudusg', 'serial'] for d in dlist: dname = os.path.join(srcpth, d) if os.path.isdir(dname): print('Removing ', dname) shutil.rmtree(os.path.join(srcpth, d)) # compile MODFLOW-USG pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=False, debug=False) assert os.path.isfile(target), 'Target does not exist.'
def download_dfn(branch, new_dfn_pth): pymake = None try: import pymake except: pass if pymake is None: msg = ('Error. The pymake package must be installed in order to ' 'generate the MODFLOW 6 classes. pymake can be installed using ' 'pip install pymake. Stopping.') print(msg) return mf6url = 'https://github.com/MODFLOW-USGS/modflow6/archive/{}.zip' mf6url = mf6url.format(branch) print(' Downloading MODFLOW 6 repository from {}'.format(mf6url)) with tempfile.TemporaryDirectory() as tmpdirname: pymake.download_and_unzip(mf6url, tmpdirname) downloaded_dfn_pth = os.path.join(tmpdirname, 'modflow6-{}'.format(branch)) downloaded_dfn_pth = os.path.join(downloaded_dfn_pth, 'doc', 'mf6io', 'mf6ivar', 'dfn') shutil.copytree(downloaded_dfn_pth, new_dfn_pth) return
def compile_code(): # Remove the existing modpath6 directory if it exists if os.path.isdir(mp6pth): shutil.rmtree(mp6pth) # Download the MODFLOW-2005 distribution url = "https://water.usgs.gov/ogw/modpath/Modpath_7_1_000.zip" pymake.download_and_unzip(url, pth=dstpth) # modify source files that prevent compiling with gfortran pth = os.path.join(srcpth, 'utl7u1.f') if os.path.isfile(pth): os.remove(pth) fname1 = os.path.join(srcpth, 'ModpathSubCellData.f90') f = open(fname1, 'r') fname2 = os.path.join(srcpth, 'ModpathSubCellData_mod.f90') f2 = open(fname2, 'w') for line in f: line = line.replace('location.', 'location%') f2.write(line) f.close() f2.close() os.remove(fname1) os.rename(fname2, fname1) fname1 = os.path.join(srcpth, 'ModpathCellData.f90') f = open(fname1, 'r') fname2 = os.path.join(srcpth, 'ModpathCellData_mod.f90') f2 = open(fname2, 'w') for line in f: line = line.replace('dimension(grid%GetCellCount())', 'dimension(:)') line = line.replace('dimension(grid%GetReducedConnectionCount())', 'dimension(:)') f2.write(line) f.close() f2.close() os.remove(fname1) os.rename(fname2, fname1) fname1 = os.path.join(srcpth, 'MPath7.f90') f = open(fname1, 'r') fname2 = os.path.join(srcpth, 'MPath7_mod.f90') f2 = open(fname2, 'w') for line in f: line = line.replace("form='binary', access='stream'", "form='unformatted', access='stream'") f2.write(line) f.close() f2.close() os.remove(fname1) os.rename(fname2, fname1) # allow line lengths greater than 132 columns fflags = 'ffree-line-length-512' # make modpath 7 pymake.main(srcpth, target, 'gfortran', 'gcc', makeclean=True, expedite=False, dryrun=False, double=False, debug=False, fflags=fflags) assert os.path.isfile(target), 'Target does not exist.'
import numpy as np import matplotlib as mpl import flopy import pymake # Print python package versions flopypth = flopy.__path__[0] pymakepth = pymake.__path__[0] print("python version: {}".format(sys.version)) print("numpy version: {}".format(np.__version__)) print("matplotlib version: {}".format(mpl.__version__)) print("flopy version: {}".format(flopy.__version__)) print("pymake version: {}".format(pymake.__version__)) print("") print("flopy is installed in: {}".format(flopypth)) print("pymake is installed in: {}".format(pymakepth)) # Update flopy from GitHub repo flopy.mf6.utils.generate_classes(branch="develop", backup=False) # Get executables bin_pth = os.path.join("..", "bin") if not os.path.isdir(bin_pth): os.makedirs(bin_pth) pymake.getmfexes(bin_pth, verbose=True) # Replace MODFLOW 6 executables with the latest versions url = pymake.get_repo_assets( "MODFLOW-USGS/modflow6-nightly-build")["linux.zip"] pymake.download_and_unzip(url, bin_pth, verbose=True)
def build_target(starget, exe_name, url, dirname, srcname='src', replace_function=None, verify=True, keep=True, dble=dbleprec, include_subdirs=False): print('Determining if {} needs to be built'.format(starget)) if platform.system().lower() == 'windows': exe_name += '.exe' exe_exists = flopy.which(exe_name) if exe_exists is not None and keep: print('No need to build {}'.format(starget) + ' since it exists in the current path') return fct, cct = set_compiler(starget) # set up target target = os.path.abspath(os.path.join(bindir, exe_name)) # get current directory cpth = os.getcwd() # create temporary path dstpth = os.path.join('tempbin') print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) # Download the distribution pymake.download_and_unzip(url, verify=verify) # Set srcdir name srcdir = os.path.join(dirname, srcname) if replace_function is not None: replace_function(srcdir) # compile code print('compiling...{}'.format(os.path.relpath(target))) pymake.main(srcdir, target, fct, cct, makeclean=True, expedite=False, dryrun=False, double=dble, debug=False, include_subdirs=include_subdirs) # change back to original path os.chdir(cpth) msg = '{} does not exist.'.format(os.path.relpath(target)) assert os.path.isfile(target), msg # Clean up downloaded directory print('delete...{}'.format(dstpth)) if os.path.isdir(dstpth): shutil.rmtree(dstpth) return
def test_build_gridgen(keep=True): if pymake is None: return starget = 'GRIDGEN' exe_name = 'gridgen' dirname = 'gridgen.1.0.02' url = "https://water.usgs.gov/ogw/gridgen/{}.zip".format(dirname) print('Determining if {} needs to be built'.format(starget)) if platform.system().lower() == 'windows': exe_name += '.exe' exe_exists = flopy.which(exe_name) if exe_exists is not None and keep: print('No need to build {}'.format(starget) + ' since it exists in the current path') return # get current directory cpth = os.getcwd() # create temporary path dstpth = os.path.join('tempbin') print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) pymake.download_and_unzip(url) # clean print('Cleaning...{}'.format(exe_name)) apth = os.path.join(dirname, 'src') cmdlist = ['make', 'clean'] run_cmdlist(cmdlist, apth) # build with make print('Building...{}'.format(exe_name)) apth = os.path.join(dirname, 'src') cmdlist = ['make', exe_name] run_cmdlist(cmdlist, apth) # move the file src = os.path.join(apth, exe_name) dst = os.path.join(bindir, exe_name) try: shutil.move(src, dst) except: print('could not move {}'.format(exe_name)) # change back to original path os.chdir(cpth) # Clean up downloaded directory print('delete...{}'.format(dstpth)) if os.path.isdir(dstpth): shutil.rmtree(dstpth) # make sure the gridgen was built msg = '{} does not exist.'.format(os.path.relpath(dst)) assert os.path.isfile(dst), msg return
def run_mfusg(regression=True): """ Download run and compare biscayne quadtree problem """ # Set the testname testname = '02_quadtree' testpth = os.path.join(config.testdir, testname) # Delete folder if exists, then download the MODFLOW-USG distribution if os.path.isdir(testpth): shutil.rmtree(testpth) url = "http://water.usgs.gov/ogw/mfusg/02_quadtree.zip" pymake.download_and_unzip(url, pth=config.testdir) # Go through name file and replace \ with / oldnam = os.path.join(testpth, 'biscayne.nam') newnam = os.path.join(testpth, 'biscayne.new.nam') fold = open(oldnam, 'r') fnew = open(newnam, 'w') for i, line in enumerate(fold): line = line.replace('\\', '/') line = line.replace('biscayne.disu', 'biscayne.new.disu') line = line.replace('biscayne.oc', 'biscayne.new.oc') fnew.write(line) fold.close() fnew.close() # Change the number of stress periods from 1000 to 10 olddis = os.path.join(testpth, 'input', 'biscayne.disu') newdis = os.path.join(testpth, 'input', 'biscayne.new.disu') fold = open(olddis, 'r') fnew = open(newdis, 'w') for i, line in enumerate(fold): if i == 1: line = line.replace('1000', '10') fnew.write(line) fold.close() fnew.close() # Write oc so that head and budget are always saved. newoc = os.path.join(testpth, 'input', 'biscayne.new.oc') fnew = open(newoc, 'w') fnew.write('HEAD SAVE UNIT 51' + '\n') fnew.write('COMPACT BUDGET' + '\n') for kper in range(10): fnew.write('PERIOD {} STEP 1'.format(kper + 1) + '\n') fnew.write(' PRINT BUDGET' + '\n') fnew.write(' SAVE BUDGET' + '\n') fnew.write(' SAVE HEAD' + '\n') fnew.close() # run test models print('running model...{}'.format(testname)) nam = 'biscayne.new.nam' exe_name = os.path.abspath(config.target) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=True) assert success, 'biscayne model did not run successfully.' # If it is a regression run, then setup and run the model with the # release target if regression: testname_reg = testname + '_reg' testpth_reg = os.path.join(config.testdir, testname_reg) pymake.setup(os.path.join(testpth, nam), testpth_reg) outdir = os.path.join(testpth_reg, 'output') if not os.path.isdir(outdir): os.mkdir(outdir) print('running regression model...{}'.format(testname_reg)) exe_name = os.path.abspath(config.target_release) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth_reg, silent=True) # Make comparison success = compare(os.path.join(testpth, nam), os.path.join(testpth_reg, nam)) assert success, 'Biscayne regression test did not pass' # Clean things up if success and not config.retain: pymake.teardown(testpth) if regression: pymake.teardown(testpth_reg) return
def test_build_modflow(): fct = fc cct = cc starget = 'MODFLOW-2005' # parse command line arguments to see if user specified options # relative to building the target msg = '' for idx, arg in enumerate(sys.argv): if arg.lower() == '--ifort': if len(msg) > 0: msg += '\n' msg += '{} - '.format(arg.lower()) + \ '{} will be built with ifort.'.format(starget) fct = 'ifort' elif arg.lower() == '--cl': if len(msg) > 0: msg += '\n' msg += '{} - '.format(arg.lower()) + \ '{} will be built with cl.'.format(starget) cct = 'cl' elif arg.lower() == '--clang': if len(msg) > 0: msg += '\n' msg += '{} - '.format(arg.lower()) + \ '{} will be built with clang.'.format(starget) cct = 'clang' # set up target target = os.path.abspath(os.path.join('..', 'bin', 'mf2005dbl')) # get current directory cpth = os.getcwd() # create temporary path dstpth = os.path.join('temp') print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) # Download the MODFLOW-2005 distribution url = "https://water.usgs.gov/ogw/modflow/MODFLOW-2005_v1.12.00/MF2005.1_12u.zip" pymake.download_and_unzip(url) # Set dir name dirname = 'MF2005.1_12u' srcdir = os.path.join(dirname, 'src') # compile code print('compiling...{}'.format(os.path.relpath(target))) pymake.main(srcdir, target, fct, cct, makeclean=True, expedite=False, dryrun=False, double=True, debug=False) msg = '{} does not exist.'.format(os.path.relpath(target)) assert os.path.isfile(target), msg # change back to original path os.chdir(cpth) # Clean up downloaded directory print('delete...{}'.format(dstpth)) if os.path.isdir(dstpth): shutil.rmtree(dstpth) return
print("Copying documentation") for din, dout in doclist: dst = os.path.join(fd["doc"], dout) print(" copying {} ===> {}".format(din, dst)) shutil.copy(din, dst) print("\n") print("Downloading published reports for inclusion in distribution") for url in [ "https://pubs.usgs.gov/tm/06/a57/tm6a57.pdf", "https://pubs.usgs.gov/tm/06/a55/tm6a55.pdf", "https://pubs.usgs.gov/tm/06/a56/tm6a56.pdf", ]: print(" downloading {}".format(url)) download_and_unzip(url, pth=fd["doc"], delete_zip=False, verify=False) print("\n") # Prior to zipping, enforce os line endings on all text files windows_line_endings = True convert_line_endings(distfolder, windows_line_endings) # Zip the distribution uflag = "u" if win_target_os: uflag = "" zipname = version + uflag + ".zip" if os.path.exists(zipname): print("Removing existing file: {}".format(zipname)) os.remove(zipname) print("Creating zipped file: {}".format(zipname))
# Get executables pth = os.path.join("bin") if not os.path.isdir(pth): os.makedirs(pth) pymake.getmfexes(pth, verbose=True) # Replace MODFLOW 6 executables with the latest versions osname = sys.platform.lower() if osname == "win32": key = "win64.zip" elif osname == "darwin": key = "mac.zip" elif osname == "linux": key = "linux.zip" url = pymake.get_repo_assets("MODFLOW-USGS/modflow6-nightly-build")[key] pymake.download_and_unzip(url, "bin", verbose=True) # local copy (put your own path here) pth = "/Users/jdhughes/Documents/Development/modflow6_git/modflow6_fork/bin/" files = [ file_name for file_name in os.listdir(pth) if not file_name.endswith(".exe") ] for f in files: src = os.path.join(pth, f) dst = os.path.join("bin", f) shutil.copyfile(src, dst) # clean up examples directory - just for local runs expth = os.path.join("examples") examples = [ os.path.join(expth, d) for d in os.listdir(expth)
def build_target(starget, exe_name, url, dirname, srcname='src', replace_function=None, verify=True, keep=True, dble=None, include_subdirs=False): print('Determining if {} needs to be built'.format(starget)) # update exe_name, if necessary, and set double (dble) flag if dble is None: dble, exe_name = set_dbl(exe_name) if platform.system().lower() == 'windows': exe_name += '.exe' # set compiler fct, cct = set_compiler(starget) # set up architecture arch = set_arch() # set up target target = os.path.abspath(os.path.join(bindir, exe_name)) # test if executable exists epth = exe_name for idx, arg in enumerate(sys.argv): if '--root' in arg.lower() or '--appdir' in arg.lower(): epth = target exe_exists = flopy.which(epth) if exe_exists is not None and keep: print('No need to build {}'.format(starget) + ' since it exists in the current path') return # get current directory cpth = os.getcwd() # create temporary path dstpth = os.path.join('tempbin') print('create...{}'.format(dstpth)) if not os.path.exists(dstpth): os.makedirs(dstpth) os.chdir(dstpth) # Download the distribution pymake.download_and_unzip(url, verify=verify) # Set srcdir name srcdir = os.path.join(dirname, srcname) if replace_function is not None: replace_function(srcdir) # compile code print('compiling...{}'.format(os.path.relpath(target))) pymake.main(srcdir, target, fct, cct, makeclean=True, expedite=False, dryrun=False, double=dble, debug=False, include_subdirs=include_subdirs, arch=arch) # change back to original path os.chdir(cpth) # Clean up downloaded directory print('delete...{}'.format(dstpth)) if os.path.isdir(dstpth): try: shutil.rmtree(dstpth) except: pass msg = '{} does not exist.'.format(os.path.relpath(target)) assert os.path.isfile(target), msg return