1010import  numpy  as  np 
1111from  astropy .coordinates  import  ICRS , BarycentricTrueEcliptic , Galactic 
1212from  astropy .io  import  fits 
13- from  astropy .nddata  import  block_reduce 
1413from  astropy_healpix  import  (
1514    HEALPix ,
1615    level_to_nside ,
@@ -204,6 +203,8 @@ def reproject_to_hips(
204203    # Determine center of image and radius to furthest corner, to determine 
205204    # which HiPS tiles need to be generated 
206205
206+     # TODO: this will fail for e.g. allsky maps 
207+ 
207208    ny , nx  =  array_in .shape [- 2 :]
208209
209210    cen_x , cen_y  =  (nx  -  1 ) /  2 , (ny  -  1 ) /  2 
@@ -216,9 +217,6 @@ def reproject_to_hips(
216217
217218    radius  =  cor_world .separation (cen_world ).max () *  2 
218219
219-     print (cen_world )
220-     print (cor_world )
221- 
222220    # TODO: in future if astropy-healpix implements polygon searches, we could 
223221    # use that instead 
224222
@@ -227,9 +225,9 @@ def reproject_to_hips(
227225    nside  =  level_to_nside (level )
228226    hp  =  HEALPix (nside = nside , order = "nested" , frame = frame )
229227
230-     indices  =  hp .cone_search_skycoord (cen_world , radius = radius )
228+     #  indices = hp.cone_search_skycoord(cen_world, radius=radius)
231229
232-     print ( indices )
230+     indices   =   np . arange ( hp . npix )
233231
234232    logger .info (f"Found { len (indices )} { level }  )
235233
@@ -241,12 +239,28 @@ def reproject_to_hips(
241239
242240    # Iterate over the tiles and generate them 
243241    def  process (index ):
244-         header  =  tile_header (level = level , index = index , frame = frame , tile_size = tile_size )
245242        if  hasattr (wcs_in , "deepcopy" ):
246243            wcs_in_copy  =  wcs_in .deepcopy ()
247244        else :
248245            wcs_in_copy  =  deepcopy (wcs_in )
249-         array_out , footprint  =  reproject_function ((array_in , wcs_in_copy ), header , ** kwargs )
246+ 
247+         header  =  tile_header (level = level , index = index , frame = frame , tile_size = tile_size )
248+ 
249+         if  isinstance (header , tuple ):
250+             array_out1 , footprint1  =  reproject_function (
251+                 (array_in , wcs_in_copy ), header [0 ], ** kwargs 
252+             )
253+             array_out2 , footprint2  =  reproject_function (
254+                 (array_in , wcs_in_copy ), header [1 ], ** kwargs 
255+             )
256+             array_out  =  (
257+                 np .nan_to_num (array_out1 ) *  footprint1  +  np .nan_to_num (array_out2 ) *  footprint2 
258+             ) /  (footprint1  +  footprint2 )
259+             footprint  =  (footprint1  +  footprint2 ) /  2 
260+             header  =  header [0 ]
261+         else :
262+             array_out , footprint  =  reproject_function ((array_in , wcs_in_copy ), header , ** kwargs )
263+ 
250264        if  tile_format  !=  "png" :
251265            array_out [np .isnan (array_out )] =  0.0 
252266        if  np .all (footprint  ==  0 ):
@@ -260,6 +274,7 @@ def process(index):
260274                    extension = EXTENSION [tile_format ],
261275                ),
262276                array_out ,
277+                 header ,
263278            )
264279        else :
265280            if  tile_format  ==  "png" :
@@ -294,76 +309,76 @@ def process(index):
294309
295310    indices  =  np .array (generated_indices )
296311
297-     # Iterate over higher levels and compute lower resolution tiles 
298-     for  ilevel  in  range (level  -  1 , - 1 , - 1 ):
299- 
300-         # Find index of tiles to produce at lower-resolution levels 
301-         indices  =  np .sort (np .unique (indices  //  4 ))
302- 
303-         make_tile_folders (level = ilevel , indices = indices , output_directory = output_directory )
304- 
305-         for  index  in  indices :
306- 
307-             header  =  tile_header (level = ilevel , index = index , frame = frame , tile_size = tile_size )
308- 
309-             if  tile_format  ==  "fits" :
310-                 array  =  np .zeros ((tile_size , tile_size ))
311-             elif  tile_format  ==  "png" :
312-                 array  =  np .zeros ((tile_size , tile_size , 4 ), dtype = np .uint8 )
313-             else :
314-                 array  =  np .zeros ((tile_size , tile_size , 3 ), dtype = np .uint8 )
315- 
316-             for  subindex  in  range (4 ):
317- 
318-                 current_index  =  4  *  index  +  subindex 
319-                 subtile_filename  =  tile_filename (
320-                     level = ilevel  +  1 ,
321-                     index = current_index ,
322-                     output_directory = output_directory ,
323-                     extension = EXTENSION [tile_format ],
324-                 )
325- 
326-                 if  os .path .exists (subtile_filename ):
327- 
328-                     if  tile_format  ==  "fits" :
329-                         data  =  block_reduce (fits .getdata (subtile_filename ), 2 , func = np .mean )
330-                     else :
331-                         data  =  block_reduce (
332-                             np .array (Image .open (subtile_filename ))[::- 1 ], (2 , 2 , 1 ), func = np .mean 
333-                         )
334- 
335-                     if  subindex  ==  0 :
336-                         array [256 :, :256 ] =  data 
337-                     elif  subindex  ==  2 :
338-                         array [256 :, 256 :] =  data 
339-                     elif  subindex  ==  1 :
340-                         array [:256 , :256 ] =  data 
341-                     elif  subindex  ==  3 :
342-                         array [:256 , 256 :] =  data 
343- 
344-             if  tile_format  ==  "fits" :
345-                 fits .writeto (
346-                     tile_filename (
347-                         level = ilevel ,
348-                         index = index ,
349-                         output_directory = output_directory ,
350-                         extension = EXTENSION [tile_format ],
351-                     ),
352-                     array ,
353-                     header ,
354-                 )
355-             else :
356-                 image  =  as_transparent_rgb (array .transpose (2 , 0 , 1 ))
357-                 if  tile_format  ==  "jpeg" :
358-                     image  =  image .convert ("RGB" )
359-                 image .save (
360-                     tile_filename (
361-                         level = ilevel ,
362-                         index = index ,
363-                         output_directory = output_directory ,
364-                         extension = EXTENSION [tile_format ],
365-                     )
366-                 )
312+     # #  Iterate over higher levels and compute lower resolution tiles 
313+     #  for ilevel in range(level - 1, -1, -1):
314+ 
315+     #      # Find index of tiles to produce at lower-resolution levels
316+     #      indices = np.sort(np.unique(indices // 4))
317+ 
318+     #      make_tile_folders(level=ilevel, indices=indices, output_directory=output_directory)
319+ 
320+     #      for index in indices:
321+ 
322+     #          header = tile_header(level=ilevel, index=index, frame=frame, tile_size=tile_size)
323+ 
324+     #          if tile_format == "fits":
325+     #              array = np.zeros((tile_size, tile_size))
326+     #          elif tile_format == "png":
327+     #              array = np.zeros((tile_size, tile_size, 4), dtype=np.uint8)
328+     #          else:
329+     #              array = np.zeros((tile_size, tile_size, 3), dtype=np.uint8)
330+ 
331+     #          for subindex in range(4):
332+ 
333+     #              current_index = 4 * index + subindex
334+     #              subtile_filename = tile_filename(
335+     #                  level=ilevel + 1,
336+     #                  index=current_index,
337+     #                  output_directory=output_directory,
338+     #                  extension=EXTENSION[tile_format],
339+     #              )
340+ 
341+     #              if os.path.exists(subtile_filename):
342+ 
343+     #                  if tile_format == "fits":
344+     #                      data = block_reduce(fits.getdata(subtile_filename), 2, func=np.mean)
345+     #                  else:
346+     #                      data = block_reduce(
347+     #                          np.array(Image.open(subtile_filename))[::-1], (2, 2, 1), func=np.mean
348+     #                      )
349+ 
350+     #                  if subindex == 0:
351+     #                      array[256:, :256] = data
352+     #                  elif subindex == 2:
353+     #                      array[256:, 256:] = data
354+     #                  elif subindex == 1:
355+     #                      array[:256, :256] = data
356+     #                  elif subindex == 3:
357+     #                      array[:256, 256:] = data
358+ 
359+     #          if tile_format == "fits":
360+     #              fits.writeto(
361+     #                  tile_filename(
362+     #                      level=ilevel,
363+     #                      index=index,
364+     #                      output_directory=output_directory,
365+     #                      extension=EXTENSION[tile_format],
366+     #                  ),
367+     #                  array,
368+     #                  header,
369+     #              )
370+     #          else:
371+     #              image = as_transparent_rgb(array.transpose(2, 0, 1))
372+     #              if tile_format == "jpeg":
373+     #                  image = image.convert("RGB")
374+     #              image.save(
375+     #                  tile_filename(
376+     #                      level=ilevel,
377+     #                      index=index,
378+     #                      output_directory=output_directory,
379+     #                      extension=EXTENSION[tile_format],
380+     #                  )
381+     #              )
367382
368383    # Generate properties file 
369384
0 commit comments