question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

-JEPSG:4326 not writing CRS to netcdf in surface

See original GitHub issue

Description of the problem

Using a Spanish government gravity dataset converted to epsg:4326. Run blockmedian and surface seems to work fine. If I put a proj string in it seems to write that projection to the netcdf file.

However, -JEPSG:4326 (or -Jepsg:4326 or 4326) do

es not.

I made a script version and put a debug print in the utils library generating the run arguments and that gives this:

RUNARGS -I65.15s -R-9.34335092522/3.46340435922/35.8532670285/43.8612236053 RUNARGS -GD:\Spain\GMTtext15.nc -I65.15s -J4326 -R-9.34335092522/3.46340435922/35.8532670285/43.8612236053 -Vc

Full code that generated the error

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import pygmt
import pandas as pd
import geopandas as gpd
import numpy as np
#import rasterio


#import pyvista as pv

from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from matplotlib.patches import Rectangle

import verde as vd
#import cartopy.crs as ccrs
import dask
import pyproj
import pooch
import xarray as xr
import os

spain = gpd.read_file(r'D:\Spain\Gravimetria.shp')

# In[60]:

spain.head()

# In[3]:

Z = spain['VALU_BOU26'].values
lat =  spain.geometry.y.values
lon =  spain.geometry.x.values

# In[4]:

xmin = spain.total_bounds[0]
xmax = spain.total_bounds[2]
ymin = spain.total_bounds[1]
ymax = spain.total_bounds[3]
region = [xmin, xmax, ymin, ymax]

# In[5]:

datatable = pd.DataFrame()
datatable['longitude'] = lon.tolist()
datatable['latitude'] = lat.tolist()
datatable['bouguer_anomaly'] = Z.tolist()

# In[61]:

region2=[-9.31, 3.430053434000058, 35.92, 43.77780739100007]
region3="-9.34335092522/3.46340435922/35.8532670285/43.8612236053"

datatable_blockmedian = pygmt.blockmedian(table=datatable, spacing="65.15s", region=region3)  #e is metres
#grid_s = pygmt.surface(datatable_blockmedian['longitude'],datatable_blockmedian['latitude'],datatable_blockmedian['bouguer_anomaly'],spacing="65.15s",region=region3,J="epsg:4326",V="c",outfile=r'D:\Spain\GMTtext15.nc')
grid_s = pygmt.surface(datatable_blockmedian['longitude'],datatable_blockmedian['latitude'],datatable_blockmedian['bouguer_anomaly'],spacing="65.15s",region=region3,J="4326",V="c",outfile=r'D:\Spain\GMTtext15.nc')

 #+proj=longlat+ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0

# In[62]:

get_ipython().system('gdalinfo "D:\\Spain\\GMTText15.nc"')

# In[ ]:

Full error message

From gdalinfo
Driver: netCDF/Network Common Data Format

Warning 1: Recode from UTF-8 to CP_ACP failed with the error: "Invalid argument".
Warning 1: dimension #1 (x) is not a Longitude/X dimension.
Warning 1: dimension #0 (y) is not a Latitude/Y dimension.
C:\Users\rscott\AppData\Local\Continuum\anaconda3\envs\pygmt\lib\threading.py:874: ResourceWarning: unclosed file <_io.BufferedWriter name=4>
  del self._target, self._args, self._kwargs
ResourceWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\rscott\AppData\Local\Continuum\anaconda3\envs\pygmt\lib\threading.py:874: ResourceWarning: unclosed file <_io.BufferedReader name=5>
  del self._target, self._args, self._kwargs
ResourceWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\rscott\AppData\Local\Continuum\anaconda3\envs\pygmt\lib\threading.py:874: ResourceWarning: unclosed file <_io.BufferedReader name=6>
  del self._target, self._args, self._kwargs
ResourceWarning: Enable tracemalloc to get the object allocation traceback


Files: D:\Spain\GMTText15.nc
Size is 709, 443
Origin = (-9.352395243923700,43.870282379708144)
Pixel Size = (0.018088637407401,-0.018117548816290)
Metadata:
  NC_GLOBAL#Conventions=CF-1.7
  NC_GLOBAL#GMT_version=6.1.1 [64-bit]
  NC_GLOBAL#history=surface @GMTAPI@-S-I-D-V-T-N-000000 -GD:\Spain\GMTtext15.nc -I65.15s -J+4326 -R-9.34335092522/3.46340435922/35.8532670285/43.8612236053 -Vc
  NC_GLOBAL#title=Data gridded with continuous surface splines in tension
  x#actual_range={-9.343350925219999,3.46340435922}
  x#long_name=x
  y#actual_range={35.8532670285,43.8612236053}
  y#long_name=y
  z#actual_range={-143.2124328613281,312.9815673828125}
  z#long_name=z
  z#_FillValue=-nan(ind)
Corner Coordinates:
Upper Left  (  -9.3523952,  43.8702824) 
Lower Left  (  -9.3523952,  35.8442083) 
Upper Right (   3.4724487,  43.8702824) 
Lower Right (   3.4724487,  35.8442083) 
Center      (  -2.9399733,  39.8572453) 
Band 1 Block=709x1 Type=Float32, ColorInterp=Undefined
  NoData Value=nan
  Metadata:
    actual_range={-143.2124328613281,312.9815673828125}
    long_name=z
    NETCDF_VARNAME=z
    _FillValue=-nan(ind)
In [ ]:

System information

Please paste the output of python -c "import pygmt; pygmt.show_versions()":

PyGMT information:
  version: v0.2.0
System information:
  python: 3.8.6 | packaged by conda-forge | (default, Oct  7 2020, 18:22:52) [MSC v.1916 64 bit (AMD64)]
  executable: C:\Users\rscott\AppData\Local\Continuum\anaconda3\envs\pygmt\python.exe
  machine: Windows-10-10.0.18362-SP0
Dependency information:
  numpy: 1.19.2
  pandas: 1.1.3
  xarray: 0.16.1
  netCDF4: 1.5.3
  packaging: 20.4
  ghostscript: 9.53.3
  gmt: 6.1.1
GMT library information:
  binary dir: C:/Users/rscott/AppData/Local/Continuum/anaconda3/envs/pygmt
  cores: 12
  grid layout: rows
  library path: C:/Users/rscott/AppData/Local/Continuum/anaconda3/envs/pygmt/Library/bin/gmt.dll
  padding: 2
  plugin dir: C:/Users/rscott/AppData/Local/Continuum/anaconda3/envs/pygmt/Library/bin/gmt_plugins
  share dir: C:/Users/rscott/AppData/Local/Continuum/anaconda3/envs/pygmt/Library/share/gmt
  version: 6.1.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:48 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
weiji14commented, Oct 21, 2020

There’s different versions of WKT?!! Great 😂 Looking at https://gdal.org/programs/gdalsrsinfo.html#cmdoption-gdalsrsinfo-o, there’s wkt1, wkt2_2015, wkt2_2018, and so on. Now my question is: “How can we tell GMT which wkt version to use?”. And why isn’t the current default “wkt: Latest WKT version supported, currently wkt2_2018”, but wkt1?

1reaction
joa-quimcommented, Oct 21, 2020

The conversions are done via calls to GDAL functions, but there are several versions of WKT. I don’t remember to select anyone in particular but it looks you are getting wkt1, e.g.

$ gdalsrsinfo EPSG:3031 -o wkt1

PROJCS["WGS 84 / Antarctic Polar Stereographic",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.0174532925199433,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Polar_Stereographic"],
    PARAMETER["latitude_of_origin",-71],
    PARAMETER["central_meridian",0],
    PARAMETER["false_easting",0],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AXIS["Easting",NORTH],
    AXIS["Northing",NORTH],
    AUTHORITY["EPSG","3031"]]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using rioxarray to assign spatial reference (EPSG:4326) to ...
As I want to be able to dynamically inspect each variable at a given time as a map, I want to convert it...
Read more >
NetCDF variable comes without crs and extent - Stack Overflow
The problem is that I do not manage to extract the crs and extent of the netCDF data with brick() . The image()...
Read more >
Coordinate Transforms | netCDF-Java Documentation
The steps to using your own CoordinateTransform in the Netcdf-Java library: Write a class that implements ucar.unidata.geoloc.Projection or ucar ...
Read more >
NetCDF Climate and Forecast (CF) Metadata Conventions
The features of these conventions that allow writing netCDF files that are not COARDS conforming are summarized below. COARDS standardizes the description ...
Read more >
Handling NetCDF Files using XArray for Absolute Beginners
Unlike files in .csv or .xlsx, NetCDF format cannot be accessed and ... netCDF4-python for basic netCDF operation such as reading/writing ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found