PK.PAT.RAD.DATA,
A^prefix,
B^report,
"FORMAT.REPORT",@1,
END;

[1]::FORMAT.REPORT
K(/T.REC),
0^T^THDR,
^$(R1)RT[report]^RAD.MAIN,
$(R1)RT[report]^MAIN.VAR,
"REPORT.DATA",@2,
IF{MAIN.VAR|3="CAN" "**SPECIMEN CANCELLED***"^/T.REC[T+1^T];
   IF{/CONF[.rad.usertf]&(+([RAD.MAIN,"RTF",RTF],LINE)^RTF) 1,
      "FORMAT.RTF",@3;
      "FORMAT.SECTIONS",@17},
   "Don't know if addenda also already included in RTF.",
   "Risk redundancy over exclusion. Similarly for REPORT.DATA.",
   "FORMAT.ADDENDA",@18},
END;

[2]::REPORT.DATA
MAIN.VAR|2^aa,
$(A)AA[aa]|3^/T.INFO["MRN"],
$(A)AA[aa]|0^/T.INFO["ACCT"],
[RAD.MAIN,"C"]|0^/T.INFO["TESTNAME"],
[RAD.MAIN,"T"]|0^/T.INFO["UPDATENODE"],
[RAD.MAIN,"DT"]|0_[RAD.MAIN,"DT"]|1^/T.INFO["TESTDATE"],
"RAD"^/T.INFO["TESTTYPE"],
"Header"^/T.INFO["HEADERSECTION"],
"Results"^/T.INFO["RPTSECTION"],
MAIN.VAR|3^/T.INFO["STATUS"],
"Report: "_[RAD.MAIN]|5^/T.HEADER[THDR+1^THDR],
"Status: "_[RAD.MAIN]|3^/T.HEADER[THDR+1^THDR],
" "^/T.HEADER[THDR+1^THDR],
"Orders",
DO{+([RAD.MAIN,"O",RO],XX)^RO "Order: "_XX|1^/T.REC[T+1^T],
                              " Procedure: "_&(R)RP[$(R)RO[XX|0]|5,$(R)RO[XX|0]|6]|1^/T.REC[T+1^T],
                              " Ordered by: "_&(G)GGG[$(R)RO[XX|0]|4]|1^/T.REC[T+1^T],
                              "Impressions",
                              "Impressions:"^/T.REC[T+1^T],""^OI,
                              DO{+([RAD.MAIN,"O",O,"I",OI],YY)^OI OI^/T.REC[T+1^T]}},
" "^/T.REC[T+1^T],
IF{[RAD.MAIN]|8^usr IF{&(G)GGG[usr] &(G)GGG[usr]|1^usr;"GET.USER",@19},
   "Dictated: "_usr_"   "_[RAD.MAIN,"T"]|7_" "_[RAD.MAIN,"T"]|8^/T.HEADER[THDR+1^THDR]},
IF{[RAD.MAIN]|9^usr "GET.USER",@19,
   "Transcribed: "_usr_"   "_[RAD.MAIN,"T"]|5_" "_[RAD.MAIN,"T"]|6^/T.HEADER[THDR+1^THDR]},
IF{[RAD.MAIN,"DT"]|11^usr "GET.USER",@19,
   "Signed: "_usr_"   "_[RAD.MAIN,"DT"]|9_" "_[RAD.MAIN,"DT"]|10^/T.HEADER[THDR+1^THDR]},
" "^/T.HEADER[THDR+1^THDR];

[3]::FORMAT.RTF
""^RTF,
DO{+([RAD.MAIN,"RTF",RTF],LINE)^RTF 0^POS,
                                    DO{LINE#POS^CHAR IF{"{"=CHAR "PUSH.STATE",@4;
                                                        "}"=CHAR "POP.STATE",@5;
                                                        "INVALID.STATE",@6;
                                                        "\"=CHAR "GET.CONTROL",@7;
                                                        "WRITE.CHAR",@9},
                                                     POS+1^POS},
                                    IF{(RTF>2)&TEXT SET.ID+1^SET.ID,
                                                       TEXT^/T.REC[T+1^T]},
                                    ""^TEXT};

[4]::PUSH.STATE
STATE+1^STATE;

[5]::POP.STATE
STATE-1^STATE;

[6]::INVALID.STATE
1'=STATE;

[7]::GET.CONTROL
""^CTRL^STOP,
POS+1^POS,
IF{LINE#POS^CHAR IF{"\"=CHAR @8;
                    "{"=CHAR @9,1^STOP;
                    "}"=CHAR @9,1^STOP;
                    IF{L(" \{}",CHAR)<4 1;
                       CHAR'?0AN @9;
                       ""} 1^STOP;
                    CTRL_CHAR^CTRL},
                 IF{STOP;
                    ""^STOP,
                    DO{POS+1^POS,
                       LINE#POS^CHAR&'STOP IF{IF{L(" \{}",CHAR)<4 1;
                                                 CHAR'?0AN @9;
                                                 ""} 1^STOP;
                                              CTRL_CHAR^CTRL}},
                    POS-2^POS}},
IF{"b"=CTRL @10;
   "i"=CTRL @11;
   "ul"=CTRL @12;
   "plain"=CTRL @13;
   "emdash"=CTRL @14;
   "tab"=CTRL @15;
   "rtf1"=CTRL @16;
   "JUNK"};

[8]::BACKSLASH
TEXT_""^TEXT;

[9]::WRITE.CHAR
IF{(E(CHAR)<32)!(E(CHAR)>126);
TEXT_CHAR^TEXT};

[10]::BOLD
1^ON,
TEXT_""^TEXT;

[11]::ITALICS
1^ON,
TEXT_""^TEXT;

[12]::UNDERLINE
1^ON,
TEXT_""^TEXT;

[13]::RESET.CHARACTER.PROPERTIES
TEXT_""^TEXT;

[14]::DASHES
TEXT_"--"^TEXT;

[15]::TAB
TEXT_"       "^TEXT;

[16]::RTF1
DO{POS+1^POS,LINE#POS^CHAR,"{"'=CHAR},
POS-1^POS;

[17]::FORMAT.SECTIONS
"Sections",
""^S,
DO{+([RAD.MAIN,"S",S],XX)^S "SECTION: "_S_IF{XX|4="Y" "  Final";"  Not Final"}^/T.REC[T+1^T],
                            DO{+([RAD.MAIN,"S",S,SL],YY)^SL YY^/T.REC[T+1^T]}};

[18]::FORMAT.ADDENDA
"Addenda",
""^ADD^S2^ADQ,
DO{+([RAD.MAIN,"S2",S2,"ADD",ADD,"R",ADQ],ZZ)^ADQ "  "_ADQ^/T.REC[T+1^T];
   +([RAD.MAIN,"S2",S2,"ADD",ADD],YY)^ADD "Addenda signed by: "_(&(G)GGG[YY|9]|1)_"  "_(YY|10)_"  "_(YY|11)^/T.REC[T+1^T],
                                          IF{YY|19 "Addenda co-signed by: "_(Y&(G)GGG[YYY|19]|1)_"  "_(YY|20)_"  "_(YY|21)^/T.REC[T+1^T]};
   +([RAD.MAIN,"S2",S2],XX)^S2};

[19]::GET.USER
IF{&(G)GU[usr]|6^ggg &(G)GGG[ggg]|1;
   &(HU)HUU[usr]|4;
   usr}^usr;