How to fix 'pygmt-session [ERROR]: Not available in classic mode'

Hello everyone!

I encountered some difficulties while using PyGMT.

When I use ‘fig = gmt.Figure()’ in a function, an error is returned:

‘pygmt-session [ERROR]: Not available in classic mode’

I tried solution given by:https://github.com/GenericMappingTools/pygmt/issues/217

After I added ‘reload(pygmt)’, a new error comes out:

‘free(): invalid next size (fast)’ or 'double free or corruption (out)

How to fix this problem? I hope some of you could help me. Thanks in advance :grinning:

1 Like

Hi @sherfy, thanks for reporting the issue. Could you provide the output of:

import pygmt

pygmt.show_versions()

Just want to see what version of GMT/PyGMT you have installed.

Is this error happening on reload(pygmt), or another line in your Python script? If it is another function (e.g. fig.plot()), could you turn on debugging mode using e.g. fig.plot(..., verbose=True), and post the output from that? That would make it easier to know where the issue is.

2 Likes

Thank you for responding to my question.

The version of my PyGMT is: 6.4.0. And my full error is:

pygmt-session [ERROR]: Not available in classic mode
Module ‘figure’ failed with status code 30

I tried to add ‘verbose=True’, and here is my output:

plot [INFORMATION]: Processing input table data
plot [INFORMATION]: Central meridian not given, default to 127.95
plot [INFORMATION]: Map scale is 7.79236 km per cm or 1:779236.
plot [INFORMATION]: Reading Data Table from Input memory location via vector
double free or corruption (out)
Aborted (core dumped)

Open a terminal, run the command gmt clear sessions, and then try your Python script again.

I tried it and it only worked once and then I got the same error again.

OK, now I know the reason. Since your Python script crashes, the GMT session is not correctly ended. Could you please try run gmt clear sessions again, and then run the following commands in your Python interpreter:

import pygmt

pygmt.show_versions()

ok, the output is:

version: 6.4.0

I just tried:

sudo apt install libtcmalloc-minimal4
export LD_PRELOAD=“/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4”

After I used this method, the problem was solved. But I still don’t understand why this problem occurs.

Sorry, here is my full output:

pygmt.show_versions()
PyGMT information:
version: v0.10.0
System information:
python: 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0]
executable: /home/user/anaconda3/envs/SWS/bin/python
machine: Linux-5.15.0-82-generic-x86_64-with-glibc2.31
Dependency information:
numpy: 1.26.2
pandas: 2.1.4
xarray: 2023.6.0
netCDF4: 1.6.2
packaging: 23.1
contextily: None
geopandas: None
IPython: 8.19.0
rioxarray: None
ghostscript: 10.02.1
GMT library information:
binary version: 6.4.0
cores: 192
grid layout: rows
image layout:
library path: /home/user/anaconda3/envs/SWS/lib/libgmt.so
padding: 2
plugin dir: /home/user/anaconda3/envs/SWS/lib/gmt/plugins
share dir: /home/user/anaconda3/envs/SWS/share/gmt
version: 6.4.0

I use this method solved the problem:double free or corruption (out). But a new one has arisen, I will have Segmentation fault (core dumped) when I run my code.

I use this method solved the problem:double free or corruption (out). But a new one has arisen, I will have Segmentation fault (core dumped) when I run my code.

Can you share the Python script you’re running?

Sure.

def plot_2d_map(region,lon,lat,station_lat,station_long,depth,grd_path,data_path):
    mindepth = float(min(depth))/1000
    fig = pygmt.Figure()
    pygmt.config(MAP_FRAME_TYPE="plain", FONT_ANNOT_PRIMARY="14p")
    fig.basemap(region=region, projection="M10c",frame="a")
    fig.coast(
        projection="M10c",
        region=region,
        frame="a",
        land="white",
        water="lightblue",
        borders="1/1p,black",
    )
    pygmt.makecpt(
        cmap="gray",
        series="0.95/1/0.01",
        continuous=True
    )
    fig.grdimage(
        grid=os.path.join(grd_path,'earth_relief_15s.grd'),
        region=region,
        projection="M10c",
        shading="+a-20+nt0.5+m0",
        cmap=True,
        frame=True
    )

    fig.plot(x=lon,y=lat,style="c0.15c",fill='black')
    fig.plot(x=station_long,y=station_lat,style="t0.3c",fill='black')
    fig.savefig(os.path.join(data_path,'map.png'))
    return 1

def plot_3d(region1,region2,lon,lat,depth,timelist,grd_path,data_path):
    # PyGMT Figure
    fig = pygmt.Figure()
    # Set the azimuth and elevation angle
    perspective = [220, 30]
    # Plot 3D scatter points
    fig.plot3d(
        region=region2,
        x=lon,
        y=lat,
        z=depth,
        projection="M5c",
        style="g0.2c",
        pen="black",
        fill='black',
        frame=[
            "WSZ4",
            'xag+l"latitude"',
            'yag+l"longitude"',
            "z+ldepth(m)",
        ],
        perspective=perspective,
    )
    # Plot basemap
    fig.shift_origin(yshift=8)
    fig.coast(region=region1,
        projection='M5c',
        shorelines='0.25p,black',
        land='grey90',
        perspective=perspective,)

    pygmt.makecpt(
        cmap="gray",
        series="0.95/1/0.01", 
        continuous=True
    )

    fig.grdimage(
        grid=os.path.join(grd_path,'earth_relief_15s.grd'),
        region=region1,
        projection="M5c",
        shading="+a-20+nt0.5+m0", 
        cmap=True, 
        frame=True,  
        perspective=perspective
    )
    fig.savefig(os.path.join(data_path,'3d.png'))
    return

def plot_map(data_path,station_path,grd_path):
    
    station_list = np.loadtxt(station_path,dtype=str)
    station_long = station_list[:,4]
    station_lat = station_list[:,3]

    data = np.loadtxt(os.path.join(data_path,'location.txt'),dtype='str',delimiter=' ')
    time = data[:,0]
    lon = [float(x) for x in data[:,2]]
    lat = [float(x) for x in data[:,3]]
    depth = [float(x) for x in data[:,4]]

    timelist = pd.DataFrame({
        'time': time,
        'value': np.random.rand(len(time))
    })

    timelist['time'] = timelist['time'].astype("datetime64[ns]")

    minlon, maxlon = round(float(min(station_long))-0.1,1),round(float(max(station_long))+0.1,1)
    minlat, maxlat = round(float(min(station_lat))-0.1,1),round(float(max(station_lat))+0.1,1)
    maxdepth = float(max(depth))/1000
    region1 = [minlon, maxlon, minlat, maxlat]
    region2 = [minlon, maxlon, minlat, maxlat,-maxdepth, 0]

    plot_2d_map(region1,lon,lat,station_lat,station_long,depth,grd_path,data_path)
    plot_3d(region1,region2,lon,lat,depth,timelist,grd_path,data_path)

if __name__ == '__main__':
    data_path = config.get_Location_save_path()
    station_path = config.get_station_file()
    grd_path = config.get_ppt_file_path()
    plot_map(data_path,station_path,grd_path)

Thank you for your answers. I really appreciate you helping me with this. :smiley:
The problem should be caused by the bash GMT code with os.system before running this code. :joy:
But how to close the GMT correctly? By using the “gmt clear sessions”?