From 86a1038da146965a4c62239de20ea4fdc96211f6 Mon Sep 17 00:00:00 2001 From: tradke Date: Fri, 26 May 2000 09:43:21 +0000 Subject: Advertise output files. git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/IOBasic/trunk@58 b589c3ab-70e8-4b4d-a09f-cba2dd200880 --- src/Write.c | 158 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 96 insertions(+), 62 deletions(-) (limited to 'src/Write.c') diff --git a/src/Write.c b/src/Write.c index 4174304..65be41d 100644 --- a/src/Write.c +++ b/src/Write.c @@ -30,19 +30,21 @@ void IOBasic_Write (cGH *GH, int index, const char *alias) DECLARE_CCTK_PARAMETERS char *openmode; FILE *file; - CCTK_REAL *data_real; - CCTK_INT *data_int; + void *data; + char *fname; ioGH *ioUtilGH; - int decnot; + iobasicGH *myGH; + char *format_str_real, *format_str_int; + struct stat fileinfo; - ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")]; - - /* check if decimal or exponential notation desired*/ - decnot=CCTK_Equals(out_format,"f")?1:0; + /* 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 (index))) { + if (! CCTK_QueryGroupStorageI (GH, CCTK_GroupIndexFromVarI (index))) + { char *fullname; fullname = CCTK_FullName (index); @@ -52,73 +54,105 @@ void IOBasic_Write (cGH *GH, int index, const char *alias) return; } - /* Open the file (we write only on proc0) */ - if (CCTK_MyProc (GH) == 0) + /* set the output format string for the desired notation */ + if (CCTK_Equals (out_format, "f")) { - char *fname; - iobasicGH *myGH; - struct stat fileinfo; - - myGH = (iobasicGH *) GH->extensions [CCTK_GHExtensionHandle ("IOBasic")]; - - 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 (ioUtilGH->recovered) - openmode = stat (fname, &fileinfo) == 0 ? "a" : "w"; - else - openmode = "w"; - } else - openmode = "a"; - - file = fopen (fname, openmode); - if (file == NULL) { - CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING, - "Could not open output file '%s'", fname); - free (fname); - return; - } + format_str_real = "%f %25.13f\n"; + format_str_int = "%f %d\n"; + } + else + { + format_str_real = "%e %25.13e\n"; + format_str_int = "%e %d\n"; + } - if (*openmode == 'w') { - char title_start_char; + /* get the GH extensions for IOUtil and IOBasic */ + ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")]; + myGH = (iobasicGH *) GH->extensions [CCTK_GHExtensionHandle ("IOBasic")]; - if (CCTK_Equals(outScalar_style,"gnuplot")) - title_start_char = '#'; - else - title_start_char = 34; /* this is for xgraph */ + /* 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 (ioUtilGH->recovered) + 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; + } - fprintf (file,"%c%s v time\n",title_start_char,alias); + if (*openmode == 'w') + { + char title_start_char; + IOUtil_AdvertisedFileDesc_t advertised_file; - /* just store a non-NULL pointer in database */ - StoreNamedData (&myGH->filenameListScalar, fname, (void *) 1); + if (CCTK_Equals (outScalar_style, "gnuplot")) + { + title_start_char = '#'; + advertised_file.mimetype = "application/gnuplot"; } + else + { + title_start_char = 34; /* this is for xgraph */ + advertised_file.mimetype = "application/x-graph"; + } + + /* write the header */ + fprintf (file, "%c%s v time\n", title_start_char, alias); - switch (CCTK_VarTypeI(index)) { + /* 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 (index); + advertised_file.description = "Scalar value"; + + IOUtil_AdvertiseFile (GH, fname, &advertised_file); + + free (advertised_file.varname); + } + + /* get the data pointer */ + data = CCTK_VarDataPtrI (GH, 0, index); + + switch (CCTK_VarTypeI (index)) + { case CCTK_VARIABLE_REAL: - data_real = ((CCTK_REAL ***) GH->data) [index][0]; - if (decnot) - fprintf(file,"%f %25.13f\n",GH->cctk_time,*data_real); - else - fprintf(file,"%e %25.13e\n",GH->cctk_time,*data_real); + fprintf (file, format_str_real, GH->cctk_time, + (double) *(CCTK_REAL *) data); break; case CCTK_VARIABLE_INT: - data_int = ((CCTK_INT ***) GH->data) [index][0]; - if (decnot) - fprintf(file,"%f %d\n",GH->cctk_time,*data_int); - else - fprintf(file,"%e %d\n",GH->cctk_time,*data_int); + fprintf (file, format_str_int, GH->cctk_time, + (int) *(CCTK_INT *) data); break; default: CCTK_WARN (3, "Unsupported data type"); break; - } - - fclose(file); - free (fname); - } + + /* close the output file */ + fclose (file); + + /* clean up */ + free (fname); } -- cgit v1.2.3