Ticket #670: eccodes_patch

File eccodes_patch, 5.4 KB (added by warrick, 4 years ago)

Shell script for patching ecCodes source

Line 
1#!/bin/sh
2
3# INSTRUCTIONS: This patch is intended to be used prior to compiling ecCodes. It
4# reduces the severity error/warning when ecCodes encodes a missing data value
5# to a BUFR file, otherwise an overwhelming number of warnings are output.
6# Within the ropp2bufr_eccodes and eum2bufr_eccodes tools,
7# ECCODES_BUFR_SET_TO_MISSING_IF_OUT_OF_RANGE is set to 1 to make sure
8# that a missing data value is set in out-of-range cases. If the ecCodes BUFR
9# library is to be used to write missing data to BUFR files by other means than
10# these tools, then this environment variable must be set to 1 manually.
11#
12# Users should provide the location of their unzipped ecCodes source,
13# 'eccodes_path', as a command line argument. If this is not set, the default
14# location in $ROPP_SRC will be used instead.
15
16# Location of un-tar'd eccodes source
17eccodes_path=${1:-$ROPP_SRC/eccodes-2.12.5-Source/}
18echo 'Using ecCodes path: '$eccodes_path
19# Quit if can't find eccodes_path
20if [[ ! -d "$eccodes_path" ]] ; then echo "ecCodes path '$eccodes_path' doesn't exist, please fix." ; exit ; fi
21
22# Check if version 2.16 or newer
23cd $eccodes_path
24package=eccodes-2
25for d in $(ls -d $eccodes_path/../$package*); do
26 thisdir=${d%%/} # save this directory/name
27 d=$(basename $d); d=${d#$1} # remove <path><pack> parts
28 v=$(expr index $d - 2>/dev/null) # remove anything else before first '-'...
29 if [[ v -le 0 ]]; then
30 v=$(expr index $d _ 2>/dev/null) # ... or '_' if no '-'
31 fi
32 if [[ v -gt 0 ]]; then
33 thisver=${d:$v} # what's left is <vers>
34 else
35 thisver="0" # assume files with no detectable version are 'v0'
36 fi
37 if [[ -z "$latestver" ]]; then # initialise with first file found
38 latestdir=$thisdir
39 latestver=$thisver
40 fi
41 if [[ $thisver > $latestver ]]; then # update if higher <vers>
42 latestdir=$thisdir
43 latestver=$thisver
44 echo 'latestver: '$latestver
45 fi
46done
47
48if [[ $latestver > 2.16 ]]
49then
50 version_2_16_onwards=true
51else
52 version_2_16_onwards=false
53fi
54echo '####### version_2_16_onwards: '$version_2_16_onwards
55
56# Check the file is there:
57if [ ! -f $eccodes_path/src/grib_accessor_class_bufr_data_array.c ]; then
58 echo "File not found, exiting. Make sure eccodes_path is set to top-level of unpacked ecCodes."
59 exit 1
60fi
61
62# If grep doesn't find 3 instances of the pattern, code may have changed. so don't proceed with the patch
63n_instances=$(grep -n "Setting it to missing value" $eccodes_path/src/grib_accessor_class_bufr_data_array.c | wc -l)
64if [ $n_instances != 3 ] ; then echo "Wrong number of instances found. Exiting" && exit 2 ; fi
65
66# Get line numbers where we need to edit
67first=$(grep -n "Setting it to missing value" $eccodes_path/src/grib_accessor_class_bufr_data_array.c | cut -f1 -d: | head -1 )
68second=$(grep -n "Setting it to missing value" $eccodes_path/src/grib_accessor_class_bufr_data_array.c | cut -f1 -d: | head -2 | tail -1)
69third=$(grep -n "Setting it to missing value" $eccodes_path/src/grib_accessor_class_bufr_data_array.c | cut -f1 -d: | tail -1 )
70
71first="$(($first-1))"
72second="$(($second-1))"
73third="$(($third-1))"
74
75# Make a backup of the file we're about to edit
76cp $eccodes_path/src/grib_accessor_class_bufr_data_array.c $eccodes_path/src/grib_accessor_class_bufr_data_array.c_backup
77
78# Check that backup is made
79if [[ ! -f $eccodes_path/src/grib_accessor_class_bufr_data_array.c_backup ]]; then
80 echo "Back up was not created. Check permissions? Exiting."
81 exit
82fi
83
84# Change the error type to GRIB_LOG_DEBUG
85if [ "$version_2_16_onwards" = false ]
86then
87 sed -i "$first"','"$first"'s/GRIB_LOG_ERROR/GRIB_LOG_DEBUG/' $eccodes_path/src/grib_accessor_class_bufr_data_array.c
88 sed -i "$second"','"$second"'s/GRIB_LOG_ERROR/GRIB_LOG_DEBUG/' $eccodes_path/src/grib_accessor_class_bufr_data_array.c
89 sed -i "$third"','"$third"'s/GRIB_LOG_ERROR/GRIB_LOG_DEBUG/' $eccodes_path/src/grib_accessor_class_bufr_data_array.c
90fi
91if [ "$version_2_16_onwards" = true ]
92then
93 sed -i "$first"','"$first"'s/ECCODES WARNING/GRIB_LOG_DEBUG/' $eccodes_path/src/grib_accessor_class_bufr_data_array.c
94 sed1_exit_code=$?
95 sed -i "$second"','"$second"'s/ECCODES WARNING/GRIB_LOG_DEBUG/' $eccodes_path/src/grib_accessor_class_bufr_data_array.c
96 sed2_exit_code=$?
97 sed -i "$third"','"$third"'s/ECCODES WARNING/GRIB_LOG_DEBUG/' $eccodes_path/src/grib_accessor_class_bufr_data_array.c
98 sed3_exit_code=$?
99fi
100
101#grep for GRIB_LOG_DEBUG on the relevant lines to test success
102if sed "$first"'q;d' $eccodes_path/src/grib_accessor_class_bufr_data_array.c | grep -q GRIB_LOG_DEBUG $eccodes_path/src/grib_accessor_class_bufr_data_array.c
103then
104
105 if sed "$second"'q;d' $eccodes_path/src/grib_accessor_class_bufr_data_array.c | grep -q GRIB_LOG_DEBUG $eccodes_path/src/grib_accessor_class_bufr_data_array.c
106 then
107
108 if sed "$third"'q;d' $eccodes_path/src/grib_accessor_class_bufr_data_array.c | grep -q GRIB_LOG_DEBUG $eccodes_path/src/grib_accessor_class_bufr_data_array.c
109 then
110
111 printf '\nSuccess. Look at the difference of grib_accessor_class_bufr_data_array.c
112 and grib_accessor_class_bufr_data_array.c_backup to make sure
113 the patch has been applied correctly. If it looks correct, delete
114 grib_accessor_class_bufr_data_array.c_backup\n\n'
115 fi
116 fi
117
118else
119
120 printf '\nThere was a problem applying the patch. Try double checking the eccodes exists
121 and is unpacked at the path you supplied for eccodes_path in this script.\n\n'
122
123fi