Displaying grid files on screen - problem with context manager

Any comments why image does not show up by using context manager (Case 1) but does show when not using context manager (Case 2)? I would prefer using context manager as it prevents errors where context has been already created and basically you have to comment out line gxp = gxgx.GXpy() in order to be able to run code anew. Or is there a way to test whether context has been created?

Case 1. Using context manager for GXpy: no image shows (but I can see in C:\Users\username\AppData\Local\Temp\GeosoftTemp that there is a new folder which actually contains that image as png file)

import geosoft.gxpy.grid as gxgrid
import geosoft.gxpy.gx as gxgx
from IPython.display import Image

with gxgx.GXpy() as gxp:
image_file = gxgrid.Grid.open('mag_data_mag.grd').image_file(shade=True, pix_width=500)
Image(image_file
)

Case 2. Using no context manager for GXpy: image shows

import geosoft.gxpy.grid as gxgrid
import geosoft.gxpy.gx as gxgx
from IPython.display import Image

gxp = gxgx.GXpy()
image_file = gxgrid.Grid.open('mag_data_mag.grd').image_file(shade=True, pix_width=500)
Image(image_file)

Answers

  • SerbanMarin1
    SerbanMarin1 Posts: 11 admin

    hi Petri. If you want to use the python with statement for context managers (https://www.geeksforgeeks.org/python/with-statement-in-python/) you need all gx developer python code to be in the indented scope like so:

    with gxgx.GXpy() as gxp
    #gx developer python code goes here
    image_file = gxgrid.Grid.open('mag_data_mag.grd').image_file(shade=True, pix_width=500)
    Image(image_file)
    #gxp is now out of scope and wont work
    print("hello")

  • As you can see in your own post (or at least as it shows to me), this forum does not obey tabs or seemingly other white space in the beginnign of lines. Of course I know how context manager works…

    My question is why does not this code open the grid in Jupyter notebook? This time I include code as image here. The cell is run but no image is shown.

    grid_open.jpg
  • SerbanMarin1
    SerbanMarin1 Posts: 11 admin
    edited September 8

    I'm unable to reproduce this issue. Its able to show me grids in both case.

    image.png

    It will give you a "A GXpy instance has already been created for current thread." error if you try to run either cell after running the second example ( gxp = gxpy.GXpy() ) because the context is never disposed. If this is the issue that you're having you need to manually dispose the context gxp.__exit__(None, None, None)

  • Thanx SerbanMarin1!

    It seems that your last line does the trick: display(Image(filename=image_file)) when using first piece of code. Image does not show up without that line:

    image.png

    However, it is not needed when you don't use context manager, so even this code below shows up the image:

    image.png

    I wonder if there is something to learn here or I just have to take as it is.

    Would you be able to give me also some hints what options are available through features option in gxgrid.figure_map('grid.grd', features= …)? I am able to include geographical grid (with longitudes and latitudes) on map with following lines:

    image_file = gxgrid.figure_map(r'mag.grd', features=('NEATLINE', 'SCALE', 'LEGEND', 'ANNOT_LL')).image_file(pix_width=500) 

    Image(image_file) 

    display(Image(filename=image_file))

    But what if I wanted to use projected coordinates instead on grid? I mean Easting and Northing values in meters (ETRF TM35 FIN for example, grid file already has this coordinate system). Is this something that I can easily just change in features options somehow?

  • PetriValasti1
    PetriValasti1 Posts: 5 Calcite Rank Badge
    edited September 9

    I actually found (AI is sometimes useful) how to change to projected coordinates by changing ANNOT_LL to ANNOT_XY. But it does not show the grid lines on map:

    image.png

    While ANNOT_LL does (I mean those blue lines):

    image.png

    Is there some way to show up also grid lines for ANNOT_XY?

  • SerbanMarin1
    SerbanMarin1 Posts: 11 admin
    edited September 10

    Regarding the "display" command. I made a mistake in my post. Image(image_file) can just be replaced with

    display(Image(filename=image_file)). For me, it only shows up if I use "display" but I'm using the jupyter notebook in VSCode, perhaps the engine you're using is different and infers the display command.

    Regarding "figure_map" this method may be too limited for your map requirements. You can see the implementation below (notice the call stack goes down to the figure_map in agg.py):

    https://github.com/GeosoftInc/gxpy/blob/b01b7db309f7f0d7fbc7ed02e21309f11052f2b8/geosoft/gxpy/agg.py#L357