This example shell script retrieves a GRIB input file from ECFS and then:

  • compiles and links a Fortran program with the Magics++ library and

  • executes the program to create a PDF file

The SBATCH directives specify a time at which the job script should be run.


#!/bin/bash
#
# **************************** LICENSE START ***********************************
#
# Copyright 2021 ECMWF. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************
#
# magicsjob         USER SERVICES  NOVEMBER 2021 - ECMWF 
#
# 
#
#       This shell-script:
#
#             - gets a GRIB input file from ECFS
#             - compiles and links a Fortran program with the
#               MAGICS++ library  
#             - executes the program to create a PDF file
#             - specifies a time when to run the job script 
#
#       This shell script produces the standard output file 
# 
#         magicsjob.<JOB-ID>.out
#
#       in the workdir directory. 
#
#       For more information on MAGICS please refer to 
#
#       https://confluence.ecmwf.int/display/MAGP/Magics 
#
#       WARNING: In this example, the input data file is taken from ECFS.
#
#-------------------------------
# setting options for SLURM
#-------------------------------
# Options that are specified within the script file should precede the
# first executable shell command in the file. 
# All options, which are case sensitive, are preceded by #SBATCH. 
# These lines become active only when the script is submitted
# using the "sbatch" command. 
# All job output is written to the workdir directory, by default. 


#SBATCH --qos=ef

        # Specifies that your job will run in the queue (Quality Of
        # Service) "ef".

#SBATCH --job-name=magicsjob

        # Assigns the specified name to the request

#SBATCH --output=magicsjob.%j.out

        # Specifies the name and location of STDOUT where %j is the job-id
        # The file will be # written in the workdir directory if it is a 
        # relative path. If not given, the default is slurm-%j.out in the
        # workdir.

#SBATCH --error=magicsjob.%j.out

        # Specifies the name and location of STDERR where %j is the job-id
        # The file will be # written in the workdir directory if it is a 
        # relative path. If not given, the default is slurm-%j.outin the
        # workdir.

#SBATCH --chdir=/scratch/...

        # Sets the working directory of the batch script before it is
        # executed.

#SBATCH --mail-type=FAIL
        
        # Specifies that an email should be sent in case the job fails.
        # Other options include BEGIN, END, REQUEUE and ALL (any state
        # change).

#SBATCH --time=00:05:00

        # Specifies that your job my run up to HH:MM:SS of wall clock
        # time. The job will be killed if it exceeds this limit. If
        # time is not defined, the default limit for the queue (qos)
        # will be used.

#SBATCH --begin=2021-01-30T13:30:00
        # Specifies when you want to run the job step. If not
        # specified, the current date and time are used. The syntax
        # is: startdate = DATE TIME.  
        # DATE is expressed as MM/DD/YYYY, and TIME is expressed as
        # HH:mm(:ss).  If you specify DATE but not TIME, the default
        # start time is 00:00:00. This means that the job will start
        # as soon as possible on the specified date. If you specify
        # TIME but not DATE, the default start date is the current
        # day.

#-------------------------------
# setting environment variables
#-------------------------------

export PATH=$PATH:.             # Allows you to run any of your programs or
                                # scripts held in the current directory (not 
                                # required if already done in your .user_profile 
                                # or .user_kshrc)

set -ev

#-------------------------------
# commands to be executed
#-------------------------------

cd $SCRATCHDIR                  # All the files created in this directory will
                                # be deleted when the job terminates.


# cat is used to read the Fortran program from the input stream 
# and to write it to the file 'contour.f'.
# cat reads line by line until it reaches the line EOF.
#
cat >contour.f <<\EOF

      program ex
c
      dimension rlev (5)
      data rlev /500.,530.,560.,575.,590./
c
c     open magics and define pdf device  
c     output file name: output.pdf in portrait mode
c
      call popen
      call psetc ('output_format','pdf')                   
      call psetc ('output_name','output')
      
 
      call psetr('super_page_x_length', 21.)
      call psetr('super_page_y_length', 29.7)
      call psetr('page_x_length', 21.)
      call psetr('page_y_length', 29.7)
c
c     define the geographical area 
c     full global mercator projection
c
      call psetc ('subpage_map_projection','mercator')
      call pseti ('map_coastline_thickness',1)
      call psetc ('map_coastline_colour','charcoal')
      call psetc ('map_grid_colour','grey') 
      call psetc ('map_label_colour','charcoal')
      call psetc ('map_grid_line_style','dash')
     
      call pcoast
      
c
c     pass the data to magics
c
                
      call psetc ('grib_input_file_name','contour')
      call pgrib

c
c     solid blue contours defined by list of levels
c
      call psetc ('contour_level_selection_type','level_list')
      call pset1r ('contour_level_list',rlev,5)
      call psetc ('contour_line_colour','navy')
      call psetc ('contour_highlight_colour','navy')
      call psetc ('contour_line_style','solid')     
      call pseti ('contour_label_frequency',1)      
      call pcont
c
c     set title
c
      call pset1c ('text_lines',
     +     (/'Full global Mercator projection'/), 1)
      call psetc ('text_colour', 'charcoal')
      call psetr ('text_font_size', 0.6)
      call ptext
c
c     close magics
c
      call pclose
      END
EOF

#-------------------------------
# Retrieve the actual GRIB input data 
# from ECFS into a target data file 'contour'.
#-------------------------------

ecp ec:/usc/public/job_ex_in/REG500  contour

if [ $? != 0 ]                          # check ecp exit code
then
      echo " The ECFS retrieve has failed."
      exit 1
fi


#-------------------------------
# load the GNU compiler
#-------------------------------

module load prgenv/gnu   # Or "module load pg"

#-------------------------------
# load the default Magics module  
#-------------------------------

module load ecmwf-toolbox

#-------------------------------
# compile and link
#-------------------------------

gfortran -march=native -O3 -o magics_exe contour.f $MAGPLUSLIB_SHARED      

#-------------------------------
# execution
#-------------------------------
./magics_exe    # The PDF file to be plotted is created and
                # called 'output.pdf'. 

mv output.pdf $HOME/contour.pdf

                # It is recommended to save the PDF file 'contour.pdf'
                # for example in your $HOME directory.


#-------------------------------# To preview on screen type: 
# previewing                    # xdg-open $HOME/contour.pdf
#-------------------------------#

#-------------------------------
# tidy up by deleting unwanted files
#-------------------------------
# This is done automatically when using $SCRATCHDIR.

exit 0

# End of example job 'magicsjob'