-
Notifications
You must be signed in to change notification settings - Fork 0
/
remap.py
184 lines (130 loc) · 5.89 KB
/
remap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from netCDF4 import Dataset
import numpy as np
from osgeo import osr
from osgeo import gdal
import time as t
# Define KM_PER_DEGREE
KM_PER_DEGREE = 111.32
# GOES-16 Spatial Reference System # updated to lon = -75.0 to match movement of system
sourcePrj = osr.SpatialReference()
# sourcePrj.ImportFromProj4('+proj=geos +h=35786023.0 +a=6378137.0 +b=6356752.31414 +f=0.00335281068119356027489803406172 +lat_0=0.0 +lon_0=-89.5 +sweep=x +no_defs')
sourcePrj.ImportFromProj4('+proj=geos +h=35786023.0 +a=6378137.0 +b=6356752.31414 +f=0.00335281068119356027489803406172 +lat_0=0.0 +lon_0=-75.0 +sweep=x +no_defs')
# Lat/lon WSG84 Spatial Reference System
targetPrj = osr.SpatialReference()
targetPrj.ImportFromProj4('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
# targetPrj.ImportFromProj4('+proj=lcc +lon_0=-90')
def exportImage(image,path):
driver = gdal.GetDriverByName('netCDF')
return driver.CreateCopy(path,image,0)
def getGeoT(extent, nlines, ncols):
# Compute resolution based on data dimension
resx = (extent[2] - extent[0]) / ncols
resy = (extent[3] - extent[1]) / nlines
return [extent[0], resx, 0, extent[3] , 0, -resy]
def getScaleOffset(path):
nc = Dataset(path, mode='r')
scale = nc.variables['CMI'].scale_factor
offset = nc.variables['CMI'].add_offset
nc.close()
return scale, offset
def getScaleOffsetRad(path):
nc = Dataset(path, mode='r')
scale = nc.variables['Rad'].scale_factor
offset = nc.variables['Rad'].add_offset
nc.close()
return scale, offset
def remap(path, extent, resolution, x1, y1, x2, y2):
# GOES-16 Extent (satellite projection) [llx, lly, urx, ury]
GOES16_EXTENT = [x1, y1, x2, y2]
# Setup NetCDF driver
gdal.SetConfigOption('GDAL_NETCDF_BOTTOMUP', 'NO')
# Read scale/offset from file
scale, offset = getScaleOffset(path)
try:
connectionInfo = 'NETCDF:\"' + path + '\":CMI'
# Open NetCDF file (GOES-16 data)
raw = gdal.Open(connectionInfo)
except:
connectionInfo = 'HDF5:\"' + path + '\"://CMI'
# Open NetCDF file (GOES-16 data)
raw = gdal.Open(connectionInfo)
# Setup projection and geo-transformation
raw.SetProjection(sourcePrj.ExportToWkt())
#raw.SetGeoTransform(getGeoT(GOES16_EXTENT, raw.RasterYSize, raw.RasterXSize))
raw.SetGeoTransform(getGeoT(GOES16_EXTENT, raw.RasterYSize, raw.RasterXSize))
#print (KM_PER_DEGREE)
# Compute grid dimension
sizex = int(((extent[2] - extent[0]) * KM_PER_DEGREE) / resolution)
sizey = int(((extent[3] - extent[1]) * KM_PER_DEGREE) / resolution)
# Get memory driver
memDriver = gdal.GetDriverByName('MEM')
# Create grid
grid = memDriver.Create('grid', sizex, sizey, 1, gdal.GDT_Float32)
# Setup projection and geo-transformation
grid.SetProjection(targetPrj.ExportToWkt())
grid.SetGeoTransform(getGeoT(extent, grid.RasterYSize, grid.RasterXSize))
# Perform the projection/resampling
print ('Remapping', path)
start = t.time()
gdal.ReprojectImage(raw, grid, sourcePrj.ExportToWkt(), targetPrj.ExportToWkt(), gdal.GRA_NearestNeighbour, options=['NUM_THREADS=ALL_CPUS'])
print ('- finished! Time:', t.time() - start, 'seconds')
# Close file
raw = None
# Read grid data
array = grid.ReadAsArray()
# Mask fill values (i.e. invalid values)
np.ma.masked_where(array, array == -1, False)
# Apply scale and offset
array = array * scale + offset
grid.GetRasterBand(1).SetNoDataValue(-1)
grid.GetRasterBand(1).WriteArray(array)
return grid
def remap2(path, extent, resolution, x1, y1, x2, y2, targetPrj=''):
# GOES-16 Extent (satellite projection) [llx, lly, urx, ury]
GOES16_EXTENT = [x1, y1, x2, y2]
# Setup NetCDF driver
gdal.SetConfigOption('GDAL_NETCDF_BOTTOMUP', 'NO')
# Read scale/offset from file
scale, offset = getScaleOffsetRad(path)
try:
raw = gdal.Open('NETCDF:"'+path+'":Rad')
except:
connectionInfo = 'HDF5:\"' + path + r'\"://Rad'
# Open NetCDF file (GOES-16 data)
raw = gdal.Open(connectionInfo)
# store the numpy array
raw2 = raw.ReadAsArray()
# Setup projection and geo-transformation
raw.SetProjection(sourcePrj.ExportToWkt())
#raw.SetGeoTransform(getGeoT(GOES16_EXTENT, raw.RasterYSize, raw.RasterXSize))
raw.SetGeoTransform(getGeoT(GOES16_EXTENT, raw.RasterYSize, raw.RasterXSize))
#print (KM_PER_DEGREE)
# Compute grid dimension
sizex = int(((extent[2] - extent[0]) * KM_PER_DEGREE) / resolution)
sizey = int(((extent[3] - extent[1]) * KM_PER_DEGREE) / resolution)
# Get memory driver
memDriver = gdal.GetDriverByName('MEM')
# Create grid
grid = memDriver.Create('grid', sizex, sizey, 1, gdal.GDT_Float32)
# Setup projection and geo-transformation
grid.SetProjection(targetPrj.ExportToWkt())
grid.SetGeoTransform(getGeoT(extent, grid.RasterYSize, grid.RasterXSize))
# Perform the projection/resampling
#print ('Remapping', path)
start = t.time()
# print(sourcePrj)
gdal.ReprojectImage(raw, grid, sourcePrj.ExportToWkt(), targetPrj.ExportToWkt(), gdal.GRA_NearestNeighbour, options=['NUM_THREADS=ALL_CPUS'])
#print ('- finished! Time:', t.time() - start, 'seconds')
# Close file
raw = None
# Read grid data
array = grid.ReadAsArray()
# Mask fill values (i.e. invalid values)
np.ma.masked_where(array, array == -1, False)
# Apply scale and offset
array = array * scale + offset
grid.GetRasterBand(1).SetNoDataValue(-1)
grid.GetRasterBand(1).WriteArray(array)
return raw2, grid