PK.RADKEY,
A^prefix,
@OPEN.RAD,
@PATS,
END;

OPEN.RAD
@admdir,
O(&,@misdict),
O(:,@admdata),
@raddir,
O(*,@raddata),
O(\,@radrpt),
@pkdir

PATS
/ARGV[1]^DT,
2^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^ACT,
   :AAAI[ACT]^aa,
   @RAD,
   nr+1^nr}

RAD
:AA[aa]|4^ea,""^rpt,
DO{+*RAD.PT.RPT[ea,rpt]^rpt IF{'/CONF[.results.rad.nostatus][*RAD.RPT[rpt]ST] @SEND}}

SEND
"taking the latest time into SEC",
-(*RAD.RPT[rpt]ED[""],XX)^ed,XX|0^SEC1,
*RAD.RPT[rpt]SOV^SEC2,
*RAD.RPT[rpt]S^SEC3,
*RAD.RPT[rpt]ES|0^SEC4,
"data in ES|0 appears to be seconds followed by a counter, at least at one site",
SEC4$L(SEC4,".")^SEC4,
(SEC1)!(SEC2)!(SEC3)!(SEC4)^SEC,
""^rdt^rex,
*RAD.RPT[rpt]PT^mpi,
DO{+*RAD.RPT[rpt]EX[rdt,rex]^rex "got an exam",
      *RAD[mpi]EX[rdt]EX[rex]AA^exam.aa,
      IF{*RAD[mpi]EX[rdt]EX[rex]AT^arriv.time "got arrival time",
            %Z.date.clin.to.npr(rdt);
         "no arrival time, use TCHTM",
            %Z.date.in.magic(*RAD[mpi]EX[rdt]EX[rex]TCHTM)}^exam.dt,
      IF{(aa=exam.aa)&(DT'>exam.dt) @SEND.EXAM};
   +*RAD.RPT[rpt]EX[rdt]^rdt}

SEND.EXAM
"<Document>"^!,
"<InstanceMnemonic>"_prefix_rpt_"."_rdt_"."_rex_"</InstanceMnemonic>"^!,
"append x to key to ensure that its not empty",
"<Key>"_"x"_SEC_"</Key>"^!,
"<PersonMnemonic>"_:AA[aa]|3_"</PersonMnemonic>"^!,
"<EventEndDateTime>"_exam.dt_"</EventEndDateTime>"^!,
"<EventSetMnemonic>RAD</EventSetMnemonic>"^!,
"</Document>"^!
