Problem with temporary files when rendering maps in a cycle using the pygmt library



The problem is that when using the datasets.load_earth_magnetic_anomaly function from the pygmt library in a cycle, an error related to temporary files occurs: pygmt-session [ERROR]: gmtlib_get_tile_list: Unable to open the temporary file. This error is shown in the first image. If you use the same function outside the cycle, the magnetic anomaly maps are drawn as expected, as shown in Photo 2. The cycle iterates through the lists in the dictionary, which represent the four coordinates of the sector’s corners required for drawing. The dictionary elements, which are a list of coordinates, are displayed separately in Photo 3 for clarity. The question is how to solve the temporary file issue and identify its source. I have tried reinstalling the libraries, but it did not resolve the problem. The latest GMT version is 6.6.0. I have access rights to write temporary files to the folder, and I have checked this. The temporary file is also created, and I have checked this.

Here is the code:

import requests  
import pygmt
import numpy as np
import matplotlib.pyplot as plt
import tempfile
import os
import pandas as pd


s = 'http://mag.gcras.ru/geomag-service/v2/ls'
response = requests.get(s)
response.raise_for_status()  # Проверяем статус
data = response.json()  #  Прямая загрузка и разбор JSON

X= [ ]
Y= []

for k in data.keys():
    # print(data[k]['Lat'], data[k]['Lon'])
    mydict[k] = [data[k]['Lat'], data[k]['Lon'] ]
    X.append(data[k]['Lon'])
    Y.append(data[k]['Lat'])

map_height_deg = 1.0  # Высота карты по широте
map_width_deg = 1.0   # Ширина карты по долготе

for elements in mydict:
    #for coordinates in elements:
        
        top_left_lat = mydict[elements][0] + map_height_deg / 2.0
        top_left_lon = mydict[elements][1] - map_width_deg / 2.0

        # Верхний правый угол
        top_right_lat = mydict[elements][0] + map_height_deg / 2.0
        top_right_lon = mydict[elements][1] + map_width_deg / 2.0

        # Нижний левый угол
        bottom_left_lat = mydict[elements][0] - map_height_deg / 2.0
        bottom_left_lon = mydict[elements][1] - map_width_deg / 2.0

        # Нижний правый угол
        bottom_right_lat = mydict[elements][0] - map_height_deg / 2.0
        bottom_right_lon = mydict[elements][1] + map_width_deg / 2.0

        mydict2[elements] = [top_left_lon, top_right_lon, bottom_left_lat, top_left_lat]

fig = pygmt.Figure()


for i in mydict2:
    grid_subset_mag = pygmt.datasets.load_earth_magnetic_anomaly (resolution="02m", region=i, data_source="emag2_4km" )
    
    fig.grdimage(grid = grid_subset_mag)
    fig.grdcontour(grid = grid_subset_mag, annotation=150, levels=150)
    fig.colorbar(cmap=True, position="JMR", frame=["a100f50", "x+lnT"])
    
    fig.show()

Screen captures to show code are not very well come.

if you loop over mydict2 like this: for i in mydict2: then you should refer to i and not to mydict2[i] in your region specification: region=i, not region=(mydict2[i])

Thank you but Its not solve the problem. Still the same mistake.

The code example you presented above contains way too many trivial Python syntax errors. It’s not even a complete code, rather, it’s a mix of short fragments of a longer Ipython notebook session copied and pasted here one after the other. It’s a mess. This code is syntactically wrong, it can not be run.

I don’t think GMT user forum is a right place to teach the beginner programmers on how to write Python code correctly.

Hi @Naturale,

Thanks for sharing your script. I formatted it as code to make it easier to read.

Some points you should have a look at:

  • Both mydict and mydict2 are never introduced.
  • You probably want a plot for each region? In this case, fig = pygmt.Figure() has to be within the loop.
  • The loop over mydict2 is probably wrong, as already mentioned. You could use the keys of the dictionary to iterate over the dictionary.
  • To get a proper map, you should first use Figure.basemap and specify the frame, region, and projection parameters.

Thanks a lot for you

I think you just dont know how to solve the problem. Trivial syntax errors are here because mistake is in another part. So if you cant do anything i think its an option to give others an opportunity

Absolutely! Good luck to you and to everybody wishing to do your work and write some correct and working Python code for you!