#!/bin/sh
#
# ---------------------------------------------------------------------
# Script Name: glofs_sfcmarobs.sh
#
# Location: /nwprod/ush
#
# Technical Contact:   	John G.W. Kelley             	Org:  NOS/CSDL
#                      	Phone:  603-862-1628    	E-Mail: John.Kelley@noaa.gov
#
# Author:    John G.W. Kelley   Date:  10/22/2004 
#
# Revisions:   none
#        12/14/2004   Add code for C-MAN station
#        12/15/2004   Add code for fixed buoys
#        06/06/2005   Z Bronder   Made code to make CORMS flags
#        07/14/2005   Added remarks
#        08/18/2005   Z Bronder   Lowered the input file size limits for CORMS flags
#                                 to reflect recent file sizes. 
#
# Abstract:   Core script to download hourly file of sfc marine observations from 
#             fixed buoys, drifing buoys, VOS ships, airway stations (metar), and C-MAN
#             station
#
# Usage:      Interactive:  glofs_sfcmarobs.sh YYYY MM DD HH
#             Via cron glofs_sfcmarobs.sh is called by  scripts exglofs.sh.sms
#
# Language:   Bourne shell script  
#
# Target Computer:  IBM CCC prodsp.ncep.noaa.gov
#
# Script Execution Time: 20 to 21 minutes past top of the hour 
#
# Programs Called:
#           Name                          Location                             Description 
#
# Input Files:
#    Unit No.       Name                         Location                      Description
#    none  glofs.tHHz.metars.bufr_d.unblk /com/hourly/hourly.YYYYMMDD   Unblocked BUFR file of sfc metar obs 
#    none  glofs.tHHz.marine.bufr_d.unblk /com/hourly/hourly.YYYYMMDD   Unblocked BUFR file of sfc marine obs
#
# Output Files: 
#    Unit No.        Name              Location                                Description
#      none  glofs.tHHz.metars.bufr_d.blk                       Reblocked BUFR file of sfc metar obs  
#      none  glofs.tHHz.marine.bufr_d.blk                       Reblocked BUFR file of sfc marine obs
#      none  YYYYMMDDHH-marobs.txt                                 Text file of sfc marobs
#
# Remarks: The output text files include these fields: 
# date time station_id platform_type agency lat lon water_body cloud_cover% mean_sealevel_pressure(mb) airtemp(C)
# dew_point_temp(C) wind_direction(direction from, 0 degrees is from north) wind_speed(meters/sec) peak_wind_gust(m/s) wind_speed_10m_AGL(m/s) SST(C)
# See http://www.ccom.unh.edu/mmap/odaas/odaas-marobs-main.htm for details
# 
# ------------------------------------------------------------------------------------------------------------
#
# Check if sufficient number of arguments entered

if test $# != 4 
  then
    echo ***Error: You must supply 4 arguments for selected hour 
    echo 'Example: glofs_sfcmarobs.sh 2005 09 16 00'
    exit
fi
 
YYYY=$1
MM=$2
DD=$3
HH=$4
CYCLE=${HH}00
YYYYMMDD=$YYYY$MM$DD
 
echo 'The script proc-bufr.sh' $YYYY $MM $DD $HH 'has started'
 
echo ' '
echo 'INFORMATION ON REQUESTED HOURLY OBSERVATION FILE'
echo '  date/time (Greenwich Day)'
echo '    year      :'  $YYYY
echo '    month     :'  $MM
echo '    day       :'  $DD
echo '    hour (UTC):'  $HH
echo '    date      :'  $YYYYMMDD
echo ' '
 
echo ' '
echo 'Location of NCEP CCS Output is: ' $FtpIBMserver
 
# Set sleep parameters

SLEEPTIME=900
maxsleeps=2
sleepcount=0
MN=00
MIN=`date +%M`
 
# Set output file names

BLKMETARSBUFRFL='glofs.t'$HH'z.metars.bufr_d.blk'
BLKMARINEBUFRFL='glofs.t'$HH'z.marine.bufr_d.blk'
 
TMPMAROBSTXTFL='recentday-'$HH$MIN'z-marobs.txt'
MAROBSTXTFL=$YYYY$MM$DD$HH$MN'_sfcmarobs.txt'
 
# -----------------------------------------------------------------------------------------------------
# Create control file for FORTRAN program which includes directories
# for input and output files
 
echo " &IOFILES " > decodebufr.ctl
echo "  BUOYFIXSTNLIST='"$FIXglofs/glofs_buoy_list"'," >> decodebufr.ctl
echo "  CMANSTNLIST='"$FIXglofs/glofs_cman_list"'," >> decodebufr.ctl
echo "  METARSTNLIST='"$FIXglofs/glofs_metar_list"'," >> decodebufr.ctl
echo "  BLKMETARSBUFRFL='glofs.$cycle.metars.bufr_d'," >> decodebufr.ctl
echo "  BLKMARINEBUFRFL='glofs.$cycle.marine.bufr_d'," >> decodebufr.ctl
echo "  MAROBSTXTFL='"$TMPMAROBSTXTFL"'">> decodebufr.ctl
 
echo " &END" >> decodebufr.ctl
 
echo 'Decode the BUFR files using create-marobs program.'
$EXECglofs/glofs_marobs  < decodebufr.ctl > $pgmout 2>errfile
export err=$?; err_chk
 
#  Sort the obs in the text file

echo 'Sort the obs in the text file.'
sort $TMPMAROBSTXTFL >> tmpmarobs.txt
 
#  Remove Duplicates

echo 'Remove duplicates the obs in the text file.'
uniq tmpmarobs.txt $TMPMAROBSTXTFL
 
# --------------------------------------------------------------------------------------------------------
#  Archive the text and netCDF files of surface meteorological observations

echo 'Archive the text files of surface meteorological observations'
cp $TMPMAROBSTXTFL $COMOUT/$MAROBSTXTFL
 
echo 'END'

