Bug #945

grdedit -G does not work

Added by Remko 8 months ago. Updated 6 months ago.

Status:ClosedStart date:2016-07-26
Priority:NormalDue date:
Assignee:Remko% Done:

100%

Category:-Estimated time:1.00 hour
Target version:Candidate for next bugfix release
Affected version:5.x-svn Platform:

Description

From: http://gmt.soest.hawaii.edu/boards/1/topics/3897?r=3906

The command:

gmt grdedit antarctica_ice_speed_900m.nc -R-2800000/2799800/-2799800/2800000 -D"x [m]/y [m]/ice speed [m/year]" -Gk.nc

does not create a file k.nc
Applies to GMT 5.2 and 5.3, at least.

Associated revisions

Revision 16861
Added by Remko 8 months ago

Make grdedit -G work again. This fixes issue #945 for GMT 5.3

Revision 16862
Added by Remko 8 months ago

Make grdedit -G work again. This fixes issue #945 for GMT 5.2

Revision 16865
Added by Paul 8 months ago

Address issue #945

History

#1 Updated by Andreas 8 months ago

Just to add;

Doing a

grdedit -J.. old.nc -Gnew.nc

the projection information is actually written to old.nc, but new.nc is not created (modifies existing file whether -G is given or not)

#2 Updated by Remko 8 months ago

Yes, I forgot to mention that. I observed the same.

#3 Updated by Joaquim 8 months ago

Hmm, I was not even aware of grdedit -G and imo that's a superfluous option that doesn't deserve the work of making it work. If anyone wants to keep a copy of the original it's easy, make a copy.

#4 Updated by Remko 8 months ago

Yes, Joaquim, that can be done, but it should not be necessary. Besides, the grid is written again, from scratch, irrespectively, so the -G option actually serves a purpose, because it is faster that way.

But what I find alarming is that when I add -Vd all the debug info looks good, as if a new file is written. But it is not. That means that somewhere deep in the weeds of GMT_Write_Data something goes terribly wrong. And this can have serious effects on all GMT programs ... at least the ones handling grids.

grdedit: Object ID 0 : Registered GMT Grid File antarctica_ice_speed_900m.nc as an Input resource with geometry Surface [n_objects = 1]
grdedit: api_begin_io: Input resource access is now enabled [container]
grdedit: api_import_grid: Passed ID = 0 and mode = 1
grdedit: Grid is Cartesian
grdedit: GMT_End_IO: Input resource access is now disabled
G: 1 ./k.nc antarctica_ice_speed_900m.nc ./k.nc
grdedit: Editing parameters for grid antarctica_ice_speed_900m.nc:
grdedit: Decode and change attributes in file antarctica_ice_speed_900m.nc
grdedit: api_begin_io: Input resource access is now enabled [container]
grdedit: api_import_grid: Passed ID = 0 and mode = 2
grdedit: Reading grid from file antarctica_ice_speed_900m.nc
grdedit: processing at most 480 (10x48) chunks at a time (30.9 MiB)...
grdedit: packed z-range: [0,3916.32]
grdedit: Grid is Cartesian
grdedit: Chosen boundary condition for all edges: natural
grdedit: Set boundary condition for all edges: natural
grdedit: Set boundary condition for left   edge: natural
grdedit: Set boundary condition for right  edge: natural
grdedit: Set boundary condition for bottom edge: natural
grdedit: Set boundary condition for top    edge: natural
grdedit: GMT_End_IO: Input resource access is now disabled
grdedit: GMT_End_IO: Input resource access is now disabled
grdedit: Reset region in file antarctica_ice_speed_900m.nc to -2.8e+06/2.7998e+06/-2.7998e+06/2.8e+06
grdedit: Reset grid-spacing in file antarctica_ice_speed_900m.nc to 900/900
grdedit: Object ID 1 : Registered GMT Grid File ./k.nc as an Output resource with geometry Surface [n_objects = 2]
grdedit: api_begin_io: Output resource access is now enabled [container]
grdedit: api_export_grid: Passed ID = 1 and mode = 1
grdedit: Updating grid header for file ./k.nc
grdedit: netCDF Library version: 4
grdedit: GMT_End_IO: Output resource access is now disabled
grdedit: Modified grid written to file ./k.nc.
grdedit: gmtapi_garbage_collection: Destroying object: C=0 A=0 ID=0 W=Input F=GMT Grid M=File S=Used P=7ffbd9400890 D=7ffbd9400890 N=antarctica_ice_speed_900m.nc
grdedit: GMTAPI_Garbage_Collection freed 1 memory objects
grdedit: gmtapi_unregister_io: Unregistering object no 0 [n_objects = 1]
grdedit: gmtapi_unregister_io: Unregistering object no 1 [n_objects = 0]
grdedit (gmtlib_free_tmp_arrays): tried to free unallocated memory

#5 Updated by Remko 8 months ago

  • Status changed from New to Resolved
  • Assignee set to Remko
  • % Done changed from 0 to 100
  • Estimated time set to 1.00

#6 Updated by Joaquim 8 months ago

  • Assignee deleted (Remko)
  • % Done changed from 100 to 0
  • Estimated time deleted (1.00)

Ok, here is what is currently going on, despite the printing debug messages

In gmtnc_grd_info, line

if (!strcmp (header→name,"=")) return (GMT_GRDIO_NC_NO_PIPE);

the header→name still holds the input file name and hence it's this file that sees the header updated, so this is the source of the bug ... BUT

in api_export_grid() that code was called inside the branch

if (mode & GMT_GRID_HEADER_ONLY) {    /* Update header structure only */

so in fact only the header was updated and no new file was created (again despite the printed messages).

A corollary of this is, it it wasn't the bug we would end up with a new grid with ... only the header.

This only reinforces my point that -G should be removed.

#7 Updated by Remko 8 months ago

No, the easier way was to use GMT_Write_Data with mode = GMT_GRID_ALL when -G is used.
See r16861 (GMT5.3) and r16862 (GMT5.2).

#8 Updated by Remko 8 months ago

  • Assignee set to Remko
  • % Done changed from 0 to 100
  • Estimated time set to 1.00

#9 Updated by Joaquim 8 months ago

And it now crashes on Windows.

#10 Updated by Remko 8 months ago

:(

That's odd. Works fine on Mac. And it should also on Windows. The concept is simple.

#11 Updated by Joaquim 8 months ago

Two things:

  1. I'm using debug builds. Sometimes it makes a difference
  1. I know you will smile on this but I've been there really many times (Paul can testify) the fact is that compilers on Mac are too permissive to small memory errors. I lost count to cases where VS detected a problem (often by crashing) that clang did let go. We have one of those with grdimage and MEX/Julia that we have not been able to solve.

#12 Updated by Joaquim 8 months ago

  • Status changed from Resolved to In Progress

#13 Updated by Paul 8 months ago

Joaquim was testing things like gmt grdedit JP.nc -Jm0.1i -Glixo.grd which definitively crashes since grdedit did not actually read the grid data (only the header) but then tries to write the entire grid to the new file. I fixed this and added some general precautions in the API against that sort of problem for grids and images. In r16865.

#14 Updated by Remko 8 months ago

Thanks. I thought looked carefully that in all cases the whole grid would be read. But I guess I was not careful enough.
I've backported the same change (r16867) to GMT5.2 (r16869).

#15 Updated by Paul 6 months ago

  • Status changed from In Progress to Resolved

I think this is resolved now, yes?

#16 Updated by Remko 6 months ago

  • Status changed from Resolved to Closed

Yes, this was resolved.

Also available in: Atom PDF