#include #include // this program reads the values from the temp/volt logs written by // temps_log.pl. Filenames are like this: // mvd_tempslog_16feb03.dat // compile/link with: cc -o read_temps read_temps.c // or (on phoncs0) gcc -o read_temps read_temps.c // // Feb 03 Hubert van Hecke //------------------------------------------------------------------------- int main (int argv, char *argc[]) { int istat; char filename[25]; if( argv < 2 ) { // check required # of arguments fprintf(stderr,"\nProper usage is: ./read_temps mvd_tempslog_16feb03.dat\n" ); exit(1); } strcpy (filename,"mvd_tempslog_16feb03.dat"); istat = read_init (filename); // open the file //istat = read_record(date, data); // get a good data record } // end of main //=================================================================// int read_init (char file[]) { FILE *fpin; int i, ndata, nrecords=0, eof=0, true=1, badrec = 0, hh,mm,ss; unsigned short int data[384]; // 12-bit ADC values, packed in 16 bits short int c1, c2; float dectime; // decimal time: 23:30:00 -> 23.50 char date[24] = "Fri Feb 00 00:00:00 2003"; if (( fpin = fopen( file, "r" )) == NULL ) { fprintf(stderr, "Error opening input file %s.\n", file); exit(1); } while (true) { // loop over all records for (i=0; i<24; i++){ // unpack the date into string date[24] date[i] = getc(fpin); } date[24] = '\0'; // terminate for C hh = (date[11] - '0')*10 + (date[12] - '0'); // hours, convert char to int mm = (date[14] - '0')*10 + (date[15] - '0'); // minutes ss = (date[17] - '0')*10 + (date[18] - '0'); // seconds dectime = hh + (mm + (float)ss/60.0)/60.0; // convert to fractional hours c1 = getc(fpin); c1 = getc(fpin); c1 = getc(fpin); // skip 3 dummy bytes for (ndata=0; ndata<384; ndata++) { // unpack 384 data words, 2 bytes each c1 = getc(fpin); c2 = getc(fpin); if (c1 != '\n' && c2 != '\n') { // check for EOL on bad records data[ndata] = c1*256 + c2; // save data in array //printf("%5d", data[ndata]); //if ((ndata+1)%16 == 0) {printf("\n");} //if ((ndata+1)%96 == 0) {printf("\n");} } else { badrec++; //printf("%4d Bad record seen, length %d \n",badrec, ndata); break; // jump out of 384 loop } // good/bad record } // end loop 384 data words for (i=0; i<10000; i++){ // If a bad record was seen, we have so skip forward c1 = getc(fpin); // to the start of the next good one. if (c1 == '\n') { // check for end-of-line break; // jump out of trailing bytes } //EOL seen if (c1 == EOF) { // check for end-of-file eof = 1; // save for later break; // jump out of trailing bytes } } // skip trailing bytes if (eof){break;} // EOF: also skip out of the main loop if (ndata == 384){ // good records get processed here: nrecords++; printf("Record %4d good at time %f. val(1,2,3...)= %d %d %d ...\n", nrecords, dectime, data[0], data[1], data[2]); } } // while loop over good/bad records fclose(fpin); // close input file, print summary: printf("\nFile %s closed: %d good records, about %d bad.\n", file, nrecords, badrec); } // end of read_temps.c //=====================================================================//