aboutsummaryrefslogtreecommitdiff
path: root/src/Write.c
blob: 6eb155ac5fd9284be64f28d95e323416d86921fc (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
165
166
167
168
169
170
171
172
173
174
/*@@
   @routine    Write.c
   @date       18th September 1999
   @author     Gabrielle Allen
   @desc 
   Writes scalar grid variable data.
   @enddesc 
   @calls    
   @calledby   
   @history
   @hauthor  @hdate 
   @hdesc 
   @hendhistory
@@*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>      /* strlen(3) */
#include <sys/types.h>
#include <sys/stat.h>    /* stat(2) */

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusBase/IOUtil/src/ioutil_AdvertisedFiles.h"
#include "CactusBase/IOUtil/src/ioutil_CheckpointRecovery.h"
#include "iobasicGH.h"


void IOBasic_Write (cGH *GH, int vindex, const char *alias)
{
  DECLARE_CCTK_PARAMETERS
  char *openmode;
  FILE *file;
  void *data;
  char *fname, buffer[128];
  iobasicGH *myGH;
  char format_str_real[15], format_str_int[15];
  struct stat fileinfo;


  /* output is done by processor 0 only */
  if (CCTK_MyProc (GH) != 0) 
  {
    return;
  }

  /* first, check if variable has storage assigned */
  if (! CCTK_QueryGroupStorageI (GH, CCTK_GroupIndexFromVarI (vindex)))
  {
    char *fullname;

    fullname = CCTK_FullName (vindex);
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "IOBasic_Write: No scalar output for '%s' (no storage)", 
		fullname);
    free (fullname);
    return;
  }

  /* set the output format string for the desired notation */
  sprintf (format_str_real, "%%%s\t%%%s\n", out_format, out_format);
  sprintf (format_str_int,  "%%%s\t%%d\n", out_format);

  /* get the GH extensions for IOBasic */
  myGH = (iobasicGH *) GH->extensions [CCTK_GHExtensionHandle ("IOBasic")];

  /* build the output filename */
  fname = (char *) malloc (strlen (myGH->outdirScalar) + strlen (alias) + 5);
  sprintf (fname, "%s/%s.tl", myGH->outdirScalar, alias);

  /* see if output files for this alias name were already created */
  if (GetNamedData (myGH->filenameListScalar, fname) == NULL)
  {
    /* if restart from recovery, all existing files are opened
       in append mode */
    if (IOUtil_RestartFromRecovery (GH))
      openmode = stat (fname, &fileinfo) == 0 ? "a" : "w";
    else
      openmode = "w";
  }
  else
  {
    openmode = "a";
  }

  /* open the output file with the given mode */
  file = fopen (fname, openmode);
  if (file == NULL)
  {
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Could not open output file '%s'", fname);
    free (fname);
    return;
  }

  if (*openmode == 'w')
  {
    char comment_char;
    ioAdvertisedFileDesc advertised_file; 

    if (CCTK_Equals (outScalar_style, "gnuplot")) 
    {
      comment_char = '#';
      advertised_file.mimetype = "application/gnuplot";
    }
    else
    {
      comment_char = '"';    /* this is for xgraph */
      advertised_file.mimetype = "application/x-graph";
    }

    /* just store a non-NULL pointer in database */
    StoreNamedData (&myGH->filenameListScalar, fname, (void *) 1);

    /* advertise the file for downloading */
    advertised_file.slice = "tl";
    advertised_file.thorn = CCTK_THORNSTRING;
    advertised_file.varname = CCTK_FullName (vindex);
    advertised_file.description = "Scalar value";

    IOUtil_AdvertiseFile (GH, fname, &advertised_file);

    /* write the file info and the header */
    if (CCTK_Equals (out_fileinfo, "parameter filename") ||
        CCTK_Equals (out_fileinfo, "all"))
    {
      buffer[0] = 0;
      CCTK_ParameterFilename (sizeof (buffer), buffer);
      fprintf (file, "%cParameter file %s\n", comment_char, buffer);
    }
    if (CCTK_Equals (out_fileinfo, "creation date") ||
        CCTK_Equals (out_fileinfo, "all"))
    {
      buffer[0] = 0;
      Util_CurrentDate (sizeof (buffer), buffer);
      fprintf (file, "%cCreated %s ", comment_char, buffer);
      Util_CurrentTime (sizeof (buffer), buffer);
      fprintf (file, "%s\n", buffer);
    }
    if (CCTK_Equals (out_fileinfo, "axis labels") ||
        CCTK_Equals (out_fileinfo, "all"))
    {
      fprintf (file, "%cx-label time\n", comment_char);
      fprintf (file, "%cy-label %s\n", comment_char, advertised_file.varname);
    }
    fprintf (file, "%c%s v time\n", comment_char, alias);

    free (advertised_file.varname);
  }

  /* get the data pointer */
  data = CCTK_VarDataPtrI (GH, 0, vindex); 

  switch (CCTK_VarTypeI (vindex))
  {
    case CCTK_VARIABLE_REAL:
      fprintf (file, format_str_real, GH->cctk_time,
               (double) *(CCTK_REAL *) data);
      break;
    case CCTK_VARIABLE_INT:
      fprintf (file, format_str_int, GH->cctk_time,
               (int) *(CCTK_INT *) data);
      break;
    default:
      CCTK_WARN (3, "Unsupported data type");
      break;
  }

  /* close the output file */
  fclose (file);

  /* clean up */
  free (fname);
}