I came up with a code to transform the nc into geotiff. I am still a bit skeptical, but this is what I have:
I needed to clip the only Australia from the full disk image, So I found the bbox for Australia (https://www.data.jma.go.jp/mscweb/data/himawari/). So I could filter the coordinates inside the netcdf using xarray. After filtering the netcdf I went from a xarray with dimensions 5500x5500 to 1591x2324. Based on that I calculated the pixel height, pixel width and subsequently I created the range of lat and lon taking into account the boundaries provided on the website. See the code below:
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
from affine import Affine
import rioxarray
import numpy as np
# Specify the path to your netCDF file
filename = "himawari9_sample_files\AHI-CHGT_v1r1_h09_s202502070840211_e202502070849405_c202502070858053.nc"
# Open the dataset using xarray
ds = xr.open_dataset(filename)
# Print the min and max values of the geolocation data variables
lat_pc_min = ds["Latitude_Pc"].min().values
lat_pc_max = ds["Latitude_Pc"].max().values
lon_pc_min = ds["Longitude_Pc"].min().values
lon_pc_max = ds["Longitude_Pc"].max().values
print("Latitude_Pc ranges from", lat_pc_min, "to", lat_pc_max)
print("Longitude_Pc ranges from", lon_pc_min, "to", lon_pc_max)
# Define the approximate bounding box for Australia.
#Australia BBOX https://www.data.jma.go.jp/mscweb/data/himawari/
lat_min, lat_max = -45.0, -10.0
lon_min, lon_max = 110.0, 155.0
# Retrieve the full geolocation arrays (assumed to be 2D: Rows x Columns)
lat_pc = ds["Latitude_Pc"].values
lon_pc = ds["Longitude_Pc"].values
# Create a boolean mask for the Australia bounding box condition.
condition = (lat_pc >= lat_min) & (lat_pc <= lat_max) & \
(lon_pc >= lon_min) & (lon_pc <= lon_max)
# Use np.where to obtain the row and column indices where the condition is True.
indices = np.where(condition)
if indices[0].size == 0:
print("No points found within the specified Australia bounding box. Please check your bounds.")
exit()
# Determine the rectangular slice edges from the indices.
row_min, row_max = indices[0].min(), indices[0].max()
col_min, col_max = indices[1].min(), indices[1].max()
print(f"Subsetting rows from {row_min} to {row_max}, and columns from {col_min} to {col_max}.")
# Subset the dataset using isel on the corresponding dimensions.
# Adjust dimension names if needed (here assumed as 'Rows' and 'Columns').
subset_ds = ds.isel(Rows=slice(row_min, row_max+1), Columns=slice(col_min, col_max+1))
###Estimating the coordinates from the suBset file, considering the australia
# Calculate pixel width
pixel_width_lon = (lon_max-lon_min) / subset_ds.dims['Columns']
# Generate the range for longitudes
lon_values = np.arange(lon_min, lon_max, pixel_width_lon)
# Calculate pixel width
pixel_height_lat = (lat_max-lat_min) / subset_ds.dims['Rows']
# Generate the range
lon_values = np.arange(lon_min, lon_max, pixel_width_lon)
lat_values = np.flip(np.arange(lat_min, lat_max, pixel_height_lat))
# Extract the temperature variable and rename dimensions.
da = subset_ds["CldTopTemp"].rename({"Rows": "y", "Columns": "x"})
# # Extract 1D coordinate arrays from the first row/column.
# x_coords = subset_ds["Longitude_Pc"].isel(Rows=0).values
# y_coords = subset_ds["Latitude_Pc"].isel(Columns=0).values
x_coords=lon_values
y_coords=lat_values
# # Assign 1D coordinates.
da = da.assign_coords(x=("x", x_coords), y=("y", y_coords))
# # Ensure y is in descending order.
if y_coords[0] < y_coords[-1]:
da = da.sortby("y", ascending=False)
y_coords = da["y"].values
# # Calculate pixel resolutions.
res_x = (x_coords[1] - x_coords[0]).item()
res_y = (y_coords[0] - y_coords[1]).item()
# # Compute the affine transform.
x_min = x_coords[0]
y_max = y_coords[0]
transform = Affine.translation(x_min - res_x/2, y_max + res_y/2) * Affine.scale(res_x, -res_y)
# # Set spatial dimensions, transform, and CRS.
da.rio.set_spatial_dims(x_dim="x", y_dim="y", inplace=True)
da.rio.write_transform(transform, inplace=True)
da.rio.write_crs("EPSG:4326", inplace=True)
output_tif = "suBset_value_12_aus.tif"
#Export to GeoTIFF.
da.rio.to_raster(output_tif)
print("GeoTIFF saved to:", output_tif) ```