Skip to content
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

Option to silent function TerrainAttribute #57

Open
adehecq opened this issue Sep 8, 2022 · 2 comments
Open

Option to silent function TerrainAttribute #57

adehecq opened this issue Sep 8, 2022 · 2 comments

Comments

@adehecq
Copy link

adehecq commented Sep 8, 2022

The function TerrainAttribute prints references to the screen when run. E.g. for the slope:

A Slope calculation (degrees)
C Horn, B.K.P., 1981. Hill shading and the reflectance map. Proceedings of the IEEE 69, 14–47. doi:10.1109/PROC.1981.11918

I understand the idea behind it, but it can become annoying when looping over many items, and trying to add a progress bar. I could not find any option to silent that print. Please let me know if there is a way, otherwise would it be possible to include this as an option?

Since the print is called from the C++ function, it does not seem possible to catch the stdout in Python directly. I tried for example this option and this option, without success. It works with a pure Python function though. So I don't think there is any way on my end to capture that output that comes from the C++ function.

@FraserParlane
Copy link

FraserParlane commented Dec 6, 2023

Agreed—this reduces the usability of the library. It'd be great to have a fix to silence the printing!

@FraserParlane
Copy link

Here's a solution that I found:

import os

class Silence:
    """A context manager that prevents shared C libraries from printing to
    console. Specifically, this was built to silence richdem."""
    def __init__(self):
        self.stderr_fd = sys.stderr.fileno()
        self.orig_fd = os.dup(self.stderr_fd)
        self.null_fd = os.open(os.devnull, os.O_WRONLY)

    def __enter__(self):
        os.dup2(self.null_fd, self.stderr_fd)

    def __exit__(self, type, value, traceback):
        os.dup2(self.orig_fd, self.stderr_fd)

# Instantiating this once helps with performance.
silence = Silence()

with silence:
    rd.TerrainAttribute()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants