Index: program/src/rrd_rpncalc.c
===================================================================
--- program/src/rrd_rpncalc.c	(revision 1113)
+++ program/src/rrd_rpncalc.c	(working copy)
@@ -172,6 +172,7 @@
             add_op(OP_SORT, SORT)
             add_op(OP_REV, REV)
             add_op(OP_TREND, TREND)
+            add_op(OP_TRENDNAN, TRENDNAN)
             add_op(OP_RAD2DEG, RAD2DEG)
             add_op(OP_DEG2RAD, DEG2RAD)
             add_op(OP_AVG, AVG)
@@ -364,6 +365,7 @@
             match_op(OP_SORT, SORT)
             match_op(OP_REV, REV)
             match_op(OP_TREND, TREND)
+            match_op(OP_TRENDNAN, TRENDNAN)
             match_op(OP_RAD2DEG, RAD2DEG)
             match_op(OP_DEG2RAD, DEG2RAD)
             match_op(OP_AVG, AVG)
@@ -757,6 +759,7 @@
             }
             break;
         case OP_TREND:
+		case OP_TRENDNAN:
             stackunderflow(1);
             if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
                 rrd_set_error("malformed trend arguments");
@@ -766,16 +769,23 @@
                 time_t    step = (time_t) rpnp[rpi - 2].step;
 
                 if (output_idx > (int) ceil((float) dur / (float) step)) {
-                    double    accum = 0.0;
-                    int       i = 0;
+					int 	ignorenan = (rpnp[rpi].op == OP_TREND);
+					double 	accum = 0.0;
+					int 	i = 0;
+					int 	count = 0;
 
-                    do {
-                        accum +=
-                            rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
-                        dur -= step;
-                    } while (dur > 0);
-
-                    rpnstack->s[--stptr] = (accum / -i);
+					do {
+			 			double val = 
+							rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
+						if (ignorenan || !isnan(val)) {
+							accum += val;
+							++count;
+			 			}
+				
+						dur -= step;
+					} while (dur > 0);
+			
+					rpnstack -> s[--stptr] = (count == 0) ? DNAN : (accum / count);
                 } else
                     rpnstack->s[--stptr] = DNAN;
             }
Index: program/src/rrd_rpncalc.h
===================================================================
--- program/src/rrd_rpncalc.h	(revision 1113)
+++ program/src/rrd_rpncalc.h	(working copy)
@@ -16,7 +16,7 @@
     OP_COS, OP_LOG, OP_EXP, OP_LT, OP_LE, OP_GT, OP_GE, OP_EQ, OP_IF,
     OP_MIN, OP_MAX, OP_LIMIT, OP_FLOOR, OP_CEIL,
     OP_UN, OP_END, OP_LTIME, OP_NE, OP_ISINF, OP_PREV_OTHER, OP_COUNT,
-    OP_ATAN, OP_SQRT, OP_SORT, OP_REV, OP_TREND,
+    OP_ATAN, OP_SQRT, OP_SORT, OP_REV, OP_TREND, OP_TRENDNAN,
     OP_ATAN2, OP_RAD2DEG, OP_DEG2RAD,
     OP_AVG, OP_ABS
 };
Index: program/src/rrd_graph.c
===================================================================
--- program/src/rrd_graph.c	(revision 1113)
+++ program/src/rrd_graph.c	(working copy)
@@ -2849,6 +2849,17 @@
    but it seems more stable this way. */
 
 
+static cairo_status_t cairo_write_func_file(
+	void *closure,
+	const unsigned char *data,
+	unsigned int length)
+{
+	if (fwrite(data, length, 1, closure) != 1)
+		return CAIRO_STATUS_WRITE_ERROR;
+	return CAIRO_STATUS_SUCCESS;
+}
+
+
 /* draw that picture thing ... */
 int graph_paint(
     image_desc_t *im,
@@ -3276,11 +3287,20 @@
 
     switch (im->imgformat) {
     case IF_PNG:
-        if (cairo_surface_write_to_png(im->surface, im->graphfile) !=
-            CAIRO_STATUS_SUCCESS) {
-            rrd_set_error("Could not save png to '%s'", im->graphfile);
-            return 1;
-        }
+		{
+			cairo_status_t status;
+		
+			if (strcmp(im->graphfile, "-") == 0) {
+    	    	status = cairo_surface_write_to_png_stream(im->surface, &cairo_write_func_file, (void*)stdout);
+			} else {
+    	    	status = cairo_surface_write_to_png(im->surface, im->graphfile);
+			}
+		
+	        if (status != CAIRO_STATUS_SUCCESS) {
+    	        rrd_set_error("Could not save png to '%s'", im->graphfile);
+        	    return 1;
+	        }
+		}
         break;
     default:
         cairo_show_page(im->cr);
Index: program/doc/rrdgraph_rpn.pod
===================================================================
--- program/doc/rrdgraph_rpn.pod	(revision 1113)
+++ program/doc/rrdgraph_rpn.pod	(working copy)
@@ -150,7 +150,7 @@
 
 Example: C<CDEF:x=a,b,c,d,4,AVG>
 
-B<TREND>
+B<TREND, TRENDNAN>
 
 Create a "sliding window" average of another data series.
 
@@ -174,6 +174,12 @@
      Value at sample (t1) will be the average between (t1-delay) and (t1)
      Value at sample (t2) will be the average between (t2-delay) and (t2)
 
+TRENDNAN is - in contrast to TREND - NAN-safe. If you use TREND and one 
+source value is NAN the complete sliding window is affected. The TRENDNAN 
+operation ignores all NAN-values in a sliding window and computes the 
+average of the remaining values.
+
+
 =item Special values
 
 B<UNKN>
