@@ -457,6 +457,9 @@ def generate_images_group(self, image_format='png', world_file=False, granule_id
457
457
width_deg = region [3 ] - region [2 ]
458
458
self .region = Region (region )
459
459
460
+ if self .are_all_lon_lat_invalid (lon_array , lat_array ):
461
+ raise Exception ("Can't generate images for empty granule" )
462
+
460
463
output_dimensions = (int (height_deg * self .ppd ), int (width_deg * self .ppd ))
461
464
(self .rows , self .cols ) = output_dimensions
462
465
@@ -573,42 +576,26 @@ def non_nan_neighbors(arr, x, y):
573
576
574
577
return img_with_neighbor_filled
575
578
576
- def are_all_lon_lat_invalid (self ):
579
+ def are_all_lon_lat_invalid (self , lon , lat ):
577
580
"""
578
- Checks if all longitude and latitude values in a NetCDF file are invalid.
581
+ Checks if all coordinate pairs contain at least one invalid value .
579
582
580
583
Parameters:
581
- file_path (str): Path to the NetCDF file.
582
- lon_var (str): Name of the longitude variable in the file.
583
- lat_var (str): Name of the latitude variable in the file.
584
-
584
+ lon (array-like): Array of longitude values.
585
+ lat (array-like): Array of latitude values.
585
586
Returns:
586
- bool: True if all longitude and latitude values are invalid, False otherwise.
587
+ bool: True if all coordinate pairs have at least one invalid value,
588
+ False if there exists at least one valid coordinate pair.
587
589
"""
588
590
try :
589
- with xr .open_dataset (self .input_file ) as ds :
590
-
591
- lon_var = self .config .get ('lonVar' )
592
- lat_var = self .config .get ('latVar' )
593
-
594
- if lon_var not in ds .variables or lat_var not in ds .variables :
595
- raise ValueError (f"Missing required variables: '{ lon_var } ' or '{ lat_var } ' in the file." )
596
-
597
- lon = ds [lon_var ]
598
- lat = ds [lat_var ]
599
-
600
- # Define valid ranges
601
- valid_lon = (lon >= - 180 ) & (lon <= 180 )
602
- valid_lat = (lat >= - 90 ) & (lat <= 90 )
603
-
604
- # Check if all values are invalid
605
- all_invalid_lon = (~ valid_lon ).all ().item ()
606
- all_invalid_lat = (~ valid_lat ).all ().item ()
607
-
608
- return all_invalid_lon and all_invalid_lat
591
+ # Define valid ranges
592
+ valid_lon_mask = (lon >= - 180 ) & (lon <= 180 )
593
+ valid_lat_mask = (lat >= - 90 ) & (lat <= 90 )
594
+ # Check if any pair is completely valid
595
+ valid_pairs = valid_lon_mask & valid_lat_mask
596
+ return not valid_pairs .any ()
609
597
except Exception as e :
610
- self .logger .error (f"Error: { e } " )
611
- raise e
598
+ raise RuntimeError (f"Error checking longitude/latitude validity: { e } " ) from e
612
599
613
600
def process_variable (self ,
614
601
var ,
0 commit comments