cdoStream.cc 3.56 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
34
35
{
  isopen = false;
  m_vlistID = -1;
  m_tsID = -1;
36
  m_filetype = CDI_UNDEFID;
37
38
  m_name = "";
  m_varID = -1;
39
40
41
42
43
44
45
  m_nvals = 0;
}

int
CdoStream::getID()
{
  return m_cdoStreamID;
46
47
}

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

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

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

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

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

76
  m_datarangelist.resize(nvars);
77
78
79

  for (int varID = 0; varID < nvars; ++varID)
    {
80
81
82
83
84
      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);
85

86
      m_datarangelist[varID].check_datarange = false;
87

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

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

      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;
100
              m_datarangelist[varID].datatype = datatype;
101
102
103
104
105
106
            }

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

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

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