/* Subroutines for img file operations 23 February 2009, WHFS */ #include "img_predict.h" int check_imgfile_size (char *filename, int *nx, int *ny) { /* Use stat to get the file size. There are four valid possibilities, corresponding to 1 or 2 minute files (nx must be 10800 or 21600) and whether the latitude range is 72.006 or 80.738 or 85.051. If the file size is one of the four valid ones, set nx and ny and return zero; else return -1 to signal an error condition. This kind of if, else if, else if, etc. is handled more elegantly in C by using a "switch" operator. However, "switch" must be able to promote all the comparison types to "int", and it wouldn't compile when forced to operate on the type "off_t", used in the struct stat. So I had to resort to the clumsy if, else if, ... format. */ struct stat sb; /* buffer for input file status, used to figure out its size */ const off_t img721 = 2 * 21600 * 12672; /* file size of a 1-minute file to 72 latitude */ const off_t img722 = 2 * 10800 * 6336; /* file size of a 2-minute file to 72 latitude */ const off_t img811 = 2 * 21600 * 17280; /* file size of a 1-minute file to 81 latitude */ const off_t img812 = 2 * 10800 * 8640; /* file size of a 2-minute file to 81 latitude */ const off_t img851 = 2 * 21600 * 21600; /* file size of a 1-minute file to 81 latitude */ const off_t img852 = 2 * 10800 * 10800; /* file size of a 2-minute file to 81 latitude */ if (stat (filename, &sb) ) { fprintf (stderr, "check_imgfile_size: FATAL ERROR. Cannot stat filename %s\n", filename); return (-1); } if (sb.st_size == img721) { *nx = 21600; *ny = 12672; return (0); } if (sb.st_size == img722) { *nx = 10800; *ny = 6336; return (0); } if (sb.st_size == img811) { *nx = 21600; *ny = 17280; return (0); } if (sb.st_size == img812) { *nx = 10800; *ny = 8640; return (0); } if (sb.st_size == img851) { *nx = 21600; *ny = 21600; return (0); } if (sb.st_size == img852) { *nx = 10800; *ny = 10800; return (0); } fprintf (stderr, "check_imgfile_size: FATAL_ERROR. %s has unrecognized file size of %d bytes.\n", filename, (int)sb.st_size); return (-1); }