#!/usr/bin/perl

use strict;

#########################################################################
#   Copyright (C) 2026 by Bassem W Jamaleddine. All Rights Reserved.    #
#   How to Build Your Desktop Supercomputer ISBN 9798161121221 Oct 2026 #
#########################################################################

use DataManagement::Displayables::DisplayMatrix;
use DataManagement::LoadFile;

die "Must provide a neatplot data file!\n" unless @ARGV;

my $indata;
my $selected;
my $scroll;
my $title;

my $verbose = 0;

my $zoom;

while(@ARGV) {
	if ($ARGV[0] eq '-indata') {
		shift @ARGV;
		$indata = shift @ARGV
	} elsif ($ARGV[0] eq '-scroll') { #
		shift @ARGV;
		$scroll = shift @ARGV;
	} elsif ($ARGV[0] eq '-title') { #
		shift @ARGV;
		$title = shift @ARGV;
	} elsif ($ARGV[0] eq '-selected') { #
		shift @ARGV;
		$selected = shift @ARGV;
	} elsif ($ARGV[0] eq '-zoom') { #
		shift @ARGV;
		$zoom = shift @ARGV;
	} elsif ($ARGV[0] eq '-verbose') { #
		shift @ARGV;
		$verbose = 1;
	} elsif ($ARGV[0] eq '-www?') {
		open(F, 'lynx -dump http://node2.tcnd.com/STAT/tools/ptxdmat.html|');
		while(<F>) {print $_; }
		exit;
	} elsif (($ARGV[0] eq "-h") || ($ARGV[0] eq "--Help") || ($ARGV[0] eq '-?')) {
		usage();
	} elsif (($ARGV[0] eq "-V") || ($ARGV[0] eq "--Version")) {
		pversion();
	} else { die "unrecognized arguments <@ARGV>"; }
}


sub usage { print STDERR join '',(<DATA>); exit; }

sub pversion {
print STDERR <<END;

    ptxdmat v1.0
    COPYRIGHT (C) 2005,2017 TOTAL COMPUTING & NETWORK DESIGN Inc. 2017

    ptxdmat -?
    ptxdmat -www?

END
exit;
}



##############################################################################################
usage() unless $indata;

die "Cannot read file $indata!\n" unless -r $indata;

my $rammem_ref=[]; 
my $stridesize=0; 
my $fields=[];
my $fldsformats=[];
my $selectedformats=[];

my $flspec = {
	filename => $indata,
	rammem_ref => $rammem_ref, # \@rammem,
	stride_ref => \$stridesize,
	fields => $fields,
	layout => 2, # layout takes precedence over specified stridesize
	haltonbadrecords => 1,
	selected => \$selected,
	fldsformats => $fldsformats,
	selectedformats => $selectedformats,
};


my $inst = DataManagement::LoadFile-> new();
$inst-> readDataFileInMem($flspec);


my $finalfields_ref=[]; 
@$finalfields_ref = $selected ? @$selected : @$fields;;

print DataManagement::Displayables::DisplayMatrix::display_matrix(
	$rammem_ref,
	{
		title=>$title,
indexsize => 4,

	fldsformats => $selectedformats,
		scroll=>$scroll,
		collab=>$finalfields_ref,
		row0=>0,col0=>0,
#		zoom => "2282,0,2300,15",
		zoom => $zoom,
	}
);

__DATA__
  Program to Display Matrix from Plain Text Data File

  The Plain Text Data File, ptxd, are regular data text files that contains plain textual data that
  can be processed by Gnuplot scripts. However, if you have DisplayableMatrix module installed on 
  your computer, then data files that have a PTXD header, it can process the data file
  and generate the appropriate matrices.
  
(see also dispmat6.pl  dispmat7.pl)
ptxdmat -indata /tmp/junk/brownian3Deg-2D-c2-sc1-lg700-hu69.data -zoom 11,2,17,7 -title jhjh -scroll 5
ptxdmat -indata /tmp/junk/brownian3Deg-2D-c2-sc1-lg700-hu69.data -zoom 11,2,17,7 -title jhjh -scroll 5 -selected b1_1,b2_1

  -indata
  -scroll
  -title
  -selected
  -zoom

The scroll specifies to wrap the header of the matrix every scroll-number, hence allowing to view large matrices with
lengthy rows. If scroll is not specified, then the matrix output is displayed into one single matrix.

The row0 and col0 is to start the labeling of the matrix from the specified index, for instance, to start counting
from 0,0 as the first element, you then specify row0=>0,col0=>0. The default is to label the first element starting at 1,1.

Each element of the matrix is displayed can be displayed as a float or as an integer.
To display the element and show it as a float, then you specify the number of digits to precede the mantissa (decidigits) and the
number of digits to follow the mantissa (floatdigits)

The length of a displayable matrix element is computed is such:
     a) number od decimal digits + number of float digits after the mantissa + 1 for the decimal + 1 for the space seperation
     b) but if elength (element length) is specified, and it is larger than the previous number then elength override the previous sum
     c) the column labels are truncated to the maximum length per element length as specified in a)
     d) hence  elength allows you to have a wider entries per displayable matrix element, in particular to allow you stretching the length
       of the displayable element and avoid to truncate its column labels
$M-> mprint({title=>"Matrix M",scroll=>5,row0=>0,col0=>0, rowlab=>$comm,collab=>$cols, decdig=>18, fltdig=>6, elength=>30 });

$M-> mprint({title=>"Matrix M --  7.5f ", decdig=>7, fltdig=>5, });
$M-> mprint({title=>"Matrix M -- 18.6f scroll=5  noindex  row0=0 col0=0",scroll=>5,row0=>0,col0=>0, decdig=>18, fltdig=>6, noindex=>1, });
$M-> mprint({title=>"Matrix M -- default scroll=5  collab=@$cols",scroll=>5,row0=>0,col0=>0, collab=>$cols });
#$M-> mprint({title=>"Matrix M",scroll=>5,row0=>0,col0=>0, collab=>$cols });
exit;
$B-> mprint({title=>"Matrix B"});
$Mt-> mprint({title=>"Mt Matrix transpose"});

#$MtM-> print("MtM Mt * M\n");


