Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ChunHuangPhy/CompactOject
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunHuangPhy committed Nov 8, 2024
2 parents a280149 + acb842f commit 80f6563
Show file tree
Hide file tree
Showing 5 changed files with 1,002 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__/
.DS_Store
.DS_Store
downloads/
5 changes: 3 additions & 2 deletions EOSgenerators/Compose_eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
lalpconvert = 755397844024.145 # pressure
laleconvert = 755407089027.154 # energy density

def read_compose(eosdir="./filesCompose/", eos_prefix="eos",nptsmin = 100):
def read_compose(eosdir="./filesCompose", eos_prefix="/eos",nptsmin = 100,eosname=None):

"""
Routine which reads the data from the original EoS model, Compose format is assumed
Expand Down Expand Up @@ -122,7 +122,8 @@ def read_compose(eosdir="./filesCompose/", eos_prefix="eos",nptsmin = 100):
p_compose[i]=float(tmp[3])*nB_compose[i] # pressure
eps_compose[i]=(float(tmp[9])+ 1.0)*m_n*nB_compose[i] # energy density

eosname = read_README(eosdir)
if eosname is None:
eosname = read_README(eosdir)
eps_gcm3 = eps_compose*MeVfm3_to_gcm3
p_dyncm2 = p_compose*MeVfm3_to_dyncm2

Expand Down
79 changes: 79 additions & 0 deletions EOSgenerators/download_compose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import requests
import re
import shutil
from lxml import etree
from pathlib import Path


class DownloadCompose:
table_link = "https://compose.obspm.fr/table"
base_link = "https://compose.obspm.fr"
target_files = ("eos.t", "eos.nb", "eos.thermo")

def __init__(self, download_dir=Path("./downloads/compose")):
self.download_dir = download_dir
self.title_and_link = DownloadCompose.get_title_and_link()

def get_title_and_link():
print(f"DownloadCompose: Fetching data from {DownloadCompose.table_link} \n...")
r = requests.get(DownloadCompose.table_link)
# print(f"{r.status_code=}")
# print(f"{r.encoding=}")
tree = etree.HTML(r.text)
odd_line = tree.xpath("//tr[@class='odd']")
even_line = tree.xpath("//tr[@class='even']")
all_line = odd_line + even_line
print(
f"DownloadCompose: Find {len(all_line)} EOS data on website {DownloadCompose.table_link}"
)
ret = {}
for line in all_line:
title = line.xpath("td[2]/text()")[0].strip()
link = line.xpath(".//@href")[0]
id = int(re.search(r"\d+", link).group())
if id in ret:
raise ValueError(f"Duplicate id={id}")
ret[id] = (title, link)
return dict(sorted(ret.items()))

def print_eos_list(self):
for id, (title, _) in self.title_and_link.items():
print(f"id = {id:3}, name = {title}")

def eos_name(self, id: int):
return self.title_and_link[id][0]

def eos_download_dir(self, id: int):
dir = self.download_dir / f"{id:03}"
dir.mkdir(parents=True, exist_ok=True)
return dir

def requests_download(url: str, folder, force=False):
local_filename = Path(folder) / url.split("/")[-1]
if local_filename.exists() and not force:
print(f"{local_filename} already exists, skip download {url}")
return

with requests.get(url, stream=True, allow_redirects=True) as r:
with open(local_filename, "wb") as f:
shutil.copyfileobj(r.raw, f)
print(f"Downloaded {url} to {local_filename}")

def download_id(self, id: int, force=False):
_, tail_link = self.title_and_link[id]
eos_link = DownloadCompose.base_link + tail_link
r = requests.get(eos_link)
tree = etree.HTML(r.text)
hrefs = tree.xpath("//a/@href")
# print(f"{hrefs=}")
target_hrefs = [
href for href in hrefs if href.endswith(DownloadCompose.target_files)
]
# print(f"{target_hrefs=}")
for href in target_hrefs:
url = DownloadCompose.base_link + href
DownloadCompose.requests_download(
url,
self.eos_download_dir(id),
force=force,
)
Loading

0 comments on commit 80f6563

Please sign in to comment.