[Thread Prev][Thread Next][Index]

Re: [ferret_users] Checking netcdf files / Redirecting Ferret stdout



Hi Carolyn,

The SET REDIRECT command is designed to do this kind of in a Ferret or PyFerret session -- it takes all of the messages that normally go to the terminal in an interactive session and writes them to a designated file.  It writes everything that would be written to stdout or stderr or both, depending on the call to SET REDIRECT.

So a python or shell script for file testing could run a journal file that defines an output filename, executes a SET REDIRECT command, and then issues the "USE" command to open and test the netCDF file.

Ansley

(The content of this  message are mine personally.  I am not affiliated with PMEL.)

 

On 11/11/2020 1:46 PM, Carolyn Whitlock - NOAA Affiliate wrote:
Hi folks,

I'd like to make a standalone script that sniffs a netcdf file for basic compatibility with Ferret and prints the Notes that Ferret provides on what is wrong if it does not. I thought that I could do this by piping the stdout from Ferret into a string and searching the string for Notes; however, I'm not able to catch the part of the output stream that has the Notes in it using the following code:

def test_pyferret_read(nc_file):
    oldout = sys.stdout
    stdout_fd = sys.stdout.fileno()
    out = StringIO.StringIO()
    with open('output.txt', 'w') as f, stdout_redirected(f):
      pyferret.start(quiet = True)
      pyferret.run("use " + nc_file)
      out = pyferret.showdata(brief = True)
      pyferret.stop()
    sys.stdout = oldout
    content = myout.getvalue()
    errcount = content.count("error")
    print("count of errors")
    print(str(errcount))
    if errcount > 0:
        print("Pyferret found at least " + str(errcount) + " standards error in this file.")
        print(content)
    else:
        print("No errors in formatting found")

Looking through some of the online python documentation, this seems to be an issue with the C-level stdout redirect - normal python redirects don't work in this context. However, I'm still not able to get the recommended c-level redirect working properly. For that matter,starting a python subprocess dedicated to the pyferret command and redirecting that standard output isn't working either. Nor is redirecting the output to a file and starting up a c-shell wrapper script to search it.

Given that, I have the following questions:
1) Is the way that I'm trying to do this the best way to go about doing it? I.e. is there a better approach or are there better Ferret functions I could use for this?
2) Any advice on redirecting the stdout if that is the right way to go about this?

--
Carolyn Whitlock

[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement