ARTS  2.2.66
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 "matpackI.h"
21 #include "bifstream.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 
31 {
32  for (int j = 0; j < 65 && is.good (); j++)
33  {
34  pph[j] = is.readInt (4);
35 #ifdef VERBOSE
36  cout << j << " " << pph[j] << " " << is.error () << endl;
37 #endif
38  }
39 }
40 
41 void readppdata (bifstream& is, PPHeader& pph, Vector& v)
42 {
43  const int EXTRA_DATA = 3;
44  v.resize (pph[15] + EXTRA_DATA);
45 
46  for (int j = 0; j < pph[15] + EXTRA_DATA && is.good (); j++)
47  {
48  v[j] = is.readFloat (binio::Single);
49  }
50 
51  if (!is.good ())
52  {
53  cerr << "Error: " << is.error() << endl;
54  }
55 }
56 
57 int main (int argc, char *argv[])
58 {
59  if (argc < 2)
60  {
61  cerr << "Usage: " << argv[0] << " PPFILE [MAXFIELDS]" << endl;
62  exit (EXIT_FAILURE);
63  }
64 
65  long maxfields = -1;
66  if (argc > 2) maxfields = strtol (argv[2], NULL, 10);
67 
68  bifstream is (argv[1]);
69  if (is.good())
70  {
71  PPHeader pph;
72  Vector v;
73  long field = 0;
74 
75  is.setFlag (binio::BigEndian, true);
76  while (is.good() && field != maxfields)
77  {
78  binio::Error e;
79  field++;
80 
81  // Check if more data is available in file
82  is.peekInt (4);
83  if (is.error () & 32) return (EXIT_FAILURE);
84 
85  readppheader (is, pph);
86  if ((e = is.error ()))
87  {
88  cerr << "Reading " << field << ". header failed with error "
89  << e << endl;
90  exit (EXIT_FAILURE);
91  }
92  else
93  {
94  cout << "Field # " << setw(5) << field
95  << " -- STASH code " << setw(5) << pph[42]
96  << " -- PP code " << setw(4) << pph[23]
97  << " -- PP VCT " << setw(3) << pph[26] << endl;
98  }
99 
100  readppdata (is, pph, v);
101  if ((e = is.error ()))
102  {
103  cerr << "Reading " << field << ". data failed with error "
104  << e << endl;
105  exit (EXIT_FAILURE);
106  }
107  }
108  }
109  else
110  {
111  cerr << "Error reading from " << argv[1] << endl;
112  return (EXIT_FAILURE);
113  }
114 
115  return (EXIT_SUCCESS);
116 }
117 
The Vector class.
Definition: matpackI.h:556
void readppheader(bifstream &is, PPHeader &pph)
Definition: test_readpp.cc:30
int main(int argc, char *argv[])
Definition: test_readpp.cc:57
Float readFloat(FType ft)
Definition: binio.cc:154
Int readInt(unsigned int size)
Definition: binio.cc:130
Binary output file stream class.
Definition: bifstream.h:44
This file contains the class declaration of bifstream.
Error error()
Definition: binio.cc:107
int Error
Definition: binio.h:86
void resize(Index n)
Assignment operator from VectorView.
Definition: matpackI.cc:798
void setFlag(Flag f, bool set=true)
Definition: binio.cc:94
void readppdata(bifstream &is, PPHeader &pph, Vector &v)
Definition: test_readpp.cc:41
long PPHeader[65]
Definition: test_readpp.cc:25
Int peekInt(unsigned int size)
Definition: binio.cc:367