GenericMappingTools / gmt

The Generic Mapping Tools
https://www.generic-mapping-tools.org
Other
858 stars 359 forks source link

Plotting grid sub-regions in linear projections is bugged from Julia #6140

Closed joa-quim closed 2 years ago

joa-quim commented 2 years ago

This came out in the forum post and it doesn't look like a Julia bug. See, it screws with linear projections (including Plate Carre) but works well for true projections (Mercator in this example). Note that plotting directly from the file (i.e. without loading it first) also works well.

julia> G = gmtread("@earth_relief_05m");

julia> grdimage(G, region=(-180,-40,15,80), color=:geo, figsize=10, Vd=1, show=1)
        grdimage  -R-180/-40/15/80 -JX10/0 -Baf -BWSen -n+a -Cgeo -P -K > C:\TEMP\GMTjl_tmp.ps

julia> grdimage(G, region=(-180,-40,15,80), color=:geo, figsize=10, proj=:merc, Vd=1, show=1)
        grdimage  -R-180/-40/15/80 -JM10 -Baf -BWSen -n+a -Cgeo -P -K > C:\TEMP\GMTjl_tmp.ps

GMTjl_tmp-fs8

GMTjl_tmp-fs8

PaulWessel commented 2 years ago

Plotting geographic with linear is always odd. I suspect on the CLI front we know the data is geographic and that sets basically -fg under the hood. Please try to add -fg (or use -JX10d/0 or something to tell it) to see if that is the case. But you also say -JQ which definitively should set -fg...

joa-quim commented 2 years ago

Adding -fg makes no difference. I tried -JQ, even changed central meridian, to test it too.

PaulWessel commented 2 years ago

Will try to debug later today.

PaulWessel commented 2 years ago

OK, migrated all my stuff from Mac Mini to new MacBook pro laptop. Julia on mac ports for M1 still screwed so using the alternate direct install. Can reproduce the problem. I noticed these too:

G = gmtread("@earth_relief_05m");
GMT [WARNING]: Max total memory allocated was 35.792 Mb [37530568 bytes]
GMT [WARNING]: Single largest allocation was 35.596 Mb [37324800 bytes]
GMT [WARNING]: MEMORY NOT FREED: 0.062 kb [64 bytes]
GMT [WARNING]: Items allocated: 247 reallocated: 134 freed: 245
GMT [WARNING]: Items NOT PROPERLY FREED: 2
GMT [WARNING]: Memory not freed first allocated in gmt_parse.c:955(GMT_Make_Option) (ID = 8): 0.031 kb [32 bytes]
GMT [WARNING]: Memory not freed first allocated in gmt_parse.c:955(GMT_Make_Option) (ID = 9): 0.031 kb [32 bytes]

Added stop points in Xcode and it all works fine - no sluggishness or spinning wheels. Will see what I learn.

PaulWessel commented 2 years ago

Found an issue. Testing the fix.

PaulWessel commented 2 years ago

I cannot get to the memory with ID 8 since it seems it was allocated by GMT_Create_Session and I cannot get a stop in that function. Does "using GMT" start a session?

PaulWessel commented 2 years ago

OK, got there. I see using GMT is processing some options and those are not freed when the gmtread ends, hence a memory leak. Probably up to @joa-quim to fix that.

joa-quim commented 2 years ago

Julia on mac ports for M1 still screwed so using the alternate direct install.

Always use the Julia binaries provided in the Julialang site. Still want me to fix any mem leak? Julia starts a session that lasts ... till we kill it.

PaulWessel commented 2 years ago

No big deal, but it seems gmtread ends a session (?) and therefore complains about unfreed memory. It is a bit odd.

joa-quim commented 2 years ago

Some modules still leave the session in a non-cleaned state. So when I find one such situation I delete the session at the end ant a new one will be created when needed. gmtread also creates a second session but, in principle, only when reading an ogr file, but maybe there is an indirect recreation in the middle that I'm not seeing right now

# Because of the certificates shits on Windows. But for some reason the set in gmtlib_check_url_name() is not visible
(Sys.iswindows()) && run(`cmd /c set GDAL_HTTP_UNSAFESSL=YES`)
API2 = GMT_Create_Session("GMT", 2, GMT_SESSION_NOEXIT + GMT_SESSION_EXTERNAL + GMT_SESSION_NOGDALCLOSE + GMT_SESSION_COLMAJOR);
...
GMT_Destroy_Session(API2)
PaulWessel commented 2 years ago

Seems that will lead to some memory leaks. You can see them if you compile GMT with the -DDEBUG and -DMEM_DEBUG options (see the advanced Cmake file).

joa-quim commented 2 years ago

It's -DMEMDEBUG and I do build with them but don't see those warnings!

PaulWessel commented 2 years ago

Hah, that is odd. I got it both when just running the Julia commands in the terminal as well as when hooked to Xcode.