#! /bin/csh -f

set echo

set ID = $1
set SERIES = hmi.lev1_nrt
set SERIES_HG = $SERIES"_hgpatch"
# set PARMS = ( -t locunits=pixels boxunits=pixels t_ref=2011.12.15_20:50_TAI x=3900 y=1500 width=400 height=400 requestid=$ID)
set PARMS = ( -t locunits=pixels boxunits=pixels t_ref=2011.12.15_20:50_TAI x=1400 y=1500 width=400 height=400 requestid=$ID)

set prev_count = `show_info -cq $SERIES_HG'[][][]['$ID']'`
set new_sn = `show_info -q $SERIES'[2011.12.15/2d][? FSN < 400000000 ?][? EXPTIME < 1.0 AND EXPTIME > 0.05 ?]' n=-1 key=FSN `
if ($prev_count > 0) then
  set prev_sn = `show_info -q $SERIES_HG'[][$][]['$ID']' key=FSN`
  @ prev_sn = $prev_sn + 1
else
  @ prev_sn = $new_sn - 2
endif

im_patch $PARMS in=$SERIES'[]['$prev_sn'-'$new_sn'][? EXPTIME < 1.0 AND EXPTIME > 0.05 ?]' log=hg_patch.log 
