Line data Source code
1 : /*
2 : * Copyright (c) 2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #include <string>
34 : #include <fstream>
35 : #include "file_io_asc.h"
36 : #include "common/error.h"
37 : #include "common/util/string_util.h"
38 :
39 : using namespace std;
40 :
41 : namespace ug{
42 :
43 0 : FileReaderASC::FileReaderASC() :
44 0 : m_cellSize(0),
45 : m_llcorner(0, 0),
46 0 : m_noDataValue(0)
47 : {
48 0 : m_privateField = make_sp(new Field<number>);
49 0 : m_field = m_privateField.get();
50 0 : }
51 :
52 0 : FileReaderASC::~FileReaderASC()
53 : {
54 0 : }
55 :
56 0 : void FileReaderASC::set_field(Field<number>* field)
57 : {
58 0 : if(field)
59 0 : m_field = field;
60 : else
61 0 : m_field = m_privateField.get();
62 0 : }
63 :
64 :
65 0 : void FileReaderASC::load_file(const char* filename)
66 : {
67 0 : ifstream in(filename);
68 0 : UG_COND_THROW(!in, "Couldn't access file " << filename);
69 :
70 : int numCols = 0, numRows = 0;
71 :
72 : // indicate whether the lower left corner was specified through cell-center
73 : bool llcenterx = false;
74 : bool llcentery = false;
75 :
76 : // parse header
77 0 : for(int i = 0; i < 6; ++i){
78 : string name;
79 : double value;
80 0 : in >> name >> value;
81 0 : UG_COND_THROW(!in, "Couldn't parse expected name-value pair in row " << i
82 : << " in file '" << filename << "'");
83 :
84 0 : name = ToLower(name);
85 :
86 0 : if(name.compare("ncols") == 0)
87 0 : numCols = (int)value;
88 0 : else if(name.compare("nrows") == 0)
89 0 : numRows = (int)value;
90 0 : else if(name.compare("xllcenter") == 0){
91 0 : m_llcorner.x() = value;
92 : llcenterx = true;
93 : }
94 0 : else if(name.compare("xllcorner") == 0)
95 0 : m_llcorner.x() = value;
96 0 : else if(name.compare("yllcenter") == 0){
97 0 : m_llcorner.y() = value;
98 : llcentery = true;
99 : }
100 0 : else if(name.compare("yllcorner") == 0)
101 0 : m_llcorner.y() = value;
102 0 : else if(name.compare("cellsize") == 0)
103 0 : m_cellSize = value;
104 0 : else if(name.compare("nodata_value") == 0)
105 0 : m_noDataValue = value;
106 : else{
107 0 : UG_THROW("unknown identifier in header: " << name);
108 : }
109 : }
110 :
111 0 : if(llcenterx)
112 0 : m_llcorner.x() -= 0.5 * m_cellSize;
113 0 : if(llcentery)
114 0 : m_llcorner.y() -= 0.5 * m_cellSize;
115 :
116 : // parse values
117 0 : Field<number>& field = *m_field;
118 0 : field.resize_no_copy(numCols, numRows);
119 0 : for(int irow = numRows - 1; irow >= 0; --irow){
120 0 : for(int icol = 0; icol < numCols; ++icol){
121 0 : in >> field.at(icol, irow);
122 0 : UG_COND_THROW(!in, "Couldn't read value at col: " << icol << ", row: " << numRows - irow);
123 : }
124 : }
125 0 : }
126 :
127 0 : bool LoadGridFromASC(Grid& grid, const char* filename, AVector3& aPos)
128 : {
129 0 : if(!grid.has_vertex_attachment(aPos))
130 0 : grid.attach_to_vertices(aPos);
131 :
132 :
133 0 : FileReaderASC reader;
134 0 : reader.load_file(filename);
135 :
136 : vector2 offset(reader.lower_left_corner_x(), reader.lower_left_corner_y());
137 :
138 : Grid::VertexAttachmentAccessor<AVector3> aaPos(grid, aPos);
139 0 : CreateGridFromField(grid, reader.field(),
140 0 : vector2(reader.cell_size(), reader.cell_size()),
141 : offset,
142 : reader.no_data_value(),
143 : aaPos);
144 0 : return true;
145 0 : }
146 :
147 : }// end of namespace
|