aboutsummaryrefslogtreecommitdiff
path: root/src/DumpVar.c
blob: 35920bbdb938e74b98d598086485e536a42d4426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <stdlib.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusPUGH/PUGHSlab/src/PUGHSlab.h"
#include "IOJpeg.h"

int IOJpeg_Output(cGH *GH, int index, int timelevel,  CCTK_REAL *data, 
                     int sdim, int *hsize, int vtype, FILE *fid);


int IOJpeg_DumpVar (cGH *GH, int index, int timelevel, IOJpegGeo_t *geo, FILE *fid)
{
  DECLARE_CCTK_PARAMETERS
  void *data=NULL;
  int *hsizes, slabstart[SLABSKEL_MAXDIM];
  IOJpegGH *ssGH;
  int vtype;
  int idim, vdim;
  
  int sdir[3] = {0,0,0};

  /* Get the handle for StreamedHDF5 extensions */
  ssGH = (IOJpegGH *) GH->extensions [CCTK_GHExtensionHandle ("IOJpeg")];

  /* Allocate buffer to hold actual slab sizes */
  hsizes = (int*)malloc(geo->sdim*sizeof(int));

  vdim= geo->vdim;

  /* Checks for vartype can go here */
  vtype = CCTK_VarTypeI (index);

  /* TEMPORARY FIX DUE TO INCONSISTENT DIR SPECIFICATION */
  /* direction vector of 1d line in 3d volume */
  if (geo->sdim==1) {
    sdir[0] = (geo->direction[0]==0) ? 1:0;
    sdir[1] = (geo->direction[0]==1) ? 1:0;
    sdir[2] = (geo->direction[0]==2) ? 1:0;
  } 
  /* norm vector of 2d surface in 3d volume */
  else if (geo->sdim==2)
  {
    sdir[0] = ((geo->direction[0]==1)&&(geo->direction[1]==2)) ? 1:0;
    sdir[1] = ((geo->direction[0]==0)&&(geo->direction[1]==2)) ? 1:0;
    sdir[2] = ((geo->direction[0]==0)&&(geo->direction[1]==1)) ? 1:0;
  }
  /* spanning directions for 3d */
  else if (geo->sdim==3)
  {
    sdir[0] = geo->direction[0];
    sdir[1] = geo->direction[1];
    sdir[2] = geo->direction[2];
  }
  else
  {
    char *fullname = CCTK_FullName (index);
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "No 0-dim extraction possible: '%s'", fullname);
    free (fullname);
    return (-1);
  }

  /* Get the start indices for the slab from slab_start. */
  /* Slab_start from the geo structure is not a true start index, it is currently
     used as the intersection point of n surfaces , that's why two entries
     have to be set to zero in the case of a surface (one entry for line) FIXME */
    for (idim=0;idim<vdim;idim++)
      slabstart[idim] = geo->slab_start[idim];
    for (idim=0;idim<geo->sdim;idim++)
      slabstart[geo->direction[idim]]=0;

  /* Data Extraction takes place, data will be gathered onto processor 0
     hyperslab data will be in data, 
     hyperslab sizes: hsizes[SLAB_DIM].
  */
  if (Hyperslab_GetHyperslab (GH, 0, index, timelevel, geo->sdim, slabstart,
                              sdir, geo->length, geo->downs,
                              &data, hsizes) < 0)
  {
    char *fullname = CCTK_FullName (index);
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Failed to extract hyperslab for variable '%s'", fullname);
    free (fullname);
    return (-1);
  }

  /* Output the hyperslab data */
  if (IOJpeg_Output (GH, index, timelevel, data, geo->sdim, hsizes,
      vtype, fid)<0)
  {
    char *fullname = CCTK_FullName (index);


    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "IOJpeg_Output failed for variable '%s'", fullname);
    free (fullname);
    return (-1);
  }

  /* free allocated resources */
  if (data)
  {
    free (data);
  }
  if (hsizes)
  {
    free (hsizes);
  }

  return (0);
}



int IOJpeg_Output(cGH *GH, int index, int timelevel, CCTK_REAL *data, 
                     int sdim, int *hsize, int vtype, FILE *fid)
{
  DECLARE_CCTK_PARAMETERS

  unsigned char *dataout=(unsigned char *)malloc(3*hsize[0]*hsize[1]);
  CCTK_REAL min,max;

  if (sdim!=2) return(-1);

  if (CCTK_Equals(colormap,"custom"))
  {
    min = colormap_min;
    max = colormap_max;
  }
  else
  {
    int reduction_handle,ierr;
    reduction_handle = CCTK_ReductionHandle ("maximum");
    ierr = CCTK_Reduce (GH, 0, reduction_handle, 1, 
                 CCTK_VARIABLE_REAL,&max, 1, index);
    reduction_handle = CCTK_ReductionHandle ("minimum");
    ierr = CCTK_Reduce (GH, 0, reduction_handle, 1, 
                 CCTK_VARIABLE_REAL,&min, 1, index);
  }

  if(fid)
  {
    AutoColorDataSlice(hsize[0],
                       hsize[1],
                       data,
                       dataout,
                       min,
                       max,
                       colormap_bias,
                       colormap_factor);
    
    WriteJPEGToFileRGB(hsize[0],
                       hsize[1], 
                       dataout, 
                       colormap_quality, 
                       fid);
  }

  free(dataout);
  return(1);
}