| 1 | #include <stdio.h>
|
|---|
| 2 | #include <stdlib.h>
|
|---|
| 3 | #include <string.h>
|
|---|
| 4 | #include <ctype.h>
|
|---|
| 5 | #include <float.h>
|
|---|
| 6 | #include <math.h>
|
|---|
| 7 | #include <time.h>
|
|---|
| 8 | #include <sys/stat.h>
|
|---|
| 9 | #include <sys/types.h>
|
|---|
| 10 |
|
|---|
| 11 | #ifndef FNAME_H
|
|---|
| 12 | #define FNAME_H
|
|---|
| 13 |
|
|---|
| 14 | /*
|
|---|
| 15 | FORTRAN naming convention
|
|---|
| 16 | default cpgs_setup, etc.
|
|---|
| 17 | -DUPCASE CPGS_SETUP, etc.
|
|---|
| 18 | -DUNDERSCORE cpgs_setup_, etc.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #ifdef UPCASE
|
|---|
| 22 | # define FORTRAN_NAME(low,up) up
|
|---|
| 23 | #else
|
|---|
| 24 | #ifdef UNDERSCORE
|
|---|
| 25 | # define FORTRAN_NAME(low,up) low##_
|
|---|
| 26 | #else
|
|---|
| 27 | # define FORTRAN_NAME(low,up) low
|
|---|
| 28 | #endif
|
|---|
| 29 | #endif
|
|---|
| 30 |
|
|---|
| 31 | #endif
|
|---|
| 32 |
|
|---|
| 33 | #define byte_reverse FORTRAN_NAME(byte_reverse, BYTE_REVERSE )
|
|---|
| 34 | #define byte_reverse8 FORTRAN_NAME(byte_reverse8, BYTE_REVERSE8 )
|
|---|
| 35 | #define byte_open FORTRAN_NAME(byte_open, BYTE_OPEN )
|
|---|
| 36 | #define byte_close FORTRAN_NAME(byte_close, BYTE_CLOSE )
|
|---|
| 37 | #define byte_rewind FORTRAN_NAME(byte_rewind, BYTE_REWIND )
|
|---|
| 38 | #define byte_read FORTRAN_NAME(byte_read, BYTE_READ )
|
|---|
| 39 | #define byte_write FORTRAN_NAME(byte_write, BYTE_WRITE )
|
|---|
| 40 | #define get_bytesw_write FORTRAN_NAME(get_bytesw_write, GET_BYTESW_WRITE)
|
|---|
| 41 | #define set_bytesw_write FORTRAN_NAME(set_bytesw_write, SET_BYTESW_WRITE)
|
|---|
| 42 |
|
|---|
| 43 | #define READ 1
|
|---|
| 44 | #define WRITE 2
|
|---|
| 45 | #define MAX_NAME 132
|
|---|
| 46 |
|
|---|
| 47 | #define SWAP(a,b) temp=(a); (a)=(b); (b)=temp;
|
|---|
| 48 |
|
|---|
| 49 | static FILE *fp=NULL;
|
|---|
| 50 | static int flag=0;
|
|---|
| 51 | static char name[MAX_NAME+1];
|
|---|
| 52 |
|
|---|
| 53 | int bytesw_write=0;
|
|---|
| 54 | int bytesw_read=0;
|
|---|
| 55 |
|
|---|
| 56 | /*************************************byte.c***********************************/
|
|---|
| 57 |
|
|---|
| 58 | #ifdef UNDERSCORE
|
|---|
| 59 | void exitt_();
|
|---|
| 60 | #else
|
|---|
| 61 | void exitt();
|
|---|
| 62 | #endif
|
|---|
| 63 |
|
|---|
| 64 | void byte_reverse(float *buf, int *nn,int *ierr)
|
|---|
| 65 | {
|
|---|
| 66 | int n;
|
|---|
| 67 | char temp, *ptr;
|
|---|
| 68 |
|
|---|
| 69 | if (*nn<0)
|
|---|
| 70 | {
|
|---|
| 71 | printf("byte_reverse() :: n must be positive\n");
|
|---|
| 72 | *ierr=1;
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | for (ptr=(char *)buf,n=*nn; n--; ptr+=4)
|
|---|
| 77 | {
|
|---|
| 78 | SWAP(ptr[0],ptr[3])
|
|---|
| 79 | SWAP(ptr[1],ptr[2])
|
|---|
| 80 | }
|
|---|
| 81 | *ierr=0;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void byte_reverse8(float *buf, int *nn,int *ierr)
|
|---|
| 85 | {
|
|---|
| 86 | int n;
|
|---|
| 87 | char temp, *ptr;
|
|---|
| 88 |
|
|---|
| 89 | if (*nn<0)
|
|---|
| 90 | {
|
|---|
| 91 | printf("byte_reverse8() :: n must be positive\n");
|
|---|
| 92 | *ierr=1;
|
|---|
| 93 | return;
|
|---|
| 94 | }
|
|---|
| 95 | if(*nn % 2 != 0)
|
|---|
| 96 | {
|
|---|
| 97 | printf("byte_reverse8() :: n must be multiple of 2\n");
|
|---|
| 98 | *ierr=1;
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | for (ptr=(char *)buf,n=*nn,n=n+2; n-=2; ptr+=8)
|
|---|
| 103 | {
|
|---|
| 104 | SWAP(ptr[0],ptr[7])
|
|---|
| 105 | SWAP(ptr[1],ptr[6])
|
|---|
| 106 | SWAP(ptr[2],ptr[5])
|
|---|
| 107 | SWAP(ptr[3],ptr[4])
|
|---|
| 108 | }
|
|---|
| 109 | *ierr=0;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | void byte_open(char *n,int *ierr,int nlen)
|
|---|
| 114 | {
|
|---|
| 115 | int i,len,istat;
|
|---|
| 116 | char slash;
|
|---|
| 117 | char dirname[MAX_NAME+1];
|
|---|
| 118 |
|
|---|
| 119 | if (nlen>MAX_NAME)
|
|---|
| 120 | {
|
|---|
| 121 | printf("byte_open() :: invalid string length\n");
|
|---|
| 122 | *ierr=1;
|
|---|
| 123 | return;
|
|---|
| 124 | }
|
|---|
| 125 | strncpy(name,n,nlen);
|
|---|
| 126 | for (i=nlen-1; i>0; i--) if (name[i] != ' ') break;
|
|---|
| 127 | name[i+1] = '\0';
|
|---|
| 128 |
|
|---|
| 129 | for (i=nlen-1; i>0; i--) if (name[i] == '/') break;
|
|---|
| 130 | if (i>0) {
|
|---|
| 131 | strncpy(dirname,name,i);
|
|---|
| 132 | dirname[i] = '\0';
|
|---|
| 133 | istat = mkdir(dirname,0755);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | *ierr=0;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | void byte_close(int *ierr)
|
|---|
| 140 | {
|
|---|
| 141 | if (!fp) return;
|
|---|
| 142 |
|
|---|
| 143 | if (fclose(fp))
|
|---|
| 144 | {
|
|---|
| 145 | printf("byte_close() :: couldn't fclose file!\n");
|
|---|
| 146 | *ierr=1;
|
|---|
| 147 | return;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | fp=NULL;
|
|---|
| 151 | *ierr=0;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | void byte_rewind()
|
|---|
| 155 | {
|
|---|
| 156 | if (!fp) return;
|
|---|
| 157 |
|
|---|
| 158 | rewind(fp);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | void byte_write(float *buf, int *n,int *ierr)
|
|---|
| 163 | {
|
|---|
| 164 | int flags;
|
|---|
| 165 | mode_t mode;
|
|---|
| 166 |
|
|---|
| 167 | if (*n<0)
|
|---|
| 168 | {
|
|---|
| 169 | printf("byte_write() :: n must be positive\n");
|
|---|
| 170 | *ierr=1;
|
|---|
| 171 | return;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | if (!fp)
|
|---|
| 175 | {
|
|---|
| 176 | if (!(fp=fopen(name,"wb")))
|
|---|
| 177 | {
|
|---|
| 178 | printf("byte_write() :: fopen failure!\n");
|
|---|
| 179 | *ierr=1;
|
|---|
| 180 | return;
|
|---|
| 181 | }
|
|---|
| 182 | flag=WRITE;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | if (flag==WRITE)
|
|---|
| 186 | {
|
|---|
| 187 | if (bytesw_write == 1)
|
|---|
| 188 | byte_reverse (buf,n,ierr);
|
|---|
| 189 | fwrite(buf,sizeof(float),*n,fp);
|
|---|
| 190 | }
|
|---|
| 191 | else
|
|---|
| 192 | {
|
|---|
| 193 | printf("byte_write() :: can't fwrite after freading!\n");
|
|---|
| 194 | *ierr=1;
|
|---|
| 195 | return;
|
|---|
| 196 | }
|
|---|
| 197 | *ierr=0;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 | void byte_read(float *buf, int *n,int *ierr)
|
|---|
| 202 | {
|
|---|
| 203 | int flags;
|
|---|
| 204 | mode_t mode;
|
|---|
| 205 |
|
|---|
| 206 | if (*n<0)
|
|---|
| 207 | {printf("byte_read() :: n must be positive\n"); *ierr=1; return;}
|
|---|
| 208 |
|
|---|
| 209 | if (!fp)
|
|---|
| 210 | {
|
|---|
| 211 | if (!(fp=fopen(name,"rb")))
|
|---|
| 212 | {
|
|---|
| 213 | printf("%s\n",name);
|
|---|
| 214 | printf("byte_read() :: fopen failure2!\n");
|
|---|
| 215 | *ierr=1;
|
|---|
| 216 | return;
|
|---|
| 217 | }
|
|---|
| 218 | flag=READ;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | if (flag==READ)
|
|---|
| 222 | {
|
|---|
| 223 | if (bytesw_read == 1)
|
|---|
| 224 | byte_reverse (buf,n,ierr);
|
|---|
| 225 | fread(buf,sizeof(float),*n,fp);
|
|---|
| 226 | if (ferror(fp))
|
|---|
| 227 | {
|
|---|
| 228 | printf("ABORT: Error reading %s\n",name);
|
|---|
| 229 | *ierr=1;
|
|---|
| 230 | return;
|
|---|
| 231 | }
|
|---|
| 232 | else if (feof(fp))
|
|---|
| 233 | {
|
|---|
| 234 | printf("ABORT: EOF found while reading %s\n",name);
|
|---|
| 235 | *ierr=1;
|
|---|
| 236 | return;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | }
|
|---|
| 240 | else
|
|---|
| 241 | {
|
|---|
| 242 | printf("byte_read() :: can't fread after fwriting!\n");
|
|---|
| 243 | *ierr=1;
|
|---|
| 244 | return;
|
|---|
| 245 | }
|
|---|
| 246 | *ierr=0;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | void set_bytesw_write (int *pa)
|
|---|
| 250 | {
|
|---|
| 251 | if (*pa != 0)
|
|---|
| 252 | bytesw_write = 1;
|
|---|
| 253 | else
|
|---|
| 254 | bytesw_write = 0;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | void set_bytesw_read (int *pa)
|
|---|
| 258 | {
|
|---|
| 259 | if (*pa != 0)
|
|---|
| 260 | bytesw_read = 1;
|
|---|
| 261 | else
|
|---|
| 262 | bytesw_read = 0;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | void get_bytesw_write (int *pa)
|
|---|
| 266 | {
|
|---|
| 267 | *pa = bytesw_write;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | void get_bytesw_read (int *pa)
|
|---|
| 271 | {
|
|---|
| 272 | *pa = bytesw_read;
|
|---|
| 273 | }
|
|---|