Skip to content

Commit

Permalink
Merge pull request #6 from PrincetonUniversity/lint_deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
changgoo authored Sep 18, 2024
2 parents 9e57031 + 44271fa commit 6e5273c
Show file tree
Hide file tree
Showing 14 changed files with 903 additions and 583 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.3
rev: v0.6.5
hooks:
- id: ruff
types_or: [python, pyi, jupyter]
Expand Down
2 changes: 2 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[lint]
ignore = ["E741"]
847 changes: 517 additions & 330 deletions astro_tigress/athena_read.py

Large diffs are not rendered by default.

35 changes: 19 additions & 16 deletions astro_tigress/map_circular_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,60 @@
import math
from scipy.integrate import quad


def get_gauss_stamp(n):
nx = n
ny = n
nxc = nx/2
nyc = ny/2
nxc = nx / 2
nyc = ny / 2
ix = np.zeros((nx, ny))
iy = np.zeros((nx, ny))
for i in range(nx):
for j in range(ny):
ix[i, j] = j-nyc
iy[i, j] = i-nxc
ix[i, j] = j - nyc
iy[i, j] = i - nxc

def gauss(x):
sigmax = 0.5
ret = 1./(np.sqrt(2*math.pi)*sigmax) * np.exp(-0.5*(x/sigmax)**2)
ret = 1.0 / (np.sqrt(2 * math.pi) * sigmax) * np.exp(-0.5 * (x / sigmax) ** 2)
return ret

stamp = np.zeros((nx, ny))
for i in range(nx):
for j in range(ny):
x1 = ix[i, j] - 0.5
x2 = x1 + 1.
x2 = x1 + 1.0
y1 = iy[i, j] - 0.5
y2 = y1 + 1.
y2 = y1 + 1.0
inte = quad(gauss, x1, x2)[0] * quad(gauss, y1, y2)[0]
stamp[i, j] = inte
return stamp

#average data using a stamp

# average data using a stamp
def stamp_avg(data, stamp):
nxd, nyd = data.shape
nxs, nys = stamp.shape
ret = np.zeros(data.shape)
nxc = nxs/2
nyc = nys/2
nxc = nxs / 2
nyc = nys / 2
ix = np.zeros((nxs, nys))
iy = np.zeros((nxs, nys))
for i in range(nxs):
for j in range(nys):
ix[i, j] = i-nxc
iy[i, j] = j-nyc
ix[i, j] = i - nxc
iy[i, j] = j - nyc
for i in range(nxd):
for j in range(nyd):
for istamp in range(nxs):
for jstamp in range(nys):
iret = i + ix[istamp, jstamp]
jret = j + iy[istamp, jstamp]
if (iret >= 0) and (iret < nxd
) and (jret >= 0) and (jret < nyd):
ret[iret, jret] += data[i, j]*stamp[istamp, jstamp]
if (iret >= 0) and (iret < nxd) and (jret >= 0) and (jret < nyd):
ret[iret, jret] += data[i, j] * stamp[istamp, jstamp]
return ret


def map_circular_beam(data, nstamp=9):
stamp = get_gauss_stamp(nstamp)
return stamp_avg(data, stamp)

6 changes: 2 additions & 4 deletions astro_tigress/tigress_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ def _download_file(self, f):

target = osp.join(self.dir_master, f)
if osp.isfile(target):
print(
"{} ({:.5f}GB) already exists".format(f, osp.getsize(target) / 2**30)
)
print("{} ({:.5f}GB) already exists".format(f, osp.getsize(target) / 2**30))
while True:
answer = input("overwrite? [y/n]:")
if answer.lower() in ["y", "n"]:
Expand All @@ -184,7 +182,7 @@ def _download_file(self, f):
req = urllib.request.Request(source)

try:
response = urllib.request.urlopen(req)
urllib.request.urlopen(req)
except URLError as e:
if hasattr(e, "reason"):
print("We failed to reach a server.")
Expand Down
9 changes: 5 additions & 4 deletions astro_tigress/tigress_utils.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import os
import shutil


def copy_file(fn_old, fn_new):
"""Copy file from old to new location. Make new directory if needed.
Raise warning if old file does not exist.
inputs:
fn_old: string, old filename
fn_new: string, new filename"""
if fn_old == fn_new:
raise ValueError('New filename cannot be the same as the original one.')
raise ValueError("New filename cannot be the same as the original one.")
if os.path.isfile(fn_old):
if os.path.isfile(fn_new):
print("New file already exists: "+fn_new)
print("New file already exists: " + fn_new)
while True:
answer = input("Overwrite? (y/n):")
if answer == "y":
break
elif answer == "n":
return
return
dir_new = os.path.dirname(fn_new)
if not os.path.exists(dir_new):
os.makedirs(dir_new)
shutil.copyfile(fn_old, fn_new)
else:
raise RuntimeError("file does not exist: "+fn_old)
raise RuntimeError("file does not exist: " + fn_old)
return
2 changes: 1 addition & 1 deletion astro_tigress/ytathena.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, fname=os.path.join(dirpath, "coolftn.txt")):
self.nT = len(self.T1)

def get_Tidx(self, T):
if type(T) == np.ndarray:
if type(T) is np.ndarray:
Tidx = np.log10(T / self.Tmin) / self.dT
Tidx[np.where(T < self.Tmin)] = 0
Tidx[np.where(T >= self.Tmax)] = self.nT - 2
Expand Down
100 changes: 51 additions & 49 deletions docs/example_2-synthetic-dustpol.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@
"# add path to astro_tigress module\n",
"# this can also be done using PYTHONPATH environment variable\n",
"import sys\n",
"sys.path.insert(0,'../')\n",
"\n",
"sys.path.insert(0, \"../\")\n",
"\n",
"import astro_tigress\n",
"\n",
"# Need to set the master directory where the data is stored\n",
"dir_master = \"../data/\"\n",
"# name of the simulation model\n",
"model_id = \"R8_4pc\" \n",
"model = astro_tigress.Model(model_id,dir_master) #reading the model information"
"model_id = \"R8_4pc\"\n",
"model = astro_tigress.Model(model_id, dir_master) # reading the model information"
]
},
{
Expand All @@ -51,7 +53,7 @@
"metadata": {},
"outputs": [],
"source": [
"#load the MHD data set for the snapshot ivtk=300\n",
"# load the MHD data set for the snapshot ivtk=300\n",
"model.load(300, dataset=\"MHD\")"
]
},
Expand All @@ -69,11 +71,11 @@
"metadata": {},
"outputs": [],
"source": [
"Temp = model.MHD.grid[('gas','temperature')].in_units(\"K\").value.T\n",
"nH = model.MHD.grid[('gas','nH')].in_units(\"cm**-3\").value.T\n",
"Bx = model.MHD.grid[('gas','magnetic_field_x')].in_units(\"uG\").value.T\n",
"By = model.MHD.grid[('gas','magnetic_field_y')].in_units(\"uG\").value.T\n",
"Bz = model.MHD.grid[('gas','magnetic_field_z')].in_units(\"uG\").value.T"
"Temp = model.MHD.grid[(\"gas\", \"temperature\")].in_units(\"K\").value.T\n",
"nH = model.MHD.grid[(\"gas\", \"nH\")].in_units(\"cm**-3\").value.T\n",
"Bx = model.MHD.grid[(\"gas\", \"magnetic_field_x\")].in_units(\"uG\").value.T\n",
"By = model.MHD.grid[(\"gas\", \"magnetic_field_y\")].in_units(\"uG\").value.T\n",
"Bz = model.MHD.grid[(\"gas\", \"magnetic_field_z\")].in_units(\"uG\").value.T"
]
},
{
Expand All @@ -90,9 +92,9 @@
"metadata": {},
"outputs": [],
"source": [
"xcc = model.MHD.grid[(\"gas\",\"x\")][:,0,0].to('pc').value\n",
"ycc = model.MHD.grid[(\"gas\",\"y\")][0,:,0].to('pc').value\n",
"zcc = model.MHD.grid[(\"gas\",\"z\")][0,0,:].to('pc').value"
"xcc = model.MHD.grid[(\"gas\", \"x\")][:, 0, 0].to(\"pc\").value\n",
"ycc = model.MHD.grid[(\"gas\", \"y\")][0, :, 0].to(\"pc\").value\n",
"zcc = model.MHD.grid[(\"gas\", \"z\")][0, 0, :].to(\"pc\").value"
]
},
{
Expand All @@ -109,8 +111,8 @@
"metadata": {},
"outputs": [],
"source": [
"dx_pc = model.MHD.grid[(\"gas\",\"dx\")][0,0,0].to('pc').value\n",
"dx_cm = model.MHD.grid[(\"gas\",\"dx\")][0,0,0].to('cm').value"
"dx_pc = model.MHD.grid[(\"gas\", \"dx\")][0, 0, 0].to(\"pc\").value\n",
"dx_cm = model.MHD.grid[(\"gas\", \"dx\")][0, 0, 0].to(\"cm\").value"
]
},
{
Expand All @@ -128,7 +130,7 @@
"metadata": {},
"outputs": [],
"source": [
"I,Q,U = calc_IQU(nH,Bx,By,Bz,dx_pc,los='y')"
"I, Q, U = calc_IQU(nH, Bx, By, Bz, dx_pc, los=\"y\")"
]
},
{
Expand Down Expand Up @@ -156,24 +158,24 @@
}
],
"source": [
"fig, axes = plt.subplots(1,2, figsize=(12,5))\n",
"fig, axes = plt.subplots(1, 2, figsize=(12, 5))\n",
"# integration along the y-axis (edge on view)\n",
"plt.sca(axes[0])\n",
"NH = nH.sum(axis=1)*dx_cm\n",
"NH = nH.sum(axis=1) * dx_cm\n",
"\n",
"plt.pcolormesh(xcc,zcc,NH,vmax=1.e22,shading='nearest')\n",
"plt.gca().set_aspect('equal')\n",
"plt.xlabel('x[pc]')\n",
"plt.ylabel('z[pc]')\n",
"cbar = plt.colorbar(label=r'$N_H\\,[{\\rm cm^{-2}}]$')\n",
"plt.pcolormesh(xcc, zcc, NH, vmax=1.0e22, shading=\"nearest\")\n",
"plt.gca().set_aspect(\"equal\")\n",
"plt.xlabel(\"x[pc]\")\n",
"plt.ylabel(\"z[pc]\")\n",
"cbar = plt.colorbar(label=r\"$N_H\\,[{\\rm cm^{-2}}]$\")\n",
"\n",
"plt.sca(axes[1])\n",
"\n",
"plt.pcolormesh(xcc,zcc,I,vmax=5,shading='nearest')\n",
"plt.gca().set_aspect('equal')\n",
"plt.xlabel('x[pc]')\n",
"plt.ylabel('z[pc]')\n",
"cbar = plt.colorbar(label=r'$I$')"
"plt.pcolormesh(xcc, zcc, I, vmax=5, shading=\"nearest\")\n",
"plt.gca().set_aspect(\"equal\")\n",
"plt.xlabel(\"x[pc]\")\n",
"plt.ylabel(\"z[pc]\")\n",
"cbar = plt.colorbar(label=r\"$I$\")"
]
},
{
Expand All @@ -190,7 +192,7 @@
"metadata": {},
"outputs": [],
"source": [
"P = np.sqrt(Q**2+U**2)"
"P = np.sqrt(Q**2 + U**2)"
]
},
{
Expand All @@ -210,34 +212,34 @@
}
],
"source": [
"fig, axes = plt.subplots(2,2, figsize=(13,10))\n",
"fig, axes = plt.subplots(2, 2, figsize=(13, 10))\n",
"axes = axes.flatten()\n",
"# integration along the y-axis (edge on view)\n",
"plt.sca(axes[0])\n",
"plt.pcolormesh(xcc,zcc,I,vmax=5,shading='nearest')\n",
"plt.gca().set_aspect('equal')\n",
"plt.xlabel('x[pc]')\n",
"plt.ylabel('z[pc]')\n",
"cbar = plt.colorbar(label=r'$I$')\n",
"plt.pcolormesh(xcc, zcc, I, vmax=5, shading=\"nearest\")\n",
"plt.gca().set_aspect(\"equal\")\n",
"plt.xlabel(\"x[pc]\")\n",
"plt.ylabel(\"z[pc]\")\n",
"cbar = plt.colorbar(label=r\"$I$\")\n",
"plt.sca(axes[1])\n",
"plt.pcolormesh(xcc,zcc,P/I,vmax=0.2,shading='nearest')\n",
"plt.gca().set_aspect('equal')\n",
"plt.xlabel('x[pc]')\n",
"plt.ylabel('z[pc]')\n",
"cbar = plt.colorbar(label=r'$P/I$')\n",
"plt.pcolormesh(xcc, zcc, P / I, vmax=0.2, shading=\"nearest\")\n",
"plt.gca().set_aspect(\"equal\")\n",
"plt.xlabel(\"x[pc]\")\n",
"plt.ylabel(\"z[pc]\")\n",
"cbar = plt.colorbar(label=r\"$P/I$\")\n",
"\n",
"plt.sca(axes[2])\n",
"plt.pcolormesh(xcc,zcc,Q,vmin=-0.1,vmax=0.1,shading='nearest',cmap=plt.cm.RdBu)\n",
"plt.gca().set_aspect('equal')\n",
"plt.xlabel('x[pc]')\n",
"plt.ylabel('z[pc]')\n",
"cbar = plt.colorbar(label=r'$Q$')\n",
"plt.pcolormesh(xcc, zcc, Q, vmin=-0.1, vmax=0.1, shading=\"nearest\", cmap=plt.cm.RdBu)\n",
"plt.gca().set_aspect(\"equal\")\n",
"plt.xlabel(\"x[pc]\")\n",
"plt.ylabel(\"z[pc]\")\n",
"cbar = plt.colorbar(label=r\"$Q$\")\n",
"plt.sca(axes[3])\n",
"plt.pcolormesh(xcc,zcc,U,vmin=-0.1,vmax=0.1,shading='nearest',cmap=plt.cm.RdBu)\n",
"plt.gca().set_aspect('equal')\n",
"plt.xlabel('x[pc]')\n",
"plt.ylabel('z[pc]')\n",
"cbar = plt.colorbar(label=r'$U$')"
"plt.pcolormesh(xcc, zcc, U, vmin=-0.1, vmax=0.1, shading=\"nearest\", cmap=plt.cm.RdBu)\n",
"plt.gca().set_aspect(\"equal\")\n",
"plt.xlabel(\"x[pc]\")\n",
"plt.ylabel(\"z[pc]\")\n",
"cbar = plt.colorbar(label=r\"$U$\")"
]
},
{
Expand Down
Loading

0 comments on commit 6e5273c

Please sign in to comment.