S
Sheldon
Good day Everyone,
I am a still very new at learning C and I have thrown myself in the
deep end. I started with a simple program and kept widening the scope.
This has taught me many things about C and some, I must admit, have not
really sunk in yet. Still, I push on. Now I am taken a library of C
programs that were designed to read HDF files. I work on a Unix server
and in Mandrake10. The program below is most likely broken but I cannot
solve any problem unitl I get pass my current problem in compiling. All
the codes that I need: *.c and *.h are located in directory given in
the compilation command:
cc -g msgpps_functions.c -I/data/proj/safworks/MSG_PPS_COMP/c_codes
And I get the following oputput that baffles me:
/home/tmp/ccAn0IYi.o(.text+0x67f): In function `readHDF':
/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:197:
undefined reference to `initHlHdf'
/home/tmp/ccAn0IYi.o(.text+0x694):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:201:
undefined reference to `readHL_NodeList'
/home/tmp/ccAn0IYi.o(.text+0x6cc):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:207:
undefined reference to `selectNode'
/home/tmp/ccAn0IYi.o(.text+0x6fc):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:212:
undefined reference to `fetchMarkedNodes'
/home/tmp/ccAn0IYi.o(.text+0x70d):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:214:
undefined reference to `getNode'
/home/tmp/ccAn0IYi.o(.text+0x87b):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:243:
undefined reference to `freeHL_NodeList'
collect2: ld returned 1 exit status
Now, I have included the code but not the dependencies -- thought that
that would be too much. I would really like to know why this
compilation problem arose. All of the functions mentioned above are
declared in one or another of the dependencies in the path given and
the #include files.
Appreciate the help,
Sheldon
**********************************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <read_vhlhdf.h>
#include <read_hlhdf.h>
#include <hlhdf.h>
#include <sys/types.h>
#include <dirent.h>
#include <vhlhdf.h>
#define ROW 1215
#define COL 1215
#define Auxdatadir "/data/proj/safworks/sheldon/MSG_PPS_COMP/data/aux/"
#define Region "ibla_57n20e"
#define Date 200512
static int inithdf = 1; // Är 1 om initHlHdf ej har anropats, 0
annars. This routine must be run
int readHDF(float** , char *hdfname, char *fieldname);
// Variable Declaration
char Filename[] =
"/data/proj/safworks/sheldon/MSG_PPS_COMP/data/aux/test_array.dat";
char Reprodir[100];
char Physdata[100];
//*********************************************************************************************************
struct hdfstruct {
float** latitude;
float** longitude;
float** landuse;
float** fractofland;
float** sunangle;
float** viewingangle;
};
int main(void) {
struct hdfstruct hdfdata;
FILE *fp = NULL;
double** array;
int i, j, num, stat;
int dataprecision = 4;
unsigned char** tmp;
double count;
char* strp;
char Latfile[150];
char Lonfile[150];
char hdfname[] =
"/data/proj/safworks/pps_v11/import/AUX_data/remapped/physiography.ibla_35n00e.hdf";
char latfile[150];
// Putting the file strings together for lat and long files
strcpy(Latfile,Auxdatadir);
strcat(Latfile,Region);
strcat(Latfile,"_lat.data");
strcpy(Lonfile,Auxdatadir);
strcat(Lonfile,Region);
strcat(Lonfile,"_lon.data");
strcpy(latfile,Auxdatadir);
strcat(latfile,Region);
strcat(latfile,"_latlon.h5");
printf("\n%s\n\n", Latfile);
printf("%s\n\n", Lonfile);
// allocate memory with error checking for lon and lat files
hdfdata.latitude = calloc(ROW,sizeof(float *));
hdfdata.longitude = calloc(ROW,sizeof(float *));
hdfdata.landuse = calloc(ROW,sizeof(float *));
hdfdata.fractofland = calloc(ROW,sizeof(float *));
hdfdata.sunangle = calloc(ROW,sizeof(float *));
hdfdata.viewingangle = calloc(ROW,sizeof(float *));
if(hdfdata.latitude == NULL ||
hdfdata.longitude == NULL ||
hdfdata.landuse == NULL ||
hdfdata.fractofland == NULL ||
hdfdata.sunangle == NULL ||
hdfdata.viewingangle == NULL) { // Memory for the ROWS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
for (i = 0; i < ROW; i++) {
hdfdata.latitude = calloc(COL,sizeof(float));
hdfdata.longitude = calloc(COL,sizeof(float));
hdfdata.landuse = calloc(COL,sizeof(float));
hdfdata.fractofland = calloc(COL,sizeof(float));
hdfdata.sunangle = calloc(COL,sizeof(float));
hdfdata.viewingangle = calloc(COL,sizeof(float));
if(hdfdata.latitude == NULL ||
hdfdata.longitude == NULL ||
hdfdata.landuse == NULL ||
hdfdata.fractofland == NULL ||
hdfdata.sunangle == NULL ||
hdfdata.viewingangle == NULL) { // Memory for the COLS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
}
// Reading the hdf data into the arrays
stat = readHDF(hdfdata.latitude,latfile,"/LAT");
if (stat == 0) {
fprintf(stderr, "Failed reading latitude hdf");
return EXIT_FAILURE;
}
if((tmp=calloc(ROW,sizeof(float *))) == NULL) { // Memory for the
ROWS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
for (i = 0; i < ROW; i++) {
if((tmp=calloc(COL,sizeof(float))) == NULL) { // Memory for the
COLS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
}
// Opening and reading lon and lat files
printf("Opening lat file: %s\n",Latfile);
if ((fp = fopen(Latfile, "rb")) == NULL) {
fprintf(stderr, "Error opening file %s in read mode.\n", Latfile);
fclose(fp);
goto fail;
}
else
printf("File opened successfully.\n\n");
printf("Reading said file.\n\n");
if (ROW*COL*4 != fread(tmp, sizeof(unsigned char), ROW*COL*4, fp)) {
fprintf(stderr, "Error reading file: %s\n", Latfile);
fclose(fp);
goto fail;
}
else {
printf("File read successfully.\n\n");
fclose(fp);
}
// allocate memory with error checking test array
if((array=calloc(ROW,sizeof(double *))) == NULL) { // Memory for the
ROWS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
for (i = 0; i < ROW; i++) {
if((array=calloc(COL,sizeof(double))) == NULL) { // Memory for
the COLS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
}
// display read array for debuging
printf("Printing array\n");
for (i = 0; i < 5; i++) {
printf("latitudes[%d,%d] = \t%f\n", i,j, hdfdata.latitude[i,j]);
}
fail:
if(array) {
for (i = 0; i < ROW; i++) {
free(array);
}
free(array);
}
return EXIT_FAILURE;
}
int readHDF(float **arp, char *hdfname, char *fieldname) {
HL_NodeList *nodelist = NULL; // These are take from read_vlhdf.h
where some explanation is given
HL_Node *cloudproduct; // These are pointers to structures
HL_Node *dataset;
HL_CompoundTypeAttribute *attr; // pointer to structure
int i, xsize, ysize;
int dataprecision = sizeof(float);
if (inithdf) {
initHlHdf();
inithdf = 0;
}
nodelist = readHL_NodeList(hdfname);
if (!nodelist) {
fprintf(stderr, "Error reading hdf-file: %s\n", hdfname);
goto error;
}
if (!selectNode(nodelist, fieldname)) {
fprintf(stderr, "Dataset in %s was not found in %s.\n",fieldname,
hdfname);
goto error;
}
fetchMarkedNodes(nodelist); // gets the data from selected dataset
cloudproduct = getNode(nodelist, fieldname);
if (!cloudproduct) {
fprintf(stderr, "Error reading data from %s\n", hdfname);
goto error;
}
for (i = 0; i < cloudproduct->compoundDescription->nAttrs; i++) {
attr = cloudproduct->compoundDescription->attrs;
if(0 == strcmp(attr->attrname, "xsize")) {
xsize = *((int *)&cloudproduct->data[attr->offset]);
} else if (0 == strcmp(attr->attrname,"ysize")) { // checking to
see if there is such an attribute
ysize = *((int *)&cloudproduct->data[attr->offset]); // casting
from binary to int
}
}
if (xsize != COL || ysize != ROW) {
if (xsize <= 0 || ysize <= 0) {
fprintf(stderr, "Error: hdf-file has undefined size.\n");
goto error;
} else {
fprintf(stderr,
"Warning: hdf-file has unexpected size: %d×%d\n",
xsize, ysize);
}
}
memcpy(arp, cloudproduct->data, ROW * COL * dataprecision); // This
converts the data from binary to float
error: {
if (nodelist) freeHL_NodeList(nodelist);
if (arp) free(arp);
return (0);
}
return (1);
}
I am a still very new at learning C and I have thrown myself in the
deep end. I started with a simple program and kept widening the scope.
This has taught me many things about C and some, I must admit, have not
really sunk in yet. Still, I push on. Now I am taken a library of C
programs that were designed to read HDF files. I work on a Unix server
and in Mandrake10. The program below is most likely broken but I cannot
solve any problem unitl I get pass my current problem in compiling. All
the codes that I need: *.c and *.h are located in directory given in
the compilation command:
cc -g msgpps_functions.c -I/data/proj/safworks/MSG_PPS_COMP/c_codes
And I get the following oputput that baffles me:
/home/tmp/ccAn0IYi.o(.text+0x67f): In function `readHDF':
/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:197:
undefined reference to `initHlHdf'
/home/tmp/ccAn0IYi.o(.text+0x694):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:201:
undefined reference to `readHL_NodeList'
/home/tmp/ccAn0IYi.o(.text+0x6cc):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:207:
undefined reference to `selectNode'
/home/tmp/ccAn0IYi.o(.text+0x6fc):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:212:
undefined reference to `fetchMarkedNodes'
/home/tmp/ccAn0IYi.o(.text+0x70d):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:214:
undefined reference to `getNode'
/home/tmp/ccAn0IYi.o(.text+0x87b):/data/proj/safworks/MSG_PPS_COMP/c_codes/msgpps_functions.c:243:
undefined reference to `freeHL_NodeList'
collect2: ld returned 1 exit status
Now, I have included the code but not the dependencies -- thought that
that would be too much. I would really like to know why this
compilation problem arose. All of the functions mentioned above are
declared in one or another of the dependencies in the path given and
the #include files.
Appreciate the help,
Sheldon
**********************************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <read_vhlhdf.h>
#include <read_hlhdf.h>
#include <hlhdf.h>
#include <sys/types.h>
#include <dirent.h>
#include <vhlhdf.h>
#define ROW 1215
#define COL 1215
#define Auxdatadir "/data/proj/safworks/sheldon/MSG_PPS_COMP/data/aux/"
#define Region "ibla_57n20e"
#define Date 200512
static int inithdf = 1; // Är 1 om initHlHdf ej har anropats, 0
annars. This routine must be run
int readHDF(float** , char *hdfname, char *fieldname);
// Variable Declaration
char Filename[] =
"/data/proj/safworks/sheldon/MSG_PPS_COMP/data/aux/test_array.dat";
char Reprodir[100];
char Physdata[100];
//*********************************************************************************************************
struct hdfstruct {
float** latitude;
float** longitude;
float** landuse;
float** fractofland;
float** sunangle;
float** viewingangle;
};
int main(void) {
struct hdfstruct hdfdata;
FILE *fp = NULL;
double** array;
int i, j, num, stat;
int dataprecision = 4;
unsigned char** tmp;
double count;
char* strp;
char Latfile[150];
char Lonfile[150];
char hdfname[] =
"/data/proj/safworks/pps_v11/import/AUX_data/remapped/physiography.ibla_35n00e.hdf";
char latfile[150];
// Putting the file strings together for lat and long files
strcpy(Latfile,Auxdatadir);
strcat(Latfile,Region);
strcat(Latfile,"_lat.data");
strcpy(Lonfile,Auxdatadir);
strcat(Lonfile,Region);
strcat(Lonfile,"_lon.data");
strcpy(latfile,Auxdatadir);
strcat(latfile,Region);
strcat(latfile,"_latlon.h5");
printf("\n%s\n\n", Latfile);
printf("%s\n\n", Lonfile);
// allocate memory with error checking for lon and lat files
hdfdata.latitude = calloc(ROW,sizeof(float *));
hdfdata.longitude = calloc(ROW,sizeof(float *));
hdfdata.landuse = calloc(ROW,sizeof(float *));
hdfdata.fractofland = calloc(ROW,sizeof(float *));
hdfdata.sunangle = calloc(ROW,sizeof(float *));
hdfdata.viewingangle = calloc(ROW,sizeof(float *));
if(hdfdata.latitude == NULL ||
hdfdata.longitude == NULL ||
hdfdata.landuse == NULL ||
hdfdata.fractofland == NULL ||
hdfdata.sunangle == NULL ||
hdfdata.viewingangle == NULL) { // Memory for the ROWS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
for (i = 0; i < ROW; i++) {
hdfdata.latitude = calloc(COL,sizeof(float));
hdfdata.longitude = calloc(COL,sizeof(float));
hdfdata.landuse = calloc(COL,sizeof(float));
hdfdata.fractofland = calloc(COL,sizeof(float));
hdfdata.sunangle = calloc(COL,sizeof(float));
hdfdata.viewingangle = calloc(COL,sizeof(float));
if(hdfdata.latitude == NULL ||
hdfdata.longitude == NULL ||
hdfdata.landuse == NULL ||
hdfdata.fractofland == NULL ||
hdfdata.sunangle == NULL ||
hdfdata.viewingangle == NULL) { // Memory for the COLS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
}
// Reading the hdf data into the arrays
stat = readHDF(hdfdata.latitude,latfile,"/LAT");
if (stat == 0) {
fprintf(stderr, "Failed reading latitude hdf");
return EXIT_FAILURE;
}
if((tmp=calloc(ROW,sizeof(float *))) == NULL) { // Memory for the
ROWS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
for (i = 0; i < ROW; i++) {
if((tmp=calloc(COL,sizeof(float))) == NULL) { // Memory for the
COLS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
}
// Opening and reading lon and lat files
printf("Opening lat file: %s\n",Latfile);
if ((fp = fopen(Latfile, "rb")) == NULL) {
fprintf(stderr, "Error opening file %s in read mode.\n", Latfile);
fclose(fp);
goto fail;
}
else
printf("File opened successfully.\n\n");
printf("Reading said file.\n\n");
if (ROW*COL*4 != fread(tmp, sizeof(unsigned char), ROW*COL*4, fp)) {
fprintf(stderr, "Error reading file: %s\n", Latfile);
fclose(fp);
goto fail;
}
else {
printf("File read successfully.\n\n");
fclose(fp);
}
// allocate memory with error checking test array
if((array=calloc(ROW,sizeof(double *))) == NULL) { // Memory for the
ROWS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
for (i = 0; i < ROW; i++) {
if((array=calloc(COL,sizeof(double))) == NULL) { // Memory for
the COLS
fprintf(stderr, "Failed to allocate memory");
return EXIT_FAILURE;
}
}
// display read array for debuging
printf("Printing array\n");
for (i = 0; i < 5; i++) {
printf("latitudes[%d,%d] = \t%f\n", i,j, hdfdata.latitude[i,j]);
}
fail:
if(array) {
for (i = 0; i < ROW; i++) {
free(array);
}
free(array);
}
return EXIT_FAILURE;
}
int readHDF(float **arp, char *hdfname, char *fieldname) {
HL_NodeList *nodelist = NULL; // These are take from read_vlhdf.h
where some explanation is given
HL_Node *cloudproduct; // These are pointers to structures
HL_Node *dataset;
HL_CompoundTypeAttribute *attr; // pointer to structure
int i, xsize, ysize;
int dataprecision = sizeof(float);
if (inithdf) {
initHlHdf();
inithdf = 0;
}
nodelist = readHL_NodeList(hdfname);
if (!nodelist) {
fprintf(stderr, "Error reading hdf-file: %s\n", hdfname);
goto error;
}
if (!selectNode(nodelist, fieldname)) {
fprintf(stderr, "Dataset in %s was not found in %s.\n",fieldname,
hdfname);
goto error;
}
fetchMarkedNodes(nodelist); // gets the data from selected dataset
cloudproduct = getNode(nodelist, fieldname);
if (!cloudproduct) {
fprintf(stderr, "Error reading data from %s\n", hdfname);
goto error;
}
for (i = 0; i < cloudproduct->compoundDescription->nAttrs; i++) {
attr = cloudproduct->compoundDescription->attrs;
if(0 == strcmp(attr->attrname, "xsize")) {
xsize = *((int *)&cloudproduct->data[attr->offset]);
} else if (0 == strcmp(attr->attrname,"ysize")) { // checking to
see if there is such an attribute
ysize = *((int *)&cloudproduct->data[attr->offset]); // casting
from binary to int
}
}
if (xsize != COL || ysize != ROW) {
if (xsize <= 0 || ysize <= 0) {
fprintf(stderr, "Error: hdf-file has undefined size.\n");
goto error;
} else {
fprintf(stderr,
"Warning: hdf-file has unexpected size: %d×%d\n",
xsize, ysize);
}
}
memcpy(arp, cloudproduct->data, ROW * COL * dataprecision); // This
converts the data from binary to float
error: {
if (nodelist) freeHL_NodeList(nodelist);
if (arp) free(arp);
return (0);
}
return (1);
}