aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetIOASCII/src/util/Carpet2ygraphCat.pl
blob: 669c8e64b7ae87506dbec21bb622dc57c834d39e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/perl -s

use FileHandle;

if(@ARGV != 3)
{
  print "Usage: $0 direction <Inputfile> <Outputfile> \n";
  exit;
}

open(CARPETFILE,   "<$ARGV[1]") || die "Unable to find file \"$ARGV[1]\".";

$file = $ARGV[2].".xg";
my $fh = new FileHandle(">$file") || die "Unable to open file \"$file\".";

my %data;
my $time = -1;
my $new = 0;
my $currentit = -1;
my $lastit = -1;

my @datatoprint;
my $nsets = 1;
my $maxlength = 0;
my @lengths;

$lengths[0]=0;
while (<CARPETFILE>)
{
    $line = $_;
    if ($line =~ "iteration") {
      @itline = split(/ +/,$line);
      $currentit = @itline[2];
    }
    elsif ($line =~ /^#/)
    {
	#Do nothing for headers!
    }
    elsif ($line =~ /([0-9+-ed.]+)*/)
    {
	@dataline = split(/[ \t]+/,$line);
	if ($currentit != $lastit)
	{
	    if ($new)
	    {
		push(@datatoprint,"\n\n\"Time = ".$time."\n");
		my @sortedcoords = sort numerically (keys %data);
		my $localcoord;
		foreach $localcoord (@sortedcoords)
		{
		    push(@datatoprint, $localcoord." ".$data{$localcoord});
		}
		$maxlength = $maxlength > (scalar @sortedcoords) ? $maxlength : (scalar @sortedcoords);
		$lengths[$nsets-1]=(scalar @sortedcoords);
		$nsets++;
		$lengths[$nsets-1]=0;
		%data=();
	    }
	    $new++;
	    $time = $dataline[8];
	    $lastit = $currentit;
	}
	my $coord = $dataline[9+$ARGV[0]];
	my $val = $dataline[12];
	$data{$coord} = $val;
    }
}
push(@datatoprint, "\n\"Time = ",$time,"\n");
my @sortedcoords = sort numerically (keys %data);
my $localcoord;
foreach $localcoord (@sortedcoords)
{
    push(@datatoprint, $localcoord." ".$data{$localcoord});
}
$maxlength = $maxlength > (scalar @sortedcoords) ? $maxlength : (scalar @sortedcoords);
$lengths[$nsets-1]=(scalar @sortedcoords);
$nsets++;
$lengths[$nsets-1]=0;

my $oldline="";
$nouts=0;
my $set=0;
foreach $line (@datatoprint) {
  if ($line =~ "Time") {
    if ($oldline) {
      for (my $i=$lengths[$set-1]; $i<$maxlength;$i++) {
	$nouts++;
	print $fh $oldline;
      }
    }
    $set++;
    print $fh $line;
  }
  else {
    $nouts++;
    print $fh $line;
    $oldline=$line
  }
}
for (my $i=$lengths[$set-1]; $i<$maxlength;$i++) {
  $nouts++;
  print $fh $oldline;
}

sub numerically {$a <=> $b;}