Index: DataLoader/manipulations.py
===================================================================
--- DataLoader/manipulations.py	(revision 342a506598b5274c42e6fb6e03ee1c80e93bd37a)
+++ DataLoader/manipulations.py	(revision 729bcf61a97096efa3366450db8169f95224d077)
@@ -210,5 +210,5 @@
             
             #TODO: find better definition of x[i_q] based on q_data
-            x[i_q] = min + (i_q + 1) * self.bin_width / 2.0
+            x[i_q] += frac * q_value#min + (i_q + 1) * self.bin_width / 2.0
             y[i_q] += frac * data[npts]
             
@@ -226,5 +226,5 @@
         err_y = err_y / y_counts
         y    = y / y_counts
-        
+        x    = x / y_counts
         idx = (numpy.isfinite(y) & numpy.isfinite(x)) 
         
@@ -426,18 +426,53 @@
         :return: Data1D object
         """
-        # Get data
+        # Get data W/ finite values
         data = data2D.data[numpy.isfinite(data2D.data)]
         q_data = data2D.q_data[numpy.isfinite(data2D.data)]
+        qx_data = data2D.qx_data[numpy.isfinite(data2D.data)]
         err_data = data2D.err_data[numpy.isfinite(data2D.data)]
+        
         dq_data = None
+        
+        # Get the dq for resolution averaging
         if data2D.dqx_data != None and data2D.dqy_data != None:
-            dq_data = data2D.dqx_data[numpy.isfinite(data2D.data)] * \
-                        data2D.dqx_data[numpy.isfinite(data2D.data)]
-            dq_data += data2D.dqy_data[numpy.isfinite(data2D.data)] * \
-                        data2D.dqy_data[numpy.isfinite(data2D.data)]
+            # The pinholes and det. pix contribution present 
+            # in both direction of the 2D which must be subtracted when
+            # converting to 1D: dq_overlap should calculated ideally at
+            # q = 0. Note This method works on only pinhole geometry. 
+            # Extrapolate dqx(r) and dqy(phi) at q = 0, and take an average.
+            z_max = max(data2D.q_data)
+            z_min = min(data2D.q_data)
+            x_max = data2D.dqx_data[data2D.q_data[z_max]]
+            x_min = data2D.dqx_data[data2D.q_data[z_min]]    
+            y_max = data2D.dqy_data[data2D.q_data[z_max]]
+            y_min = data2D.dqy_data[data2D.q_data[z_min]]          
+            # Find qdx at q = 0
+            dq_overlap_x = (x_min * z_max - x_max * z_min) / (z_max - z_min)
+            # when extrapolation goes wrong
+            if dq_overlap_x > min(data2D.dqx_data):
+                dq_overlap_x = min(data2D.dqx_data)
+            dq_overlap_x *= dq_overlap_x 
+            # Find qdx at q = 0
+            dq_overlap_y = (y_min * z_max - y_max * z_min) / (z_max - z_min)
+            # when extrapolation goes wrong
+            if dq_overlap_y > min(data2D.dqy_data):
+                dq_overlap_y = min(data2D.dqy_data)
+            # get dq at q=0.
+            dq_overlap_y *= dq_overlap_y
+
+            dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y)/2.0)
+            # Final protection of dq
+            if dq_overlap < 0:
+                dq_overlap = y_min
+            dqx_data = data2D.dqx_data[numpy.isfinite(data2D.data)]
+            dqy_data = data2D.dqy_data[numpy.isfinite(data2D.data)] - dq_overlap
+            # def; dqx_data = dq_r dqy_data = dq_phi
+            # Convert dq 2D to 1D here
+            dqx = dqx_data * dqx_data            
+            dqy = dqy_data * dqy_data
+            dq_data = numpy.add(dqx, dqy)
             dq_data = numpy.sqrt(dq_data)
             
-        q_data_max = numpy.max(q_data)
-
+        #q_data_max = numpy.max(q_data)
         if len(data2D.q_data) == None:
             msg = "Circular averaging: invalid q_data: %g" % data2D.q_data
@@ -475,5 +510,6 @@
                 i_q = nbins -1              
             y[i_q] += frac * data_n
-
+            # Take dqs from data to get the q_average
+            x[i_q] += frac * q_value
             if err_data == None or err_data[npt] == 0.0:
                 if data_n < 0:
@@ -483,25 +519,24 @@
                 err_y[i_q] += frac * frac * err_data[npt] * err_data[npt]
             if dq_data != None:
-                err_x[i_q] += frac * frac * dq_data[npt] * dq_data[npt]
+                # To be consistent with dq calculation in 1d reduction, 
+                # we need just the averages (not quadratures) because 
+                # it should not depend on the number of the q points 
+                # in the qr bins.
+                err_x[i_q] += frac * dq_data[npt]
             else:
                 err_x = None
             y_counts[i_q]  += frac
         
-        # We take the center of ring area, not radius.  
-        # This is more accurate than taking the radial center of ring.
-        x = (qbins + self.bin_width) * (qbins + self.bin_width)
-        x += qbins * qbins
-        x = x / 2.0
-        x = numpy.sqrt(x)
         # Average the sums       
         for n in range(nbins):
             if err_y[n] < 0: err_y[n] = -err_y[n]
             err_y[n] = math.sqrt(err_y[n])
-            if err_x != None:
-                err_x[n] = math.sqrt(err_x[n])
+            #if err_x != None:
+            #    err_x[n] = math.sqrt(err_x[n])
             
         err_y = err_y / y_counts
         err_y[err_y==0] = numpy.average(err_y)
         y    = y / y_counts
+        x    = x / y_counts
         idx = (numpy.isfinite(y)) & (numpy.isfinite(x)) 
         if err_x != None:
@@ -514,7 +549,4 @@
             raise ValueError, msg
         
-        #elif len(y[idx])!= nbins:
-        #    print "resulted",nbins- len(y[idx])
-        #,"empty bin(s) due to tight binning..."
         return Data1D(x=x[idx], y=y[idx], dy=err_y[idx], dx=d_x)
     
@@ -746,10 +778,42 @@
         qy_data = data2D.qy_data[numpy.isfinite(data2D.data)]
         dq_data = None
-        # dx (smear) data
+            
+        # Get the dq for resolution averaging
         if data2D.dqx_data != None and data2D.dqy_data != None:
-            dq_data = data2D.dqx_data[numpy.isfinite(data2D.data)] * \
-                        data2D.dqx_data[numpy.isfinite(data2D.data)]
-            dq_data += data2D.dqy_data[numpy.isfinite(data2D.data)] * \
-                        data2D.dqy_data[numpy.isfinite(data2D.data)]
+            # The pinholes and det. pix contribution present 
+            # in both direction of the 2D which must be subtracted when
+            # converting to 1D: dq_overlap should calculated ideally at
+            # q = 0. 
+            # Extrapolate dqy(perp) at q = 0
+            z_max = max(data2D.q_data)
+            z_min = min(data2D.q_data)
+            x_max = data2D.dqx_data[data2D.q_data[z_max]]
+            x_min = data2D.dqx_data[data2D.q_data[z_min]]    
+            y_max = data2D.dqy_data[data2D.q_data[z_max]]
+            y_min = data2D.dqy_data[data2D.q_data[z_min]]          
+            # Find qdx at q = 0
+            dq_overlap_x = (x_min * z_max - x_max * z_min) / (z_max - z_min)
+            # when extrapolation goes wrong
+            if dq_overlap_x > min(data2D.dqx_data):
+                dq_overlap_x = min(data2D.dqx_data)
+            dq_overlap_x *= dq_overlap_x 
+            # Find qdx at q = 0
+            dq_overlap_y = (y_min * z_max - y_max * z_min) / (z_max - z_min)
+            # when extrapolation goes wrong
+            if dq_overlap_y > min(data2D.dqy_data):
+                dq_overlap_y = min(data2D.dqy_data)
+            # get dq at q=0.
+            dq_overlap_y *= dq_overlap_y
+
+            dq_overlap = numpy.sqrt((dq_overlap_x + dq_overlap_y) / 2.0)  
+            if dq_overlap < 0:
+                dq_overlap = y_min
+            dqx_data = data2D.dqx_data[numpy.isfinite(data2D.data)]
+            dqy_data = data2D.dqy_data[numpy.isfinite(data2D.data)] - dq_overlap
+            # def; dqx_data = dq_r dqy_data = dq_phi
+            # Convert dq 2D to 1D here
+            dqx = dqx_data * dqx_data            
+            dqy = dqy_data * dqy_data
+            dq_data = numpy.add(dqx, dqy)
             dq_data = numpy.sqrt(dq_data)
             
@@ -828,5 +892,5 @@
             ## Get the total y          
             y[i_bin] += frac * data_n
-
+            x[i_bin] += frac * q_value
             if err_data[n] == None or err_data[n] == 0.0:
                 if data_n < 0:
@@ -837,5 +901,9 @@
                 
             if dq_data != None:
-                x_err[i_bin] += frac * frac * dq_data[n] * dq_data[n]
+                # To be consistent with dq calculation in 1d reduction, 
+                # we need just the averages (not quadratures) because 
+                # it should not depend on the number of the q points 
+                # in the qr bins.
+                x_err[i_bin] += frac * dq_data[n]
             else:
                 x_err = None
@@ -846,6 +914,5 @@
             y[i] = y[i] / y_counts[i]
             y_err[i] = math.sqrt(y_err[i]) / y_counts[i]
-            if x_err != None:
-                x_err[i] = math.sqrt(x_err[i]) / y_counts[i]
+
             # The type of averaging: phi,q2, or q
             # Calculate x[i]should be at the center of the bin
@@ -856,13 +923,13 @@
                 # We take the center of ring area, not radius.  
                 # This is more accurate than taking the radial center of ring.
-                delta_r = (self.r_max - self.r_min) / self.nbins
-                r_inner = self.r_min + delta_r * i
-                r_outer = r_inner + delta_r
-                x[i] = math.sqrt((r_inner * r_inner + r_outer * r_outer) / 2)
-                
+                #delta_r = (self.r_max - self.r_min) / self.nbins
+                #r_inner = self.r_min + delta_r * i
+                #r_outer = r_inner + delta_r
+                #x[i] = math.sqrt((r_inner * r_inner + r_outer * r_outer) / 2)
+                x[i] = x[i] / y_counts[i]
         y_err[y_err==0] = numpy.average(y_err)        
         idx = (numpy.isfinite(y) & numpy.isfinite(y_err))
         if x_err != None:
-            d_x = x_err[idx]
+            d_x = x_err[idx] / y_counts[idx]
         else:
             d_x = None
