| 1 |
"""Interpolation of geographical tiepoints.""" |
| 2 |
|
| 3 |
from multiprocessing import Pool |
| 4 |
|
| 5 |
import numpy as np |
| 6 |
from numpy import arccos, arcsin, rad2deg, sign, sqrt |
| 7 |
from scipy.interpolate import RectBivariateSpline, splev, splrep |
| 8 |
|
| 9 |
from geotiepoints.geointerpolator import \ |
| 10 |
GeoInterpolator as SatelliteInterpolator |
| 11 |
|
| 12 |
EARTH_RADIUS = 6370997.0 |
| 13 |
|
| 14 |
|
| 15 |
def get_scene_splits(nlines_swath, nlines_scan, n_cpus): |
| 16 |
"""Calculate the line numbers where the swath will be split in smaller |
| 17 |
granules for parallel processing""" |
| 18 |
|
| 19 |
nscans = nlines_swath // nlines_scan |
| 20 |
if nscans < n_cpus: |
| 21 |
nscans_subscene = 1 |
| 22 |
else: |
| 23 |
nscans_subscene = nscans // n_cpus |
| 24 |
nlines_subscene = nscans_subscene * nlines_scan |
| 25 |
|
| 26 |
return range(nlines_subscene, nlines_swath, nlines_subscene) |
| 27 |
|
| 28 |
|
| 29 |
def metop20kmto1km(lons20km, lats20km): |
| 30 |
"""Getting 1km geolocation for metop avhrr from 20km tiepoints. |
| 31 |
""" |
| 32 |
cols20km = np.array([0] + list(range(4, 2048, 20)) + [2047]) |
| 33 |
cols1km = np.arange(2048) |
| 34 |
lines = lons20km.shape[0] |
| 35 |
rows20km = np.arange(lines) |
| 36 |
rows1km = np.arange(lines) |
| 37 |
|
| 38 |
along_track_order = 1 |
| 39 |
cross_track_order = 3 |
| 40 |
|
| 41 |
satint = SatelliteInterpolator((lons20km, lats20km), |
| 42 |
(rows20km, cols20km), |
| 43 |
(rows1km, cols1km), |
| 44 |
along_track_order, |
| 45 |
cross_track_order) |
| 46 |
return satint.interpolate() |
| 47 |
|
| 48 |
|
| 49 |
def modis5kmto1km(lons5km, lats5km): |
| 50 |
"""Getting 1km geolocation for modis from 5km tiepoints. |
| 51 |
|
| 52 |
http://www.icare.univ-lille1.fr/tutorials/MODIS_geolocation |
| 53 |
""" |
| 54 |
cols5km = np.arange(2, 1354, 5) / 5.0 |
| 55 |
cols1km = np.arange(1354) / 5.0 |
| 56 |
lines = lons5km.shape[0] * 5 |
| 57 |
rows5km = np.arange(2, lines, 5) / 5.0 |
| 58 |
rows1km = np.arange(lines) / 5.0 |
| 59 |
|
| 60 |
along_track_order = 1 |
| 61 |
cross_track_order = 3 |
| 62 |
|
| 63 |
satint = SatelliteInterpolator((lons5km, lats5km), |
| 64 |
(rows5km, cols5km), |
| 65 |
(rows1km, cols1km), |
| 66 |
along_track_order, |
| 67 |
cross_track_order, |
| 68 |
chunk_size=10) |
| 69 |
satint.fill_borders("y", "x") |
| 70 |
lons1km, lats1km = satint.interpolate() |
| 71 |
return lons1km, lats1km |
| 72 |
|
| 73 |
|
| 74 |
def _multi(fun, lons, lats, chunk_size, cores=1): |
| 75 |
"""Work on multiple cores. |
| 76 |
""" |
| 77 |
pool = Pool(processes=cores) |
| 78 |
|
| 79 |
splits = get_scene_splits(lons.shape[0], chunk_size, cores) |
| 80 |
|
| 81 |
lons_parts = np.vsplit(lons, splits) |
| 82 |
lats_parts = np.vsplit(lats, splits) |
| 83 |
|
| 84 |
results = [pool.apply_async(fun, |
| 85 |
(lons_parts[i], |
| 86 |
lats_parts[i])) |
| 87 |
for i in range(len(lons_parts))] |
| 88 |
|
| 89 |
pool.close() |
| 90 |
pool.join() |
| 91 |
|
| 92 |
lons, lats = zip(*(res.get() for res in results)) |
| 93 |
|
| 94 |
return np.vstack(lons), np.vstack(lats) |
| 95 |
|
| 96 |
|
| 97 |
def modis1kmto500m(lons1km, lats1km, cores=1): |
| 98 |
"""Getting 500m geolocation for modis from 1km tiepoints. |
| 99 |
|
| 100 |
http://www.icare.univ-lille1.fr/tutorials/MODIS_geolocation |
| 101 |
""" |
| 102 |
if cores > 1: |
| 103 |
return _multi(modis1kmto500m, lons1km, lats1km, 10, cores) |
| 104 |
|
| 105 |
cols1km = np.arange(1354) |
| 106 |
cols500m = np.arange(1354 * 2) / 2.0 |
| 107 |
lines = lons1km.shape[0] |
| 108 |
rows1km = np.arange(lines) |
| 109 |
rows500m = (np.arange(lines * 2) - 0.5) / 2. |
| 110 |
|
| 111 |
along_track_order = 1 |
| 112 |
cross_track_order = 3 |
| 113 |
|
| 114 |
satint = SatelliteInterpolator((lons1km, lats1km), |
| 115 |
(rows1km, cols1km), |
| 116 |
(rows500m, cols500m), |
| 117 |
along_track_order, |
| 118 |
cross_track_order, |
| 119 |
chunk_size=20) |
| 120 |
satint.fill_borders("y", "x") |
| 121 |
lons500m, lats500m = satint.interpolate() |
| 122 |
return lons500m, lats500m |
| 123 |
|
| 124 |
|
| 125 |
def modis1kmto250m(lons1km, lats1km, cores=1): |
| 126 |
"""Getting 250m geolocation for modis from 1km tiepoints. |
| 127 |
|
| 128 |
http://www.icare.univ-lille1.fr/tutorials/MODIS_geolocation |
| 129 |
""" |
| 130 |
if cores > 1: |
| 131 |
return _multi(modis1kmto250m, lons1km, lats1km, 10, cores) |
| 132 |
|
| 133 |
cols1km = np.arange(1354) |
| 134 |
cols250m = np.arange(1354 * 4) / 4.0 |
| 135 |
|
| 136 |
along_track_order = 1 |
| 137 |
cross_track_order = 3 |
| 138 |
|
| 139 |
lines = lons1km.shape[0] |
| 140 |
rows1km = np.arange(lines) |
| 141 |
rows250m = (np.arange(lines * 4) - 1.5) / 4.0 |
| 142 |
|
| 143 |
satint = SatelliteInterpolator((lons1km, lats1km), |
| 144 |
(rows1km, cols1km), |
| 145 |
(rows250m, cols250m), |
| 146 |
along_track_order, |
| 147 |
cross_track_order, |
| 148 |
chunk_size=40) |
| 149 |
satint.fill_borders("y", "x") |
| 150 |
lons250m, lats250m = satint.interpolate() |
| 151 |
|
| 152 |
return lons250m, lats250m |
| 153 |
|
| 154 |
|
| 155 |
from . import version |
| 156 |
__version__ = version.get_versions()['version'] |
| 157 |
|