/* ************************************************************* * * * WOODS HOLE OCEANOGRAPHIC INSTITUTION * * UPPER OCEAN PROCESSES GROUP * * PHYSICAL OCEANOGRAPHY DEPT. * * WOODS HOLE, MASSACHUSETTS USA 02543 * * * * File: vswrasc.c Model: - * * * * Function: ASIMET SWR binary data file to ASCII converter * * * * Project: ASIMET * * * Programmer: G.A. * * * * Copyright (c) 2002 Woods Hole Oceanographic Institution * * * ************************************************************* */ /* Additional reserved words not recognized by Source Print. uchar */ /* * Program to read the processed binary output file of the VOS C51 logger * and convert to ASCII - run vswrswab first. * * Usage: vswrasc datafile outputfile * datafile - binary data file name * outputfile - ascii data file name The ASCII output file is formatted with 1 hour's records per line, comma-separated value, as follows: hour, minute, second, day, day-of-week, month, 4 digit year, 60 float values of SWR, "used" value (0xA5A5 = 42405) CRC (unused; always zero) crlf **************************************************************** */ /* this is the SWR data record structure, 256 bytes */ // struct SWR_record // { // struct time_type time1; /* 8 bytes of time */ // float swr_cal[60]; /* 60 minutes of SWR data */ // unsigned char unused[4]; // unsigned short used; /* set to 0xA5A5 upon record write */ // unsigned short swr_CRC; /* CRC of previous 254 bytes */ // }; #include #include #include #include /* this is the usage string */ char usage[] = "\nUsage: vswrasc datafile outputfile\n datafile - binary data file name\n outputfile - ascii data file name\n\n"; /* main program */ int main(int argc,char *argv[]) { FILE *fd1,*fd2; char outbuf[100]; char buffer; short int intbuf; unsigned short used; long lbuf; float fvar; short int ret; short int j; struct tm *tptr; // printf("Program vswrasc - if you have garbled output, try running"); // printf(" the file through program vswrswab first to swap the bytes\n\n"); if (argc < 3) { printf("%s",usage); exit(-1); } /* input file name */ fd1 = fopen(argv[1],"rb"); if ( fd1 == NULL ) { printf("\nUnable to open input file %s\n",argv[1]); printf("%s",usage); exit(-1); } /* output file name */ fd2 = fopen(argv[2],"w"); if ( fd2 == NULL ) { printf("\nUnable to open output file %s\n",argv[2]); printf("%s",usage); exit(-1); } /* stay here until EOF, error, or last record aborts */ while (1) { /* first are 8 time bytes, first six are char */ for (j = 0; j < 6; j++) { /* read the byte, put a byte */ ret = fread(&buffer,sizeof(char),1,fd1); if ( ret != 1 ) goto end; /* transfer it to ASCII file */ fprintf(fd2,"%.2u,",(unsigned int)buffer); } /* next read the 2 "year" bytes */ ret = fread(&intbuf,sizeof(short),1,fd1); if ( ret != 1 ) goto end; /* transfer it to ASCII file */ fprintf(fd2,"%.2u,",intbuf); /* next read 60 minutes of swr data */ for ( j = 0; j < 60; j++ ) { /* read a value */ ret = fread(&fvar,sizeof(float),1,fd1); if ( ret != 1 ) goto end; /* transfer it to ASCII file */ fprintf(fd2,"%7.3f,",fvar); } /* next are 4 unused bytes for now - just skip */ for (j = 0; j < 4; j++) { /* read the byte, but discard */ ret = fread(&buffer,sizeof(char),1,fd1); if ( ret != 1 ) goto end; } /* next read the 2 "used" bytes - they should be A5A5 in a good record */ ret = fread(&used,sizeof(short),1,fd1); if ( ret != 1 ) goto end; /* transfer "used" to ASCII file */ fprintf(fd2,"%.2u,",used); /* next read the 2 CRC bytes - unused set to 0 for now */ ret = fread(&intbuf,sizeof(short),1,fd1); if ( ret != 1 ) goto end; /* transfer CRC to ASCII file */ fprintf(fd2,"%.2u",(unsigned short)intbuf); /* end each line with crlf */ fprintf(fd2,"\n"); /* was this a good record ? */ if (used != 0xA5A5) goto end; } /* end while */ end: printf("\n"); fclose(fd1); fclose(fd2); }