-
Notifications
You must be signed in to change notification settings - Fork 346
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
WIP: Debugging PyGMT with GMT dev #4745
Conversation
@PaulWessel PyGMT fails with recent GMT versions on Windows. Some debugging efforts find that commit b16cc28 (i.e., PR #4581) causes the failures. A failing PyGMT test looks like this: with GMTTempFile(suffix=".nc") as tmpfile:
result = grdcut("@earth_relief_01d", outgrid=tmpfile.name, region="0/180/0/90")
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
result = grdinfo(tmpfile.name, C=True)
assert result == "0 180 0 90 -8182 5651.5 1 1 180 90 1 1\n" It's equivalent to the following script:
However, on Windows it gives the following error message:
but it works on Linux and macOS. Perhaps in #4581 you forgot to close the file? Could you review the changes in #4581 and see if you can find the bug? |
Most (all) of that commit is related to the 3-D data cube, but your example is a regular 2-D grid. Which file do you suspect is not being closed? The tmp.grd output file? |
The raw error message is:
in which I guess it means the temporary file is still used by GMT. |
But it's PyGMT who writes that file, right? GMT does not write temporary files. |
Yes, PyGMT writes that temporary file and then tries to delete it, then we have the permission error. I should emphasize that I use the PyGMT master branch, and it fails with GMT b16cc28 but works with GMT bbe7601 (bbe7601 is the parent commit of b16cc28). So I'm afraid it must be changes in GMT that breaks. |
I know it's not a solution to the root issue but, it's writing a temp file just to run |
We are avoiding system calls in PyGMT, and the PyGMT tests are testing different combinations of input/output data types (grid file, xarray array etc.), so it won't solve our problem. |
Ok, that's a different discussion (as a user I would not like to lean that). But PyGMT always closes the session between GMT calls. Can you try to kill the session before attempting to delete the temp file? Because it's going to be very difficult to debug this from outside Windows. |
I am deep in two-three branches right now, but once I come up for air I am willing to step through the debugger and confirm that the -Gtmp.grd file is indeed closed by the program. I cannot imagine that it would not do that. There must be something else subtle in that commit. |
Yep. It's he netCDF lib who takes charge of closing the files it writes. |
OK, I have stepped through the grdcut command and of course it closes the file at the end. Here we have completed that step in the debugger: There must be something else going on and if it is a Windows-only thing perhaps @joa-quim could try to reproduce it? |
What about my suggestion to make sure the session is destroyed before deleting the temp file? After all PyGMT always destroys the sessions between commands. |
As I understand it, PyGMT only uses one session for all commands. |
That's what MEX and Julia do but from previous talks I have the impression that in PyGMT each command starts and ends a new session. |
That was my understanding from what @leouieda explained to me: Each module is wrapped by a Create/Destroy session pair. There is no API void pointer carried around throughout the PyGMT session as a persistent variable, no? |
Just checked the codes. I think you're right. |
Yes. |
I'm reading the codes. I think the session is destroyed first, and then PyGMT tries to delete the temp file. |
Something must be holding a grip to the file. If it's not GMT, what else can it be? Can you try not to delete it with python and at the end delete it with a |
Will try. |
Here is a minimal example to reproduce the issue (using PyGMT latest + GMT dev). The equivalent bash command is import os
import subprocess as sp
from pygmt import grdinfo
# Runing the following command in CLI to generate the sample grid
# gmt grdcut @earth_relief_01d -R0/180/0/90 -Gingrid.nc
result = grdinfo("ingrid.nc", verbose="d")
os.remove("ingrid.nc")
# sp.run("del ingrid.nc", shell=True) Here I tried three different ways to delete the
The variable Here are the debugging messages:
|
I will see what grdinfo does in debug, first in Xcode from command line, then later via PyGMT. But hungry right now... |
I stepped through grdinfo ingrid.nc in Xcode and it definitively calls nc_close at the end. I do not know what might be holding onto it in python. |
And the issue is netCDF related (not only More, this blocks the grid too but now is GDAL who does the job. We must be opening it somewhere else and not closing.
|
Good to know that you can reproduce it! b16cc28 is the commit to blame. |
I acknowledge there is a problem, but not clear if we are not closing the file or not. My tests so far shows we are closing it when grdinfo has received the header. I am thinking of replacing nc_open and nc_close with gmt_nc_open and gmt_nc_close so I can issue a debug message and we can at least know for sure when these functions are called. OK with that? |
OK. But dis you see my other message saying that |
Right... so that would argue that netCDF might cause this. @seisman, any releases of netCDF/HDF that coincides with our commit? Does building with an older netcdf library show the same problem? |
I didn't change my netcdf lib, so can't be that either. And to tell the truth, I've been witnessing this type of behavior for a long time in Mirone run from Matlab. Time to time I realized that I could not delete a nc file that was created by Mirone. Bu in Mirone I create nc with own code, or with GMT and never investigated the subject. |
Using gmt_nc_*, here is grdinfo:
I am seeing three opens and two close... think we got our boy. |
* Ensure we close file in gmt_nc_is_cube Closes #4745. * Update gmt_nc.c
Description of proposed changes
DO NOT MERGE
It seems that recent GMT changes break PyGMT on Windows (see GenericMappingTools/pygmt#801).
Some debugging efforts indicate the bug was introduced in these commits c94e83f...98bb060 (GenericMappingTools/pygmt#758 (comment)).
In this branch, we build different GMT versions, and run PyGMT tests, to find which commits introduced the bug.
This branch is for debugging only
c94e83f...98bb060
c94e83f works
c08e714 works
6d8d2f0 works.
8181c70 fails.
8ddbb3f works
b16cc28 fails.
74f61ab works.
bbe7601 works.