[Thread Prev][Thread Next][Index]

[ferret_users] Checking netcdf files / Redirecting Ferret stdout



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.

[Thread Prev][Thread Next][Index]