【问题标题】:Values change when convert geotiff to netcdf将 geotiff 转换为 netcdf 时值发生变化
【发布时间】:2021-06-25 23:53:28
【问题描述】:

我有一个 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",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4326]],
    CONVERSION["unnamed",
        METHOD["Polar Stereographic (variant B)",
            ID["EPSG",9829]],
        PARAMETER["Latitude of standard parallel",-70,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8832]],
        PARAMETER["Longitude of origin",0,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8833]],
        PARAMETER["False easting",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["easting",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["northing",north,
            ORDER[2],
            LENGTHUNIT["metre",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, Apr 2004",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Polar_Stereographic"],PARAMETER["latitude_of_origin",-70],PARAMETER["central_meridian",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1],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, 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",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4326]],
    CONVERSION["unnamed",
        METHOD["Polar Stereographic (variant B)",
            ID["EPSG",9829]],
        PARAMETER["Latitude of standard parallel",-70,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8832]],
        PARAMETER["Longitude of origin",0,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8833]],
        PARAMETER["False easting",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["easting",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["northing",north,
            ORDER[2],
            LENGTHUNIT["metre",1]]]
Data axis to CRS axis mapping: 1,2
Origin = (-1810937.500000000000000,-554687.500000000000000)
Pixel Size = (3125.000000000000000,-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, -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=117x70 Type=Byte, ColorInterp=Palette
  NoData Value=255
  Color Table (RGB with 256 entries)
    0: 0,0,138,255
    1: 0,0,138,255
    2: 0,0,138,255
    3: 0,0,138,255
    4: 0,0,138,255
    5: 0,0,138,255
    6: 0,0,138,255
    7: 0,0,138,255
    8: 0,0,138,255
    9: 0,0,138,255
   10: 0,0,138,255
   11: 0,0,138,255
   12: 0,0,138,255
   13: 0,0,138,255
   14: 0,0,138,255
   15: 0,0,138,255
   16: 0,0,138,255
   17: 0,0,138,255
   18: 0,0,138,255
   19: 0,0,138,255
   20: 29,143,254,255
   21: 29,143,254,255
   22: 29,143,254,255
   23: 29,143,254,255
   24: 29,143,254,255
   25: 29,143,254,255
   26: 29,143,254,255
   27: 29,143,254,255
   28: 29,143,254,255
   29: 29,143,254,255
   30: 29,143,254,255
   31: 29,143,254,255
   32: 29,143,254,255
   33: 29,143,254,255
   34: 29,143,254,255
   35: 29,143,254,255
   36: 29,143,254,255
   37: 29,143,254,255
   38: 29,143,254,255
   39: 29,143,254,255
   40: 29,249,159,255
   41: 29,249,159,255
   42: 29,249,159,255
   43: 29,249,159,255
   44: 29,249,159,255
   45: 29,249,159,255
   46: 29,249,159,255
   47: 29,249,159,255
   48: 29,249,159,255
   49: 29,249,159,255
   50: 29,249,159,255
   51: 29,249,159,255
   52: 29,249,159,255
   53: 29,249,159,255
   54: 29,249,159,255
   55: 29,249,159,255
   56: 29,249,159,255
   57: 29,249,159,255
   58: 29,249,159,255
   59: 29,249,159,255
   60: 33,138,33,255
   61: 33,138,33,255
   62: 33,138,33,255
   63: 33,138,33,255
   64: 33,138,33,255
   65: 33,138,33,255
   66: 33,138,33,255
   67: 33,138,33,255
   68: 33,138,33,255
   69: 33,138,33,255
   70: 33,138,33,255
   71: 33,138,33,255
   72: 33,138,33,255
   73: 33,138,33,255
   74: 33,138,33,255
   75: 33,138,33,255
   76: 33,138,33,255
   77: 33,138,33,255
   78: 33,138,33,255
   79: 33,138,33,255
   80: 0,249,0,255
   81: 0,249,0,255
   82: 0,249,0,255
   83: 0,249,0,255
   84: 0,249,0,255
   85: 0,249,0,255
   86: 0,249,0,255
   87: 0,249,0,255
   88: 0,249,0,255
   89: 0,249,0,255
   90: 0,249,0,255
   91: 0,249,0,255
   92: 0,249,0,255
   93: 0,249,0,255
   94: 0,249,0,255
   95: 0,249,0,255
   96: 0,249,0,255
   97: 0,249,0,255
   98: 0,249,0,255
   99: 0,249,0,255
  100: 124,249,0,255
  101: 124,249,0,255
  102: 124,249,0,255
  103: 124,249,0,255
  104: 124,249,0,255
  105: 124,249,0,255
  106: 124,249,0,255
  107: 124,249,0,255
  108: 124,249,0,255
  109: 124,249,0,255
  110: 124,249,0,255
  111: 124,249,0,255
  112: 124,249,0,255
  113: 124,249,0,255
  114: 124,249,0,255
  115: 124,249,0,255
  116: 124,249,0,255
  117: 124,249,0,255
  118: 124,249,0,255
  119: 124,249,0,255
  120: 172,254,46,255
  121: 172,254,46,255
  122: 172,254,46,255
  123: 172,254,46,255
  124: 172,254,46,255
  125: 172,254,46,255
  126: 172,254,46,255
  127: 172,254,46,255
  128: 172,254,46,255
  129: 172,254,46,255
  130: 172,254,46,255
  131: 172,254,46,255
  132: 172,254,46,255
  133: 172,254,46,255
  134: 172,254,46,255
  135: 172,254,46,255
  136: 172,254,46,255
  137: 172,254,46,255
  138: 172,254,46,255
  139: 172,254,46,255
  140: 249,249,0,255
  141: 249,249,0,255
  142: 249,249,0,255
  143: 249,249,0,255
  144: 249,249,0,255
  145: 249,249,0,255
  146: 249,249,0,255
  147: 249,249,0,255
  148: 249,249,0,255
  149: 249,249,0,255
  150: 249,249,0,255
  151: 249,249,0,255
  152: 249,249,0,255
  153: 249,249,0,255
  154: 249,249,0,255
  155: 249,249,0,255
  156: 249,249,0,255
  157: 249,249,0,255
  158: 249,249,0,255
  159: 249,249,0,255
  160: 249,124,0,255
  161: 249,124,0,255
  162: 249,124,0,255
  163: 249,124,0,255
  164: 249,124,0,255
  165: 249,124,0,255
  166: 249,124,0,255
  167: 249,124,0,255
  168: 249,124,0,255
  169: 249,124,0,255
  170: 249,0,0,255
  171: 249,0,0,255
  172: 249,0,0,255
  173: 249,0,0,255
  174: 249,0,0,255
  175: 249,0,0,255
  176: 249,0,0,255
  177: 249,0,0,255
  178: 249,0,0,255
  179: 249,0,0,255
  180: 185,84,210,255
  181: 185,84,210,255
  182: 185,84,210,255
  183: 185,84,210,255
  184: 185,84,210,255
  185: 185,84,210,255
  186: 185,84,210,255
  187: 185,84,210,255
  188: 185,84,210,255
  189: 185,84,210,255
  190: 147,0,210,255
  191: 147,0,210,255
  192: 147,0,210,255
  193: 147,0,210,255
  194: 147,0,210,255
  195: 147,0,210,255
  196: 147,0,210,255
  197: 147,0,210,255
  198: 147,0,210,255
  199: 119,0,89,255
  200: 119,0,89,255
  201: 0,0,0,255
  202: 0,0,0,255
  203: 0,0,0,255
  204: 0,0,0,255
  205: 0,0,0,255
  206: 0,0,0,255
  207: 0,0,0,255
  208: 0,0,0,255
  209: 0,0,0,255
  210: 0,0,0,255
  211: 0,0,0,255
  212: 0,0,0,255
  213: 0,0,0,255
  214: 0,0,0,255
  215: 0,0,0,255
  216: 0,0,0,255
  217: 0,0,0,255
  218: 0,0,0,255
  219: 0,0,0,255
  220: 0,0,0,255
  221: 0,0,0,255
  222: 0,0,0,255
  223: 0,0,0,255
  224: 0,0,0,255
  225: 0,0,0,255
  226: 0,0,0,255
  227: 0,0,0,255
  228: 0,0,0,255
  229: 0,0,0,255
  230: 0,0,0,255
  231: 0,0,0,255
  232: 0,0,0,255
  233: 0,0,0,255
  234: 0,0,0,255
  235: 0,0,0,255
  236: 0,0,0,255
  237: 0,0,0,255
  238: 0,0,0,255
  239: 0,0,0,255
  240: 0,0,0,255
  241: 0,0,0,255
  242: 0,0,0,255
  243: 0,0,0,255
  244: 0,0,0,255
  245: 0,0,0,255
  246: 0,0,0,255
  247: 0,0,0,255
  248: 0,0,0,255
  249: 0,0,0,255
  250: 0,0,0,255
  251: 99,99,99,255
  252: 0,0,0,255
  253: 0,0,0,255
  254: 0,0,0,255
  255: 0,0,0,0

Tif 文件: https://drive.google.com/file/d/1y5UGAteXUOzGaqWsuFc7DXi8XTzGMVJ_/view?usp=sharing 转换后的nc文件:https://drive.google.com/file/d/1CjZJ9pRcLJomHMkTjatkXHTKx4zQ6ehl/view?usp=sharing

【问题讨论】:

  • 你的 tif 似乎是正确的数据类型......它的颜色映射一直到 255。你能添加gdalinfo -hist 和/或gdalinfo -stats吗?

标签: python gdal netcdf geotiff


【解决方案1】:

看起来您正在将输入数据(很可能是 uint8 转换为 int8),因此高于数据类型最大“溢出”的值将转换为负数。您需要将数据类型专门设置为Byte(在gdal 行话中是uint8)。

您可以通过添加 outputType 选项关键字参数来解决此问题。此外,您可以使用 noData 选项设置所需的 nodata 值。

这是具有正确数据类型和 nodata 值为 255 的完整调用:

gdal.Translate(outputfile,
               inputfile,
               format='NetCDF',
               outputType=gdal.gdalconst.GDT_Byte,
               noData=255)

【讨论】:

  • 谢谢,可惜这并没有解决问题。
  • @Beardsley 你需要添加一些关于你的问题的更多信息,例如您的 tifnc 文件的 gdalinfo 输出。通常不使用下载链接
  • 好的,谢谢,我已经添加了 gdal.Info 输出
猜你喜欢
  • 1970-01-01
  • 2019-02-02
  • 2013-01-11
  • 2018-04-04
  • 2021-11-02
  • 2020-10-27
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
相关资源
最近更新 更多