#!/bin/csh -f
#
# met_ingest2.scr - convert a grib file, in GRIB 2 format to a seawifs met file
#
#  Usage:
#     met_ingest2.scr <file>
#
#     where <file> is the grib 2 file from NCEP (gdas1.t12z.pgrbf00.grib2,
#     for example)
#
# get the time string and the binary data for the desired grid and check 
# that it is non-zero
#
set badstat = 0
#
#  Zonal, U component wind
wgrib2 $1 -match "UGRD:10 m above ground" -bin z_wind_grib2.dat
if ( -z z_wind_grib2.dat ) then
  echo "z_wind grid was not found"
  set badstat = 1
else
  set gtime = `wgrib2 $1 -t -match "UGRD:10 m above ground"`
endif
echo z_wind_grib2.dat time string: $gtime
#
#  meridional, V component wind
wgrib2 $1 -match "VGRD:10 m above ground" -bin m_wind_grib2.dat
if ( -z m_wind_grib2.dat ) then
  echo "m_wind grid was not found"
  set badstat = 1
else
  set gtime = `wgrib2 $1 -t -match "VGRD:10 m above ground"`
endif
echo m_wind_grib2.dat time string: $gtime
#
#  Pressure at 1000 mb
wgrib2 $1 -match "PRMSL:mean sea level" -bin press_grib2.dat
if ( -z press_grib2.dat ) then
  echo "pressure grid was not found"
  set badstat = 1
else
  set gtime = `wgrib2 $1 -t -match "PRMSL:mean sea level"`
endif
echo press_grib2.dat time string: $gtime
#
#  Precipitable water
wgrib2 $1 -match "PWAT:entire atmosphere" -bin p_water_grib2.dat
if ( -z p_water_grib2.dat ) then
  echo "precipitable water grid was not found"
  set badstat = 1
else
  set gtime = `wgrib2 $1 -t -match "PWAT:entire atmosphere"`
endif
echo p_water_grib2.dat time string: $gtime
#
#  Relative humidity
wgrib2 $1 -match "RH:1000 mb" -bin rel_hum_grib2.dat
if ( -z rel_hum_grib2.dat ) then
  echo "relative humidity grid was not found"
  set badstat = 1
else
  set gtime = `wgrib2 $1 -t -match "RH:1000 mb"`
endif
echo rel_hum_grib2.dat time string: $gtime
#
#
# If all params there, run ancnrt and make the seawifs file
# If any param is not there, return a bad status
#
if ( $badstat == 0 ) then
ancnrt ${OCDATAROOT}/common/fillenv_met.ancnrt z_wind_grib2.dat m_wind_grib2.dat press_grib2.dat p_water_grib2.dat rel_hum_grib2.dat ./ -2 $gtime
  echo We just made all the binary fields
  set badstat = $status
endif
#
rm z_wind_grib2.dat m_wind_grib2.dat press_grib2.dat p_water_grib2.dat rel_hum_grib2.dat
#
exit ( $badstat )
