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?