#!/bin/ksh 
export LANG=US

scr=/share/ioper/tempo/MERGE/GOES/OBS/SEM_DUPLICATA

 data=20241201
dataf=20250301

while [ ${data} -le ${dataf} ]; do
 echo $data
 d_in=/share/ioper/tempo/MERGE/GOES/OBS/SEM_DUPLICATA/${data:0:6}
 cd "$d_in"

 cat OBS_SAT_REG_${data}??.txt > TMP_${data}.TXT

# Processamento com AWK - substitui o loop
awk '
{
  id = $1
  nome = $2
  lon = $3
  lat = $4
  val4 = $5
  val5 = $6
  reg = $7

  soma4[id] += val4
  soma5[id] += val5
  nome_id[id] = nome
  lon_id[id] = lon
  lat_id[id] = lat
  reg_id[id] = reg
}
END {
  for (id in soma4) {
    printf "%s %s %s %s %.2f %.2f %s\n", id, nome_id[id], lat_id[id], lon_id[id], soma4[id], soma5[id], reg_id[id]
  }
}' TMP_${data}.TXT > PREC_TMP_${data}.TXT

 # Tempo formatado
 time=$(date -d "${data} 00:00" +"%HZ%d%b%Y")

 # Rodando script GRADS
 rm -f OBS_SAT_CLI_REG_${data}.TXT
 grads -blc "run $scr/find_IDs_problem.gs  $time  PREC_TMP_${data}.TXT OBS_SAT_CLI_REG_${data}.TXT"
 python $scr/Control_Diario.py OBS_SAT_CLI_REG_${data}.TXT Aceita_${data}.TXT Rejeitadas_${data}.TXT
 cat Rejeitadas_${data}.TXT |awk '{print "^"$1"|"}'| xargs |sed "s@| @|@g"   |sed -r 's/.$//g' |sed "s@|^@ |^@g" > ID_problem_${data}.TXT
 # Limpeza
 rm -f PREC_TMP_${data}.TXT TMP_${data}.TXT
  data=`date -d "${data:0:8} 00:00 24 hours" +"%Y%m%d"`
done

exit
