How to generate tiles correctly with HipsGen from GDAL TIF images

Hello,

I am new to this forum and topic, so please forgive me if this is a trivial question.

I am having trouble converting GDAL TIF images to FITS files that can be used by HipsGen. More precisely, the final render generated by HipsGen is different to what I expected. Some transformations seem to be happening that I don’t understand, which ruins the tile generation.

To perform this conversion, I follow these steps:

I start with the command:

gdal_translate source.tif destination.fits

Once all the TIF images have been processed, I generate the tiles using this command:

java -jar .\Hipsgen.jar in=“C:\Users\Ordinateur\Desktop\fits” out=“./” id=“moon/dem” CLEANALL INDEX TILES PNG

It says that, by default, HipsGen generates 512x512 tiles. I would prefer to use 256x256 tiles, but when I try this, it does not work.

java -jar .\Hipsgen.jar in=“C:\Users\Ordinateur\Desktop\fits” out=“./” id=“moon/dem” tileWidth=256 CLEANALL INDEX TILES PNG

And this error shows up every time; I have no idea why. The generation happens in a new folder, so it should not have pre-existing tiles of a different order.

INFO  : Output directory: .\moon_P_dem
INFO  : Reference image: C:\Users\Ordinateur\Desktop\fits_flipped\ldem_1024_75s_60s_000_015.fits
INFO  : Set default target => -172.49951 -67.50049 15.0
INFO  : BITPIX found in the reference image => 16
INFO  : Data range [-8344 .. 5886] (estimation), pixel cut [-4507 .. 2163]
*WARN*: Uncompatible tileOrder=8 compared to default pre-existing survey tileOrder=9
*ERROR: Uncompatible pre-existing HiPS survey

That was my first issue, but I have another one. This one relates to how HipsGen manages latitude and longitude.

When visualising the 512x512 tiles on the index.html file generated at the end of the jar command, something strange happens.

For example, a TIF image containing data from latitudes between -60° and -45° ends up being displayed as between 45° and 60°. This happens for all my data. Also, the longitude is reversed; instead of going from left to right, it goes from right to left.

To check my results, I use the following website to see if my rendering is similar.

https://alasky.cds.unistra.fr/Planets/CDS_P_Moon_LROC-WAC-100m/

I saw that TIF images should be flipped along the Y-axis before conversion to FITS, because the origin is at the top-left corner in GDAL, but at the bottom-left corner in Hips. To avoid altering the data by flipping them in the Y-axis, I modified the header of the FITS file as follow:

header[‘CDELT2’] = -header[‘CDELT2’] # Flip Y
header[‘PC2_2’] = -header.get(‘PC2_2’, 1.0)
header[‘CRPIX2’] = naxis2 - header[‘CRPIX2’] + 1

This was necessary because changing the data did not work — the latitude was still reversed — but modifying the header made it work.

Hi @Prototype,

welcome to the OpenPlanetary forum! :waving_hand:

I’m not able to answer about HipsGen…

About GDAL translation from tif to FITS I have two questions

Hello Chiara,

Thank you for replying to my message.

After going into more detail and carrying out further tests, I realised that the issue was not related to GDAL, but to HipsGen.

I had to modify the FITS header file using the following code to apply the transformation to the data:

def flip_fits_file(hdul) -> None:
   """
   Flip the FITS file horizontally and vertically, and apply a 180° longitude offset.
   """
   data = hdul[0].data
   header = hdul[0].header
   # Get dimensions
   naxis1 = data.shape[1]  # width (X)
   naxis2 = data.shape[0]  # height (Y)
   # Flip Y Axis
   header['CDELT2'] = -header['CDELT2']
   header['PC2_2'] = -header.get('PC2_2', 1.0)
   header['CRPIX2'] = naxis2 - header['CRPIX2'] + 1
   # Apply 180° longitude offset (rotate)
   header['CRVAL1'] = (header['CRVAL1'] + 180.0) % 360.0
   # Update the header with the flipped values
   hdul[0].header = header

I don’t know if it’s possible to modify something in HipsGen to avoid this process of changing the headers.

Once tiles have been generated via HipsGen, two lines must be changed in the properties file. Set the ‘hips_frame’ parameter to ‘moon’ and add a new parameter called ‘hips_body=moon’. Adding “hips_body=moon”, set the correct longitude direction from left to right; otherwise, the longitude is reversed.

I am still having trouble changing the size of the tiles. By default, they are 512x512, but this can be changed using the tileWidth variable in the command:

java -jar .\Hipsgen.jar in=“C:\Users\Ordinateur\Desktop\fits” out=“./” id=“moon/dem” tileWidth=256 CLEANALL INDEX TILES PNG

However, it seems that the new version of HipsGen has a bug with this parameter, so it is not possible to change this value.

I am glad you sorted out your issue.
In general it is a good practice to not modify the files metadata. Now that you have a recipe it would be very useful to get in touch with the authors and report the issue.
There is a mailing list for that (see the Aladin page).

Happy HIPS!