/* VModis_latlon_resolve.cpp */ /* remap Copyright (C) 2006 Fabrice Ducos, fabrice.ducos@icare.univ-lille1.fr This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "common.h" #include #include #include "VModis_latlon_resolve.h" char *VModis_latlon_resolve(char *modis_data_file, char *modis_latlon_path) { char modis_data_file_dirbak[STRING_MAXLEN + 1]; /* calls to dirname and basename may modify their arguments, so we work on copies */ char modis_data_file_basbak[STRING_MAXLEN + 1]; char modis_latlon_pattern[STRING_MAXLEN + 1]; char modis_latlon_dir_pattern[STRING_MAXLEN + 1]; char modis_slot_time[STRING_MAXLEN + 1]; static char modis_latlon_file[STRING_MAXLEN + 1]; char *modis_data_dirname; char *modis_latlon_dirname; char *modis_data_basename; int globerr; glob_t paths; strncpy(modis_data_file_dirbak, modis_data_file, STRING_MAXLEN); strncpy(modis_data_file_basbak, modis_data_file, STRING_MAXLEN); modis_data_dirname = dirname(modis_data_file_dirbak); modis_data_basename = basename(modis_data_file_basbak); if (modis_latlon_path) { modis_latlon_dirname = modis_latlon_path; } else { modis_latlon_dirname = modis_data_dirname; } assert(modis_data_basename[1] == 'O' || modis_data_basename[1] == 'Y'); strncpy(modis_slot_time, &modis_data_basename[10], 12); // extracts YYYYDDD.HHMM in the filename modis_slot_time[12] = '\0'; snprintf(modis_latlon_pattern, STRING_MAXLEN, "M%cD03.A%s.???.?????????????.hdf", modis_data_basename[1] /* O (Terra) or Y (Aqua) */, modis_slot_time /* format YYYYDDD.HHMM */); snprintf(modis_latlon_dir_pattern, STRING_MAXLEN, "%s/%s", modis_latlon_dirname, modis_latlon_pattern); /* only the first occurence is kept (we don't expect to have several lat-lon files for the same MODIS granule) */ globerr = glob(modis_latlon_dir_pattern, 0, NULL, &paths); if ((globerr != 0) && (globerr != GLOB_NOMATCH)) { perror(modis_data_file); // FIXME: should print APPNAME before error message return NULL; } if (paths.gl_pathc < 1) { std::cerr << APPNAME << ": no lat-lon file found for " << modis_data_file << std::endl; return NULL; } strncpy(modis_latlon_file, paths.gl_pathv[0], STRING_MAXLEN); globfree(&paths); return modis_latlon_file; }