Index: prview/perspectives/pr/inversion_state.py
===================================================================
--- prview/perspectives/pr/inversion_state.py	(revision 91128648ab092f16edfa19d3fa49d60b54ee4a53)
+++ prview/perspectives/pr/inversion_state.py	(revision 410aad8b94a9997446a564f56952fd52e36114fe)
@@ -80,4 +80,8 @@
         self.iq_obs  = None
         self.iq_calc = None
+        
+        # Coefficients
+        self.coefficients = None
+        self.covariance = None
     
     def __str__(self):
@@ -152,8 +156,10 @@
         
         # File name
+        element = newdoc.createElement("filename")
         if self.file is not None:
-            element = newdoc.createElement("filename")
             element.appendChild(newdoc.createTextNode(str(self.file)))
-            top_element.appendChild(element)
+        else:
+            element.appendChild(newdoc.createTextNode(str(file)))
+        top_element.appendChild(element)
         
         element = newdoc.createElement("timestamp")
@@ -181,4 +187,12 @@
             exec "element.appendChild(newdoc.createTextNode(str(%s)))" % item[1]
             outputs.appendChild(element)
+                    
+        # Save output coefficients and its covariance matrix
+        element = newdoc.createElement("coefficients")
+        element.appendChild(newdoc.createTextNode(str(self.coefficients)))
+        outputs.appendChild(element)
+        element = newdoc.createElement("covariance")
+        element.appendChild(newdoc.createTextNode(str(self.covariance)))
+        outputs.appendChild(element)
                     
         # Save the file
@@ -244,4 +258,5 @@
                             if node.hasChildNodes():
                                 for item in node.childNodes:
+                                    # Look for standard outputs
                                     for out in out_list:
                                         if item.nodeName == out[0]:
@@ -250,4 +265,62 @@
                                             except:
                                                 exec '%s = None' % out[1]
+                                                
+                                    # Look for coefficients
+                                    # Format is [value, value, value, value]
+                                    if item.nodeName == 'coefficients':
+                                        # Remove brackets
+                                        c_values = item.childNodes[0].nodeValue.strip().replace('[','')
+                                        c_values = c_values.replace(']','')
+                                        toks = c_values.split()
+                                        self.coefficients = []
+                                        for c in toks:
+                                            try:
+                                                self.coefficients.append(float(c))
+                                            except:
+                                                # Bad data, skip. We will count the number of 
+                                                # coefficients at the very end and deal with 
+                                                # inconsistencies then.
+                                                pass
+                                        # Sanity check
+                                        if not len(self.coefficients) == self.nfunc:
+                                            # Inconsistent number of coefficients. Don't keep the data.
+                                            err_msg = "InversionState.fromXML: inconsistant number of coefficients: "
+                                            err_msg += "%d %d" % (len(self.coefficients), self.nfunc)
+                                            logging.error(err_msg)
+                                            self.coefficients = None
+                                            
+                                    # Look for covariance matrix
+                                    # Format is [ [value, value], [value, value] ]
+                                    elif item.nodeName == "covariance":
+                                        # Parse rows
+                                        rows = item.childNodes[0].nodeValue.strip().split('[')
+                                        self.covariance = []
+                                        for row in rows:
+                                            row = row.strip()
+                                            if len(row) == 0: continue
+                                            # Remove end bracket
+                                            row = row.replace(']','')
+                                            c_values = row.split()
+                                            cov_row = []
+                                            for c in c_values:
+                                                try:
+                                                    cov_row.append(float(c))
+                                                except:
+                                                    # Bad data, skip. We will count the number of 
+                                                    # coefficients at the very end and deal with 
+                                                    # inconsistencies then.
+                                                    pass
+                                            # Sanity check: check the number of entries in the row
+                                            if len(cov_row) == self.nfunc:
+                                                self.covariance.append(cov_row)
+                                        # Sanity check: check the number of rows in the covariance
+                                        # matrix
+                                        if not len(self.covariance) == self.nfunc:
+                                            # Inconsistent dimensions of the covariance matrix.
+                                            # Don't keep the data.
+                                            err_msg = "InversionState.fromXML: inconsistant dimensions of the covariance matrix: "
+                                            err_msg += "%d %d" % (len(self.covariance), self.nfunc)
+                                            logging.error(err_msg)
+                                            self.covariance = None
             else:
                 raise RuntimeError, "Unsupported P(r) file version"
@@ -349,4 +422,6 @@
             basename  = os.path.basename(path)
             root, extension = os.path.splitext(basename)
+            #TODO: eventually remove the check for .xml once
+            # the P(r) writer/reader is truly complete.
             if  extension.lower() in self.ext or \
                 extension.lower() == '.xml':
@@ -369,5 +444,5 @@
                     prstate = self._parse_prstate(entry)
                     sas_entry.meta_data['prstate'] = prstate
-                    sas_entry.filename = basename
+                    sas_entry.filename = prstate.file
                     output.append(sas_entry)
         else:
@@ -378,4 +453,6 @@
             return None
         elif len(output)==1:
+            # Call back to post the new state
+            self.call_back(output[0].meta_data['prstate'], datainfo = output[0])
             return output[0]
         else:
@@ -396,6 +473,6 @@
             if datainfo is None:
                 datainfo = DataLoader.data_info.Data1D(x=[], y=[])    
-            elif not datainfo.__class__ == DataLoader.data_info.Data1D: 
-                raise RuntimeError, "The cansas writer expects a Data1D instance"
+            elif not issubclass(datainfo.__class__, DataLoader.data_info.Data1D):
+                raise RuntimeError, "The cansas writer expects a Data1D instance: %s" % str(datainfo.__class__.__name__)
         
             # Create basic XML document
Index: prview/perspectives/pr/pr.py
===================================================================
--- prview/perspectives/pr/pr.py	(revision d483799931ad28f80987a67ff518ac50ccb6eed1)
+++ prview/perspectives/pr/pr.py	(revision 410aad8b94a9997446a564f56952fd52e36114fe)
@@ -1,3 +1,2 @@
-#TODO: Use simview to generate P(r) and I(q) pairs in sansview.
 # Make sure the option of saving each curve is available 
 # Use the I(q) curve as input and compare the output to P(r)
@@ -89,12 +88,13 @@
         from inversion_state import Reader
          
-        reader = Reader(self.set_state)
+        #TODO: get rid of the cansas flag 
+        self.state_reader = Reader(self.set_state, cansas = not self.standalone)
         l = Loader()
-        l.associate_file_reader('.prv', reader)
+        l.associate_file_reader('.prv', self.state_reader)
                 
         # Log startup
         logging.info("Pr(r) plug-in started")
         
-    def set_state(self, state):
+    def set_state(self, state, datainfo=None):
         """
             Call-back method for the inversion state reader.
@@ -102,7 +102,25 @@
             
             @param state: InversionState object
-        """
-        self.control_panel.set_state(state)
-        #print state
+            @param datainfo: Data1D object [optional]
+        """
+        try:
+            # If we are not in standalone mode, the panel will not
+            # load any data file and we need to keep track of the
+            # data here.
+            if self.standalone == False:
+                if datainfo is None:
+                    raise RuntimeError, "Pr.set_state: datainfo parameter cannot be None in standalone mode"
+                    
+                self.current_plottable = datainfo
+                self.current_plottable.group_id = datainfo.meta_data['prstate'].file
+                
+            # Load the P(r) results
+            self.control_panel.set_state(state)
+                        
+            # Make sure the user sees the P(r) panel after loading
+            self.parent.set_perspective(self.perspective)            
+
+        except:
+            logging.error("prview.set_state: %s" % sys.exc_value)
 
     def populate_menu(self, id, owner):
@@ -828,5 +846,22 @@
         self.control_panel.q_max = self.pr.x.max()
             
-
+    def save_data(self, filepath, prstate=None):
+        """
+            Save data in provided state object.
+            TODO: move the state code away from inversion_panel and move it here. 
+                    Then remove the "prstate" input and make this method private.
+                    
+            @param filepath: path of file to write to
+            @param prstate: P(r) inversion state 
+        """
+        #TODO: do we need this or can we use DataLoader.loader.save directly?
+        
+        # Add output data and coefficients to state
+        prstate.coefficients = self._last_out
+        prstate.covariance = self._last_cov
+        
+        # Write the output to file
+        self.state_reader.write(filepath, self.current_plottable, prstate)
+        
         
     def setup_plot_inversion(self, alpha, nfunc, d_max, q_min=None, q_max=None, 
