Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
readfields.cpp
Go to the documentation of this file.
1/*
2 * This file is part of Vlasiator.
3 * Copyright 2010-2016 Finnish Meteorological Institute
4 *
5 * For details of usage, see the COPYING file and read the "Rules of the Road"
6 * at http://www.physics.helsinki.fi/vlasiator/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22#include <string>
23#include <vector>
24#include <iostream>
25#include <stdint.h>
26#include "field.h"
27#include "readfields.h"
28#include "particleparameters.h"
29#include "../definitions.h"
30
31/* Debugging image output */
32#define STB_IMAGE_WRITE_IMPLEMENTATION
33#include "stb_image_write.h"
34
35std::string B_field_name;
36std::string E_field_name;
37
38/* Read the cellIDs into an array */
39std::vector<uint64_t> readCellIds(vlsvinterface::Reader& r) {
40
41 uint64_t arraySize=0;
42 uint64_t vectorSize=0;
43 uint64_t byteSize=0;
44 vlsv::datatype::type dataType;
45 std::list<std::pair<std::string,std::string> > attribs;
46 attribs.push_back(std::pair<std::string,std::string>("name","CellID"));
47 if( r.getArrayInfo("VARIABLE",attribs, arraySize,vectorSize,dataType,byteSize) == false ) {
48 std::cerr << "getArrayInfo returned false when trying to read CellID VARIABLE." << std::endl;
49 exit(1);
50 }
51
52 if(dataType != vlsv::datatype::type::UINT || byteSize != 8 || vectorSize != 1) {
53 std::cerr << "Datatype of CellID VARIABLE entries is not uint64_t." << std::endl;
54 exit(1);
55 }
56
57 /* Allocate memory for the cellIds */
58 std::vector<uint64_t> cellIds(arraySize*vectorSize);
59
60 if( r.readArray("VARIABLE",attribs,0,arraySize,(char*) cellIds.data()) == false) {
61 std::cerr << "readArray faied when trying to read CellID Variable." << std::endl;
62 exit(1);
63 }
64
65 return cellIds;
66}
67
68/* For debugging purposes - dump a field into a png file
69 * We're hardcodedly writing the z=0 plane here. */
70void debug_output(Field& F, const char* filename) {
71
72 /* Find min and max value */
73 Real min[3], max[3];
74
75 /* TODO: ugh, this is an ungly hack */
76 min[0] = min[1] = min[2] = 99999999999;
77 max[0] = max[1] = max[2] = -99999999999;
78
79 for(int i=0; i<F.dimension[0]->cells*F.dimension[1]->cells; i++) {
80 for(int j=0; j<3; j++) {
81 if(F.data[4*i+j] > max[j]) {
82 max[j] = F.data[4*i+j];
83 }
84 if(F.data[4*i+j] < min[j]) {
85 min[j] = F.data[4*i+j];
86 }
87 }
88 }
89
90 /* Allocate a rgb-pixel array */
91 std::vector<uint8_t> pixels(4*F.dimension[0]->cells*F.dimension[1]->cells);
92
93 /* And fill it with colors */
94 for(int y=0; y<F.dimension[1]->cells; y++) {
95 for(int x=0; x<F.dimension[0]->cells; x++) {
96
97 /* Rescale the field values to lie between 0..255 */
98 Vec3d scaled_val = F.getCell(x,y,0);
99 for(int i=0; i<3; i++) {
100 scaled_val[i] -= min[i];
101 scaled_val[i] /= (max[i]-min[i]);
102 scaled_val[i] *= 255.;
103 }
104
105 pixels[4*(y*F.dimension[0]->cells + x)] = (uint8_t) scaled_val[0];
106 pixels[4*(y*F.dimension[0]->cells + x)+1] = (uint8_t) scaled_val[1];
107 pixels[4*(y*F.dimension[0]->cells + x)+2] = (uint8_t) scaled_val[2];
108 pixels[4*(y*F.dimension[0]->cells + x)+3] = 255; // Alpha=1
109 }
110 }
111
112 /* Write it out */
113 if(!stbi_write_png(filename, F.dimension[0]->cells, F.dimension[1]->cells, 4, pixels.data(), F.dimension[0]->cells*4)) {
114 std::cerr << "Writing " << filename << " failed: " << strerror(errno) << std::endl;
115 }
116}
for i
Definition Dispersion.m:24
float Real
Definition definitions.h:41
#define Vec3d
const int j
std::string B_field_name
std::string E_field_name
std::vector< uint64_t > readCellIds(vlsvinterface::Reader &r)
void debug_output(Field &F, const char *filename)
int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes)
int cells
Definition boundaries.h:53
Definition field.h:34
std::vector< double > data
Definition field.h:45
Vec3d getCell(int x, int y, int z)
Definition field.h:65
Boundary * dimension[3]
Definition field.h:42
static ARCH_HOSTDEV VecSimple< T > min(VecSimple< T > const &l, VecSimple< T > const &r)
static ARCH_HOSTDEV VecSimple< T > max(VecSimple< T > const &l, VecSimple< T > const &r)