aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetIOScalar/src/util/mergeCarpetIOScalar.pl
blob: 3aae60c3b39881284e681c92ae835170c76189b3 (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
#! /usr/bin/perl -w
#/*@@
#  @file      mergeCarpetIOScalar.pl
#  @date      Tue 15 August 2006
#  @author    Thomas Radke
#  @desc
#             Perl script to merge CarpetIOScalar output files,
#             eliminating duplicate timesteps.
#
#             Source code comments also by Thomas Radke :-)
#  @enddesc
#@@*/


my $help = $#ARGV < 0;
for (my $arg = 0; $arg <= $#ARGV; $arg++) {
  $help |= ($ARGV[$arg] eq '-h'    or
            $ARGV[$arg] eq '-help' or
            $ARGV[$arg] eq '--help');
}
if ($help) {
  print << "EOF";

  Usage: $0 [-h | -help | --help] <list of files>

  This script can be used to merge  CarpetIOScalar output  written before
  and after recovery. It reads one or more files in CarpetIOScalar format
  and  writes their contents  to STDOUT,  eliminating duplicate timesteps
  (all but the last occurance are discarded).

    Example: $0 alp.norm1.asc > alp.norm1.asc.merged

EOF
  exit;
}

# Rauten-Feld zum Merken der Anzahl und Haeufigkeit vorhandener Datensaetze
my %timesteps = ();

# Liste aller Eingabe-Dateien
my @filelist = @ARGV;

# lies zeilenweise alle Eingabe-Dateien
while (<>) {

  # falls diese Zeile eine Datenzeile ist:
  # merke den Datensatz mit seinem Zeitschritt
  $timesteps{$1} = $_ if (/^(\d+(\.\d+)?)\s+/);
}

# stelle die Liste aller Eingabe-Dateien wieder her
@ARGV = @filelist;

# lies zeilenweise alle Eingabe-Dateien
while (<>) {
  # gib alle Zeilen bis zur ersten Datenzeile aus
  last if (/^\d+(\.\d+)?\s+/);
  print;
}

# gib alle Datenzeilen aus, sortiert nach Zeitschritten
foreach my $timestep (sort {$a <=> $b} keys %timesteps) {
  print $timesteps{$timestep};
}