Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gallery example "Cross-section along a transect" #2515

Merged
merged 59 commits into from
Aug 21, 2023

Conversation

yvonnefroehlich
Copy link
Member

@yvonnefroehlich yvonnefroehlich commented Apr 24, 2023

Description of proposed changes

This PR aims to re-write the example 026 at https://docs.gmt-china.org/latest/examples/ex026/ to PyGMT and adds the gallery example "cross-section plots along a transect".

Fixes: #2282

Preview: https://pygmt-dev--2515.org.readthedocs.build/en/2515/gallery/images/cross_section.html

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@yvonnefroehlich yvonnefroehlich added the documentation Improvements or additions to documentation label Apr 24, 2023
@yvonnefroehlich yvonnefroehlich added this to the 0.10.0 milestone Apr 24, 2023
@yvonnefroehlich yvonnefroehlich self-assigned this Apr 24, 2023
@yvonnefroehlich yvonnefroehlich changed the title Add gallery example "Cross-section along a transect" based on ex026 WIP: Add gallery example "Cross-section along a transect" based on ex026 Apr 24, 2023
@yvonnefroehlich yvonnefroehlich marked this pull request as draft April 24, 2023 06:57
@yvonnefroehlich
Copy link
Member Author

/format

seisman

This comment was marked as duplicate.

@yvonnefroehlich
Copy link
Member Author

yvonnefroehlich commented Aug 19, 2023

Maybe the issue regarding building the documentation for Windows is not related to creating track_df via grdtrack, but to plotting track_df.

Using the original code at the end of the script to plot track_df

# Plot elevation along the survey line
fig.plot(
    data=track_df,
    fill="gray",  # Fill the polygon in "gray"
    # Draw a 1-point thick black solid outline
    pen="1p,black,solid",
    close="+y-8000",  # Force closed polygon
    incols=[2, 3],  # Select order of input columns (zero-based indexing)
)

gives my this error message under Windows:

Windows fatal exception: code 0xc0000374


Main thread:
Current thread 0x00002db0 (most recent call first):
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\clib\session.py", line 624 in call_module
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\src\plot.py", line 267 in plot
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py", line 738 in new_module
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py", line 598 in new_module
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py", line 818 in new_module
  File "c:\users\admin\c2\eigenedokumente\studium\promotion\e_gmt\00_testing\000_general\general_testing\untitled1.py", line 114 in <module>
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\py3compat.py", line 356 in compat_exec
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 473 in exec_code
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 615 in _exec_file
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 528 in runfile
  File "C:\Users\Admin\AppData\Local\Temp\ipykernel_21272\3713215765.py", line 1 in <module>


Restarting kernel...

However, when changing the code to

# Plot elevation along the survey line
fig.plot(
    x=track_df.p,
    y=track_df.elevation,
    fill="gray",  # Fill the polygon in "gray"
    # Draw a 1-point thick black solid outline
    pen="1p,black,solid",
    close="+y-8000",  # Force closed polygon
)

the script works.


Output of pygmt.show_versions()

PyGMT information:
  version: v0.9.1.dev125
System information:
  python: 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 17:59:51) [MSC v.1935 64 bit (AMD64)]
  executable: C:\ProgramData\Anaconda3\envs\pygmt_env_dev\python.exe
  machine: Windows-10-10.0.19045-SP0
Dependency information:
  numpy: 1.24.3
  pandas: 2.0.2
  xarray: 2023.1.1.dev17
  netCDF4: 1.6.2
  packaging: 23.1
  contextily: 1.3.0
  geopandas: 0.13.2
  IPython: 8.14.0
  rioxarray: 0.14.1
  ghostscript: 9.54.0
GMT library information:
  binary version: 6.4.0
  cores: 4
  grid layout: rows
  image layout: 
  library path: C:/ProgramData/Anaconda3/envs/pygmt_env_dev/Library/bin/gmt.dll
  padding: 2
  plugin dir: C:/ProgramData/Anaconda3/envs/pygmt_env_dev/Library/bin/gmt_plugins
  share dir: C:/Program Files (x86)/gmt6/share
  version: 6.4.0

@seisman
Copy link
Member

seisman commented Aug 20, 2023

Good to know that you're a Windows developer. Could you please add verbose="d" to the original code and run the script again to see what it actually does?

@weiji14
Copy link
Member

weiji14 commented Aug 20, 2023

Maybe the issue regarding building the documentation for Windows is not related to creating track_df via grdtrack, but to plotting track_df.

Using the original code at the end of the script to plot track_df

# Plot elevation along the survey line
fig.plot(
    data=track_df,
    fill="gray",  # Fill the polygon in "gray"
    # Draw a 1-point thick black solid outline
    pen="1p,black,solid",
    close="+y-8000",  # Force closed polygon
    incols=[2, 3],  # Select order of input columns (zero-based indexing)
)

gives my this error message under Windows ...

This incols thing reminds me of previous bugs like #1313 and #1440. There may be some subtle bug remaining, which should go in a separate issue. Let's stick to passing the x and y columns directly for now in this gallery example, since this is more explicit to the user also.

@yvonnefroehlich
Copy link
Member Author

yvonnefroehlich commented Aug 20, 2023

Good to know that you're a Windows developer.

I use Windows on my private Laptop, but Linux is installed on my university computer.

Could you please add verbose="d" to the original code and run the script again to see what it actually does?

Output of verbose="d"

C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py:818: SyntaxWarning: Short-form parameter (V) is not recommended. Use long-form parameter 'verbose' instead.
  return module_func(*args, **kwargs)
plot [DEBUG]: Look for file 0/15/-8000/6000 in C:/Users/Admin/.gmt
plot [DEBUG]: Look for file 0/15/-8000/6000 in C:/Users/Admin/.gmt/cache
plot [DEBUG]: Look for file 0/15/-8000/6000 in C:/Users/Admin/.gmt/server
plot [DEBUG]: Got regular w/e/s/n for region (0/15/-8000/6000)
plot [INFORMATION]: Processing input table data
plot [DEBUG]: Operation will require 2 input columns [n_cols_start = 2]
plot [DEBUG]: Reset MAP_ANNOT_OBLIQUE to anywhere
plot [DEBUG]: Projected values in meters: 0 15 -8000 6000
plot [INFORMATION]: Linear projection implies x-axis distance exaggeration relative to the y-axis by a factor of 0.000267857
plot [DEBUG]: Computed automatic parameters using dimension scaling: 0.846667
plot [INFORMATION]: Map scale is 0.00125 km per cm or 1:125.
plot [DEBUG]: Running in PS mode modern
plot [DEBUG]: Use PS filename C:/Users/Admin/.gmt/sessions/gmt_session.7880/gmt_1.ps-
plot [DEBUG]: Append to hidden PS file C:/Users/Admin/.gmt/sessions/gmt_session.7880/gmt_1.ps-
plot [DEBUG]: Got session name as pygmt-session and default graphics formats as pdf
plot [DEBUG]: Basemap order: Frame = above  Grid = below  Tick/Annot = below
plot [DEBUG]: gmtapi_init_import: Passed family = Data Table and geometry = Line
plot [DEBUG]: gmtapi_init_import: Added 1 new sources
plot [DEBUG]: GMT_Init_IO: Returned first Input object ID = 0
plot [DEBUG]: gmtapi_begin_io: Input resource access is now enabled [container]
plot [DEBUG]: gmtapi_import_dataset: Passed ID = -1 and mode = 0
plot [INFORMATION]: Referencing data table from user 4 column arrays of length 153
plot [DEBUG]: Object ID 1 : Registered Data Table Memory Reference 2a122039000 as an Input resource with geometry Point [n_objects = 2]
plot [DEBUG]: gmtapi_import_dataset processed 1 resources
plot [DEBUG]: GMT_End_IO: Input resource access is now disabled
plot [INFORMATION]: Plotting segment 0
plot [DEBUG]: GMT memory: Initialize 2 temporary column double arrays, each of length : 0
plot [DEBUG]: Extend from (4.72515, 0.238786) to crossing point (4.72441, 0.238706) in direction -173.844 by 0.000748962" on E side with border angle -90 and delta angle -83.8444
plot [DEBUG]: GMT_Destroy_Data: freed memory for a Data Table for object 1
plot [DEBUG]: gmtlib_unregister_io: Unregistering object no 1 [n_objects = 1]
plot [DEBUG]: gmtlib_unregister_io: Object no 1 has non-NULL resource pointer
plot [DEBUG]: Current size of half-baked PS file C:/Users/Admin/.gmt/sessions/gmt_session.7880/gmt_1.ps- = 68185.
plot [DEBUG]: GMT memory: Free 2 temporary column arrays, each of length : 2097152

@yvonnefroehlich
Copy link
Member Author

Maybe the issue regarding building the documentation for Windows is not related to creating track_df via grdtrack, but to plotting track_df.

This incols thing reminds me of previous bugs like #1313 and #1440. There may be some subtle bug remaining, which should go in a separate issue. Let's stick to passing the x and y columns directly for now in this gallery example, since this is more explicit to the user also.

The gallery example Sampling along tracks also uses grdtrack. So far I don't face any issue related to using grdtrack or plotting its output. There x and y are used and not data and incols.

@yvonnefroehlich
Copy link
Member Author

yvonnefroehlich commented Aug 20, 2023

There seem to be some similarity with the issue, which came up in PR #2138, please see comments up on #2138 (comment) / #2138 (comment).

When track_df is handled as astype(int) the issue regarding plotting it via data and incols does not occur (please see commit ab5d52b) and building the documentation is successful for Windows and macOS.


Here is a reduced example: When a pandas.DataFrame with integers is used I do not face any problems, but when converting the pandas.DataFrame to float the code crashes under Windows with a similar error message as posted above.

import pandas as pd
import pygmt 


size = 5

# Set up random test data
test_dict_int = {
    'a': [ 2,  2, 2, 2],
    'z': [ 8,  6, 7, 3],
    'x': [-3, -1, 1, 3],
    'y': [ 2,  2, 2, 2],
}
test_df_int = pd.DataFrame(data=test_dict_int)


fig = pygmt.Figure()

fig.basemap(
    region=[-size, size, -size, size],
    projection="X" + str(size*2),
    frame=True,
)

fig.plot(
    # data=test_df_int,  # integers -> WORKs
    data=test_df_int.astype(float),  # floats -> FAILs
    pen="1p,black",
    fill="gray",
    close="+y-3",
    incols=[2, 3],
    # verbose="d",
)

# fig.show()
# fig.savefig(fname="bug_MWE.png")

Output of verbose="d"

plot [DEBUG]: Look for file -5/5/-5/5 in C:/Users/Admin/.gmt
plot [DEBUG]: Look for file -5/5/-5/5 in C:/Users/Admin/.gmt/cache
plot [DEBUG]: Look for file -5/5/-5/5 in C:/Users/Admin/.gmt/server
plot [DEBUG]: Got regular w/e/s/n for region (-5/5/-5/5)
plot [INFORMATION]: Processing input table data
plot [DEBUG]: Operation will require 2 input columns [n_cols_start = 2]
plot [DEBUG]: Reset MAP_ANNOT_OBLIQUE to anywhere
plot [DEBUG]: Projected values in meters: -5 5 -5 5
plot [DEBUG]: Computed automatic parameters using dimension scaling: 0.9
plot [INFORMATION]: Map scale is 0.001 km per cm or 1:100.
plot [DEBUG]: Running in PS mode modern
plot [DEBUG]: Use PS filename C:/Users/Admin/.gmt/sessions/gmt_session.20196/gmt_1.ps-
plot [DEBUG]: Append to hidden PS file C:/Users/Admin/.gmt/sessions/gmt_session.20196/gmt_1.ps-
plot [DEBUG]: Got session name as pygmt-session and default graphics formats as pdf
plot [DEBUG]: Basemap order: Frame = above  Grid = below  Tick/Annot = below
plot [DEBUG]: gmtapi_init_import: Passed family = Data Table and geometry = Line
plot [DEBUG]: gmtapi_init_import: Added 1 new sources
plot [DEBUG]: GMT_Init_IO: Returned first Input object ID = 0
plot [DEBUG]: gmtapi_begin_io: Input resource access is now enabled [container]
plot [DEBUG]: gmtapi_import_dataset: Passed ID = -1 and mode = 0
plot [INFORMATION]: Referencing data table from user 4 column arrays of length 4
plot [DEBUG]: Object ID 1 : Registered Data Table Memory Reference 1e0f157bfc0 as an Input resource with geometry Point [n_objects = 2]
plot [DEBUG]: gmtapi_import_dataset processed 1 resources
plot [DEBUG]: GMT_End_IO: Input resource access is now disabled
plot [INFORMATION]: Plotting segment 0
plot [DEBUG]: GMT memory: Initialize 2 temporary column double arrays, each of length : 0

Error message

Windows fatal exception: code 0xc0000374


Main thread:
Current thread 0x00003654 (most recent call first):
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\clib\session.py", line 624 in call_module
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\src\plot.py", line 267 in plot
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py", line 738 in new_module
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py", line 598 in new_module
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\pygmt\helpers\decorators.py", line 818 in new_module
  File "c:\users\admin\c2\eigenedokumente\studium\promotion\e_gmt\00_testing\001_gmt_pygmt\pr_tracksampling\bug_mwe_red.py", line 35 in <module>
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\py3compat.py", line 356 in compat_exec
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 473 in exec_code
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 615 in _exec_file
  File "C:\ProgramData\Anaconda3\envs\pygmt_env_dev\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 528 in runfile
  File "C:\Users\Admin\AppData\Local\Temp\ipykernel_1076\1879108342.py", line 1 in <module>


Restarting kernel...

@seisman
Copy link
Member

seisman commented Aug 21, 2023

@yvonnefroehlich Could you please open a separate issue report so that we can explore the issue later?

@seisman seisman added the final review call This PR requires final review and approval from a second reviewer label Aug 21, 2023
Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @yvonnefroehlich for working on this great example! Just one last minor comment on the units of the cross-section figure, otherwise ok!

projection="X12c/3c",
# Add annotations ("a") and ticks ("f") as well as labels ("+l")
# at the west or left and south or bottom sides ("WSrt")
frame=["WSrt", "xa2f1+lDistance+u@.", "ya4000+lElevation / m"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put units in brackets. The ° symbol will need to wait for #2584 to work.

Suggested change
frame=["WSrt", "xa2f1+lDistance+u@.", "ya4000+lElevation / m"],
frame=["WSrt", "xa2f1+lDistance (°) +u@.", "ya4000+lElevation (m)"],

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the x-axis, there is already the unit degrees add by +u@. If +u° works after PR #2584, I can change this.

Hm, I am not sure about putting units in brackets, including both [ ] or ( ). I learned this is wrong, and quantity / unit or quantity in unit should be used. However, I see again and again figures which do not follow this rule, including figues in scientific publications. Sometimes already the individual guidelines of the journals do not follow this rule. Actually, we already have some examples, which show units in brackets. What do others think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, I'm not sure what convention there is for labelling units, maybe it is journal specific also? We can keep using slash if that is more appropriate. And the degree unit can be omitted since it's on the axis already, it's not often that I see distance units in arc degrees actually.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the x-axis, there is already the unit degrees add by +u@. If +u° works after PR #2584, I can change this.

Please see commit 765f2fd for this change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, I'm not sure what convention there is for labelling units, maybe it is journal specific also? We can keep using slash if that is more appropriate.

I learned this as an official rule during my studies, but it seems that this is not handled in such a strict way everywhere 😂.

@yvonnefroehlich
Copy link
Member Author

@yvonnefroehlich Could you please open a separate issue report so that we can explore the issue later?

Please see #2637 for this bug report.

@seisman seisman merged commit 61590b0 into main Aug 21, 2023
7 checks passed
@seisman seisman deleted the rewrite-ex026-to-pygmt branch August 21, 2023 23:52
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite gallery example "track_sampling.py"
5 participants