ARTS  2.3.1285(git:92a29ea9-dirty)
test_readpp.cc
Go to the documentation of this file.
1 /* Copyright (C) 2004-2012 Oliver Lemke <olemke@core-dump.info>
2 
3  This program is free software; you can redistribute it and/or
4  modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation; either version 2 of the
6  License, or (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
16  USA. */
17 
18 #include <cstdlib>
19 #include <iostream>
20 #include "bifstream.h"
21 #include "matpackI.h"
22 
23 //#define VERBOSE
24 
25 typedef long PPHeader[65];
26 
27 void readppheader(bifstream& is, PPHeader& pph);
28 void readppdata(bifstream& is, PPHeader& pph, Vector& v);
29 
30 void readppheader(bifstream& is, PPHeader& pph) {
31  for (int j = 0; j < 65 && is.good(); j++) {
32  pph[j] = is.readInt(4);
33 #ifdef VERBOSE
34  cout << j << " " << pph[j] << " " << is.error() << endl;
35 #endif
36  }
37 }
38 
39 void readppdata(bifstream& is, PPHeader& pph, Vector& v) {
40  const int EXTRA_DATA = 3;
41  v.resize(pph[15] + EXTRA_DATA);
42 
43  for (int j = 0; j < pph[15] + EXTRA_DATA && is.good(); j++) {
44  v[j] = is.readFloat(binio::Single);
45  }
46 
47  if (!is.good()) {
48  cerr << "Error: " << is.error() << endl;
49  }
50 }
51 
52 int main(int argc, char* argv[]) {
53  if (argc < 2) {
54  cerr << "Usage: " << argv[0] << " PPFILE [MAXFIELDS]" << endl;
55  exit(EXIT_FAILURE);
56  }
57 
58  long maxfields = -1;
59  if (argc > 2) maxfields = strtol(argv[2], NULL, 10);
60 
61  bifstream is(argv[1]);
62  if (is.good()) {
63  PPHeader pph;
64  Vector v;
65  long field = 0;
66 
67  is.setFlag(binio::BigEndian, true);
68  while (is.good() && field != maxfields) {
69  binio::Error e;
70  field++;
71 
72  // Check if more data is available in file
73  is.peekInt(4);
74  if (is.error() & 32) return (EXIT_FAILURE);
75 
76  readppheader(is, pph);
77  if ((e = is.error())) {
78  cerr << "Reading " << field << ". header failed with error " << e
79  << endl;
80  exit(EXIT_FAILURE);
81  } else {
82  cout << "Field # " << setw(5) << field << " -- STASH code " << setw(5)
83  << pph[42] << " -- PP code " << setw(4) << pph[23] << " -- PP VCT "
84  << setw(3) << pph[26] << endl;
85  }
86 
87  readppdata(is, pph, v);
88  if ((e = is.error())) {
89  cerr << "Reading " << field << ". data failed with error " << e << endl;
90  exit(EXIT_FAILURE);
91  }
92  }
93  } else {
94  cerr << "Error reading from " << argv[1] << endl;
95  return (EXIT_FAILURE);
96  }
97 
98  return (EXIT_SUCCESS);
99 }
The Vector class.
Definition: matpackI.h:860
void readppheader(bifstream &is, PPHeader &pph)
Definition: test_readpp.cc:30
int main(int argc, char *argv[])
Definition: test_readpp.cc:52
Float readFloat(FType ft)
Definition: binio.cc:132
Int readInt(unsigned int size)
Definition: binio.cc:108
Binary output file stream class.
Definition: bifstream.h:42
This file contains the class declaration of bifstream.
Error error()
Definition: binio.cc:93
Implementation of Matrix, Vector, and such stuff.
int Error
Definition: binio.h:82
void resize(Index n)
Resize function.
Definition: matpackI.cc:404
void setFlag(Flag f, bool set=true)
Definition: binio.cc:84
void readppdata(bifstream &is, PPHeader &pph, Vector &v)
Definition: test_readpp.cc:39
long PPHeader[65]
Definition: test_readpp.cc:25
Int peekInt(unsigned int size)
Definition: binio.cc:356