def parse_grid_xml(self):
        """Parse the grid xyz and calculate the bounding box of the event.

        :raises: GridXmlParseError

        The grid xyz dataset looks like this::

           <?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
           <shakemap_grid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://earthquake.usgs.gov/eqcenter/shakemap"
           xsi:schemaLocation="http://earthquake.usgs.gov
           http://earthquake.usgs.gov/eqcenter/shakemap/xml/schemas/
           shakemap.xsd" event_id="20120807015938" shakemap_id="20120807015938"
           shakemap_version="1" code_version="3.5"
           process_timestamp="2012-08-06T18:28:37Z" shakemap_originator="us"
           map_status="RELEASED" shakemap_event_type="ACTUAL">
           <event magnitude="5.1" depth="206" lat="2.800000"
               lon="128.290000" event_timestamp="2012-08-07T01:55:12WIB"
               event_network="" event_description="Halmahera, Indonesia    " />
           <grid_specification lon_min="126.290000" lat_min="0.802000"
               lon_max="130.290000" lat_max="4.798000"
               nominal_lon_spacing="0.025000" nominal_lat_spacing="0.024975"
               nlon="161" nlat="161" />
           <grid_field index="1" name="LON" units="dd" />
           <grid_field index="2" name="LAT" units="dd" />
           <grid_field index="3" name="PGA" units="pctg" />
           <grid_field index="4" name="PGV" units="cms" />
           <grid_field index="5" name="MMI" units="intensity" />
           <grid_field index="6" name="PSA03" units="pctg" />
           <grid_field index="7" name="PSA10" units="pctg" />
           <grid_field index="8" name="PSA30" units="pctg" />
           <grid_field index="9" name="STDPGA" units="pctg" />
           <grid_field index="10" name="URAT" units="" />
           <grid_field index="11" name="SVEL" units="ms" />
           <grid_data>
           126.2900 04.7980 0.01 0.02 1.16 0.05 0.02 0 0.5 1 600
           126.3150 04.7980 0.01 0.02 1.16 0.05 0.02 0 0.5 1 600
           126.3400 04.7980 0.01 0.02 1.17 0.05 0.02 0 0.5 1 600
           126.3650 04.7980 0.01 0.02 1.17 0.05 0.02 0 0.5 1 600
           ...
           ... etc

        .. note:: We could have also obtained some of this data from the
           grid.xyz and event.xml but the **grid.xml** is preferred because it
           contains clear and unequivical metadata describing the various
           fields and attributes. Also it provides all the data we need in a
           single file.
        """
        LOGGER.debug('ParseGridXml requested.')
        grid_path = self.grid_file_path()
        try:
            document = minidom.parse(grid_path)
            shakemap_grid_element = document.getElementsByTagName(
                'shakemap_grid')
            shakemap_grid_element = shakemap_grid_element[0]
            self.event_id = shakemap_grid_element.attributes[
                'event_id'].nodeValue

            event_element = document.getElementsByTagName('event')
            event_element = event_element[0]
            self.magnitude = float(
                event_element.attributes['magnitude'].nodeValue)
            self.longitude = float(
                event_element.attributes['lon'].nodeValue)
            self.latitude = float(
                event_element.attributes['lat'].nodeValue)
            self.location = event_element.attributes[
                'event_description'].nodeValue.strip()
            self.depth = float(
                event_element.attributes['depth'].nodeValue)
            # Get the date - it's going to look something like this:
            # 2012-08-07T01:55:12WIB
            time_stamp = event_element.attributes['event_timestamp'].nodeValue
            # Note the timezone here is inconsistent with YZ from grid.xml
            # use the latter
            self.time_zone = time_stamp[19:]
            self.extract_date_time(time_stamp)

            specification_element = document.getElementsByTagName(
                'grid_specification')
            specification_element = specification_element[0]
            self.x_minimum = float(
                specification_element.attributes['lon_min'].nodeValue)
            self.x_maximum = float(
                specification_element.attributes['lon_max'].nodeValue)
            self.y_minimum = float(
                specification_element.attributes['lat_min'].nodeValue)
            self.y_maximum = float(
                specification_element.attributes['lat_max'].nodeValue)
            self.grid_bounding_box = QgsRectangle(
                self.x_minimum, self.y_maximum, self.x_maximum, self.y_minimum)
            self.rows = int(float(
                specification_element.attributes['nlat'].nodeValue))
            self.columns = int(float(
                specification_element.attributes['nlon'].nodeValue))
            data_element = document.getElementsByTagName(
                'grid_data')
            data_element = data_element[0]
            data = data_element.firstChild.nodeValue

            # Extract the 1,2 and 5th (MMI) columns and populate mmi_data
            longitude_column = 0
            latitude_column = 1
            mmi_column = 4
            lon_list = []
            lat_list = []
            mmi_list = []
            for line in data.split('\n'):
                if not line:
                    continue
                tokens = line.split(' ')
                longitude = tokens[longitude_column]
                latitude = tokens[latitude_column]
                mmi = tokens[mmi_column]
                lon_list.append(float(longitude))
                lat_list.append(float(latitude))
                mmi_list.append(float(mmi))

            if self.smoothing_method == NUMPY_SMOOTHING:
                LOGGER.debug('We are using NUMPY smoothing')
                ncols = len(np.where(np.array(lon_list) == lon_list[0])[0])
                nrows = len(np.where(np.array(lat_list) == lat_list[0])[0])

                # reshape mmi_list to 2D array to apply gaussian filter
                Z = np.reshape(mmi_list, (nrows, ncols))

                # smooth MMI matrix
                mmi_list = convolve(Z, gaussian_kernel(self.smoothing_sigma))

                # reshape array back to 1D long list of mmi
                mmi_list = np.reshape(mmi_list, ncols * nrows)

            elif self.smoothing_method == SCIPY_SMOOTHING:
                LOGGER.debug('We are using SCIPY smoothing')
                from scipy.ndimage.filters import gaussian_filter
                ncols = len(np.where(np.array(lon_list) == lon_list[0])[0])
                nrows = len(np.where(np.array(lat_list) == lat_list[0])[0])

                # reshape mmi_list to 2D array to apply gaussian filter
                Z = np.reshape(mmi_list, (nrows, ncols))

                # smooth MMI matrix
                # Help from Hadi Ghasemi
                mmi_list = gaussian_filter(Z, self.smoothing_sigma)

                # reshape array back to 1D long list of mmi
                mmi_list = np.reshape(mmi_list, ncols * nrows)

            # zip lists as list of tuples
            self.mmi_data = list(zip(lon_list, lat_list, mmi_list))

        except Exception as e:
            LOGGER.exception('Event parse failed')
            raise GridXmlParseError(
                'Failed to parse grid file.\n%s\n%s' % (e.__class__, str(e)))
def parse_grid_xml ( self ) : LOGGER . debug ( 'ParseGridXml requested.' ) grid_path = self . grid_file_path ( ) try : document = minidom . parse ( grid_path ) shakemap_grid_element = document . getElementsByTagName ( 'shakemap_grid' ) shakemap_grid_element = shakemap_grid_element [ 0 ] self . event_id = shakemap_grid_element . attributes [ 'event_id' ] . nodeValue event_element = document . getElementsByTagName ( 'event' ) event_element = event_element [ 0 ] self . magnitude = float ( event_element . attributes [ 'magnitude' ] . nodeValue ) self . longitude = float ( event_element . attributes [ 'lon' ] . nodeValue ) self . latitude = float ( event_element . attributes [ 'lat' ] . nodeValue ) self . location = event_element . attributes [ 'event_description' ] . nodeValue . strip ( ) self . depth = float ( event_element . attributes [ 'depth' ] . nodeValue ) time_stamp = event_element . attributes [ 'event_timestamp' ] . nodeValue self . time_zone = time_stamp [ 19 : ] self . extract_date_time ( time_stamp ) specification_element = document . getElementsByTagName ( 'grid_specification' ) specification_element = specification_element [ 0 ] self . x_minimum = float ( specification_element . attributes [ 'lon_min' ] . nodeValue ) self . x_maximum = float ( specification_element . attributes [ 'lon_max' ] . nodeValue ) self . y_minimum = float ( specification_element . attributes [ 'lat_min' ] . nodeValue ) self . y_maximum = float ( specification_element . attributes [ 'lat_max' ] . nodeValue ) self . grid_bounding_box = QgsRectangle ( self . x_minimum , self . y_maximum , self . x_maximum , self . y_minimum ) self . rows = int ( float ( specification_element . attributes [ 'nlat' ] . nodeValue ) ) self . columns = int ( float ( specification_element . attributes [ 'nlon' ] . nodeValue ) ) data_element = document . getElementsByTagName ( 'grid_data' ) data_element = data_element [ 0 ] data = data_element . firstChild . nodeValue longitude_column = 0 latitude_column = 1 mmi_column = 4 lon_list = [ ] lat_list = [ ] mmi_list = [ ] for line in data . split ( '\n' ) : if not line : continue tokens = line . split ( ' ' ) longitude = tokens [ longitude_column ] latitude = tokens [ latitude_column ] mmi = tokens [ mmi_column ] lon_list . append ( float ( longitude ) ) lat_list . append ( float ( latitude ) ) mmi_list . append ( float ( mmi ) ) if self . smoothing_method == NUMPY_SMOOTHING : LOGGER . debug ( 'We are using NUMPY smoothing' ) ncols = len ( np . where ( np . array ( lon_list ) == lon_list [ 0 ] ) [ 0 ] ) nrows = len ( np . where ( np . array ( lat_list ) == lat_list [ 0 ] ) [ 0 ] ) Z = np . reshape ( mmi_list , ( nrows , ncols ) ) mmi_list = convolve ( Z , gaussian_kernel ( self . smoothing_sigma ) ) mmi_list = np . reshape ( mmi_list , ncols * nrows ) elif self . smoothing_method == SCIPY_SMOOTHING : LOGGER . debug ( 'We are using SCIPY smoothing' ) from scipy . ndimage . filters import gaussian_filter ncols = len ( np . where ( np . array ( lon_list ) == lon_list [ 0 ] ) [ 0 ] ) nrows = len ( np . where ( np . array ( lat_list ) == lat_list [ 0 ] ) [ 0 ] ) Z = np . reshape ( mmi_list , ( nrows , ncols ) ) mmi_list = gaussian_filter ( Z , self . smoothing_sigma ) mmi_list = np . reshape ( mmi_list , ncols * nrows ) self . mmi_data = list ( zip ( lon_list , lat_list , mmi_list ) ) except Exception as e : LOGGER . exception ( 'Event parse failed' ) raise GridXmlParseError ( 'Failed to parse grid file.\n%s\n%s' % ( e . __class__ , str ( e ) ) )
Parse the grid xyz and calculate the bounding box of the event.

        :raises: GridXmlParseError

        The grid xyz dataset looks like this::

           <?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
           <shakemap_grid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://earthquake.usgs.gov/eqcenter/shakemap"
           xsi:schemaLocation="http://earthquake.usgs.gov
           http://earthquake.usgs.gov/eqcenter/shakemap/xml/schemas/
           shakemap.xsd" event_id="20120807015938" shakemap_id="20120807015938"
           shakemap_version="1" code_version="3.5"
           process_timestamp="2012-08-06T18:28:37Z" shakemap_originator="us"
           map_status="RELEASED" shakemap_event_type="ACTUAL">
           <event magnitude="5.1" depth="206" lat="2.800000"
               lon="128.290000" event_timestamp="2012-08-07T01:55:12WIB"
               event_network="" event_description="Halmahera, Indonesia    " />
           <grid_specification lon_min="126.290000" lat_min="0.802000"
               lon_max="130.290000" lat_max="4.798000"
               nominal_lon_spacing="0.025000" nominal_lat_spacing="0.024975"
               nlon="161" nlat="161" />
           <grid_field index="1" name="LON" units="dd" />
           <grid_field index="2" name="LAT" units="dd" />
           <grid_field index="3" name="PGA" units="pctg" />
           <grid_field index="4" name="PGV" units="cms" />
           <grid_field index="5" name="MMI" units="intensity" />
           <grid_field index="6" name="PSA03" units="pctg" />
           <grid_field index="7" name="PSA10" units="pctg" />
           <grid_field index="8" name="PSA30" units="pctg" />
           <grid_field index="9" name="STDPGA" units="pctg" />
           <grid_field index="10" name="URAT" units="" />
           <grid_field index="11" name="SVEL" units="ms" />
           <grid_data>
           126.2900 04.7980 0.01 0.02 1.16 0.05 0.02 0 0.5 1 600
           126.3150 04.7980 0.01 0.02 1.16 0.05 0.02 0 0.5 1 600
           126.3400 04.7980 0.01 0.02 1.17 0.05 0.02 0 0.5 1 600
           126.3650 04.7980 0.01 0.02 1.17 0.05 0.02 0 0.5 1 600
           ...
           ... etc

        .. note:: We could have also obtained some of this data from the
           grid.xyz and event.xml but the **grid.xml** is preferred because it
           contains clear and unequivical metadata describing the various
           fields and attributes. Also it provides all the data we need in a
           single file.