| 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 | 
|---|
| 17 | eccodes_path=${1:-$ROPP_SRC/eccodes-2.12.5-Source/} | 
|---|
| 18 | echo 'Using ecCodes path: '$eccodes_path | 
|---|
| 19 | # Quit if can't find eccodes_path | 
|---|
| 20 | if [[ ! -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 | 
|---|
| 23 | cd $eccodes_path | 
|---|
| 24 | package=eccodes-2 | 
|---|
| 25 | for 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 | 
|---|
| 46 | done | 
|---|
| 47 |  | 
|---|
| 48 | if [[ $latestver > 2.16 ]] | 
|---|
| 49 | then | 
|---|
| 50 | version_2_16_onwards=true | 
|---|
| 51 | else | 
|---|
| 52 | version_2_16_onwards=false | 
|---|
| 53 | fi | 
|---|
| 54 | echo '####### version_2_16_onwards: '$version_2_16_onwards | 
|---|
| 55 |  | 
|---|
| 56 | # Check the file is there: | 
|---|
| 57 | if [ ! -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 | 
|---|
| 60 | fi | 
|---|
| 61 |  | 
|---|
| 62 | # If grep doesn't find 3 instances of the pattern, code may have changed. so don't proceed with the patch | 
|---|
| 63 | n_instances=$(grep -n "Setting it to missing value" $eccodes_path/src/grib_accessor_class_bufr_data_array.c | wc -l) | 
|---|
| 64 | if [ $n_instances != 3 ] ; then echo "Wrong number of instances found. Exiting" && exit 2 ; fi | 
|---|
| 65 |  | 
|---|
| 66 | # Get line numbers where we need to edit | 
|---|
| 67 | first=$(grep -n "Setting it to missing value" $eccodes_path/src/grib_accessor_class_bufr_data_array.c | cut -f1 -d: | head -1 ) | 
|---|
| 68 | second=$(grep -n "Setting it to missing value" $eccodes_path/src/grib_accessor_class_bufr_data_array.c | cut -f1 -d: | head -2 | tail -1) | 
|---|
| 69 | third=$(grep -n "Setting it to missing value" $eccodes_path/src/grib_accessor_class_bufr_data_array.c | cut -f1 -d: | tail -1 ) | 
|---|
| 70 |  | 
|---|
| 71 | first="$(($first-1))" | 
|---|
| 72 | second="$(($second-1))" | 
|---|
| 73 | third="$(($third-1))" | 
|---|
| 74 |  | 
|---|
| 75 | # Make a backup of the file we're about to edit | 
|---|
| 76 | cp $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 | 
|---|
| 79 | if [[ ! -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 | 
|---|
| 82 | fi | 
|---|
| 83 |  | 
|---|
| 84 | # Change the error type to GRIB_LOG_DEBUG | 
|---|
| 85 | if [ "$version_2_16_onwards" = false ] | 
|---|
| 86 | then | 
|---|
| 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 | 
|---|
| 90 | fi | 
|---|
| 91 | if [ "$version_2_16_onwards" = true ] | 
|---|
| 92 | then | 
|---|
| 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=$? | 
|---|
| 99 | fi | 
|---|
| 100 |  | 
|---|
| 101 | #grep for GRIB_LOG_DEBUG on the relevant lines to test success | 
|---|
| 102 | if 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 | 
|---|
| 103 | then | 
|---|
| 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 |  | 
|---|
| 118 | else | 
|---|
| 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 |  | 
|---|
| 123 | fi | 
|---|