1

I am currently working on a project creating raster files using numpy arrays containing data from netCDFs. The rasters were created smoothly but when I plotted them, I realized they were actually upside down (mirrored vertically, not rotated).

# find data values and append to correct list
  if tas_val >= min and tas_val <= max:
     GDDs[row, column] += 1
  
  # create the transform
  x, y = lat, lon
  xres, yres = 0.25, -0.25
  transform = Affine.translation(x[0] - xres / 2, y[0] - yres / 2) * Affine.scale(xres, yres)

  # write out GDDs array to a new raster
  with rasterio.open(
          f"/content/drive/Shareddrives/Brazil/GDDTiffs/GDD_Count_{ssp}_{year}_{crop}.tif",
          mode="w",
          driver="GTiff",
          height=GDDs.shape[0],
          width=GDDs.shape[1],
          count=1,
          dtype=GDDs.dtype,
          crs="+proj=latlong",
          transform = transform) as new_dataset:
          new_dataset.write(GDDs, 1)

I essentially loop through values in a NetCDF, find if the temperature information is within bounds, and increment an index in the GDDs array that whose position matches the pixel in the NetCDF. My transformation is just an Affine translation and scale and seems to work correctly - it returns a raster the same dimensions and size as the original NetCDF which is the goal.

I've tried using different arrays, different transforms, as well as the np.invert(array) function in the new_dataset.write() line but nothing is able to actually flip my raster. Any ideas would be appreciated!

enter image description here

  • Are you just trying to vertically flip the `imshow` result? If so, did you try these yet: https://stackoverflow.com/q/8396101/13138364 – tdy Feb 15 '23 at 00:27
  • You could also simply reverse the array using slicing along the relevant axis, e.g. `arr[::-1, :]`. That said, have you considered using xarray? Plotting the data considering the corresponding axis levels should resolve this issue. – Michael Delgado Feb 15 '23 at 05:13

0 Answers0