Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial import.

Signed-off-by: Christian S. Perone <christian.perone@gmail.com>
  • Loading branch information...
commit a9496e82bcc5f6329a07b3741176a6948349544b 0 parents
@perone authored
8 README
@@ -0,0 +1,8 @@
+This is the Python pyearthquake module.
+
+This module is used to parse/retrieve USGS and MODIS RapidFire data.
+
+For more information and examples, see:
+
+http://pyevolve.sourceforge.net/wordpress/?p=1013
+http://pyevolve.sourceforge.net/wordpress/?p=1471
4 pyearthquake/__init__.py
@@ -0,0 +1,4 @@
+__author__ = "Christian S. Perone <christian.perone@gmail.com>"
+__version__ = "0.5"
+
+__all__ = ["usgs", "modis", "httputil"]
23 pyearthquake/httputil.py
@@ -0,0 +1,23 @@
+import urllib
+import csv
+
+class RemoteFile:
+ def __init__(self, url):
+ self.url = url
+ self.info = None
+ self.filename = None
+
+ def update(self):
+ self.filename, self.info = urllib.urlretrieve(self.url)
+
+ def get_info(self, key):
+ return self.info[key]
+
+class CSVRemoteFile(RemoteFile):
+ def __init__(self, url):
+ RemoteFile.__init__(self, url)
+ self.update()
+
+ def get_csv_reader(self):
+ fhandle = open(self.filename, "r")
+ return csv.reader(fhandle)
112 pyearthquake/modis.py
@@ -0,0 +1,112 @@
+# pyearthquake - Python Earthquake and MODIS utilities
+# Copyright (C) 2010 Christian S. Perone
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+from pyearthquake import httputil
+
+from PIL import Image
+from mpl_toolkits.basemap import Basemap
+import pylab
+
+import tempfile
+import zipfile
+import os
+import datetime
+import urllib
+
+class BadZipFile(Exception):
+ pass
+
+class InvalidSatellite(Exception):
+ pass
+
+class InvalidResolution(Exception):
+ pass
+
+URL_RAPIDFIRE_SUBSET = "http://rapidfire.sci.gsfc.nasa.gov/subsets/?subset=%s.%s%s.%s.%s.zip"
+URL_METADATA = "http://rapidfire.sci.gsfc.nasa.gov/subsets/?subset=%s.%s%s.%s.%s.txt"
+IMAGE_FILE = '%s.%s%s.%s.%s.jpg'
+
+RESOLUTIONS = ["250m", "500m", "2km", "1km"]
+SATELLITES = ["terra", "aqua"]
+
+def parse_term(metadata, term):
+ start = metadata.find(term + ":") + len(term)+1
+ end = metadata[start:].find("\n")
+ val = float(metadata[start:start+end])
+ return val
+
+def get_modis_subset(dt, subset_name, satellite_name="terra", resolution="1km", coastline=True, show=True):
+ if satellite_name not in SATELLITES:
+ raise InvalidSatellite("Satellite name invalid, use one of %s" % SATELLITES)
+
+ if resolution not in RESOLUTIONS:
+ raise InvalidResolution("Resolution not available, use one of %s" % RESOLUTIONS)
+
+
+ day_of_year = "%03d" % dt.timetuple().tm_yday
+ year = str(dt.year)
+
+ tuple_config = (subset_name, year, day_of_year, satellite_name, resolution)
+ url = URL_RAPIDFIRE_SUBSET % tuple_config
+ image_filename = IMAGE_FILE % tuple_config
+ url_metadata = URL_METADATA % tuple_config
+
+ rfile = httputil.RemoteFile(url)
+ rfile.update()
+ filename = rfile.filename
+
+ try:
+ zipf = zipfile.ZipFile(rfile.filename)
+ except zipfile.BadZipfile:
+ raise BadZipFile("Maybe the data is not yet ready on MODIS site !")
+ sys.exit(-1)
+
+ tempdir = tempfile.mkdtemp()
+
+ for name in zipf.namelist():
+ data = zipf.read(name)
+ outfile = os.path.join(tempdir, name)
+ f = open(outfile, 'wb')
+ f.write(data)
+ f.close()
+
+ zipf.close()
+
+ image_path = os.path.join(tempdir, image_filename)
+ image_modis = Image.open(image_path)
+
+ filein = urllib.urlopen(url_metadata)
+ metadata_data = filein.read()
+ filein.close()
+
+ ll_lon = parse_term(metadata_data, "LL lon")
+ ll_lat = parse_term(metadata_data, "LL lat")
+ ur_lon = parse_term(metadata_data, "UR lon")
+ ur_lat = parse_term(metadata_data, "UR lat")
+
+ m = Basemap(projection='cyl', llcrnrlat=ll_lat, urcrnrlat=ur_lat,\
+ llcrnrlon=ll_lon, urcrnrlon=ur_lon, resolution='i')
+ if coastline:
+ m.drawcoastlines()
+ m.drawmapboundary(fill_color='aqua')
+ m.imshow(image_modis)
+ pylab.title("MODIS %s Image for %s on %s (%s res.)" % (satellite_name,
+ subset_name,
+ dt.strftime("%d/%m/%Y"),
+ resolution))
+ if show: pylab.show()
+ return m
130 pyearthquake/usgs.py
@@ -0,0 +1,130 @@
+# pyearthquake - Python Earthquake and MODIS utilities
+# Copyright (C) 2010 Christian S. Perone
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+from pyearthquake import httputil
+import datetime
+import email.utils as eut
+
+CATALOGS = {}
+CATALOGS["M1+PAST_HOUR"] = "http://earthquake.usgs.gov/earthquakes/catalogs/eqs1hour-M1.txt"
+CATALOGS["M1+PAST_DAY"] = "http://earthquake.usgs.gov/earthquakes/catalogs/eqs1day-M1.txt"
+CATALOGS["M1+PAST_7DAY"] = "http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt"
+
+SHAKEMAPS = {}
+SHAKEMAPS["INSTUMENTAL_INTENSITY"] = "http://earthquake.usgs.gov/earthquakes/shakemap/global/shake/%s/download/intensity.jpg"
+SHAKEMAPS["PEAK_GROUND_ACC"] = "http://earthquake.usgs.gov/earthquakes/shakemap/global/shake/%s/download/pga.jpg"
+SHAKEMAPS["UNCERTAINTY"] = "http://earthquake.usgs.gov/earthquakes/shakemap/global/shake/%s/download/sd.jpg"
+SHAKEMAPS["DECORATED"] = "http://earthquake.usgs.gov/earthquakes/shakemap/global/shake/%s/download/tvmap.jpg"
+SHAKEMAPS["BARE"] = "http://earthquake.usgs.gov/earthquakes/shakemap/global/shake/%s/download/tvmap_bare.jpg"
+
+class CatalogNotFound(Exception):
+ pass
+
+class ShakemapNotFound(Exception):
+ pass
+
+class ShakemapNotAvailable(Exception):
+ pass
+
+class USGSCatalog:
+ def __init__(self, remote_data):
+ self.remote_data = remote_data
+ self.data = []
+ csvreader = remote_data.get_csv_reader()
+ self.header = csvreader.next()
+ for row in csvreader:
+ self.data.append(dict(zip(self.header, row)))
+
+ def __getitem__(self, key):
+ return self.data[key]
+
+ def __iter__(self):
+ return iter(self.data)
+
+ def __len__(self):
+ return len(self.data)
+
+ @property
+ def last_updated(self):
+ dtstr = self.remote_data.get_info("last-modified")
+ #return datetime.datetime(*eut.parsedate(dtstr)[:6])
+ return dtstr
+
+
+def retrieve_catalog(catalog_name):
+ if catalog_name not in CATALOGS:
+ raise CatalogNotFound("Use one of %s catalogs" % CATALOGS.keys())
+
+ remote_data = httputil.CSVRemoteFile(CATALOGS[catalog_name])
+ return USGSCatalog(remote_data)
+
+
+def retrieve_shakemap(event, shake_map_type):
+ import pylab
+
+ if shake_map_type not in SHAKEMAPS:
+ raise ShakemapNotFound("Use one of %s shakemap types" % SHAKEMAPS.keys())
+
+ eqid = event["Eqid"]
+ rfile = httputil.RemoteFile(SHAKEMAPS[shake_map_type] % eqid)
+ rfile.update()
+
+ fhandle = open(rfile.filename, "r")
+ head = fhandle.read(300)
+ fhandle.close()
+
+ if head.find("File Not Found (404)") >= 0:
+ raise ShakemapNotAvailable("Shakemap not available for this event !")
+
+ img = pylab.imread(rfile.filename)
+ pylab.imshow(pylab.flipud(img))
+ pylab.show()
+
+
+def plot_events(events, map_basemap=None, cmap=None, point_size=30, show=True):
+ from mpl_toolkits.basemap import Basemap
+ import pylab
+
+ if cmap is None: color_map = pylab.cm.jet
+ else: color_map = cmap
+
+ xlist = [float(e["Lon"]) for e in events]
+ ylist = [float(e["Lat"]) for e in events]
+ mag = [float(e["Magnitude"]) for e in events]
+
+ if map_basemap is None:
+ m = Basemap(projection='cyl', resolution='l')
+ m.drawcoastlines()
+ m.drawcountries()
+ m.drawmapboundary()
+ else:
+ m = map_basemap
+
+ m.scatter(xlist, ylist, point_size, c=mag,
+ cmap=color_map, marker='o', edgecolors='none',
+ zorder=10, alpha=0.75)
+
+ if show: pylab.show()
+ return m
+
+
+
+
+
+
+
+
33 setup.py
@@ -0,0 +1,33 @@
+# pyearthquake - Python Earthquake and MODIS utilities
+# Copyright (C) 2010 Christian S. Perone
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+import pyearthquake
+from setuptools import setup
+
+setup(name='pyearthquake',
+ version=pyearthquake.__version__,
+ description='Python Earthquake and MODIS utilities',
+ author='Christian S. Perone',
+ author_email='christian.perone@gmail.com',
+ url='http://pyevolve.sourceforge.net/wordpress',
+ packages=['pyearthquake'],
+ install_requires=["matplotlib >= 0.99.0",
+ "numpy >= 1.3.0",
+ "PIL >= 1.1.6",
+ "basemap >= 0.99.4"],
+ license = "GPLv2",
+ keywords = "python modis usgs earthquake analysis aqua terra satellite"
+ )

0 comments on commit a9496e8

Please sign in to comment.
Something went wrong with that request. Please try again.