Skip to content

writing output to file #6

Open
Open
@matthuszagh

Description

@matthuszagh

I'm having trouble writing the output of execution to a local file. For example

#+header: :results output file silent
#+header: :session nil
#+header: :file analog-type-I-phase-detector.dat :output-dir data/
#+begin_src sage
import numpy as np
from sage.symbolic.integration.integral import definite_integral

x = var("x")
phi = var("phi")
fref = sin(x)
fsig = sin(x + phi)
for phase in np.linspace(0, 2 * np.pi, num=10):
    avg = definite_integral(fref * fsig(phi=phase), x, 0, 2 * pi)
    print("{:.3f}\t{:.3f}".format(phase, float(avg)))
#+end_src

Does not write the data/analog-type-I-phase-detector.dat file. It simply echos the file link in the minibuffer. A similar python example works fine:

#+begin_src python :results output silent file :file test.txt
print("hello")
#+end_src

Is there a way to write stdout to a local file with sage? thanks.

EDIT:
Something like this works, but it would be nice to be able to use the file header directly instead of delegating to python logic.

#+begin_src sage :results output silent :var cwd=(file-name-directory buffer-file-name)
import numpy as np
from os.path import abspath
from sage.symbolic.integration.integral import definite_integral

fname = cwd + "data/analog-type-I-phase-detector.dat"

x = var("x")
phi = var("phi")
fref = sin(x)
fsig = sin(x - phi)
with open(fname, "w") as f:
    f.write("phi vout\n")
    for phase in np.linspace(0, 2 * np.pi, num=1000):
        avg = definite_integral(fref * fsig(phi=phase), x, 0, 2 * pi)
        f.write("{:.3f}\t{:.3f}\n".format(phase, float(avg)))
#+end_src

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions