Changeset 525


Ignore:
Timestamp:
Apr 2, 2012 11:46:38 AM (12 years ago)
Author:
toby
Message:

reorder CIF logic to return after finally for better clarity

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/G2importphase_CIF.py

    r524 r525  
    4343        return True
    4444    def Reader(self,filename,filepointer, ParentFrame=None):
     45        returnstat = False
    4546        cellitems = (
    4647            '_cell_length_a','_cell_length_b','_cell_length_c',
     
    8485                    str_blklist.append(blk)
    8586            if not str_blklist:
    86                 return False            # no blocks with coordinates
    87             elif len(str_blklist) == 1: # no choices
     87                selblk = None # no block to choose
     88            elif len(str_blklist) == 1: # only one choice
    8889                selblk = 0
    8990            else:                       # choose from options
     
    120121                    size=(600,100)
    121122                    )
    122                 if selblk is None: return False # User pressed cancel
    123             blknm = str_blklist[selblk]
    124             blk = cf[str_blklist[selblk]]
    125             SpGrp = blk.get("_symmetry_space_group_name_H-M")
    126             if SpGrp:
    127                  E,SGData = G2spc.SpcGroup(SpGrp)
    128             if E:
    129                 self.warnings += ' ERROR in space group symbol '+SpGrp
    130                 self.warnings += ' N.B.: make sure spaces separate axial fields in symbol'
    131                 self.warnings += G2spc.SGErrors(E)
     123            if selblk is None:
     124                returnstat = False # no block selected or available
    132125            else:
    133                 self.Phase['General']['SGData'] = SGData
    134             # cell parameters
    135             cell = []
    136             for lbl in (
    137                 '_cell_length_a','_cell_length_b','_cell_length_c',
    138                 '_cell_angle_alpha','_cell_angle_beta','_cell_angle_gamma',
    139                 ):
    140                 cell.append(cif.get_number_with_esd(blk[lbl])[0])
    141             Volume = G2lat.calc_V(G2lat.cell2A(cell))
    142             self.Phase['General']['Cell'] = [False,]+cell+[Volume,]
    143             # read in atoms
    144             atomloop = blk.GetLoop('_atom_site_label')
    145             atomkeys = [i.lower() for i in atomloop.keys()]
    146             if blk.get('_atom_site_aniso_label'):
    147                 anisoloop = blk.GetLoop('_atom_site_aniso_label')
    148                 anisokeys = [i.lower() for i in anisoloop.keys()]
    149             else:
    150                 anisoloop = None
    151                 anisokeys = []
    152             self.Phase['Atoms'] = []
    153             G2AtomDict = {  '_atom_site_type_symbol' : 1,
    154                             '_atom_site_label' : 0,
    155                             '_atom_site_fract_x' : 3,
    156                             '_atom_site_fract_y' : 4,
    157                             '_atom_site_fract_z' : 5,
    158                             '_atom_site_occupancy' : 6,
    159                             '_atom_site_aniso_u_11' : 11,
    160                             '_atom_site_aniso_u_22' : 12,
    161                             '_atom_site_aniso_u_33' : 13,
    162                             '_atom_site_aniso_u_12' : 14,
    163                             '_atom_site_aniso_u_13' : 15,
    164                             '_atom_site_aniso_u_23' : 16, }
    165             for aitem in atomloop:
    166                 atomlist = ['','','',0,0,0,1.0,'',0,'I',0.01,0,0,0,0,0,0]
    167                 atomlist.append(ran.randint(0,sys.maxint)) # add a unique label
    168                 for val,key in zip(aitem,atomkeys):
    169                     col = G2AtomDict.get(key)
    170                     if col >= 3:
    171                         atomlist[col] = cif.get_number_with_esd(val)[0]
    172                     elif col is not None:
    173                         atomlist[col] = val
    174                     elif key in ('_atom_site_thermal_displace_type',
    175                                '_atom_site_adp_type'):   #Iso or Aniso?
    176                         if val.lower() == 'uani':
    177                             atomlist[9] = 'A'
    178                     elif key == '_atom_site_u_iso_or_equiv':
    179                         atomlist[10] =cif.get_number_with_esd(val)[0]
    180                 ulbl = '_atom_site_aniso_label'
    181                 if  atomlist[9] == 'A' and atomlist[0] in blk.get(ulbl):
    182                     for val,key in zip(anisoloop.GetKeyedPacket(ulbl,atomlist[0]),
    183                                        anisokeys):
     126                blknm = str_blklist[selblk]
     127                blk = cf[str_blklist[selblk]]
     128                SpGrp = blk.get("_symmetry_space_group_name_H-M")
     129                if SpGrp:
     130                     E,SGData = G2spc.SpcGroup(SpGrp)
     131                if E:
     132                    self.warnings += ' ERROR in space group symbol '+SpGrp
     133                    self.warnings += ' N.B.: make sure spaces separate axial fields in symbol'
     134                    self.warnings += G2spc.SGErrors(E)
     135                else:
     136                    self.Phase['General']['SGData'] = SGData
     137                # cell parameters
     138                cell = []
     139                for lbl in (
     140                    '_cell_length_a','_cell_length_b','_cell_length_c',
     141                    '_cell_angle_alpha','_cell_angle_beta','_cell_angle_gamma',
     142                    ):
     143                    cell.append(cif.get_number_with_esd(blk[lbl])[0])
     144                Volume = G2lat.calc_V(G2lat.cell2A(cell))
     145                self.Phase['General']['Cell'] = [False,]+cell+[Volume,]
     146                # read in atoms
     147                atomloop = blk.GetLoop('_atom_site_label')
     148                atomkeys = [i.lower() for i in atomloop.keys()]
     149                if blk.get('_atom_site_aniso_label'):
     150                    anisoloop = blk.GetLoop('_atom_site_aniso_label')
     151                    anisokeys = [i.lower() for i in anisoloop.keys()]
     152                else:
     153                    anisoloop = None
     154                    anisokeys = []
     155                self.Phase['Atoms'] = []
     156                G2AtomDict = {  '_atom_site_type_symbol' : 1,
     157                                '_atom_site_label' : 0,
     158                                '_atom_site_fract_x' : 3,
     159                                '_atom_site_fract_y' : 4,
     160                                '_atom_site_fract_z' : 5,
     161                                '_atom_site_occupancy' : 6,
     162                                '_atom_site_aniso_u_11' : 11,
     163                                '_atom_site_aniso_u_22' : 12,
     164                                '_atom_site_aniso_u_33' : 13,
     165                                '_atom_site_aniso_u_12' : 14,
     166                                '_atom_site_aniso_u_13' : 15,
     167                                '_atom_site_aniso_u_23' : 16, }
     168                for aitem in atomloop:
     169                    atomlist = ['','','',0,0,0,1.0,'',0,'I',0.01,0,0,0,0,0,0]
     170                    atomlist.append(ran.randint(0,sys.maxint)) # add a unique label
     171                    for val,key in zip(aitem,atomkeys):
    184172                        col = G2AtomDict.get(key)
    185                         if col:
     173                        if col >= 3:
    186174                            atomlist[col] = cif.get_number_with_esd(val)[0]
    187                 atomlist[7],atomlist[8] = G2spc.SytSym(atomlist[3:6],SGData)
    188                 self.Phase['Atoms'].append(atomlist)
    189             for lbl in phasenamefields: # get a name for the phase
    190                 name = blk.get(lbl)
    191                 if name is None:
    192                     continue
    193                 name = name.strip()
    194                 if name == '?' or name == '.':
    195                     continue
    196                 else:
    197                     break
    198             else: # no name found, use block name for lack of a better choice
    199                 name = blknm
    200             self.Phase['General']['Name'] = name.strip()[:20]
    201             return True
     175                        elif col is not None:
     176                            atomlist[col] = val
     177                        elif key in ('_atom_site_thermal_displace_type',
     178                                   '_atom_site_adp_type'):   #Iso or Aniso?
     179                            if val.lower() == 'uani':
     180                                atomlist[9] = 'A'
     181                        elif key == '_atom_site_u_iso_or_equiv':
     182                            atomlist[10] =cif.get_number_with_esd(val)[0]
     183                    ulbl = '_atom_site_aniso_label'
     184                    if  atomlist[9] == 'A' and atomlist[0] in blk.get(ulbl):
     185                        for val,key in zip(anisoloop.GetKeyedPacket(ulbl,atomlist[0]),
     186                                           anisokeys):
     187                            col = G2AtomDict.get(key)
     188                            if col:
     189                                atomlist[col] = cif.get_number_with_esd(val)[0]
     190                    atomlist[7],atomlist[8] = G2spc.SytSym(atomlist[3:6],SGData)
     191                    self.Phase['Atoms'].append(atomlist)
     192                for lbl in phasenamefields: # get a name for the phase
     193                    name = blk.get(lbl)
     194                    if name is None:
     195                        continue
     196                    name = name.strip()
     197                    if name == '?' or name == '.':
     198                        continue
     199                    else:
     200                        break
     201                else: # no name found, use block name for lack of a better choice
     202                    name = blknm
     203                self.Phase['General']['Name'] = name.strip()[:20]
     204                returnstat = True
    202205        except Exception as detail:
    203206            print 'CIF error:',detail # for testing
     
    205208            import traceback
    206209            print traceback.format_exc()
    207             return False
     210            returnstat = False
    208211        finally:
    209212            self.DoneBusy()
    210 
     213        return returnstat
Note: See TracChangeset for help on using the changeset viewer.