Skip to content

grdinfo fails to report z_min/max when z var has no *actual_range* attribute #6765

Closed

Description

@joa-quim

It was brought to my attention today by Christian Ferreira from MB that grdinfo fails to report the z min/max values. It turns out that that the grids in question have no actual_range but oddly the report is that v_min and v_max are zero. Not z_min, z_max.

Example grid can be downloaded from here

So shamelessly GEBCO is not setting the actual_range attribute and calls elevation to the z array. But this is a grdinfo regression, I think.

grdinfo gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: Title: The GEBCO_2021 Grid - a continuous terrain model for oceans and land at 15 arc-second intervals
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: Command: Information on the development of the data set and the source data sets included in the grid can be found in the data set documentation available from https://www.gebco.net
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: Remark:
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: Pixel node registration used [Geographic grid]
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: Grid file format: ns = GMT netCDF format (16-bit integer), CF-1.7
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: x_min: 22.9458333333 x_max: 27.2041666667 x_inc: 0.00416666666667 (15 sec) name: longitude n_columns: 1022
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: y_min: 31.9625 y_max: 35.5041666667 y_inc: 0.00416666666667 (15 sec) name: latitude n_rows: 850
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: v_min: 0 v_max: 0 name: Elevation relative to sea level [m]
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: scale_factor: 1 add_offset: 0
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: format: classic
gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: Default CPT:

Activity

  1. added
    bug
    on Jun 1, 2022
  2. PaulWessel commented on Jun 1, 2022

    @PaulWessel
    Contributor

    No regression, I think. We have had -M for ages to find data min/max in grdinfo. We do not loop over the data unless -M is given.

  3. PaulWessel commented on Jun 1, 2022

    @PaulWessel
    Contributor

    and old 6.2.rc0 gives the same.

  4. joa-quim commented on Jun 1, 2022

    @joa-quim
    MemberAuthor

    And we never crossed with this before? So GEBCO and EMODNET are fscrewing their grids. Because grdinfo may be called in scripts (it is in MB), we should automatically trigger -M when no actual_range, no?

  5. PaulWessel commented on Jun 1, 2022

    @PaulWessel
    Contributor

    But there are tons of reasons why someone might call grdinfo and often knowing the range of z is not one of them. So we do not want to add that burden unless requested. Any scripts calling this need to be aware that grids not written by GMT can be fucked. But GMT grids always writes min/max.

  6. cdsferreira commented on Jun 2, 2022

    @cdsferreira

    I'm not sure if GEBCO or EMODNET are doing something wrong here. I had a look at the CF Convention (link bellow), and the as I understand in section 4.3 (Vertical (Height or Depth) Coordinate) the default is use V and not Z for the vertical axis. The standard even says: "Optionally, the vertical type may be indicated additionally by providing the standard_name attribute with an appropriate value, and/or the axis attribute with the value Z."

    http://cfconventions.org/Data/cf-conventions/cf-conventions-1.9/cf-conventions.html#vertical-coordinate

    To me sounds that a grid to be fully compliant with the CF Convention should use V over Z.

    Also, if GMT is able to recognise these grids as COARDS, CF-1.5 (under the IDs nb, ns, ni, nf and nd), the same standard was already there in 2010 (link bellow).

    http://cfconventions.org/Data/cf-conventions/cf-conventions-1.5/build/cf-conventions.html#vertical-coordinate

    Therefore I presume that the use of -M inside grdinfo could perhaps be standard when files are recognised by GMT as COARDS, CF-1.5 (or later), isn't? What are the drawbacks of enabling -M as default for the IDs nb, ns, ni, nf and nd inside GMT?

  7. PaulWessel commented on Jun 2, 2022

    @PaulWessel
    Contributor

    I dont think GMT is or will be parsing all possible attributes in a CF file. However ncdump -h shows this

    		:geospatial_vertical_min = -10977. ;
    		:geospatial_vertical_max = 8685. ;
    
    

    as the only range given for depth. But this is the global depths for the whole grid. This particular subset is just

    gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: v_min: -4439 at x = 26.1270833333 y = 34.4354166667 v_max: 2372 at x = 24.03125 y = 35.29375

    which we get via -M by reading the whole grid. Looks to me whatever tool wrote this subset did not save the range of this subset's values. I am not sure we want to force GMT to always read the entire grid file if there is no info on the range since many uses of grdinfo has nothing to do with the range of the data. But we can have a discussion on this of course. For instance, if there is no info on the range, perhaps we should turn on -M unless some other option has been selected that implies we do not need the z-range. And if we had parsed the geospatial_vertical attributes we would have returned the wrong range.

  8. PaulWessel commented on Jun 2, 2022

    @PaulWessel
    Contributor

    Sorry forgot: The only drawback is that we are forced to read all the values of the grid rather than just the header. This can take time for very large grids. It is not really noticeable for small files of course, but that is why we have a -M option.

  9. cdsferreira commented on Jun 2, 2022

    @cdsferreira

    I agree, if the grid "complaint" with the CF convention doesn't follow it and just copy/paste the header of the main GEBCO grid then things get tricky. But I must note that when running grdinfo in the full GEBCO grid I get:

    christian@Christians-MacBook-Pro-2 gebco_2021 % gmt grdinfo GEBCO_2021.nc
    GEBCO_2021.nc: Title: The GEBCO_2021 Grid - a continuous terrain model for oceans and land at 15 arc-
    GEBCO_2021.nc: Command: Information on the development of the data set and the source data sets included in the grid can be found in the data set documentation available from https://www.gebco.net
    GEBCO_2021.nc: Remark:
    GEBCO_2021.nc: Pixel node registration used [Geographic grid]
    GEBCO_2021.nc: Grid file format: ns = GMT netCDF format (16-bit integer), CF-1.7
    GEBCO_2021.nc: x_min: -180 x_max: 180 x_inc: 0.00416666666667 (15 sec) name: longitude n_columns: 86400
    GEBCO_2021.nc: y_min: -90 y_max: 90 y_inc: 0.00416666666667 (15 sec) name: latitude n_rows: 43200
    GEBCO_2021.nc: v_min: 0 v_max: 0 name: Elevation relative to sea level [m]
    GEBCO_2021.nc: scale_factor: 1 add_offset: 0
    GEBCO_2021.nc: format: netCDF-4 chunk_size: 0,0 shuffle: off deflation_level: 0

    christian@Christians-MacBook-Pro-2 gebco_2021 % gmt grdinfo GEBCO_2021.nc -M
    GEBCO_2021.nc: Title: The GEBCO_2021 Grid - a continuous terrain model for oceans and land at 15 arc-
    GEBCO_2021.nc: Command: Information on the development of the data set and the source data sets included in the grid can be found in the data set documentation available from https://www.gebco.net
    GEBCO_2021.nc: Remark:
    GEBCO_2021.nc: Pixel node registration used [Geographic grid]
    GEBCO_2021.nc: Grid file format: ns = GMT netCDF format (16-bit integer), CF-1.7
    GEBCO_2021.nc: x_min: -180 x_max: 180 x_inc: 0.00416666666667 (15 sec) name: longitude n_columns: 86400
    GEBCO_2021.nc: y_min: -90 y_max: 90 y_inc: 0.00416666666667 (15 sec) name: latitude n_rows: 43200
    GEBCO_2021.nc: v_min: -10956 at x = 142.589583333 y = 11.36875 v_max: 8627 at x = 86.9270833333 y = 27.9895833333
    GEBCO_2021.nc: scale_factor: 1 add_offset: 0
    GEBCO_2021.nc: 0 nodes (0.0%) set to NaN
    GEBCO_2021.nc: format: netCDF-4 chunk_size: 0,0 shuffle: off deflation_level: 0

    And if I run ncdump on that file the header shows me:

    	        :geospatial_vertical_min = -10977. ;
    		:geospatial_vertical_max = 8685. ;
    

    Is it right that grdinfo didn't get that from the netCDF header when not using -M? The file in question is available here:

    https://www.gebco.net/data_and_products/gridded_bathymetry_data/

    PS: I only work with grids up to 4-8GB, and therefore it doesn't bother me much force -M. But I know people working with 3D visualisation using netCDF layers of hundreds of Terabytes. That must be hard if the header is wrong and you're forced to read again the whole file to get the min/max values. That's something I should personally look into since I'm going to work with them this year.

  10. joa-quim commented on Jun 2, 2022

    @joa-quim
    MemberAuthor

    The geospatial_vertical_min|max are a different story because they are global attributes and ofc we are not going to parse all the global attributes. The problem is that GEBCO and apparently EMODNET are screwing by not inserting the z (or whatever name the 2D variable has) actual_range attribute. I must confess that I have not GEBCO in high consideration (why the hell do they distribute others grids using their own name?) and if it was only for GEBCO I would say ... But if EMODNET is doing the same shit we may have to do something.

    An alternative to Paul's idea, which I don't fully grasp how it would work, is to expand the -M option to -M[f|l], where -Mforce (the default for -M) means what it does now and -Mlilght means that if z_range is not given in file then GMT will get it.

    BTW, this fails miserably

    julia> imshow("gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc")
    

    GMTjl_tmp

  11. PaulWessel commented on Jun 2, 2022

    @PaulWessel
    Contributor

    Seems to me the solution is to tell those who write these grids to fix their mistakes and not impose extra solutions for us. I dont see why it is on us to fix others mistakes. In the meantime, -M is there to help deal with such grids. It cannot become a default just because some grids lack proper metadata.

  12. joa-quim commented on Jun 2, 2022

    @joa-quim
    MemberAuthor

    I was not proposing to make it the default. Just to have a light weight -Ml that could be thrown into scripts that absolutely need to have z range but at same time not spending resources and users time when grids are well behaved.

  13. PaulWessel commented on Jun 2, 2022

    @PaulWessel
    Contributor

    OK, I can see that as a possibility.

  14. PaulWessel commented on Jun 2, 2022

    @PaulWessel
    Contributor

    If you have time to do this @joa-quim then go ahead. I would prefer

    -M[c|f] for conditional (only if no z-range) or forced [Default]

  15. PaulWessel commented on Jun 2, 2022

    @PaulWessel
    Contributor

    Note that normal -M affects the output format. I would think -Mc would only get the min/max (and report them) but not do the rest that -M does (find the location of those min, max).

  16. joa-quim commented on Jun 3, 2022

    @joa-quim
    MemberAuthor

    I'll try but won't have time to look at it before Sunday night.

  17. dwcaress commented on Jun 4, 2022

    @dwcaress

    I'm late to the party but agree that implementing -M[c|f] as described by Paul should let us solve the MB-System problem identified by Christian. Thanks!

  18. PaulWessel commented on Jun 5, 2022

    @PaulWessel
    Contributor

    @joa-quim I am working on this at the airport.

  19. PaulWessel commented on Jun 5, 2022

    @PaulWessel
    Contributor

    All tests pass, and that funny grid with -Mc now reports

    gebco_2021_n35.50506591796875_s31.96197509765625_w22.9449462890625_e27.2021484375.nc: v_min: -4439 v_max: 2372 name: Elevation relative to sea level [m]

    Of course, -Mf does the same regardless of what the header says, and plain -M still means we report the location of the extrema as well. When the header has a valid range and you use -Mc then (with -Vd) you can see it skips the read:

    grdinfo [DEBUG]: Found valid actual data range in header - no need to read matrix

  20. joa-quim commented on Jun 5, 2022

    @joa-quim
    MemberAuthor

    Still at airports? Well, the good side is that it's good for GMT.

  21. PaulWessel commented on Jun 5, 2022

    @PaulWessel
    Contributor
  22. cdsferreira commented on Jun 7, 2022

    @cdsferreira

    Thanks a lot Paul and Joaquim. Dave and I will start testing it and work to adapt the MB-System scripts to the new grdinfo.

    PS: I have a few people I know at GEBCO and EMODNET (that seam to care a lot about data quality and compliance to standards) I know and I can report that their grids are written wrong. I don't know if that will produce improvements but I was puzzled that the netCDF grids from these two large entities that was supposed to target GMT users were not GMT/netCDF compatible. But I experienced already that with some crazy netCDF files from Ifremer where everything was scrabbled in GMT, so I used the ESRI ASCII grids that were written properly. I will also contact the CF Convention committee and suggest build a quick test suite to check netCDF grid compliance to CF Conventions, and help spot these mishandling of the standard. Maybe with that would not be always on us to adapt our software to read broken grids.

  23. joa-quim commented on Jun 7, 2022

    @joa-quim
    MemberAuthor

    Thanks Christian. Having them fixing their grids would be the best thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions