问题描述
我有一个 geotiff,它的值从 0 到 200,nodata 值为 255。 我在 python 中使用 gdal 将其转换为 netcdf:
ds = gdal.Translate(outputfile,inputfile,format='NetCDF')
[按照 Val 的建议,我尝试添加 outputType=gdal.gdalconst.GDT_Byte,noData=255
但它也没有解决]
现在 netcdf 中的所有 0 和 nodata 值都是 0。但主要问题是其他值现在范围从 127 到 -128。 tif 中的最高值现在最低(即负值),最低值现在是最正值。
这是来自数据层上的 netcdf 的信息
<class 'netCDF4._netCDF4.Variable'>
int8 Band1(y,x)
long_name: GDAL Band Number 1
_Unsigned: true
valid_range: [ 0 255]
_FillValue: 0
grid_mapping: polar_stereographic
unlimited dimensions:
current shape = (752,752)
filling on
这是 netcdf 的 gdal.Info:
Driver: netCDF/Network Common Data Format
Files: /Users/clip_asi-AMSR2-s3125-20200305-v5.4.nc
Size is 117,163
Coordinate System is:
PROJCRS["IDL GeoTIFF Suport
Projection = Polar Stereographic
True scale: -70.0000deg
Gunnar Spreen,Apr 2004",BASEGEOGCRS["WGS 84",DATUM["World Geodetic System 1984",ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]]],PRIMEM["Greenwich",ANGLEUNIT["degree",0.0174532925199433]],ID["epsg",4326]],CONVERSION["unnamed",METHOD["Polar Stereographic (variant B)",9829]],ParaMETER["Latitude of standard parallel",-70,0.0174532925199433],8832]],ParaMETER["Longitude of origin",8833]],ParaMETER["False easting",1],8806]],ParaMETER["False northing",8807]]],CS[Cartesian,2],AXIS["easting",north,ORDER[1],1]],AXIS["northing",ORDER[2],1]]]
Data axis to CRS axis mapping: 1,2
Origin = (-1810937.500000000000000,-554687.500000000000000)
Pixel Size = (3125.000000000000000,-3125.000000000000000)
Metadata:
Band1#grid_mapping=polar_stereographic
Band1#long_name=GDAL Band Number 1
Band1#valid_range={0,255}
Band1#_FillValue=-1
Band1#_Unsigned=true
NC_GLOBAL#Conventions=CF-1.5
NC_GLOBAL#GDAL=GDAL 3.0.2,released 2019/10/28
NC_GLOBAL#GDAL_AREA_OR_POINT=Area
NC_GLOBAL#GDAL_TIFFTAG_DATETIME=2020:03:06 05:10:59
NC_GLOBAL#GDAL_TIFFTAG_DOCUMENTNAME=/ssmi/www/htdocs/data/amsr2/asi_daygrid_swath/s3125/2020/mar/Amundsen/asi-AMSR2-s3125-20200305-v5.4.tif
NC_GLOBAL#GDAL_TIFFTAG_IMAGEDESCRIPTION=IDL TIFF file
NC_GLOBAL#GDAL_TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
NC_GLOBAL#GDAL_TIFFTAG_SOFTWARE=IDL 8.1,ITT Visual information Solutions
NC_GLOBAL#GDAL_TIFFTAG_XRESOLUTION=100
NC_GLOBAL#GDAL_TIFFTAG_YRESOLUTION=100
NC_GLOBAL#history=Wed Mar 31 18:36:11 2021: GDAL Createcopy( /Users/gjmacdonald/Downloads/clip_asi-AMSR2-s3125-20200305-v5.4.nc,... )
polar_stereographic#false_easting=0
polar_stereographic#false_northing=0
polar_stereographic#GeoTransform=-1810937.5 3125 0 -554687.5 0 -3125
polar_stereographic#grid_mapping_name=polar_stereographic
polar_stereographic#inverse_flattening=298.257223563
polar_stereographic#latitude_of_projection_origin=-90
polar_stereographic#longitude_of_prime_meridian=0
polar_stereographic#long_name=CRS deFinition
polar_stereographic#semi_major_axis=6378137
polar_stereographic#spatial_ref=PROJCS["IDL GeoTIFF Suport
Projection = Polar Stereographic
True scale: -70.0000deg
Gunnar Spreen,GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",AUTHORITY["epsg","7030"]],"6326"]],0],UNIT["degree",0.0174532925199433,"9122"]],"4326"]],PROJECTION["Polar_Stereographic"],ParaMETER["latitude_of_origin",-70],ParaMETER["central_meridian",ParaMETER["false_easting",ParaMETER["false_northing",UNIT["metre",AXIS["Easting",norTH],AXIS["northing",norTH]]
polar_stereographic#standard_parallel=-70
polar_stereographic#straight_vertical_longitude_from_pole=0
x#long_name=x coordinate of projection
x#standard_name=projection_x_coordinate
x#units=m
y#long_name=y coordinate of projection
y#standard_name=projection_y_coordinate
y#units=m
Corner Coordinates:
Upper Left (-1810937.500,-554687.500) (107d 1'47.10"W,72d38'40.82"S)
Lower Left (-1810937.500,-1064062.500) (120d26'14.82"W,70d47' 7.34"S)
Upper Right (-1445312.500,-554687.500) (110d59'45.57"W,75d46'46.47"S)
Lower Right (-1445312.500,-1064062.500) (126d21'39.97"W,73d32'29.83"S)
Center (-1628125.000,-809375.000) (116d25'58.56"W,73d19'46.09"S)
Band 1 Block=117x1 Type=Byte,ColorInterp=Undefined
NoData Value=255
Metadata:
grid_mapping=polar_stereographic
long_name=GDAL Band Number 1
NETCDF_VARNAME=Band1
valid_range={0,255}
_FillValue=-1
_Unsigned=true
以及转换后的 Tif 的 gdal.Info(在答案中添加 Val 的建议之后)
Driver: GTiff/GeoTIFF
Files: /Users/clip_asi-AMSR2-s3125-20200305-v5.4.tif
Size is 117,-3125.000000000000000)
Metadata:
AREA_OR_POINT=Area
TIFFTAG_DATETIME=2020:03:06 05:10:59
TIFFTAG_DOCUMENTNAME=/ssmi/www/htdocs/data/amsr2/asi_daygrid_swath/s3125/2020/mar/Amundsen/asi-AMSR2-s3125-20200305-v5.4.tif
TIFFTAG_IMAGEDESCRIPTION=IDL TIFF file
TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
TIFFTAG_SOFTWARE=IDL 8.1,ITT Visual information Solutions
TIFFTAG_XRESOLUTION=100
TIFFTAG_YRESOLUTION=100
Image Structure Metadata:
INTERLEAVE=BAND
Corner Coordinates:
Upper Left (-1810937.500,73d19'46.09"S)
Band 1 Block=117x70 Type=Byte,ColorInterp=Palette
NoData Value=255
Color Table (RGB with 256 entries)
0: 0,138,255
1: 0,255
2: 0,255
3: 0,255
4: 0,255
5: 0,255
6: 0,255
7: 0,255
8: 0,255
9: 0,255
10: 0,255
11: 0,255
12: 0,255
13: 0,255
14: 0,255
15: 0,255
16: 0,255
17: 0,255
18: 0,255
19: 0,255
20: 29,143,254,255
21: 29,255
22: 29,255
23: 29,255
24: 29,255
25: 29,255
26: 29,255
27: 29,255
28: 29,255
29: 29,255
30: 29,255
31: 29,255
32: 29,255
33: 29,255
34: 29,255
35: 29,255
36: 29,255
37: 29,255
38: 29,255
39: 29,255
40: 29,249,159,255
41: 29,255
42: 29,255
43: 29,255
44: 29,255
45: 29,255
46: 29,255
47: 29,255
48: 29,255
49: 29,255
50: 29,255
51: 29,255
52: 29,255
53: 29,255
54: 29,255
55: 29,255
56: 29,255
57: 29,255
58: 29,255
59: 29,255
60: 33,33,255
61: 33,255
62: 33,255
63: 33,255
64: 33,255
65: 33,255
66: 33,255
67: 33,255
68: 33,255
69: 33,255
70: 33,255
71: 33,255
72: 33,255
73: 33,255
74: 33,255
75: 33,255
76: 33,255
77: 33,255
78: 33,255
79: 33,255
80: 0,255
81: 0,255
82: 0,255
83: 0,255
84: 0,255
85: 0,255
86: 0,255
87: 0,255
88: 0,255
89: 0,255
90: 0,255
91: 0,255
92: 0,255
93: 0,255
94: 0,255
95: 0,255
96: 0,255
97: 0,255
98: 0,255
99: 0,255
100: 124,255
101: 124,255
102: 124,255
103: 124,255
104: 124,255
105: 124,255
106: 124,255
107: 124,255
108: 124,255
109: 124,255
110: 124,255
111: 124,255
112: 124,255
113: 124,255
114: 124,255
115: 124,255
116: 124,255
117: 124,255
118: 124,255
119: 124,255
120: 172,46,255
121: 172,255
122: 172,255
123: 172,255
124: 172,255
125: 172,255
126: 172,255
127: 172,255
128: 172,255
129: 172,255
130: 172,255
131: 172,255
132: 172,255
133: 172,255
134: 172,255
135: 172,255
136: 172,255
137: 172,255
138: 172,255
139: 172,255
140: 249,255
141: 249,255
142: 249,255
143: 249,255
144: 249,255
145: 249,255
146: 249,255
147: 249,255
148: 249,255
149: 249,255
150: 249,255
151: 249,255
152: 249,255
153: 249,255
154: 249,255
155: 249,255
156: 249,255
157: 249,255
158: 249,255
159: 249,255
160: 249,124,255
161: 249,255
162: 249,255
163: 249,255
164: 249,255
165: 249,255
166: 249,255
167: 249,255
168: 249,255
169: 249,255
170: 249,255
171: 249,255
172: 249,255
173: 249,255
174: 249,255
175: 249,255
176: 249,255
177: 249,255
178: 249,255
179: 249,255
180: 185,84,210,255
181: 185,255
182: 185,255
183: 185,255
184: 185,255
185: 185,255
186: 185,255
187: 185,255
188: 185,255
189: 185,255
190: 147,255
191: 147,255
192: 147,255
193: 147,255
194: 147,255
195: 147,255
196: 147,255
197: 147,255
198: 147,255
199: 119,89,255
200: 119,255
201: 0,255
202: 0,255
203: 0,255
204: 0,255
205: 0,255
206: 0,255
207: 0,255
208: 0,255
209: 0,255
210: 0,255
211: 0,255
212: 0,255
213: 0,255
214: 0,255
215: 0,255
216: 0,255
217: 0,255
218: 0,255
219: 0,255
220: 0,255
221: 0,255
222: 0,255
223: 0,255
224: 0,255
225: 0,255
226: 0,255
227: 0,255
228: 0,255
229: 0,255
230: 0,255
231: 0,255
232: 0,255
233: 0,255
234: 0,255
235: 0,255
236: 0,255
237: 0,255
238: 0,255
239: 0,255
240: 0,255
241: 0,255
242: 0,255
243: 0,255
244: 0,255
245: 0,255
246: 0,255
247: 0,255
248: 0,255
249: 0,255
250: 0,255
251: 99,99,255
252: 0,255
253: 0,255
254: 0,255
255: 0,0
Tif 文件: https://drive.google.com/file/d/1y5UGAteXUOzGaqWsuFc7DXi8XTzGMVJ_/view?usp=sharing 转换后的 nc 文件:https://drive.google.com/file/d/1CjZJ9pRcLJomHMkTjatkXHTKx4zQ6ehl/view?usp=sharing
解决方法
看起来您正在将输入数据转换为最有可能的 uint8
到 int8
,因此高于数据类型最大“溢出”的值进入负侧。您需要专门将数据类型设置为 Byte
(在 uint8
行话中是 gdal
)。
您可以通过添加 outputType
选项关键字参数来解决此问题。此外,您可以使用 noData
选项设置所需的无数据值。
这是具有正确数据类型和无数据值为 255 的完整调用:
gdal.Translate(outputfile,inputfile,format='NetCDF',outputType=gdal.gdalconst.GDT_Byte,noData=255)