From 1469de89fab2258fd7b617a2bb08ea176e317a79 Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Sun, 12 Apr 2015 12:35:54 +0000
Subject: [PATCH] afterburner: added support for output pipes

---
 src/Afterburner.c    | 24 ++++++++++++++++++++----
 src/Makefile.am      |  1 +
 src/Makefile.in      |  8 ++++----
 src/afterburnerlib.c |  8 ++++++++
 src/pstream.h        | 16 ++--------------
 5 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/src/Afterburner.c b/src/Afterburner.c
index 98bc46f8c..a40e705fa 100644
--- a/src/Afterburner.c
+++ b/src/Afterburner.c
@@ -1,4 +1,4 @@
-#if  defined  (HAVE_CONFIG_H)
+#if defined(HAVE_CONFIG_H)
 #include "config.h"
 #endif
 
@@ -7,8 +7,7 @@
 #if defined(CDO)
 #include "cdo.h"
 #include "cdo_int.h"
-//#include "pstream.h"
-#include "util.h"
+#include "pstream_write.h"
 #endif
 
 #if defined(AFTERBURNER)
@@ -78,6 +77,8 @@ static char *ifile  = NULL;
 static char *ofile  = NULL;
 static char *ofile2 = NULL;
 
+static int ofileidx = 0;
+
 static int specGridID  = -1;
 static int gaussGridID = -1;
 static int iVertID = -1;
@@ -1942,8 +1943,12 @@ void after_processing(struct Control *globs, struct Variable *vars)
 {
   int i;
 
+  //#if defined(_PSTREAM_H)
+  //  globs->istreamID = streamOpenRead(cdoStreamName(0));
+  //#else
   globs->istreamID = streamOpenRead(ifile);
   if ( globs->istreamID < 0 ) cdiError(globs->istreamID, "Open failed on %s", ifile);
+  //#endif
 
   globs->ivlistID = streamInqVlist(globs->istreamID);
   globs->taxisID  = vlistInqTaxis(globs->ivlistID);
@@ -1951,8 +1956,12 @@ void after_processing(struct Control *globs, struct Variable *vars)
 
   if ( globs->Mean != 2 )
     {
-      globs->ostreamID  = streamOpenWrite(ofile,  ofiletype);
+#if defined(_PSTREAM_WRITE_H)
+      globs->ostreamID = streamOpenWrite(cdoStreamName(ofileidx), ofiletype);
+#else
+      globs->ostreamID = streamOpenWrite(ofile, ofiletype);
       if ( globs->ostreamID < 0 ) cdiError(globs->ostreamID, "Open failed on %s", ofile);
+#endif
 
       if ( globs->Szip ) streamDefCompType(globs->ostreamID, COMPRESS_SZIP);
 
@@ -2085,8 +2094,13 @@ void after_processing(struct Control *globs, struct Variable *vars)
 
   after_control(globs, vars);
 
+#if defined(_PSTREAM_WRITE_H)
+  if ( globs->ostreamID2 != CDI_UNDEFID ) pstreamClose(globs->ostreamID2);
+  if ( globs->ostreamID  != CDI_UNDEFID ) pstreamClose(globs->ostreamID);
+#else
   if ( globs->ostreamID2 != CDI_UNDEFID ) streamClose(globs->ostreamID2);
   if ( globs->ostreamID  != CDI_UNDEFID ) streamClose(globs->ostreamID);
+#endif
   streamClose(globs->istreamID);
 
   if ( globs->rcoslat )          free(globs->rcoslat);
@@ -2311,6 +2325,8 @@ void *Afterburner(void *argument)
   int streamCnt = cdoStreamCnt();
   int nfiles = streamCnt - 1;
 
+  ofileidx = nfiles;
+
   ifile = cdoStreamName(0)->args;
   ofile = cdoStreamName(nfiles)->args;
 
diff --git a/src/Makefile.am b/src/Makefile.am
index ac5742740..04219dc35 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -97,6 +97,7 @@ libcdo_la_SOURCES =            \
                process.h       \
                pstream.c       \
                pstream.h       \
+               pstream_write.h \
                pstream_int.h   \
                pthread_debug.c \
                pthread_debug.h \
diff --git a/src/Makefile.in b/src/Makefile.in
index d9f2d7b53..d10468d5a 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -560,10 +560,10 @@ libcdo_la_SOURCES = cdo_int.h compare.h cdo_pthread.c cdo_vlist.c \
 	nth_element.h operator_help.h par_io.c par_io.h percentiles.c \
 	percentiles.h pipe.c pipe.h pragma_omp_atomic_update.h \
 	printinfo.h process.c process.h pstream.c pstream.h \
-	pstream_int.h pthread_debug.c pthread_debug.h readline.c \
-	realtime.c remap.h remaplib.c remapsort.c remap_scrip_io.c \
-	remap_search_reg2d.c remap_search_latbins.c remap_store_link.c \
-	remap_store_link.h remap_store_link_cnsrv.c \
+	pstream_write.h pstream_int.h pthread_debug.c pthread_debug.h \
+	readline.c realtime.c remap.h remaplib.c remapsort.c \
+	remap_scrip_io.c remap_search_reg2d.c remap_search_latbins.c \
+	remap_store_link.c remap_store_link.h remap_store_link_cnsrv.c \
 	remap_store_link_cnsrv.h remap_conserv.c remap_conserv_scrip.c \
 	remap_distwgt_scrip.c remap_bicubic_scrip.c \
 	remap_bilinear_scrip.c stdnametable.c stdnametable.h \
diff --git a/src/afterburnerlib.c b/src/afterburnerlib.c
index bc2091c31..c3b3fa771 100644
--- a/src/afterburnerlib.c
+++ b/src/afterburnerlib.c
@@ -3,6 +3,14 @@
 
 #include "cdi.h"
 
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#if defined(CDO)
+#include "cdo_int.h"
+#include "pstream_write.h"
+#endif
+#endif
+
 #include "error.h"
 #include "afterburner.h"
 #include "constants.h"
diff --git a/src/pstream.h b/src/pstream.h
index ff2e21254..4550d4b6c 100644
--- a/src/pstream.h
+++ b/src/pstream.h
@@ -18,9 +18,10 @@
 #ifndef _PSTREAM_H
 #define _PSTREAM_H
 
+#include "pstream_write.h"
+
 #include <sys/types.h> /* off_t */
 
-#define  streamOpenWrite          pstreamOpenWrite
 #define  streamOpenRead           pstreamOpenRead
 #define  streamOpenAppend         pstreamOpenAppend
 #define  streamClose              pstreamClose
@@ -29,16 +30,11 @@
 #define  streamInqByteorder       pstreamInqByteorder
 
 #define  streamInqVlist           pstreamInqVlist
-#define  streamDefVlist           pstreamDefVlist
 
-#define  streamDefTimestep        pstreamDefTimestep
 #define  streamInqTimestep        pstreamInqTimestep
 
-#define  streamDefRecord          pstreamDefRecord
 #define  streamInqRecord          pstreamInqRecord
 
-#define  streamWriteRecord        pstreamWriteRecord
-#define  streamWriteRecordF       pstreamWriteRecordF
 #define  streamReadRecord         pstreamReadRecord
 
 #define  streamCopyRecord         pstreamCopyRecord
@@ -48,7 +44,6 @@
 #define  vlistCopyFlag            cdoVlistCopyFlag
 
 
-int     pstreamOpenWrite(const argument_t *argument, int filetype);
 int     pstreamOpenRead(const argument_t *argument);
 int     pstreamOpenAppend(const argument_t *argument);
 void    pstreamClose(int pstreamID);
@@ -56,17 +51,12 @@ void    pstreamClose(int pstreamID);
 int     pstreamInqFiletype(int pstreamID);
 int     pstreamInqByteorder(int pstreamID);
 
-void    pstreamDefVlist(int pstreamID, int vlistID);
 int     pstreamInqVlist(int pstreamID);
 
-void    pstreamDefTimestep(int pstreamID, int tsID);
 int     pstreamInqTimestep(int pstreamID, int tsID);
 
-void    pstreamDefRecord(int pstreamID, int  varID, int  levelID);
 int     pstreamInqRecord(int pstreamID, int *varID, int *levelID);
 
-void    pstreamWriteRecord(int pstreamID, double *data, int nmiss);
-void    pstreamWriteRecordF(int pstreamID, float *data, int nmiss);
 void    pstreamReadRecord(int pstreamID, double *data, int *nmiss);
 void    pstreamCopyRecord(int pstreamIDdest, int pstreamIDsrc);
 
@@ -74,6 +64,4 @@ void    pstreamInqGRIBinfo(int pstreamID, int *intnum, float *fltnum, off_t *big
 
 void    cdoVlistCopyFlag(int vlistID2, int vlistID1);
 
-
-
 #endif  /* _PSTREAM_H */
-- 
GitLab