                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                se {
	print STDERR 'Old Start Symbol could not be kept for A record: ',$_;
	$outfld[6] = '0';
    }
    # end symbol 
    if (defined $sym{$fld[15]}) {
        $outfld[7] = $sym{$fld[15]};  # to keep old symbol
    } else {
	print STDERR 'Old End Symbol could not be kept for A record: ',$_;
	$outfld[7] = '0';
    }
    # percent done date
    $outfld[8] = &ymd_to_utc($fld[18], $fld[16], $fld[17]);

    # percent done
    $outfld[9] = $fld[19];

    # baseline start date
    $outfld[10] = &ymd_to_utc($fld[23], $fld[21], $fld[22]);

    # baseline end date
    $outfld[11] = &ymd_to_utc($fld[26], $fld[24], $fld[25]);

    # fill pattern
    $outfld[12] = $fld[20];
    # layer
    $outfld[13] = $fld[27];
    # display time
    $outfld[14] = '0';  
    # horiz text position
    $outfld[15] = '2';
    # vert text position
    $outfld[++$#outfld] = '2';
    # font
    $outfld[++$#outfld] = '0';   
    # point
    $outfld[++$#outfld] = $font_size;
    # color
    $outfld[++$#outfld] = $color;
    # text
    $outfld[++$#outfld] = '""';
    # idtext
    $outfld[++$#outfld] = '""';
    # idtog
    $outfld[++$#outfld] = '0';
    # idhpos
    $outfld[++$#outfld] = '0';
    # idvpos
    $outfld[++$#outfld] = '2';

    $recout[++$#recout] = join(' ',@outfld);
}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_B_record       
#
# DESCRIPTION
#       Translate level record
#
##########################################################################
#
sub translate_B_record {

    $outfld[0] = $fld[0];
    # X offset
    if ($fld[1] + $fld[4] > $max_x_offset) {
	$outfld[1] = $max_x_offset;
    } else {
	$outfld[1] = $fld[1] + $fld[4];
    }
    # Y offset
    if ($fld[2] + $fld[4] > $max_y_offset) {
	$outfld[2] = $max_y_offset;
    } else {
	$outfld[2] = $fld[2] + $fld[4];
    }
    # Text
    $outfld[3] = $fld[7];
    # font
    if ((($fld[5] >= '0') && ($fld[5] <= '5')) || ($fld[5] eq '12')) {
	$outfld[4] = $font{'0'};
    } else {
	$outfld[4] = $font{'1'};
    }
    # point
    if (($fld[5] eq '3') || ($fld[5] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[5] = $point{$fld[5]};
    # radius
    $outfld[6] = $fld[4];
    # page
    $outfld[7] = $fld[3];
    # layer
    $outfld[8] = $fld[6];
    # color
    $outfld[9] = $color;

    $recout[++$#recout] = join(' ', @outfld);
}
#
#

##########################################################################
#
# SUBROUTINE
#       translate_C_record       
#
# DESCRIPTION
#       Translate the chart record (only one per file)
#
##########################################################################
#
sub translate_C_record {
    $outfld[0] = $fld[0];
    # start date
    $outfld[1] = &ymd_to_utc($fld[3], $fld[1], $fld[2]);

    # end date
    $outfld[2] = &ymd_to_utc($fld[6], $fld[4], $fld[5]);

    # status date
    $outfld[3] = &ymd_to_utc($fld[9], $fld[7], $fld[8]);

    # status flag
    $outfld[4] = $fld[10];
    # font
    if ((($fld[11] >= '0') && ($fld[11] <= '5')) || ($fld[11] eq '12')) {
	$outfld[5] = $font{'0'};
    } else {
	$outfld[5] = $font{'1'};
    }
    # point
    if (($fld[11] eq '3') || ($fld[11] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[6] = $point{$fld[11]};
    # color
    $outfld[7] = $color;
    # year flag
    if ($fld[12] eq '0') {
	$outfld[8] = '1';
    } elsif ($fld[12] eq '1') {
	$outfld[8] = '2';
    } elsif ($fld[12] eq '2') {
        $outfld[8] = '0';
    }
    # resolution
    if ($fld[13] > 5) {
        print STDERR 'Reset the resolution in plot setup for DAILY or DOW';
    }
    $outfld[9] = $fld[13];
    # layer
    $outfld[10] = '0';
    # utc_bar	
    if (defined $utc) {
        $outfld[11] = $utc;
    } else {
	$outfld[11] = '0';
    }
    # vgrid		# 0 = solid    
    $outfld[12] = '0';

    $C_line = join(' ',@outfld);
}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_D_record       
#
# DESCRIPTION
#       Translate simple activity record
#
##########################################################################
#
sub translate_D_record {
    
    $outfld[0] = $fld[0];
    # start date
    $outfld[1] = &ymd_to_utc($fld[3], $fld[1], $fld[2]);

    # end date
    $outfld[2] = &ymd_to_utc($fld[6], $fld[4], $fld[5]);

    # line
    $outfld[3] = $fld[7];
    # start symbol (none)
    if (defined $sym{$fld[8]}) {
        $outfld[4] = $sym{$fld[8]};      # to keep old symbol
    } else {
	print STDERR 'Old Start Symbol could not be kept for D record: ',$_;
	$outfld[4] = '0';
    }
    # end symbol 
    if (defined $sym{$fld[9]}) {
        $outfld[5] = $sym{$fld[9]};  # to keep old symbol
    } else {
	print STDERR 'Old End Symbol could not be kept for D record: ',$_;
	$outfld[5] = '0';
    }
    # percent date
    $outfld[6] = &ymd_to_utc($fld[12], $fld[10], $fld[11]);

    # done percent
    $outfld[7] = $fld[13];
    # fill pattern
    $outfld[8] = $fld[14];
    # layer
    $outfld[9] = $fld[15];
    # display time
    $outfld[10] = $fld[16];
    # horizontal text position (left, center, right)
    $outfld[11] = $fld[17];
    # vertical text position (above, below, center)
    $outfld[++$#outfld] = '2';
    # font
    if ((($fld[18] >= '0') && ($fld[18] <= '5')) || ($fld[18] eq '12')) {
	$outfld[++$#outfld] = $font{'0'};
    } else {
	$outfld[++$#outfld] = $font{'1'};
    }
    # point
    if (($fld[18] eq '3') || ($fld[18] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[++$#outfld] = $point{$fld[18]};
    # color
    $outfld[++$#outfld] = $color;
    # text
    @text_flds = split('"',$_);
    $outfld[++$#outfld] = '"' . $text_flds[1] . '"';
    # idtext
    $outfld[++$#outfld] = '""';
    # idtog
    $outfld[++$#outfld] = '0';
    # idhpos
    $outfld[++$#outfld] = '0';
    # idvpos
    $outfld[++$#outfld] = '2';


    $recout[++$#recout] = join(' ', @outfld);
}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_E_record       
#
# DESCRIPTION
#       Translate event record
#
##########################################################################
#
sub translate_E_record {
     
    $outfld[0] = $fld[0];
    # start date
    if (length($fld[3]) < 4) {
	if ($fld[3] < 40) {
	    $fld[3] = '20' . $fld[3];
   	} else {
	    $fld[3] = '19' . $fld[3];
	}
    }
    $outfld[1] = &ymd_to_utc($fld[3], $fld[1], $fld[2]);

    # line
    $outfld[2] = $fld[4];
    # symbol
    if (defined $sym{$fld[5]}) {
        $outfld[3] = $sym{$fld[5]};      # to keep old symbol
    } else {
	print STDERR 'Old Start Symbol could not be kept for E record: ',$_;
	$outfld[3] = '0';
    }
    # label char in quotes 
    $outfld[4] = $fld[12];
    # moffset 	
    $outfld[5] = '0';
    @text = split('"', $_);
    $outfld[6] = '"' . $text[3] . '"';
    # text2                
    $outfld[7] = '"' . $text[5] . '"';
    @text = ();
    # font
    if ((($fld[6] >= '0') && ($fld[6] <= '5')) || ($fld[6] eq '12')) {
	$outfld[8] = $font{'0'};
    } else {
	$outfld[8] = $font{'1'};
    }
    # point
    if (($fld[6] eq '3') || ($fld[6] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[9] = $point{$fld[6]};
    # hpos1
    if ($fld[7] eq '1') {
	$outfld[10] = '2';
    } elsif ($fld[7] eq '2') {
	$outfld[10] = '1';
    } else {
        $outfld[10] = $fld[7];
    }
    # hpos2
    if ($fld[9] eq '1') {
	$outfld[11] = '2';
    } elsif ($fld[9] eq '2') {
	$outfld[11] = '1';
    } else {
        $outfld[11] = $fld[9];
    }
    # vpos1
    if ($fld[8] eq '1') {
	$outfld[12] = '2';
    } elsif ($fld[8] eq '2') {
	$outfld[12] = '1';
    } else {
        $outfld[12] = $fld[8];
    }
    # vpos2
    if ($fld[10] eq '1') {
	$outfld[13] = '2';
    } elsif ($fld[10] eq '2') {
	$outfld[13] = '1';
    } else {
        $outfld[13] = $fld[10];
    }
    # layer
    $outfld[14] = $fld[11];
    # color
    $outfld[15] = $color;
    # disp
    $outfld[16] = '0';
    # idtext
    $outfld[17] = '""';
    # idtog
    $outfld[18] = '0';
    # idhpos
    $outfld[19] = '0';
    # idvpos
    $outfld[20] = '2';
  
    $recout[++$#recout] = join(' ', @outfld);
}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_H_record       
#
# DESCRIPTION
#       Translate horizontal line record
#
##########################################################################
#
sub translate_H_record {

    foreach $i (0 .. 4) {
        $outfld[$i] = $fld[$i];
    }
    $outfld[5] = $color;
    # font
    if ((($fld[5] >= '0') && ($fld[5] <= '5')) || ($fld[5] eq '12')) {
	$outfld[6] = $font{'0'};
    } else {
	$outfld[6] = $font{'1'};
    }
    # point
    if (($fld[5] eq '3') || ($fld[5] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[7] = $point{$fld[5]};
    # grid
    $outfld[8] = $fld[6];
    # text
    @text_flds = split('"',$_);
    $outfld[++$#outfld] = '"' . $text_flds[1] . '"';

    $H_line[++$#H_line] = join(' ', @outfld);
}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_I_record       
#
# DESCRIPTION
#       Translate image record
#
##########################################################################
#
sub translate_I_record {
    
    print STDERR 'Change icon file to bitmap file: ',$fld[3];
    $outfld[0] = $fld[0];
    # X offset
    if ($fld[1] > $max_x_offset) {
	$outfld[1] = $max_x_offset;
    } else {
	$outfld[1] = $fld[1];
    }
    # Y offset
    if ($fld[2] > $max_y_offset) {
	$outfld[2] = $max_y_offset;
    } else {
	$outfld[2] = $fld[2];
    }
    # scale (1,2,3,4)                
    $outfld[3] = '1';
    # color
    $outfld[4] = $color;
    # layer
    $outfld[++$#outfld] = '0';
    # file
    # get rid of double quote 
    substr($fld[3],0,1) = '';
    $outfld[++$#outfld] = $image_path . $fld[3];

    $recout[++$#recout] = join(' ', @outfld);
}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_L_record       
#
# DESCRIPTION
#       Translate symbol record
#
##########################################################################
#
sub translate_L_record {

    $outfld[0] = $fld[0];
    # X offset
    if ($fld[1] > $max_x_offset) {
        $outfld[1] = $max_x_offset;
    } else {
        $outfld[1] = $fld[1];
    }
    # Y offset
    if ($fld[2] > $max_y_offset) {
        $outfld[2] = $max_y_offset;
    } else {
        $outfld[2] = $fld[2];
    }
    # symbol
    if (defined $sym{$fld[3]}) {
        $outfld[3] = $sym{$fld[3]};      # to keep old symbol
    } else {
	print STDERR 'Old Start Symbol could not be kept for L record: ',$_;
	$outfld[3] = '0';
    }
    # page size layer
    foreach $i (4 .. $#fld) {
	$outfld[$i] = $fld[$i];
    }
    $outfld[++$#outfld] = $color;

    $recout[++$#recout] = join(' ',@outfld);

}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_M_record       
#
# DESCRIPTION
#       Translate internals record
#
##########################################################################
#
sub translate_M_record {

    $outfld[0] = 'F';
    # number of pages
    $outfld[1] = $fld[1];
    # current page
    $outfld[2] = $fld[2];
    # lines per page    
    $outfld[3] = $fld[3];
    # snap event
    $outfld[4] = $fld[4];
    # snap text
    $outfld[5] = $fld[5];
    # issue_disp
    $outfld[6] = 1;     
    # auto_renum
    $outfld[7] = $fld[7];    
    # save utc
    $utc = $fld[6];

    $F_line = join(' ', @outfld);

}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_N_record       
#
# DESCRIPTION
#       Translate chart line
#
##########################################################################
#
sub translate_N_record {

    $outfld[0] = 'A'; 
    foreach $i (1 .. 7) {
        $outfld[++$#outfld] = $fld[$i];
    }
    if (($outfld[2] eq '0') && ($outfld[3] > $max_x_offset)) {
	$outfld[3] = $max_x_offset;
    } elsif (($outfld[2] eq '1') && ($outfld[3] > $max_y_offset)) {
	$outfld[3] = $max_y_offset;
    }
    # position border lines and chart date lines
    if ($outfld[1] eq '0') {
	$outfld[3] = '0';
    }
    if (($outfld[1] eq '4') && ($outfld[2] eq '0')) {
	$outfld[3] = '999';
    }
    if (($outfld[1] eq '2') && ($outfld[2] eq '1')) {
	$outfld[3] = '154';
    }
    $chart_line[++$#chart_line] = join(' ', @outfld);
}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_P_record       
#
# DESCRIPTION
#       Translate page record
#
##########################################################################
#
sub translate_P_record {
    
    $outfld[0] = $fld[0];
    # X offset
    if ($fld[1] > $max_x_offset) {
        $outfld[1] = $max_x_offset;
    } else {
        $outfld[1] = $fld[1];
    }
    # Y offset
    if ($fld[2] > $max_y_offset) {
        $outfld[2] = $max_y_offset;
    } else {
        $outfld[2] = $fld[2];
    }
    # font
    if ((($fld[3] >= '0') && ($fld[3] <= '5')) || ($fld[3] eq '12')) {
	$outfld[3] = $font{'0'};
    } else {
	$outfld[3] = $font{'1'};
    }
    # point
    if (($fld[3] eq '3') || ($fld[3] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[4] = $point{$fld[3]};
    # layer
    $outfld[5] = $fld[4];
    # color
    $outfld[6] = $color;

    $recout[++$#recout] = join(' ', @outfld);
}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_S_record       
#
# DESCRIPTION
#       Translate slip record
#
##########################################################################
#
sub translate_S_record {

    $outfld[0] = $fld[0];
    # start date
    $outfld[1] = &ymd_to_utc($fld[3], $fld[1], $fld[2]);

    # end date
    $outfld[2] = &ymd_to_utc($fld[6], $fld[4], $fld[5]);

    # line
    $outfld[3] = $fld[7];
    # start symbol
    if (defined $sym{$fld[8]}) {
        $outfld[4] = $sym{$fld[8]};      # to keep old symbol
    } else {
	print STDERR 'Old Start Symbol could not be kept for S record: ',$_;
	$outfld[4] = '0';
    }
    # end symbol
    if (defined $sym{$fld[9]}) {
        $outfld[5] = $sym{$fld[9]};      # to keep old symbol
    } else {
	print STDERR 'Old Start Symbol could not be kept for S record: ',$_;
	$outfld[5] = '0';
    }
    # schar
    $outfld[++$#outfld] = $fld[12];
    # echar
    $outfld[++$#outfld] = $fld[13];
    # line type
    $outfld[++$#outfld] = $fld[10];
    # color
    $outfld[++$#outfld] = $color;
    # font
    $outfld[++$#outfld] = '0';
    # point
    $outfld[++$#outfld] = $font_size;
    # text
    $outfld[++$#outfld] = '""';
    # horiz position
    $outfld[++$#outfld] = '1';
    # vert position
    $outfld[++$#outfld] = '2';
    # layer
    $outfld[++$#outfld] = $fld[11];
    # disp
    $outfld[++$#outfld] = '0';
    # idtext
    $outfld[++$#outfld] = '""';
    # idtog
    $outfld[++$#outfld] = '0';   
    # idhpos
    $outfld[++$#outfld] = '0';  
    # idvpos
    $outfld[++$#outfld] = '2';   

    $recout[++$#recout] = join(' ', @outfld);
}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_T_record       
#
# DESCRIPTION
#       Translate text record
#
##########################################################################
#
sub translate_T_record {
    
    $outfld[0] = $fld[0];
    # X offset
    if ($fld[1] > $max_x_offset) {
	$outfld[1] = $max_x_offset;
    } else {
	$outfld[1] = $fld[1];
    }
    # Y offset
    if ($fld[2] - 12 > $max_y_offset) {
	$outfld[2] = $max_y_offset;
    } else {
	$outfld[2] = $fld[2] - 12;
    }
    # justification
    $outfld[3] = $fld[4];
    # font
    if ((($fld[5] >= '0') && ($fld[5] <= '5')) || ($fld[5] eq '12')) {
	$outfld[4] = $font{'0'};
    } else {
	$outfld[4] = $font{'1'};
    }
    # point
    if (($fld[5] eq '3') || ($fld[5] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[5] = $point{$fld[5]};
    # color
    $outfld[6] = $color;
    # layer
    $outfld[7] = '0';
    # page
    $outfld[8] = '0';
    # text                     
    $outfld[9] = '';
    foreach $i (7 .. $#fld) {
        $outfld[9] .= $fld[$i] . ' ';
    }

    $recout[++$#recout] = join(' ', @outfld);
}

#
#
##########################################################################
#
# SUBROUTINE
#       translate_U_record       
#
# DESCRIPTION
#       Translate sequence record
#
##########################################################################
#
sub translate_U_record {

    $outfld[0] = $fld[0];
    # start date
    $outfld[1] = &ymd_to_utc($fld[3], $fld[1], $fld[2]);

    # end date
    $outfld[2] = &ymd_to_utc($fld[6], $fld[4], $fld[5]);

    # disp
    $outfld[3] = $fld[7];
    # solid
    $outfld[4] = $fld[8];
    # text position
    $outfld[5] = $fld[9];
    $outfld[6] = '2';
    # line
    $outfld[++$#outfld] = $fld[10];
    # font
    if ((($fld[12] >= '0') && ($fld[12] <= '5')) || ($fld[12] eq '12')) {
	$outfld[++$#outfld] = $font{'0'};
    } else {
	$outfld[++$#outfld] = $font{'1'};
    }
    # point
    if (($fld[12] eq '3') || ($fld[12] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[++$#outfld] = $point{$fld[12]};
    # layer
    $outfld[++$#outfld] = $fld[13];
    # color
    $outfld[++$#outfld] = $color;    
    # text
    @text_flds = split('"',$_);
    $outfld[++$#outfld] = '"' . $text_flds[1] . '"';
    # rtc       
    $outfld[++$#outfld] = '0';
    # idtext
    $outfld[++$#outfld] = '""';
    # idtog
    $outfld[++$#outfld] = '0';
    # idhpos
    $outfld[++$#outfld] = '0';
    # idvpos
    $outfld[++$#outfld] = '2';

    $recout[++$#recout] = join(' ', @outfld);
}

#
#
##########################################################################
#
# SUBROUTINE
#       translate_W_record       
#
# DESCRIPTION
#       Translate status date record
#
##########################################################################
#
sub translate_W_record {

    $outfld[0] = $fld[0];
    # X offset
    if ($fld[1] > $max_x_offset) {
        $outfld[1] = $max_x_offset;
    } else {
        $outfld[1] = $fld[1];
    }
    # Y offset
    if ($fld[2] - $point{$fld[3]} > $max_y_offset) {
        $outfld[2] = $max_y_offset;
    } else {
        $outfld[2] = $fld[2] - $point{$fld[3]};
    }
    # font
    if ((($fld[3] >= '0') && ($fld[3] <= '5')) || ($fld[3] eq '12')) {
	$outfld[3] = $font{'0'};
    } else {
	$outfld[3] = $font{'1'};
    }
    # point
    if (($fld[3] eq '3') || ($fld[3] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[4] = $point{$fld[3]};
    # layer
    $outfld[5] = $fld[4];
    # color
    $outfld[6] = $color;

    $recout[++$#recout] = join(' ', @outfld);

}
#
#
##########################################################################
#
# SUBROUTINE
#       translate_X_record       
#
# DESCRIPTION
#       Translate chart date record
#
##########################################################################
#
sub translate_X_record {

    $outfld[0] = 'Z';
    # x and y offsets and font
    foreach $i (1 .. 2) {
        $outfld[$i] = $fld[$i];
    }
    # font size
    if ((($fld[3] >= '0') && ($fld[3] <= '5')) || ($fld[3] eq '12')) {
	$outfld[3] = $font{'0'};
    } else {
	$outfld[3] = $font{'1'};
    }
    # point
    if (($fld[3] eq '3') || ($fld[3] eq '9')) {
	print STDERR 'Font 16 changed to 14 Point';
    }
    $outfld[4] = $point{$fld[3]};
    # layer
    $outfld[5] = $fld[4];
    # color
    $outfld[6] = $color;
}
#
#
##########################################################################
#
# SUBROUTINE
#       write_file
#
# DESCRIPTION
#       Write out all the records that have been constructed
#
##########################################################################
#
sub write_file {

    print XOPPS '$$OPPS';

    if (defined $F_line) {
        print XOPPS $F_line;
    } else {
        print STDERR 'F line is missing';
    }
    if ($#chart_line > 0) {
        foreach $i (0 .. $#chart_line) {
            print XOPPS $chart_line[$i];
        }
    }
    if (defined $C_line) {
        print XOPPS $C_line;
    } else {
        print STDERR 'C line is missing';
    }
    foreach $i (0 .. $#recout) {
        print XOPPS $recout[$i];
    }
    if ($#H_line > 0) {
	foreach $i (0 .. $#H_line) {
            print XOPPS $H_line[$i];
	}
    } else {
        print STDERR 'H line is missing';
    }
    
    print XOPPS '$$EOF';
}

########################################################################
#
# SUBROUTINE:
#       ymd_to_utc
#
# DESCRIPTION:
#       Converts a year-mon-day string to an UTC format
#
########################################################################

sub ymd_to_utc {

    local($year, $month, $day) = @_;

    if ($year % 4) {
	$day += $mndays[$month - 1];
    } else {
	$day += $lpdays[$month - 1];
    }
    $temp = sprintf("%04d-%03d", $year, $day);
}

