CPLErr GMTRasterBand::IReadBlock( CPL_UNUSED int nBlockXOff, int nBlockYOff, void * pImage ) { size_t start[2], edge[2]; int nErr = NC_NOERR; int cdfid = ((GMTDataset *) poDS)->cdfid; CPLMutexHolderD(&hNCMutex); start[0] = nBlockYOff * nBlockXSize; edge[0] = nBlockXSize; if( eDataType == GDT_Byte ) nErr = nc_get_vara_uchar( cdfid, nZId, start, edge, (unsigned char *) pImage ); else if( eDataType == GDT_Int16 ) nErr = nc_get_vara_short( cdfid, nZId, start, edge, (short int *) pImage ); else if( eDataType == GDT_Int32 ) { if( sizeof(long) == 4 ) nErr = nc_get_vara_long( cdfid, nZId, start, edge, (long *) pImage ); else nErr = nc_get_vara_int( cdfid, nZId, start, edge, (int *) pImage ); } else if( eDataType == GDT_Float32 ) nErr = nc_get_vara_float( cdfid, nZId, start, edge, (float *) pImage ); else if( eDataType == GDT_Float64 ) nErr = nc_get_vara_double( cdfid, nZId, start, edge, (double *) pImage ); if( nErr != NC_NOERR ) { CPLError( CE_Failure, CPLE_AppDefined, "GMT scanline fetch failed: %s", nc_strerror( nErr ) ); return CE_Failure; } else return CE_None; }
/* This function use the first dimension to parallel and reads and writes all needed data only once, there is no iteration. Stride is not supported */ int extract_unary_1D_all(int mpi_rank,int mpi_size, int ncid,int vlid,int ncidout,int vlidout,int ndims,nc_type vtype,size_t *shape,size_t *begins,size_t *ends,size_t preLen,size_t *outLen){ int i,res; size_t *start=(size_t*)malloc(sizeof(size_t)*ndims); //start position for reading element from input file size_t *countOut=(size_t *)malloc(sizeof(size_t)*ndims); size_t *startOut=(size_t*)malloc(sizeof(size_t)*ndims); //start position for writing element to output file size_t *shapeOut=(size_t*)malloc(sizeof(size_t)*ndims); //output dimension shape size_t startOut0; size_t countOut0; int len0=1; int lenOut=1; for(i=0;i<ndims;++i){ /* shapeOut[0]=(ends[0]-begins[0])/strides[0]+1;*/ shapeOut[i]=ends[i]-begins[i]+1; lenOut*=shapeOut[i]; if(i>0){ startOut[i]=0; countOut[i]=shapeOut[i]; start[i]=begins[i]; len0*=ends[i]-begins[i]+1; } } if(outLen!=NULL) *outLen=lenOut; if(shapeOut[0]>=mpi_size){ startOut0=mpi_rank*(shapeOut[0]/mpi_size); if(mpi_rank!=mpi_size-1){ countOut0=(shapeOut[0]/mpi_size); }else{ countOut0=shapeOut[0]-startOut0; } }else{ if(mpi_rank<shapeOut[0]){ startOut0=mpi_rank; countOut0=1; }else{ return 0; } } int dataEnd=countOut0*len0; printf("mpi_rank %d,countOut0 %d\n",mpi_rank,countOut0); startOut[0]=startOut0+preLen/len0; countOut[0]=countOut0; /* start[0]=begins[0]+startOut0*strides[0];*/ start[0]=begins[0]+startOut0; switch(vtype){ case NC_BYTE: { unsigned char* dataOut=(unsigned char *)malloc(sizeof(unsigned char)*dataEnd); if((res=nc_get_vara_uchar(ncid,vlid,start,countOut,dataOut))) BAIL(res); if((res=nc_put_vara_uchar(ncidout,vlidout,startOut,countOut,(unsigned char *)dataOut))) BAIL(res); free(dataOut); } break; case NC_CHAR: { char* dataOut=(char *)malloc(sizeof(char)*dataEnd); if((res=nc_get_vara_schar(ncid,vlid,start,countOut,(signed char *)dataOut))) BAIL(res); if((res=nc_put_vara_schar(ncidout,vlidout,startOut,countOut,(signed char *)dataOut))) BAIL(res); free(dataOut); } break; case NC_SHORT: { short *dataOut=(short *)malloc(sizeof(short)*dataEnd); if((res=nc_get_vara_short(ncid,vlid,start,countOut,(short *)dataOut))) BAIL(res); if((res=nc_put_vara_short(ncidout,vlidout,startOut,countOut,(short *)dataOut))) BAIL(res); free(dataOut); } break; case NC_INT: { int * dataOut=(int *)malloc(sizeof(int)*dataEnd); if((res=nc_get_vara_int(ncid,vlid,start,countOut,(int *)dataOut))) BAIL(res); if((res=nc_put_vara_int(ncidout,vlidout,startOut,countOut,(int *)dataOut))) BAIL(res); free(dataOut); } break; case NC_FLOAT: { float * dataOut=(float *)malloc(sizeof(float)*dataEnd); if((res=nc_get_vara_float(ncid,vlid,start,countOut,(float *)dataOut))) BAIL(res); if((res=nc_put_vara_float(ncidout,vlidout,startOut,countOut,(float *)dataOut))) BAIL(res); free(dataOut); } break; case NC_DOUBLE: { double* dataOut=(double *)malloc(sizeof(double)*dataEnd); if((res=nc_get_vara_double(ncid,vlid,start,countOut,dataOut))) BAIL(res); if((res=nc_put_vara_double(ncidout,vlidout,startOut,countOut,(double *)dataOut))) BAIL(res); free(dataOut); } break; default: printf("Unknown data type\n"); } free(start); free(startOut); free(shapeOut); free(countOut); return 0; }
int extract_unary_1D(int mpi_rank,int mpi_size, int ncid,int vlid,int ncidout,int vlidout,int ndims,nc_type vtype,size_t *shape,size_t *begins,size_t *ends, ptrdiff_t *strides,size_t preLen,size_t *outLen){ int i,j,res; size_t *divider=(size_t *)malloc(sizeof(size_t)*ndims); //input divider size_t *dividerOut=(size_t *)malloc(sizeof(size_t)*ndims); // output divider size_t *start=(size_t*)malloc(sizeof(size_t)*ndims); //start position for reading element from input file size_t *count=(size_t*)malloc(sizeof(size_t)*ndims); size_t *countOut=(size_t *)malloc(sizeof(size_t)*ndims); size_t *startOut=(size_t*)malloc(sizeof(size_t)*ndims); //start position for writing element to output file size_t *shapeOut=(size_t*)malloc(sizeof(size_t)*ndims); //output dimension shape size_t startOut0; size_t countOut0; int len0=1; int lenOut=1; for(i=0;i<ndims;++i){ shapeOut[i]=(ends[i]-begins[i])/strides[i]+1; lenOut*=shapeOut[i]; if(i>0){ startOut[i]=0; countOut[i]=shapeOut[i]; start[i]=begins[i]; count[i]=ends[i]-begins[i]+1; len0*=ends[i]-begins[i]+1; } } if(outLen!=NULL) *outLen=lenOut; if(shapeOut[0]>=mpi_size){ startOut0=mpi_rank*(shapeOut[0]/mpi_size); if(mpi_rank!=mpi_size-1){ countOut0=(shapeOut[0]/mpi_size); }else{ countOut0=shapeOut[0]-startOut0; } }else{ if(mpi_rank<shapeOut[0]){ startOut0=mpi_rank; countOut0=1; }else{ return 0; } } int dataEnd=lenOut/shapeOut[0]; void* data=(void*)malloc(sizeof(double)*len0); void* dataOut=(void*)malloc(sizeof(double)*dataEnd); size_t* poses=(size_t*)malloc(sizeof(size_t)*dataEnd); getDivider(ndims,count,divider); getDivider(ndims,countOut,dividerOut); transfer_pos(poses,ndims-1,strides,dataEnd,dividerOut,divider); printf("mpi_rank %d,countOut0 %d\n",mpi_rank,countOut0); for(i=0;i<countOut0;++i){ startOut[0]=startOut0+i+preLen/len0; countOut[0]=1; start[0]=begins[0]+(startOut0+i)*strides[0]; count[0]=1; switch(vtype){ case NC_BYTE: if((res=nc_get_vara_uchar(ncid,vlid,start,count,(unsigned char *)data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_uchar(ncidout,vlidout,startOut,countOut,(unsigned char *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((unsigned char *)dataOut)[j]=((unsigned char *)data)[poses[j]]; } if((res=nc_put_vara_uchar(ncidout,vlidout,startOut,countOut,(unsigned char *)dataOut))) BAIL(res); } break; case NC_CHAR: if((res=nc_get_vara_schar(ncid,vlid,start,count,(signed char *)data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_schar(ncidout,vlidout,startOut,countOut,(signed char *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((signed char *)dataOut)[j]=((signed char *)data)[poses[j]]; } if((res=nc_put_vara_schar(ncidout,vlidout,startOut,countOut,(signed char *)dataOut))) BAIL(res); } break; case NC_SHORT: if((res=nc_get_vara_short(ncid,vlid,start,count,data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_short(ncidout,vlidout,startOut,countOut,(short *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((short *)dataOut)[j]=((short *)data)[poses[j]]; } if((res=nc_put_vara_short(ncidout,vlidout,startOut,countOut,(short *)dataOut))) BAIL(res); } break; case NC_INT: if((res=nc_get_vara_int(ncid,vlid,start,count,(int *)data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_int(ncidout,vlidout,startOut,countOut,(int *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((int *)dataOut)[j]=((int *)data)[poses[j]]; } if((res=nc_put_vara_int(ncidout,vlidout,startOut,countOut,(int *)dataOut))) BAIL(res); } break; case NC_FLOAT: if((res=nc_get_vara_float(ncid,vlid,start,count,data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_float(ncidout,vlidout,startOut,countOut,(float *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((float *)dataOut)[j]=((float *)data)[poses[j]]; } if((res=nc_put_vara_float(ncidout,vlidout,startOut,countOut,(float *)dataOut))) BAIL(res); } break; case NC_DOUBLE: if((res=nc_get_vara_double(ncid,vlid,start,count,(double *)data))) BAIL(res); if(len0==dataEnd){ if((res=nc_put_vara_double(ncidout,vlidout,startOut,countOut,(double *)data))) BAIL(res); }else{ for(j=0;j<dataEnd;++j){ ((double *)dataOut)[j]=((double *)data)[poses[j]]; } if((res=nc_put_vara_double(ncidout,vlidout,startOut,countOut,(double *)dataOut))) BAIL(res); } break; default: printf("Unknown data type\n"); } } /*free resourses*/ free(divider); free(dividerOut); free(start); free(startOut); free(shapeOut); free(data); free(dataOut); free(poses); return 0; }
int main(int argc, char *argv[]) { /* IDs, names, and parameters for the var[asm1] functions. */ int ncid, varid[NUM_TYPES], attid, dimid; char varname[MAX_VARNAME]; size_t index1[NUM_DIMS], start[NUM_DIMS], offset[NUM_DIMS]; size_t count[NUM_DIMS], imap[NUM_DIMS]; ptrdiff_t stride[NUM_DIMS]; /* Phoney data we will write. */ unsigned char ubyte_data_out[] = {0,1,2,3,4}; unsigned short ushort_data_out[] = {0,11,22,33,44}; unsigned int uint_data_out[] = {0,111,222,333,444}; nc_int64 int64_data_out[] = {0,-111111111,2222222222,-3333333333,444444444}; nc_uint64 uint64_data_out[] = {0,111111111,2222222222,33333333,44444444}; unsigned char bool_data_out[] = {0,1,0,1,0}; /* We will read back in the phoney data with these. */ unsigned char ubyte_data_in[SIZE]; unsigned short ushort_data_in[SIZE]; unsigned int uint_data_in[SIZE]; nc_int64 int64_data_in[SIZE]; nc_uint64 uint64_data_in[SIZE]; unsigned char bool_data_in[SIZE]; int i; int type, num_errors = 0, res = NC_NOERR; int errors = 0, total_errors = 0; #ifdef USE_PARALLEL MPI_Init(&argc, &argv); #endif /* Uncomment the following line to get verbose feedback. */ /*nc_set_log_level(2);*/ printf("\n\n*** Testing netCDF-4 new atomic types...\n"); /* Open a netcdf-4 file, and one dimension. */ if ((res = nc_create(FILENAME, NC_NETCDF4, &ncid))) BAIL(res); if ((res = nc_def_dim(ncid, "dim1", SIZE, &dimid))) BAIL(res); /* Create vars of the new types. Take advantage of the fact that * new types are numbered from NC_UBYTE (7) through NC_BOOL (12).*/ for(type = 0; type < NUM_TYPES; type++) { /* Create a var... */ sprintf(varname, "var_%d", type); printf("*** creating var %s, type: %d...\t\t", varname, type+NC_UBYTE); if ((res = nc_def_var(ncid, varname, type+NC_UBYTE, 1, &dimid, &varid[type]))) BAIL(res); printf("ok!\n"); } /* Test the varm functions. */ printf("*** testing varm functions...\t\t\t"); /* CLEAN_INPUT_BUFFERS; errors = 0; start[0] = 0; count[0] = 1; stride[0] = 1; imap[0] = 0; if ((res = nc_put_varm_ubyte(ncid, varid[0], start, count, stride, imap, ubyte_data_out))) BAIL(res); if ((res = nc_get_varm_ubyte(ncid, varid[0], start, count, stride, imap, ubyte_data_in))) BAIL(res); for (i=0; i<STRIDE_SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_varm_ushort(ncid, varid[1], start, count, stride, imap, ushort_data_out))) BAIL(res); if ((res = nc_get_varm_ushort(ncid, varid[1], start, count, stride, imap, ushort_data_in))) BAIL(res); for (i=0; i<STRIDE_SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_varm_uint(ncid, varid[2], start, count, stride, imap, uint_data_out))) BAIL(res); if ((res = nc_get_varm_uint(ncid, varid[2], start, count, stride, imap, uint_data_in))) BAIL(res); for (i=0; i<STRIDE_SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_varm_int64(ncid, varid[3], start, count, stride, imap, int64_data_out))) BAIL(res); if ((res = nc_get_varm_int64(ncid, varid[3], start, count, stride, imap, int64_data_in))) BAIL(res); for (i=0; i<STRIDE_SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_varm_uint64(ncid, varid[4], start, count, stride, imap, uint64_data_out))) BAIL(res); if ((res = nc_get_varm_uint64(ncid, varid[4], start, count, stride, imap, uint64_data_in))) BAIL(res); for (i=0; i<STRIDE_SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_varm_bool(ncid, varid[5], start, count, stride, imap, bool_data_out))) BAIL(res); if ((res = nc_get_varm_bool(ncid, varid[5], start, count, stride, imap, bool_data_in))) BAIL(res); for (i=0; i<STRIDE_SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; total_errors += errors; if (errors) printf("*** ERROR!! - %d errors. Sorry!\n"); else printf("ok!\n"); */ /* Test the vars functions. */ printf("*** testing vars functions...\t\t\t"); CLEAN_INPUT_BUFFERS; errors = 0; start[0] = 0; count[0] = 2; stride[0] = STRIDE_SIZE; if ((res = nc_put_vars_uchar(ncid, varid[0], start, count, stride, ubyte_data_out))) BAIL(res); if ((res = nc_get_vars_uchar(ncid, varid[0], start, count, stride, ubyte_data_in))) BAIL(res); if (ubyte_data_in[0] != ubyte_data_out[0]) ERR; if (ubyte_data_in[1] != ubyte_data_out[STRIDE_SIZE]) ERR; if ((res = nc_put_vars_ushort(ncid, varid[1], start, count, stride, ushort_data_out))) BAIL(res); if ((res = nc_get_vars_ushort(ncid, varid[1], start, count, stride, ushort_data_in))) BAIL(res); for (i=0; i<2; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_vars_uint(ncid, varid[2], start, count, stride, uint_data_out))) BAIL(res); if ((res = nc_get_vars_uint(ncid, varid[2], start, count, stride, uint_data_in))) BAIL(res); for (i=0; i<2; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_vars_int64(ncid, varid[3], start, count, stride, int64_data_out))) BAIL(res); if ((res = nc_get_vars_int64(ncid, varid[3], start, count, stride, int64_data_in))) BAIL(res); for (i=0; i<2; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_vars_uint64(ncid, varid[4], start, count, stride, uint64_data_out))) BAIL(res); if ((res = nc_get_vars_uint64(ncid, varid[4], start, count, stride, uint64_data_in))) BAIL(res); for (i=0; i<2; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_vars_bool(ncid, varid[5], start, count, stride, bool_data_out))) BAIL(res); if ((res = nc_get_vars_bool(ncid, varid[5], start, count, stride, bool_data_in))) BAIL(res); for (i=0; i<2; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; total_errors += errors; if (errors) printf("*** ERROR!! - %d errors. Sorry!\n"); else printf("ok!\n"); /* Test the vara functions. */ printf("*** testing vara functions...\t\t\t"); CLEAN_INPUT_BUFFERS; errors = 0; start[0] = 0; count[0] = SIZE; if ((res = nc_put_vara_uchar(ncid, varid[0], start, count, ubyte_data_out))) BAIL(res); if ((res = nc_get_vara_uchar(ncid, varid[0], start, count, ubyte_data_in))) BAIL(res); for (i=0; i<SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_vara_ushort(ncid, varid[1], start, count, ushort_data_out))) BAIL(res); if ((res = nc_get_vara_ushort(ncid, varid[1], start, count, ushort_data_in))) BAIL(res); for (i=0; i<SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_vara_uint(ncid, varid[2], start, count, uint_data_out))) BAIL(res); if ((res = nc_get_vara_uint(ncid, varid[2], start, count, uint_data_in))) BAIL(res); for (i=0; i<SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_vara_int64(ncid, varid[3], start, count, int64_data_out))) BAIL(res); if ((res = nc_get_vara_int64(ncid, varid[3], start, count, int64_data_in))) BAIL(res); for (i=0; i<SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_vara_uint64(ncid, varid[4], start, count, uint64_data_out))) BAIL(res); if ((res = nc_get_vara_uint64(ncid, varid[4], start, count, uint64_data_in))) BAIL(res); for (i=0; i<SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; if ((res = nc_put_vara_bool(ncid, varid[5], start, count, bool_data_out))) BAIL(res); if ((res = nc_get_vara_bool(ncid, varid[5], start, count, bool_data_in))) BAIL(res); for (i=0; i<SIZE; i++) if (ubyte_data_in[i] != ubyte_data_out[i]) ERR; total_errors += errors; if (errors) printf("*** ERROR!! - %d errors. Sorry!\n"); else printf("ok!\n"); /* Test the var1 functions. */ printf("*** testing var1 functions...\t\t\t"); CLEAN_INPUT_BUFFERS; errors = 0; index1[0] = 0; if ((res = nc_put_var1_uchar(ncid, varid[0], index1, uchar_data_out))) BAIL(res); if ((res = nc_get_var1_uchar(ncid, varid[0], index1, uchar_data_in))) BAIL(res); if (uchar_data_in[0] != uchar_data_out[0]) ERR; if ((res = nc_put_var1_ushort(ncid, varid[1], index1, ushort_data_out))) BAIL(res); if ((res = nc_get_var1_ushort(ncid, varid[1], index1, ushort_data_in))) BAIL(res); if (ushort_data_in[0] != ushort_data_out[0]) ERR; if ((res = nc_put_var1_uint(ncid, varid[2], index1, uint_data_out))) BAIL(res); if ((res = nc_get_var1_uint(ncid, varid[2], index1, uint_data_in))) BAIL(res); if (uint_data_in[0] != uint_data_out[0]) ERR; if ((res = nc_put_var1_int64(ncid, varid[3], index1, int64_data_out))) BAIL(res); if ((res = nc_get_var1_int64(ncid, varid[3], index1, int64_data_in))) BAIL(res); if (int64_data_in[0] != int64_data_out[0]) ERR; if ((res = nc_put_var1_uint64(ncid, varid[4], index1, uint64_data_out))) BAIL(res); if ((res = nc_get_var1_uint64(ncid, varid[4], index1, uint64_data_in))) BAIL(res); if (uint64_data_in[0] != uint64_data_out[0]) ERR; if ((res = nc_put_var1_bool(ncid, varid[5], index1, bool_data_out))) BAIL(res); if ((res = nc_get_var1_bool(ncid, varid[5], index1, bool_data_in))) BAIL(res); if (bool_data_in[0] != bool_data_out[0]) ERR; total_errors += errors; if (errors) printf("*** ERROR!! - %d errors. Sorry!\n"); else printf("ok!\n"); if (total_errors) printf(" *** %d total errors\n", errors); else printf(" *** success!\n"); #ifdef USE_PARALLEL MPI_Finalize(); #endif return 2 ? errors : 0; }