def get_lonlat(satscene, row, col): """Estimate lon and lat. """ conf = ConfigParser() conf.read(os.path.join(CONFIG_PATH, satscene.fullname + ".cfg")) path = conf.get("modis-level2", "dir") filename = satscene.time_slot.strftime("thin_MYD03.A%Y%j.%H%M.005.NRT.hdf") filename = os.path.join(path, filename) if(os.path.exists(filename) and (satscene.lon is None or satscene.lat is None)): data = SD(filename) lat = data.select("Latitude") fill_value = lat.attributes()["_FillValue"] satscene.lat = np.ma.masked_equal(lat.get(), fill_value) lon = data.select("Longitude") fill_value = lon.attributes()["_FillValue"] satscene.lon = np.ma.masked_equal(lon.get(), fill_value) estimate = True try: lon = satscene.lon[row, col] lat = satscene.lat[row, col] if satscene.lon.mask[row, col] == False and satscene.lat.mask[row, col] == False: estimate = False except TypeError: pass except IndexError: pass if not estimate: return lon, lat from mpop.saturn.two_line_elements import Tle tle = Tle(satellite=satscene.satname) track_start = tle.get_latlonalt(satscene.time_slot) track_end = tle.get_latlonalt(satscene.time_slot + satscene.granularity) # WGS84 # flattening f = 1/298.257223563 # semi_major_axis a = 6378137.0 s, alpha12, alpha21 = vinc_dist(f, a, track_start[0], track_start[1], track_end[0], track_end[1]) scanlines = satscene.granularity.seconds / satscene.span if row < scanlines/2: if row == 0: track_now = track_start else: track_now = vinc_pt(f, a, track_start[0], track_start[1], alpha12, (s * row) / scanlines) lat_now = track_now[0] lon_now = track_now[1] s, alpha12, alpha21 = vinc_dist(f, a, lat_now, lon_now, track_end[0], track_end[1]) fac = 1 else: if scanlines - row - 1 == 0: track_now = track_end else: track_now = vinc_pt(f, a, track_end[0], track_end[1], alpha21, (s * (scanlines - row - 1)) / scanlines) lat_now = track_now[0] lon_now = track_now[1] s, alpha12, alpha21 = vinc_dist(f, a, lat_now, lon_now, track_start[0], track_start[1]) fac = -1 if col < 1354/2: lat, lon, alp = vinc_pt(f, a, lat_now, lon_now, alpha12 + np.pi/2 * fac, 2340000.0 / 2 - (2340000.0/1354) * col) else: lat, lon, alp = vinc_pt(f, a, lat_now, lon_now, alpha12 - np.pi/2 * fac, (2340000.0/1354) * col - 2340000.0 / 2) lon = np.rad2deg(lon) lat = np.rad2deg(lat) if lon > 180: lon -= 360 if lon <= -180: lon += 360 return lon, lat
def get_lonlat(satscene, row, col): """Estimate lon and lat. """ import glob conf = ConfigParser() conf.read(os.path.join(CONFIG_PATH, satscene.fullname + ".cfg")) path = conf.get("modis-level2", "dir") geofile_tmpl = conf.get("modis-level2", "geofile") filename_tmpl = satscene.time_slot.strftime(geofile_tmpl) file_list = glob.glob(os.path.join(path, filename_tmpl)) if len(file_list) > 1: raise IOError("More than 1 geolocation file matching!" + filename_tmpl) elif len(file_list) == 0: logger.info("No MODIS geolocation file matching: " + filename_tmpl + ", estimating") filename = "" else: filename = file_list[0] logger.debug("Geolocation file = " + filename) if(os.path.exists(filename) and (satscene.lon is None or satscene.lat is None)): data = SD(filename) lat = data.select("Latitude") fill_value = lat.attributes()["_FillValue"] satscene.lat = np.ma.masked_equal(lat.get(), fill_value) lon = data.select("Longitude") fill_value = lon.attributes()["_FillValue"] satscene.lon = np.ma.masked_equal(lon.get(), fill_value) estimate = True try: lon = satscene.lon[row, col] lat = satscene.lat[row, col] if(satscene.lon.mask[row, col] == False and satscene.lat.mask[row, col] == False): estimate = False except TypeError: pass except IndexError: pass if not estimate: return lon, lat from mpop.saturn.two_line_elements import Tle tle = Tle(satellite=satscene.satname) track_start = tle.get_latlonalt(satscene.time_slot) track_end = tle.get_latlonalt(satscene.time_slot + satscene.granularity) # WGS84 # flattening f__ = 1 / 298.257223563 # semi_major_axis a__ = 6378137.0 s__, alpha12, alpha21 = vinc_dist(f__, a__, track_start[0], track_start[1], track_end[0], track_end[1]) scanlines = satscene.granularity.seconds / satscene.span if row < scanlines / 2: if row == 0: track_now = track_start else: track_now = vinc_pt(f__, a__, track_start[0], track_start[1], alpha12, (s__ * row) / scanlines) lat_now = track_now[0] lon_now = track_now[1] s__, alpha12, alpha21 = vinc_dist(f__, a__, lat_now, lon_now, track_end[0], track_end[1]) fac = 1 else: if scanlines - row - 1 == 0: track_now = track_end else: track_now = vinc_pt(f__, a__, track_end[0], track_end[1], alpha21, (s__ * (scanlines - row - 1)) / scanlines) lat_now = track_now[0] lon_now = track_now[1] s__, alpha12, alpha21 = vinc_dist(f__, a__, lat_now, lon_now, track_start[0], track_start[1]) fac = -1 if col < 1354 / 2: lat, lon, alp = vinc_pt(f__, a__, lat_now, lon_now, alpha12 + np.pi / 2 * fac, 2340000.0 / 2 - (2340000.0 / 1354) * col) else: lat, lon, alp = vinc_pt(f__, a__, lat_now, lon_now, alpha12 - np.pi / 2 * fac, (2340000.0 / 1354) * col - 2340000.0 / 2) del alp lon = np.rad2deg(lon) lat = np.rad2deg(lat) if lon > 180: lon -= 360 if lon <= -180: lon += 360 return lon, lat