Longitude-time plot using batch

I had this GMT5 script using for-loop (see forum Q/A thread) :

The goal is now to plot these combined Hovmöller diagrams (longitude-time) with GMT6.


Jump to result


The NetCDF files used here contain the meridionally averaged sea-surface temperature anomalies in the equatorial band. They’ve been generated beforehand with Matlab in my case.

Also, I have a custom gmt.conf (see main.sh)

I’m using GMT6.2beta on macOS Big Sur with bash 5

include_vars.sh

First I declare all the constant values (and paths) that will be needed to plot the script main.sh. It is also accessed by pre.sh and post.sh.


FILE_1="\${root_dir}/file_1.nc"    # The netcdf files containing my variables
FILE_2="\${root_dir}/file_2.nc"

lon1a=45    # Indian Ocean basin
lon1b=95                                     

lon2a=110    # Pacific Ocean basin
lon2b=280

titl="SST (anomalies)"    # for -B+t later

height=15    # defines the vertical size of my plot (I'm in cm)

# # Parameters for .cpt file
lim1=-5
lim2=5
inc=0.5
colr="-Bxafg -By+lK"    # for automatic annotation of the colorbar with unit "K(elvin)" at the top
barcolor="-DJRM+ef+w-$(echo \"scale=3; 0.95*${height};\" | bc)c/0.3c"   # Place the colorbar on the right of the plot with the anchor at middle height ... and add some nice style (will be discrete)

# # Canvas size
w1=$(echo "scale=3; (${lon1b} - ${lon1a})/10;" |bc)    # plot width of Indian Basin
w2=$(echo "scale=3; (${lon2b} - ${lon2a})/10;" |bc)    # plot width of Pacific Basin
gap=$(echo "scale=3; ${w1} + (${lon2a} - ${lon1b})/10;" |bc)    # Consistent space between the two

projection1="-JX${w1}cd/-${height}cT"                 # Cartesian projection with "d" for geo and "T" for absolute time
projection2="-JX${w2}cd/-${height}cT"

# gmt math -o0 -T1980-01-01T/2015-01-01T/1y T = times_start.txt     # Bug on macOS... wait for v6.2
# gmt math -o0 -T1984-12-31T/2019-12-31T/1y T = times_stop.txt
# awk '{printf "%s %s\n",$1,$1}' times_start.txt times_stop.txt > times_absolute.txt

The three last lines don’t work for me right now, but they are supposed to create a table where each line gives the T_start (col0) and T_stop (col1) for main.sh
It looks like this :

1996-01-01 2000-12-31
1997-01-01 2001-12-31
1998-01-01 2002-12-31

pre.sh

I didn’t really need this one as it can be done within the include_vars.sh, but it exists so I might as well illustrate it.
So pre.sh is used to create a hard copy (-H) of my color palette (cpt file). Like this I can create it once and call it in main.sh for all iterations (instead of having to create it at each iteration).

gmt begin
     gmt makecpt -T${lim1}/${lim2}/${inc} -Cvik -H > colr.cpt
gmt end

This colr.cpt is located in the folder test (which is the string value fed to -N batch option
If you were to run at this point :

gmt batch main.sh -Sbpre.sh -Ntest Ttimes_absolute.txt -Iinclude_vars.sh -Qs

you would end up with an arborescence similar to this :

  • working_dir ( main.sh pre.sh post.sh include_vars.sh times_absolute.txt )
    • test ( test_job_*.sh , colr.cpt )

main.sh

The plat de résistance !

Now the arborescence will look like this :

  • working_dir ( main.sh pre.sh post.sh include_vars.sh times_absolute.txt )
    • test ( test_job_*.sh , colr.cpt )
      • test_0 ( empty … then gmt.conf, test_0.pdf)
      • test_1 ( empty … then gmt.conf, test_1.pdf)

Where each sub-subfolder test_* is a parallelized active sesion.
Currently gmt6.2beta is not able to access gmt.conf if created before main.sh (ie include_vars, pre, or from a custom file) so it has to be copied into the active session.

day1=${BATCH_COL0}    # reads line by line times_absolute.txt
day2=${BATCH_COL1}
region1="-R${lon1a}/${lon1b}/${day1}/${day2}"
region2="-R${lon2a}/${lon2b}/${day1}/${day2}"

cp ${root_dir}/gmt.conf ./    # copy my custom conf into <session job_*>

gmt begin ${BATCH_NAME} pdf    # we're in working_dir/test/test_*/test_*.pdf
    # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # * Access the variable of interest in the netcdf file
    # # * Force color palette (NB: it's located in working_dir/test so ../)
    # # * x-axis tick and grid 10 deg, annote 20 deg
    # # * Primary y-axis tick and grid 1 month, no annotation
    # # * Secondary y-axis tick,grid and annotation 1 year
    # #          different style for primary and secondary
    # #          y-notation only shown left side of the Indian basin
    # # * Shift the Pacific by <gap> cm
    # # * Add the title above the Pacific basin
    # # * Add a vertical line at 180EW (dateline)
    # # * Add the stylized (discrete) colorbar
    gmt grdimage ${region1} ${projection1} ${FILE_ind}"?eqb_cl_sst" -C'../colr.cpt' -Bxa20f10g10 -Bpyf1Og1O -Bsya1Yf1Yg1Y -BWSen --MAP_GRID_PEN_PRIMARY=thinner,grey --MAP_GRID_PEN_SECONDARY=thin,black
    gmt grdimage ${region2} ${projection2} ${FILE_pac}"?eqb_cl_sst" -C'../colr.cpt' -Bxa20f10g10 -Bpyf1Og1O -Bsya1Yf1Yg1Y -BwSen+t"${titl}" -X${gap}c 
    gmt plot -W1.0,black,-. <<- END
        180 ${day1}
        180 ${day2}
    END
    gmt colorbar ${barcolor} -C'../colr.cpt' ${colr} -Np
gmt end

If you were to run :

gmt batch main.sh -Sbpre.sh -Ntest Ttimes_absolute.txt -Iinclude_vars.sh

After this, each of the sub-subfolder have been populated with a test_*.pdf then moved them to the working dir.

  • working_dir (test_0.pdf, test_1.pdf, …)
    • test (colr.cpt, …)
      • test_0 (emptied) … except for gmt.conf we copied there

And adding -Z batch options would clean every temporary file created in the process (including the sub folder test.

post.sh

Last part. If like myself you want to combine all these plot into a single pdf …

gs -dQUIET -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=${BATCH_PREFIX}.pdf -dBATCH ${BATCH_PREFIX}_*.pdf
rm -f ${BATCH_PREFIX}_*.pdf

Finally !

gmt batch main.sh -Sbpre.sh -Sfpost.sh -Ntest -Ttimes_absolute.txt -Iinclude_vars.sh -Z

1 Like