Skip to content. | Skip to navigation

Personal tools

select_chan_props.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <math.h>
#include "airs_chan_props.h"

/*
* Select the correct channel properties file for data of a given
* year, month, day from all the channel properties files in a
* given directory.
*
* It makes this choice first based on date. The date in a
* file name is the first date for which it is valid. So we
* need to select the file name with the latest date before the
* start of the data.
*
* If there are multiple files with the same date then they are
* probably different versions. Choose the latest version.
*
* WARNING: This function uses file names, not metadata, so:
* 1) It only works if the files have the expected names (see below)
* 2) For data the same date as a change in channel properties
* files, it cannot distinguish the exact time of the changeover.
*
* Channel properties files names should all follow this pattern:
* L2.chan_prop.2002.05.04.v8.1.0.anc
*/
int select_chan_props(
char * file_name,
char const * const dir_name,
const int data_year,
const int data_month,
const int data_day)
{
DIR *dirp;
struct dirent *dp;
int num_found = 0;
char fname[256][256];
int year[256], month[256], day[256], major[256], minor[256], patch[256];
double file_date[256], best_file_date, vers[256], best_vers;
int i, num_perfect, i_perfect = 0;

for (i = 0; i < 256; i++) {
fname[i][0] = 0;
year[i] = month[i] = day[i] = 0;
major[i] = minor[i] = patch[i] = 0;
file_date[i] = vers[i] = 0.0;
}

dirp = opendir(dir_name);

while (dirp) {
errno = 0;
if ((dp = readdir(dirp)) != NULL) {
int len = strlen(dp->d_name);
/* printf("Entry \"%s\"\n", dp->d_name); */
/*
* file is a candidate if:
* 1) it starts with "L2.chan_prop."
* 2) it ends with ".anc"
* 3) we can parse out year/month/day and version #
*/
if ( !strncmp(dp->d_name, "L2.chan_prop.", 13)
&& len > 3 /* make sure we're not going to overrun array */
&& !strncmp(&dp->d_name[len-4], ".anc", 4)
&& sscanf(dp->d_name, "L2.chan_prop.%d.%d.%d.v%d.%d.%d.anc",
&year[num_found], &month[num_found], &day[num_found],
&major[num_found], &minor[num_found], &patch[num_found])
== 6) {
/* eliminate any chan props files starting after the data date */
if (year[num_found] > data_year)
;
else if (year[num_found] == data_year && month[num_found] > data_month)
;
else if (year[num_found] == data_year && month[num_found] == data_month
&& day[num_found] > data_day)
;
else {
strcpy(fname[num_found], dp->d_name);
num_found++;
}
}
} else {
if (errno == 0) {
(void)closedir(dirp);
break;
}
(void)closedir(dirp);
return 1;
}
}

/* printf("Found %d good entries\n", num_found); */
/*
* Compare dates of the files. To make it easier, combine year, month,
* and day into a 64-bit value date := 12*31*year + 31*month + day
*
* similarly compose a single 64-bit version for each.
*/
best_file_date = -1.0;
for (i = 0; i < num_found; i++) {
file_date[i] = 12.0 * 31.0 * year[i]
+ 31.0 * month[i]
+ day[i];
vers[i] = 1.0e8 * major[i] + 1.0e4 * minor[i] + patch[i];
if (file_date[i] > best_file_date)
best_file_date = file_date[i];
/* printf(" \"%s\" %f %f\n", fname[i], file_date[i], vers[i]); */
}
/* printf("These start the right date:\n"); */
/*
* find the highest/best version number from all those files starting
* on the best date. (best date is latest before data date.)
*/
best_vers = -1.0;
for (i = 0; i < num_found; i++)
if (fabs(best_file_date - file_date[i]) < 1.0) {
/* printf(" \"%s\" %f %f\n", fname[i], file_date[i], vers[i]); */
if (vers[i] > best_vers)
best_vers = vers[i];
}
/*
* There really should be only one file with the best date & best version
* number, but better double-check.
*/
num_perfect = 0;
for (i = 0; i < num_found; i++)
if ( (fabs(best_file_date - file_date[i]) < 1.0)
&& (fabs(best_vers - vers[i]) < 1.0)) {
num_perfect++;
i_perfect = i;
}
if (num_perfect == 1) {
/* printf("The perfect file is \"%s\"\n", fname[i_perfect]); */
strcpy(file_name, fname[i_perfect]);
return 0;
} else if (num_perfect < 1) {
printf("Could not find any good channel props files for date %04d-%02d-%02d\n",
data_year, data_month, data_day);
printf("in directory \"%s\"\n", dir_name);
} else {
fprintf(stderr, "Error: more than one perfectly matching file:\n");
for (i = 0; i < num_found; i++)
if ( (fabs(best_file_date - file_date[i]) < 1.0)
&& (fabs(best_vers - vers[i]) < 1.0))
printf(" \"%s\"\n", fname[i]);
}

return 1;
}

Document Actions
NASA Logo - nasa.gov
NASA Privacy Policy and Important Notices
Last updated: Sep 09, 2009 02:26 PM ET
Top