cdoStream.cc 3.6 KB
Newer Older
1
2
3
4
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
  Copyright (C) 2003-2020 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  See COPYING file for copying and redistribution conditions.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string>
#include <cassert>

#include "cdoStream.h"
#include "cdo_options.h"

28
29
static int nextCdoStreamID = 1;

30
CdoStream::~CdoStream() {}
31
CdoStream::CdoStream() : m_cdoStreamID(nextCdoStreamID++)
32
33
{
  isopen = false;
34
  ispipe = false;
35
36
  m_vlistID = -1;
  m_tsID = -1;
37
  m_filetype = CDI_UNDEFID;
38
39
  m_name = "";
  m_varID = -1;
40
  m_nvals = 0;
41
  m_fileID = 0;
42
43
44
45
46
47
}

int
CdoStream::getID()
{
  return m_cdoStreamID;
48
49
}

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
int
CdoStream::getTsID()
{
  return m_tsID;
}
int
CdoStream::getVarID()
{
  return m_varID;
}
int
CdoStream::getVlistID()
{
  return m_vlistID;
}

66
void
67
CdoStream::defDatarangeList(int p_vlistID)
68
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
  auto filetype = m_filetype;
70
71
72

  if (m_vlistID != -1) cdoAbort("Internal problem, vlist already defined!");

73
  if (m_datarangelist.size() != 0) cdoAbort("Internal problem, datarangelist already allocated!");
74

Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
  const auto nvars = vlistNvars(p_vlistID);
76
77
  assert(nvars > 0);

78
  m_datarangelist.resize(nvars);
79
80
81

  for (int varID = 0; varID < nvars; ++varID)
    {
82
83
84
85
86
      m_datarangelist[varID].gridsize = gridInqSize(vlistInqVarGrid(p_vlistID, varID));
      m_datarangelist[varID].datatype = vlistInqVarDatatype(p_vlistID, varID);
      m_datarangelist[varID].missval = vlistInqVarMissval(p_vlistID, varID);
      m_datarangelist[varID].addoffset = vlistInqVarAddoffset(p_vlistID, varID);
      m_datarangelist[varID].scalefactor = vlistInqVarScalefactor(p_vlistID, varID);
87

88
      m_datarangelist[varID].check_datarange = false;
89

90
91
      const bool laddoffset = IS_NOT_EQUAL(m_datarangelist[varID].addoffset, 0);
      const bool lscalefactor = IS_NOT_EQUAL(m_datarangelist[varID].scalefactor, 1);
92

93
      auto datatype = m_datarangelist[varID].datatype;
94
95
96
97
98
99
100
101

      if (filetype == CDI_FILETYPE_NC || filetype == CDI_FILETYPE_NC2 || filetype == CDI_FILETYPE_NC4
          || filetype == CDI_FILETYPE_NC4C || filetype == CDI_FILETYPE_NC5)
        {
          if (datatype == CDI_DATATYPE_UINT8
              && (filetype == CDI_FILETYPE_NC || filetype == CDI_FILETYPE_NC2 || filetype == CDI_FILETYPE_NC5))
            {
              datatype = CDI_DATATYPE_INT16;
102
              m_datarangelist[varID].datatype = datatype;
103
104
105
106
107
108
            }

          if (datatype == CDI_DATATYPE_UINT16
              && (filetype == CDI_FILETYPE_NC || filetype == CDI_FILETYPE_NC2 || filetype == CDI_FILETYPE_NC5))
            {
              datatype = CDI_DATATYPE_INT32;
109
              m_datarangelist[varID].datatype = datatype;
110
111
112
113
114
115
            }

          if (laddoffset || lscalefactor)
            {
              if (datatype == CDI_DATATYPE_INT8 || datatype == CDI_DATATYPE_UINT8 || datatype == CDI_DATATYPE_INT16
                  || datatype == CDI_DATATYPE_UINT16)
116
                m_datarangelist[varID].check_datarange = true;
117
118
119
            }
          else if (Options::CheckDatarange)
            {
120
              m_datarangelist[varID].check_datarange = true;
121
122
123
124
            }
        }
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
  m_vlistID = p_vlistID; // used for -r/-a
126
}