IODA Bundle
Writer.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 ///
12 /// \file Writer.cc
13 ///
14 /// @author Piotr Kuchta, Feb 2009
15 
16 #include "odc/Writer.h"
17 
18 #include <algorithm>
19 #include <iostream>
20 #include <sstream>
21 #include <errno.h>
22 #include <math.h>
23 //#include <values.h>
24 
25 using namespace std;
26 
27 #include "eckit/io/DataHandle.h"
28 #include "eckit/exception/Exceptions.h"
29 #include "eckit/filesystem/PathName.h"
30 #include "eckit/io/FileDescHandle.h"
31 #include "eckit/config/Resource.h"
32 
34 #include "odc/core/Codec.h"
35 #include "odc/core/Column.h"
36 #include "odc/core/MetaData.h"
41 
42 namespace odc {
43 
44 
45 template <typename ITERATOR>
47 : path_(""),
48  dataHandle_(0),
49  rowsBufferSize_(eckit::Resource<long>("$ODB_ROWS_BUFFER_SIZE;-rowsBufferSize;rowsBufferSize", DEFAULT_ROWS_BUFFER_SIZE)),
50  openDataHandle_(true),
51  deleteDataHandle_(true)
52 {}
53 
54 template <typename ITERATOR>
55 Writer<ITERATOR>::Writer(const eckit::PathName& path)
56 : path_(path),
57  dataHandle_(0),
58  rowsBufferSize_(eckit::Resource<long>("$ODB_ROWS_BUFFER_SIZE;-rowsBufferSize;rowsBufferSize", DEFAULT_ROWS_BUFFER_SIZE)),
59  openDataHandle_(true),
60  deleteDataHandle_(true)
61 {
62  if (path_ == "/dev/stdout" || path_ == "stdout")
63  {
64  eckit::Log::info() << "Writing to stdout" << std::endl;
65  dataHandle_ = new eckit::FileDescHandle(1);
66  openDataHandle_ = false;
67  }
68 }
69 
70 template <typename ITERATOR>
71 Writer<ITERATOR>::Writer(eckit::DataHandle *dh, bool openDataHandle, bool deleteDataHandle)
72 : path_(""),
73  dataHandle_(dh),
74  rowsBufferSize_(eckit::Resource<long>("$ODB_ROWS_BUFFER_SIZE;-rowsBufferSize;rowsBufferSize", DEFAULT_ROWS_BUFFER_SIZE)),
75  openDataHandle_(openDataHandle),
76  deleteDataHandle_(deleteDataHandle)
77 {}
78 
79 template <typename ITERATOR>
80 Writer<ITERATOR>::Writer(eckit::DataHandle &dh, bool openDataHandle)
81 : path_(""),
82  dataHandle_(&dh),
83  rowsBufferSize_(eckit::Resource<long>("$ODB_ROWS_BUFFER_SIZE;-rowsBufferSize;rowsBufferSize", DEFAULT_ROWS_BUFFER_SIZE)),
84  openDataHandle_(openDataHandle),
85  deleteDataHandle_(false)
86 {}
87 
88 template <typename ITERATOR>
89 Writer<ITERATOR>::~Writer() { if (deleteDataHandle_) delete dataHandle_; }
90 
91 template <typename ITERATOR>
93 {
94  eckit::DataHandle *dh = 0;
95  if (dataHandle_ == 0)
96  {
97  dh = ODBAPISettings::instance().writeToFile(path_, eckit::Length(0), false);
98  }
99  else
100  {
101  ASSERT(dataHandle_);
102  dh = dataHandle_;
103  }
104  return typename Writer::iterator(new ITERATOR(*this, dh, openDataHandle));
105 }
106 
107 template <typename ITERATOR>
108 ITERATOR* Writer<ITERATOR>::createWriteIterator(eckit::PathName pathName, bool append)
109 {
110  eckit::Length estimatedLength = 10*1024*1024;
111  eckit::DataHandle *h = append
112  ? ODBAPISettings::instance().appendToFile(pathName, estimatedLength)
113  : ODBAPISettings::instance().writeToFile(pathName, estimatedLength);
114  return new ITERATOR(*this, h, false);
115 }
116 
117 // Explicit templates' instantiations.
118 
120 template Writer<WriterBufferingIterator>::Writer(const eckit::PathName&);
121 template Writer<WriterBufferingIterator>::Writer(eckit::DataHandle&,bool);
122 template Writer<WriterBufferingIterator>::Writer(eckit::DataHandle*,bool,bool);
123 
127 
128 } // namespace odc
eckit::DataHandle * writeToFile(const eckit::PathName &, const eckit::Length &=eckit::Length(0), bool openDataHandle=true)
eckit::DataHandle * appendToFile(const eckit::PathName &, const eckit::Length &=eckit::Length(0), bool openDataHandle=true)
static ODBAPISettings & instance()
IteratorProxy< ITERATOR, Writer > iterator
Definition: Writer.h:35
virtual ~Writer()
Definition: Writer.cc:89
bool openDataHandle_
Definition: Writer.h:63
const eckit::PathName path_
Definition: Writer.h:59
iterator begin(bool openDataHandle=true)
Definition: Writer.cc:92
eckit::DataHandle * dataHandle_
Definition: Writer.h:60
ITERATOR * createWriteIterator(eckit::PathName, bool append=false)
Definition: Writer.cc:108
Definition: ColumnInfo.h:23
Definition: encode.cc:30