NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

Changeset 67

Show
Ignore:
Timestamp:
08/20/07 14:57:17
Author:
cbc
Message:

Fulfill ticket #7: Add deep copy funtionality to Data class and ticket #8: Add string to float and !NaN conversion to Data class

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sodar/trunk/sodar/data.py

    r66 r67  
    111111        """Clean up data for analysis.""" 
    112112        self._copy() 
    113         # self._convert() 
     113        self._convert() 
    114114        # compute time interval 
    115115        # correct for missing times 
     
    127127     
    128128    def _copy(self): 
    129         """Create a deep copy of all the samples in this Data instance.""" 
    130         self.samples = [(dict(sample.header), list(sample.body)) 
    131                         for sample in self] 
    132         for sample in self.samples: 
    133             for altitude in sample[1]: 
    134                 altitude = dict(altitude) 
     129        """Create a deep copy as a list of Sample copies.""" 
     130        self.samples = [sample._copy() for sample in self] 
    135131     
    136132    def _convert(self): 
    137133        """Convert to numbers and correct for invalid values.""" 
    138134        INVALID = "-9999" 
    139         # convert to numbers and correct for invalid values 
    140135        for sample in self.samples: 
    141             for altitude in sample[1]: 
    142                 for key in altitude.keys(): 
     136            body = sample[1] 
     137            for altitude in body: 
     138                for key,value in altitude.items(): 
    143139                    try: 
    144                         if altitude[key] == INVALID: 
     140                        if value == INVALID: 
    145141                            raise ValueError 
    146                         altitude[key] = float(altitude[key]
     142                        altitude[key] = float(value
    147143                    except (ValueError, TypeError, KeyError): 
    148144                        altitude[key] = n.NaN 
     
    171167        if self.body is not None: 
    172168            self.body = Body(self.body) 
    173  
     169     
    174170    def __getitem__(self, index): 
    175171        """Index Sample by body attribute.""" 
     
    178174        except TypeError:   # sample.body may not exist 
    179175            raise IndexError('Sample index out of range') 
     176     
     177    def _copy(self): 
     178        """Create a deep copy as a tuple of header and body copies.""" 
     179        return (self.header._copy(), self.body._copy()) 
    180180 
    181181 
     
    202202        self.update(dict(zip(" ".join(headerLines[::2]).split(), 
    203203                             " ".join(headerLines[1::2]).split()))) 
    204          
     204     
     205    def _copy(self): 
     206        """Create a shallow copy as a dictionary.""" 
     207        return self.copy() 
     208 
    205209 
    206210class Body(list): 
     
    239243    def _find(self, index): 
    240244        """Find altitude data in Body.""" 
    241         for altitudeData in self: 
    242             if altitudeData['ALT'] != index: continue 
    243             return altitudeData 
     245        for altitude in self: 
     246            try: 
     247                if altitude['ALT'] != index: continue 
     248            except KeyError: 
     249                continue 
     250            return altitude 
    244251        raise IndexError('Body index, out of range') 
     252 
     253    def _copy(self): 
     254        """Create a deep copy as a list of dictionaries.""" 
     255        return [altitude.copy() for altitude in self] 
    245256 
    246257