/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/hydrotrend/hydroswap.c

Go to the documentation of this file.
00001 /*
00002  *  HydroSwap.c
00003  *
00004  *  swab - Convert between big-endian and little-endian
00005  *  file formats. It creates a second binary file which
00006  *  simulair to the created output file of HYDROTREND but
00007  *  then in a swabbed order. So if you are running hydrotrend on a PC
00008  *  you can read the swabbed binary file on an Unix platform and
00009  *  visa versa.
00010  *
00011  *  Author:     A.J. Kettner    (October 2002) (February-April 2003)
00012  *
00013  *
00014  * Variable             Def.Location    Type    Units   Usage
00015  * --------             ------------    ----    -----   -----
00016  * comLen               HydroSwap.c     int     -       length of the title string
00017  * err                  various         int     -       error flag, halts program
00018  * i                    various         int     -       temporary loop counter
00019  * nYears               HydroSwap.c     int     -       total # of output years
00020  * recperyear           HydroSwap.c     int     -       # of output records per year
00021  * start                HydroSwap.c     int     -       Start for filepointer after reading part of Header
00022  * word                 HydroSwap.c     int     -       Number of bytes of the input file
00023  * dataWord             HydroSwap.c     char    -       Holding byte info of each byte in a lope
00024  *
00025  */
00026 
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include "hydroinout.h"
00031 #include "hydroparams.h"
00032 #include "hydroclimate.h"
00033 #include "hydrotimeser.h"
00034 #include "hydroalloc_mem.h"
00035 
00036 #define DEFAULT_WORD 4
00037 
00038 /*----------------------
00039  *  Start main program
00040  *----------------------*/
00041 int hydroswap()
00042 {
00043 
00044 /*-------------------
00045  *  Local Variables
00046  *-------------------*/
00047 int i, err, verbose, comLen, nYears,p;
00048 int word;
00049 int start;
00050 static int recperyear;
00051 char *dataWord;
00052 char dummystring[300];
00053 word = DEFAULT_WORD;
00054 err = 0;
00055 verbose = 0;
00056 
00057 /*------------------------
00058  *  Opening binary files
00059  *------------------------*/
00060 if (verbose) printf("Opening %s... \n",ffnamedistot);
00061 if ( (fiddistot = fopen(ffnamedistot,"rb")) == NULL) {
00062    fprintf(stderr, "  HydroSwap ERROR: Unable to open the discharge file %s \n",ffnamedistot);
00063    err = 1;
00064 }
00065 strcpy(ffnameconvdistot,startname);
00066 strcat(ffnameconvdistot,fnameconvdis);
00067 if (verbose) printf("Opening %s... \n",ffnameconvdistot);
00068 if ( (fidconvdistot = fopen(ffnameconvdistot,"wb")) == NULL) {
00069    fprintf(stderr, "  HydroSwap ERROR: Unable to open the convdischarge file %s \n",ffnameconvdistot);
00070    err = 1;
00071 }
00072 
00073 if ( outletmodelflag == 1 ){
00074         fidconvdis = allocate_1d_F( maxnoutlet );       
00075         for (p=0; p<maxnoutlet; p++){
00076                 strcpy(ffnamedis,startname);
00077                 sprintf(dummystring,"%sOUTLET%d",ffnamedis,p+1);
00078                 strcpy(ffnamedis,dummystring);
00079                 strcat(ffnamedis,fnamedis);
00080                 if (verbose) printf("Opening %s... \n",ffnamedis);
00081                 if ( (fiddis[p] = fopen(ffnamedis,"rb")) == NULL) {
00082                         fprintf(stderr, "  HydroSwap ERROR: Unable to open the discharge file %s \n",ffnamedis);
00083                         err = 1;
00084                 }
00085                 strcpy(ffnameconvdis,startname);
00086                 sprintf(dummystring,"%sOUTLET%d",ffnameconvdis,p+1);
00087                 strcpy(ffnameconvdis,dummystring);
00088                 strcat(ffnameconvdis,fnameconvdis);
00089                 if (verbose) printf("Opening %s... \n",ffnameconvdis);
00090                 if ( (fidconvdis[p] = fopen(ffnameconvdis,"wb")) == NULL) {
00091                 fprintf(stderr, "  HydroSwap ERROR: Unable to open the convdischarge file %s \n",ffnameconvdis);
00092                 err = 1;
00093                 }
00094         }
00095 }
00096 
00097 /*-------------------
00098  *  Allocate memory
00099  *-------------------*/
00100    dataWord = (char*)malloc(sizeof(char)*word);
00101 
00102 /*------------------------------------
00103  *  Setting variables for the header
00104  *------------------------------------*/
00105    if(      timestep[0] == 'd' ) recperyear = 365;
00106    else if( timestep[0] == 'm' ) recperyear = 12;
00107    else if( timestep[0] == 's' ) recperyear = 4;
00108    else recperyear = 1;
00109 
00110    nYears       = syear[nepochs-1]+nyears[nepochs-1]-syear[0];
00111    comLen       = strlen(title[0])-1;
00112    start = (word) +comLen;
00113 
00114 /*--------------------------------------------
00115  *  Swapping parts of header + rest datafile
00116  *--------------------------------------------*/
00117 
00118                 fread(dataWord,1,word,fiddistot);
00119                 for (i=word-1;i>=0;i--)
00120                         fwrite(&dataWord[i],1,1,fidconvdistot);
00121                 fwrite(    title[0], sizeof(char), comLen, fidconvdistot);
00122                 fseek(fiddistot,start,SEEK_SET);
00123                 while ( fread(dataWord,1,word,fiddistot)==word )
00124                         for (i=word-1;i>=0;i--)
00125                                 fwrite(&dataWord[i],1,1,fidconvdistot);
00126 
00127         if (outletmodelflag == 1)
00128                 for (p=0; p<maxnoutlet; p++){
00129                         fread(dataWord,1,word,fiddis[p]);
00130                         for (i=word-1;i>=0;i--)
00131                                 fwrite(&dataWord[i],1,1,fidconvdis[p]);
00132                         fwrite(    title[0], sizeof(char), comLen, fidconvdis[p]);
00133                         fseek(fiddis[p],start,SEEK_SET);
00134                         while ( fread(dataWord,1,word,fiddis[p])==word )
00135                                 for (i=word-1;i>=0;i--)
00136                                         fwrite(&dataWord[i],1,1,fidconvdis[p]);
00137                 }
00138         
00139 /*-------------------
00140  *  Close files
00141  *------------------*/
00142         fclose(fiddistot);
00143         fclose(fidconvdistot);
00144         if (outletmodelflag == 1)
00145                 for (p=0; p<maxnoutlet; p++){
00146                         fclose(fiddis[p]);
00147                         fclose(fidconvdis[p]);
00148                 }
00149         return(err);
00150 }  /* end of HydroSwap */
00151 

Generated on Fri Jan 4 18:04:14 2008 for sedflux by  doxygen 1.5.2