1 |
#include "stdlib.h" |
2 |
#include "stdio.h" |
3 |
#include "netcdf.h" |
4 |
#include "errno.h" |
5 |
#include "string.h" |
6 |
#include <sys/types.h> |
7 |
#include <sys/stat.h> |
8 |
#include <fcntl.h> |
9 |
|
10 |
|
11 |
/* gcc main.c -I/usr/include/netcdf-3 -L/usr/lib/netcdf-3 -lnetcdf */ |
12 |
|
13 |
main(int argc, char *argv[]) |
14 |
{ |
15 |
int ncfid; |
16 |
int rc; |
17 |
int tile_number, sNx, sNy, nSx, nSy, nPx, nPy, Nx, Ny, Nr, iLo, jLo; |
18 |
int vId, dId; |
19 |
int dimT, dimX, dimY, dimZ, dError; |
20 |
float *arrIn, *arrOut; |
21 |
char *fNam, *vNam,fNam1[1024],fNam2[1024]; |
22 |
int i, j, k, t, iOffIn, iOffOut, iOffFile; |
23 |
long iSeek; |
24 |
int ti, tj; |
25 |
int fid; |
26 |
|
27 |
int dbLev; |
28 |
|
29 |
dbLev=0; |
30 |
|
31 |
/* Open a tile file */ |
32 |
fNam=argv[1]; |
33 |
rc=nc_open( fNam, 0, &ncfid ); |
34 |
vNam = argv[2]; |
35 |
|
36 |
/* Get global attributes */ |
37 |
/* o tile number */ |
38 |
rc=nc_get_att_int( ncfid, NC_GLOBAL,"tile_number", &tile_number ); |
39 |
if( dbLev > 0 ) printf( "tile_number == %d\n", tile_number ); |
40 |
/* o sNx, sNy, nSx, nSy, nPx, nPy, Nx, Ny, Nr */ |
41 |
rc=nc_get_att_int(ncfid,NC_GLOBAL,"sNx",&sNx); |
42 |
if ( dbLev > 2 ) printf("sNx == %d\n",sNx); |
43 |
rc=nc_get_att_int(ncfid,NC_GLOBAL,"sNy",&sNy); |
44 |
if ( dbLev > 2 ) printf("sNy == %d\n",sNy); |
45 |
rc=nc_get_att_int(ncfid,NC_GLOBAL,"nSx",&nSx); |
46 |
if ( dbLev > 2 ) printf("nSx == %d\n",nSx); |
47 |
rc=nc_get_att_int(ncfid,NC_GLOBAL,"nSy",&nSy); |
48 |
if ( dbLev > 2 ) printf("nSy == %d\n",nSy); |
49 |
rc=nc_get_att_int(ncfid,NC_GLOBAL,"nPx",&nPx); |
50 |
if ( dbLev > 2 ) printf("nPx == %d\n",nPx); |
51 |
rc=nc_get_att_int(ncfid,NC_GLOBAL,"nPy",&nPy); |
52 |
if ( dbLev > 2 ) printf("nPy == %d\n",nPy); |
53 |
rc=nc_get_att_int(ncfid,NC_GLOBAL,"Nx",&Nx); |
54 |
if ( dbLev > 2 ) printf("Nx == %d\n",Nx); |
55 |
rc=nc_get_att_int(ncfid,NC_GLOBAL,"Ny",&Ny); |
56 |
if ( dbLev > 2 ) printf("Ny == %d\n",Ny); |
57 |
rc=nc_get_att_int(ncfid,NC_GLOBAL,"Nr",&Nr); |
58 |
if ( dbLev > 2 ) printf("Nr == %d\n",Nr); |
59 |
|
60 |
/* Check dimensions - for now these should match */ |
61 |
/* sNx, sNy, Nr and have T = 1 */ |
62 |
rc=nc_inq_dimid( ncfid, "X", &dId ); |
63 |
rc=nc_inq_dimlen( ncfid, dId, &dimX ); |
64 |
if ( dbLev > 2 ) printf( "dimX == %d\n", dimX ); |
65 |
rc=nc_inq_dimid( ncfid, "Y", &dId ); |
66 |
rc=nc_inq_dimlen( ncfid, dId, &dimY ); |
67 |
if( dbLev > 2 ) printf( "dimY == %d\n", dimY ); |
68 |
rc=nc_inq_dimid( ncfid, "Z", &dId ); |
69 |
rc=nc_inq_dimlen( ncfid, dId, &dimZ ); |
70 |
if( dbLev > 2 ) printf( "dimZ == %d\n", dimZ ); |
71 |
rc=nc_inq_dimid( ncfid, "T", &dId ); |
72 |
rc=nc_inq_dimlen( ncfid, dId, &dimT ); |
73 |
if( dbLev > 2 ) printf( "dimT == %d\n", dimT ); |
74 |
|
75 |
dError = 0; |
76 |
if( dimX != sNx ) {dError=1;}; |
77 |
if( dimY != sNy ) {dError=1;}; |
78 |
if( dimZ != Nr ) {dError=1;}; |
79 |
if( dimT != 1 ) {dError=1;}; |
80 |
if( dError != 0 ) { |
81 |
printf( |
82 |
"Error: Inconsistent dimension variable sizes and global variable attribute settings\n"); |
83 |
} |
84 |
|
85 |
|
86 |
/* Get a piece of the data */ |
87 |
arrIn = calloc( sNx*sNy*Nr, sizeof(*arrIn) ); |
88 |
arrOut = calloc( sNx*sNy*Nr, sizeof(*arrOut) ); |
89 |
rc = nc_inq_varid( ncfid, vNam, &vId ); |
90 |
rc = nc_get_var_float( ncfid, vId, arrIn ); |
91 |
/* Reorder from T Z Y X to X Y Z */ |
92 |
iOffIn=0; |
93 |
for( t=1; t<2; t++ ){ |
94 |
for( k=1; k<=Nr; k++ ){ |
95 |
for( j=1; j<=sNy; j++ ){ |
96 |
for( i=1; i<=sNx; i++ ){ |
97 |
iOffOut=(k-1)*sNx*sNy+(j-1)*sNx+i-1; |
98 |
arrOut[iOffOut]=arrIn[iOffIn]; |
99 |
iOffIn++; |
100 |
} |
101 |
} |
102 |
} |
103 |
} |
104 |
|
105 |
|
106 |
/* Put my piece in the global binary file */ |
107 |
/* tRank=(tj-1)*nSx+ti; */ |
108 |
/* ti=mod(tRank,nSx)+1; */ |
109 |
/* tj=tRank/nSx+1; */ |
110 |
ti=(tile_number-1)%(nSx*nPx)+1; |
111 |
iLo=(ti-1)*sNx+1; |
112 |
if( dbLev > 2 ) printf( "ti == %d\n", ti ); |
113 |
if( dbLev > 2 ) printf( "iLo == %d\n", iLo ); |
114 |
tj=(tile_number-1)/(nSx*nPx)+1; |
115 |
jLo=(tj-1)*sNy+1; |
116 |
if( dbLev > 2 ) printf( "tj == %d\n", tj ); |
117 |
if( dbLev > 2 ) printf( "jLo == %d\n", jLo ); |
118 |
/* Write data to global file one row at a time */ |
119 |
sscanf( fNam, "%[^.].%[^.].", fNam1, fNam2 ); |
120 |
sprintf( fNam1, "%s.%s.data", vNam, fNam2 ); |
121 |
printf( "Writing to %s\n", fNam1 ); |
122 |
fid=open( fNam1, O_CREAT | O_RDWR , S_IRWXU ); |
123 |
if( fid <= 0 ){ |
124 |
perror( "open error" ); |
125 |
exit( 0 ); |
126 |
} |
127 |
for( t=1; t<2; t++ ){ |
128 |
for( k=1; k<=Nr; k++ ){ |
129 |
for( j=1; j<=sNy; j++ ){ |
130 |
iOffOut=(k-1)*sNx*sNy+(j-1)*sNx; |
131 |
iOffFile=(k-1)*Nx*Ny+(jLo-1)*Nx+(j-1)*Nx+iLo-1; |
132 |
iSeek=iOffFile*sizeof(*arrOut); |
133 |
/* printf( "Seeking to %d\n", iSeek ); */ |
134 |
rc=lseek( fid, (off_t)iSeek, SEEK_SET ); |
135 |
if ( rc != iSeek ){ |
136 |
perror( "lseek error" ); |
137 |
printf( "rc = %d\n", rc ); |
138 |
exit( 0 ); |
139 |
} |
140 |
rc=write( fid, arrOut+iOffOut, sizeof(*arrOut)*sNx ); |
141 |
} |
142 |
} |
143 |
} |
144 |
close( fid ); |
145 |
|
146 |
} |