def test_1000m_to_250m(self): """test the 1 km to 250 meter interpolation facility """ gfilename_hdf = "testdata/MOD03_A12278_113638_2012278145123.hdf" gfilename = "testdata/250m_lonlat_section_input.npz" result_filename = "testdata/250m_lonlat_section_result.npz" from pyhdf.SD import SD from pyhdf.error import HDF4Error gdata = None try: gdata = SD(gfilename_hdf) except HDF4Error: print "Failed reading eos-hdf file %s" % gfilename_hdf try: indata = np.load(gfilename) except IOError: return if gdata: lats = gdata.select("Latitude")[20:50, :] lons = gdata.select("Longitude")[20:50, :] else: lats = indata['lat'] / 1000. lons = indata['lon'] / 1000. verif = np.load(result_filename) vlons = verif['lon'] / 1000. vlats = verif['lat'] / 1000. tlons, tlats = modis1kmto250m(lons, lats) self.assert_(np.allclose(tlons, vlons, atol=0.05)) self.assert_(np.allclose(tlats, vlats, atol=0.05))
def get_lat_lon_modis(satscene, options): """Read lat and lon. """ filename_tmpl = satscene.time_slot.strftime(options["geofile"]) file_list = glob.glob(os.path.join(options["dir"], filename_tmpl)) if len(file_list) == 0: # Try in the same directory as the data data_dir = os.path.split(options["filename"])[0] file_list = glob.glob(os.path.join(data_dir, filename_tmpl)) if len(file_list) > 1: logger.warning("More than 1 geolocation file matching!") filename = max(file_list, key=lambda x: os.stat(x).st_mtime) coarse_resolution = 1000 elif len(file_list) == 0: logger.warning("No geolocation file matching " + filename_tmpl + " in " + options["dir"]) logger.debug("Using 5km geolocation and interpolating") filename = options["filename"] coarse_resolution = 5000 else: filename = file_list[0] coarse_resolution = 1000 logger.debug("Loading geolocation file: " + str(filename) + " at resolution " + str(coarse_resolution)) resolution = options["resolution"] data = SD(str(filename)) lat = data.select("Latitude") fill_value = lat.attributes()["_FillValue"] lat = np.ma.masked_equal(lat.get(), fill_value) lon = data.select("Longitude") fill_value = lon.attributes()["_FillValue"] lon = np.ma.masked_equal(lon.get(), fill_value) if resolution == coarse_resolution: return lat, lon cores = options["cores"] from geotiepoints import modis5kmto1km, modis1kmto500m, modis1kmto250m logger.debug("Interpolating from " + str(coarse_resolution) + " to " + str(resolution)) if coarse_resolution == 5000: lon, lat = modis5kmto1km(lon, lat) if resolution == 500: lon, lat = modis1kmto500m(lon, lat, cores) if resolution == 250: lon, lat = modis1kmto250m(lon, lat, cores) return lat, lon
def test_1000m_to_250m(self): """test the 1 km to 250 meter interpolation facility """ with h5py.File(FILENAME_250M_RESULT) as h5f: glons = h5f['longitude'][:] / 1000. glats = h5f['latitude'][:] / 1000. with h5py.File(FILENAME_250M_INPUT) as h5f: lons = h5f['longitude'][:] / 1000. lats = h5f['latitude'][:] / 1000. tlons, tlats = modis1kmto250m(lons, lats) self.assert_(np.allclose(tlons, glons, atol=0.05)) self.assert_(np.allclose(tlats, glats, atol=0.05)) tlons, tlats = modis1kmto250m(lons, lats, cores=4) self.assert_(np.allclose(tlons, glons, atol=0.05)) self.assert_(np.allclose(tlats, glats, atol=0.05))
def test_1000m_to_250m(self): """test the 1 km to 250 meter interpolation facility """ gfilename_hdf = "testdata/MOD03_A12278_113638_2012278145123.hdf" gfilename = "testdata/250m_lonlat_section_input.npz" result_filename = "testdata/250m_lonlat_section_result.npz" from pyhdf.SD import SD from pyhdf.error import HDF4Error gdata = None try: gdata = SD(gfilename_hdf) except HDF4Error: print "Failed reading eos-hdf file %s" % gfilename_hdf try: indata = np.load(gfilename) except IOError: return if gdata: lats = gdata.select("Latitude")[20:50, :] lons = gdata.select("Longitude")[20:50, :] else: lats = indata['lat'] / 1000. lons = indata['lon'] / 1000. verif = np.load(result_filename) vlons = verif['lon'] / 1000. vlats = verif['lat'] / 1000. tlons, tlats = modis1kmto250m(lons, lats) self.assert_(np.allclose(tlons, vlons, atol=0.05)) self.assert_(np.allclose(tlats, vlats, atol=0.05)) tlons, tlats = modis1kmto250m(lons, lats, cores=4) self.assert_(np.allclose(tlons, vlons, atol=0.05)) self.assert_(np.allclose(tlats, vlats, atol=0.05))
def get_lonlat(self, resolution, cores=1): """Read lat and lon. """ if resolution in self.areas: return self.areas[resolution] logger.debug("generating lon, lat at %d", resolution) if self.geofile is not None: coarse_resolution = 1000 filename = self.geofile else: coarse_resolution = 5000 logger.info("Using 5km geolocation and interpolating") filename = (self.datafiles.get(1000) or self.datafiles.get(500) or self.datafiles.get(250)) logger.debug("Loading geolocation from file: " + str(filename) + " at resolution " + str(coarse_resolution)) data = SD(str(filename)) lat = data.select("Latitude") fill_value = lat.attributes()["_FillValue"] lat = np.ma.masked_equal(lat.get(), fill_value) lon = data.select("Longitude") fill_value = lon.attributes()["_FillValue"] lon = np.ma.masked_equal(lon.get(), fill_value) if resolution == coarse_resolution: self.areas[resolution] = lon, lat return lon, lat from geotiepoints import modis5kmto1km, modis1kmto500m, modis1kmto250m logger.debug("Interpolating from " + str(coarse_resolution) + " to " + str(resolution)) if coarse_resolution == 5000: lon, lat = modis5kmto1km(lon, lat) if resolution == 500: lon, lat = modis1kmto500m(lon, lat, cores) if resolution == 250: lon, lat = modis1kmto250m(lon, lat, cores) self.areas[resolution] = lon, lat return lon, lat
def get_lonlat(self, resolution, time_slot, cores=1): """Read lat and lon. """ if (resolution, time_slot) in self.areas: return self.areas[resolution, time_slot] logger.debug("generating lon, lat at %d", resolution) if self.geofile is not None: coarse_resolution = 1000 filename = self.geofile else: coarse_resolution = 5000 logger.info("Using 5km geolocation and interpolating") filename = (self.datafiles.get(1000) or self.datafiles.get(500) or self.datafiles.get(250)) logger.debug("Loading geolocation from file: " + str(filename) + " at resolution " + str(coarse_resolution)) data = SD(str(filename)) lat = data.select("Latitude") fill_value = lat.attributes()["_FillValue"] lat = np.ma.masked_equal(lat.get(), fill_value) lon = data.select("Longitude") fill_value = lon.attributes()["_FillValue"] lon = np.ma.masked_equal(lon.get(), fill_value) if resolution == coarse_resolution: self.areas[resolution] = lon, lat return lon, lat from geotiepoints import modis5kmto1km, modis1kmto500m, modis1kmto250m logger.debug("Interpolating from " + str(coarse_resolution) + " to " + str(resolution)) if coarse_resolution == 5000: lon, lat = modis5kmto1km(lon, lat) if resolution == 500: lon, lat = modis1kmto500m(lon, lat, cores) if resolution == 250: lon, lat = modis1kmto250m(lon, lat, cores) self.areas[resolution, time_slot] = lon, lat return lon, lat