checkEmpty
Description
checkEmpty is a fast shell-scripting utility that inspects an OMG/JHC raster grid file (.r4, .8bit, .mos, or 16-bit integer grid) to determine whether it contains any valid (non-zero, non-background) data pixels. As soon as it encounters the first valid data cell after the 1024-byte header, it prints NotEmpty and exits immediately with status code 1; if the entire grid contains only no-data values (0 or 255 for 8-bit), it prints Empty and exits with status code 0.
Because it short-circuits on the very first non-empty pixel and returns distinct exit codes (0 = Empty, 1 = NotEmpty, 2 = File open error, 3 = Missing argument), checkEmpty is designed for batch processing pipelines to quickly prune empty map sheets or tile outputs.
Usage
checkEmpty <infile>
Example Commands
Check whether a gridded bathymetry tile contains any data:
checkEmpty tile_042.r4
Use checkEmpty in a bash loop to move empty .r4 or .mos files into an empty/ directory:
for f in *.r4; do
if checkEmpty "$f" > /dev/null; then
mv "$f" empty/
fi
done
CLI Arguments and Options
| Flag / Argument | Parameters | Description |
|---|---|---|
<infile> | <path> | Required. Path to an OMG raster file (.r4, .8bit, .mos, or 16-bit short grid). |
Exit Codes
| Exit Code | Standard Output | Meaning |
|---|---|---|
0 | Empty | All grid cells in the file are empty/background (0, or 255 for 8-bit files). |
1 | NotEmpty | At least one grid cell contains valid data (!= 0 and != 255). |
2 | Error opening <file> | The specified input file could not be opened in "rb+" mode. |
3 | Must provide input file (.r4 or .8bit or .mos) | No input filename was supplied on the command line (argc == 1). |
File Formats
Input Format: OMG Raster Grid (.r4, .8bit, .mos)
- Byte Offset
64(data_type):checkEmptyseeks directly to byte offset64(fseek(infile, 64, SEEK_SET)) and reads 1 byte (unsigned char data) corresponding to thedata_typefield in the 1024-byte OMGJHC_header:data == 1: 8-bit unsigned integer (unsigned char, 1 byte per pixel). Values0and255are treated as empty/no-data.data == 2: 16-bit signed integer (short, 2 bytes per pixel). Value0is treated as empty.data == 3: 32-bit IEEE floating-point (float, 4 bytes per pixel,.r4). Value0.0fis treated as empty.
- Byte Offset
1024(Data Payload): Binary array of pixels starting immediately after the 1024-byte header (fseek(infile, 1024, SEEK_SET)).
How It Works
- File Open & Data Type Detection:
checkEmptyopens the first command-line argument in"rb+"mode. It seeks to byte64of the header and reads a singleunsigned char(data) to identify whether pixel values areunsigned char(1),short(2), orfloat(3). - Fast Data Scan: The file pointer is rewound and positioned at byte
1024(start of the raster array). Inside awhile (!feof(infile))loop:- It reads one element of size
sizeof(unsigned char),sizeof(short), orsizeof(float)according todata. - If
var == 255(the 8-bit no-data sentinel), the loop continues to the next pixel. - If
var != 0 || varS != 0 || varF != 0, the program immediately printsNotEmpty\nand returns1without scanning the remainder of the file.
- It reads one element of size
- Completion: If
feof(infile)is reached without encountering any non-zero (and non-255) pixel, the program printsEmpty\n, closes the file, and returns0.