PLUTO
write_img.c File Reference
#include "pluto.h"
Include dependency graph for write_img.c:

Go to the source code of this file.

Functions

static void GetSlice (double ***, Image *, Grid *)
 
static int GET_SLICE_INDEX (int, double, Grid *)
 
void WritePPM (double ***Vdbl, char *var_name, char *filename, Grid *grid)
 

Function Documentation

int GET_SLICE_INDEX ( int  plane,
double  x,
Grid grid 
)
static

Definition at line 312 of file write_img.c.

319 {
320  int dir, i;
321  double xr,xl;
322 
323  #if DIMENSIONS == 2
324  return 0;
325  #endif
326 
327  if (plane == X12_PLANE) dir = KDIR;
328  if (plane == X23_PLANE) dir = IDIR;
329  if (plane == X13_PLANE) dir = JDIR;
330 
331  for (i = 0; i < grid[dir].np_tot_glob; i++){
332  xl = grid[dir].x_glob[i] - 0.5*grid[dir].dx_glob[i];
333  xr = grid[dir].x_glob[i] + 0.5*grid[dir].dx_glob[i];
334  if (x >= xl && x <= xr) return MAX(i-IBEG,0);
335  }
336  return 0;
337 
338 }
#define MAX(a, b)
Definition: macros.h:101
#define X12_PLANE
Definition: pluto.h:158
double * xr
Definition: structs.h:81
double * x_glob
Cell geometrical central points.
Definition: structs.h:80
double * dx_glob
Cell size.
Definition: structs.h:83
int np_tot_glob
Total number of points in the global domain (boundaries included).
Definition: structs.h:96
#define KDIR
Definition: pluto.h:195
double * xl
Definition: structs.h:82
#define IDIR
Definition: pluto.h:193
double * x
Definition: structs.h:80
int i
Definition: analysis.c:2
#define JDIR
Definition: pluto.h:194
long int IBEG
Lower grid index of the computational domain in the the X1 direction for the local processor...
Definition: globals.h:35
#define X23_PLANE
Definition: pluto.h:160
#define X13_PLANE
Definition: pluto.h:159

Here is the caller graph for this function:

void GetSlice ( double ***  Vdbl,
Image image,
Grid grid 
)
static

Definition at line 144 of file write_img.c.

159 {
160  int i, j, k;
161  int nx, ny, nz;
162  int col_offset, row_offset;
163  int offset, ir, ic;
164  char filename[256];
165  float xflt, slice_min, slice_max;
166  static float **slice;
167  static RGB **rgb;
168  FILE *fl;
169  size_t dsize = sizeof(float);
170 
171  #if DIMENSIONS == 1
172  print1 ("! PPM output disabled in 1-D\n");
173  return;
174  #endif
175 
176 /* ------------------------------------------------
177  Write the whole array in single precision
178  Slice are post-processed later
179  ------------------------------------------------ */
180 
181  fl = OpenBinaryFile ("tmp_file.out", SZ_float, "w");
182  WriteBinaryArray ((Convert_dbl2flt(Vdbl,1.0, 0))[0][0], dsize, SZ_float, fl, -1);
184  #ifdef PARALLEL
185  MPI_Barrier (MPI_COMM_WORLD);
186  #endif
187 
188  if (prank != 0) return; /* -- rank 0 will do the rest -- */
189 
190 /* ------------------------------------------------
191  get global dimensions
192  ------------------------------------------------ */
193 
194  nx = grid[IDIR].gend + 1 - grid[IDIR].nghost;
195  ny = grid[JDIR].gend + 1 - grid[JDIR].nghost;
196  nz = grid[KDIR].gend + 1 - grid[KDIR].nghost;
197 
198 /* -----------------------------------------
199  Allocate memory: make slices big
200  enough to contain slices of different
201  sizes.
202  ----------------------------------------- */
203 
204  if (slice == NULL) {
205  ic = MAX(nx,ny);
206  ir = MAX(ny,nz);
207  slice = ARRAY_2D(ir, ic, float);
208  rgb = ARRAY_2D(ir, ic, RGB);
209  }
210 
211 /* --------------------------------------------
212  set offsets for slice reading
213  -------------------------------------------- */
214 
215  fl = fopen ("tmp_file.out", "rb");
216  if (image->slice_plane == X12_PLANE){
217 
218  k = GET_SLICE_INDEX (image->slice_plane, image->slice_coord, grid);
219  offset = k*nx*ny*dsize;
220  fseek (fl, offset, SEEK_SET);
221  col_offset = 0;
222  row_offset = 0;
223  image->ncol = nx;
224  image->nrow = ny;
225 
226  } else if (image->slice_plane == X13_PLANE){
227 
228  j = GET_SLICE_INDEX (image->slice_plane, image->slice_coord, grid);
229  offset = j*nx*dsize;
230  fseek (fl, offset, SEEK_CUR);
231  col_offset = 0;
232  row_offset = nx*(ny - 1)*dsize;
233  image->ncol = nx;
234  image->nrow = nz;
235 
236  } else if (image->slice_plane == X23_PLANE){
237 
238  i = GET_SLICE_INDEX (image->slice_plane, image->slice_coord, grid);
239  offset = i*dsize;
240  fseek (fl, offset, SEEK_CUR);
241  col_offset = (nx - 1)*dsize;
242  row_offset = 0;
243  image->ncol = ny;
244  image->nrow = nz;
245 
246  }
247 
248 /* -----------------------------------------
249  Read slice
250  ----------------------------------------- */
251 
252  for (ir = 0; ir < image->nrow; ir++) {
253  for (ic = 0; ic < image->ncol; ic++) {
254  fread (&xflt, dsize, 1, fl);
255  if (feof(fl)){
256  printf (" ! end of file reached\n");
257  QUIT_PLUTO(1);
258  }
259  slice[image->nrow - 1 - ir][ic] = xflt; /* -- swap row order -- */
260  if (col_offset > 0) fseek(fl, col_offset, SEEK_CUR);
261  }
262  if (row_offset > 0) fseek(fl, row_offset, SEEK_CUR);
263  }
264  fclose(fl);
265 
266 /* -----------------------------------------------------------
267  Get slice max and min
268  ----------------------------------------------------------- */
269 
270  if (fabs(image->max - image->min) < 1.e-8) { /* -- set auto-scale -- */
271  slice_min = 1.e38;
272  slice_max = -1.e38;
273  for (ir = 0; ir < image->nrow; ir++){
274  for (ic = 0; ic < image->ncol; ic++){
275  slice_min = MIN(slice_min, slice[ir][ic]);
276  slice_max = MAX(slice_max, slice[ir][ic]);
277  }}
278  } else {
279  slice_min = image->min;
280  slice_max = image->max;
281  }
282 
283 /* -----------------------------------------------------
284  Scale the image between 0 and 255.
285  Set log/linear scaling.
286  Create {r,g,b} triplet
287  ----------------------------------------------------- */
288 
289  image->rgb = rgb;
290  for (ir = 0; ir < image->nrow; ir++){
291  for (ic = 0; ic < image->ncol; ic++){
292 
293  if (image->logscale) {
294  xflt = log10(slice[ir][ic]/slice_min)*255.;
295  xflt /= log10(slice_max/slice_min);
296  }else{
297  xflt = (slice[ir][ic] - slice_min)*255.;
298  xflt /= (slice_max - slice_min);
299  }
300 
301  i = (int) xflt;
302  i = MIN (i, 255);
303  i = MAX (i, 0);
304 
305  image->rgb[ir][ic].r = image->r[i];
306  image->rgb[ir][ic].g = image->g[i];
307  image->rgb[ir][ic].b = image->b[i];
308  }}
309 }
unsigned char b[256]
Definition: structs.h:306
#define MAX(a, b)
Definition: macros.h:101
#define X12_PLANE
Definition: pluto.h:158
void print1(const char *fmt,...)
Definition: amrPluto.cpp:511
FILE * OpenBinaryFile(char *filename, int sz, char *mode)
Definition: bin_io.c:31
int SZ_float
Definition: globals.h:27
Definition: structs.h:296
int gend
Global end index for the global array.
Definition: structs.h:114
int prank
Processor rank.
Definition: globals.h:33
float *** Convert_dbl2flt(double ***Vdbl, double unit, int swap_endian)
Definition: bin_io.c:216
#define KDIR
Definition: pluto.h:195
#define MIN(a, b)
Definition: macros.h:104
int ncol
Definition: structs.h:301
unsigned char g[256]
Definition: structs.h:306
double max
Definition: structs.h:308
#define IDIR
Definition: pluto.h:193
unsigned char r
Definition: structs.h:297
int j
Definition: analysis.c:2
int nghost
Number of ghost zones.
Definition: structs.h:104
int k
Definition: analysis.c:2
unsigned char b
Definition: structs.h:297
double slice_coord
Definition: structs.h:310
double min
Definition: structs.h:309
RGB ** rgb
Definition: structs.h:307
int i
Definition: analysis.c:2
static int GET_SLICE_INDEX(int, double, Grid *)
Definition: write_img.c:312
int logscale
Definition: structs.h:303
#define ARRAY_2D(nx, ny, type)
Definition: prototypes.h:171
unsigned char g
Definition: structs.h:297
#define JDIR
Definition: pluto.h:194
int slice_plane
Definition: structs.h:302
int CloseBinaryFile(FILE *fbin, int sz)
Definition: bin_io.c:78
int nrow
Definition: structs.h:301
#define QUIT_PLUTO(e_code)
Definition: macros.h:125
void WriteBinaryArray(void *V, size_t dsize, int sz, FILE *fl, int istag)
Definition: bin_io.c:98
#define X23_PLANE
Definition: pluto.h:160
unsigned char r[256]
Definition: structs.h:306
#define X13_PLANE
Definition: pluto.h:159

Here is the call graph for this function:

Here is the caller graph for this function:

void WritePPM ( double ***  Vdbl,
char *  var_name,
char *  filename,
Grid grid 
)

Definition at line 10 of file write_img.c.

19 {
20  int ic, ir;
21  FILE *fl;
22  char header[512];
23  Image *ppm;
24 
25  ppm = GetImage (var_name);
26  SetColorMap (ppm->r, ppm->g, ppm->b, ppm->colormap);
27  GetSlice (Vdbl, ppm, grid);
28  if (prank != 0) return;
29 
30  sprintf (header,"P6\n%d %d\n255\n", ppm->ncol, ppm->nrow);
31  fl = fopen (filename,"w");
32  fprintf(fl,"%s",header);
33  for (ir = 0; ir < ppm->nrow; ir++){
34  fwrite (ppm->rgb[ir], sizeof(RGB), ppm->ncol, fl);
35  }
36  fclose(fl);
37 }
unsigned char b[256]
Definition: structs.h:306
void SetColorMap(unsigned char *r, unsigned char *g, unsigned char *b, char *map_name)
Definition: colortable.c:11
Definition: structs.h:296
int prank
Processor rank.
Definition: globals.h:33
int ncol
Definition: structs.h:301
unsigned char g[256]
Definition: structs.h:306
char * colormap
Definition: structs.h:304
RGB ** rgb
Definition: structs.h:307
Definition: structs.h:300
int nrow
Definition: structs.h:301
static void GetSlice(double ***, Image *, Grid *)
Definition: write_img.c:144
unsigned char r[256]
Definition: structs.h:306
Image * GetImage(char *)
Definition: set_image.c:35

Here is the call graph for this function:

Here is the caller graph for this function: