Beardsley
Beardsley

Reputation: 171

Values change when convert geotiff to netcdf

I have a geotiff which has values from 0 to 200, and a nodata value of 255. I converted it to a netcdf with gdal in python:

ds = gdal.Translate(outputfile, inputfile, format='NetCDF')

[As suggested by Val I tried adding outputType=gdal.gdalconst.GDT_Byte, noData=255 but it did not solve either]

Now all the 0s and nodata values in the netcdf are 0. But the main problem is the other values now range from 127 to -128. The highest values from the tif are now the lowest (i.e. negative) and the lowest values are now the most positive.

Here is info from the netcdf on the data layer

<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

Here is the gdal.Info for the netcdf:

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

And the gdal.Info for the converted Tif (after adding Val's suggestion in answers)

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 file: https://drive.google.com/file/d/1y5UGAteXUOzGaqWsuFc7DXi8XTzGMVJ_/view?usp=sharing Converted nc file: https://drive.google.com/file/d/1CjZJ9pRcLJomHMkTjatkXHTKx4zQ6ehl/view?usp=sharing

Upvotes: 0

Views: 954

Answers (1)

Val
Val

Reputation: 7033

Looks like you are casting your input data, which is most likely uint8 into int8, so values above the datatype's maximum "overflow" into the negative side. You need to specifically set the datatype to Byte (which is uint8 in gdal jargon).

You can fix this with adding the outputType option keyword argument. Additionally, you can set your desired nodata value with the noData option.

Here's the full call with the correct datatype and a nodata value of 255:

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

Upvotes: 1

Related Questions