@@ -814,8 +814,12 @@ void cmor_reset_variable(int var_id)
814814 if (cmor_vars [var_id ].values != NULL ) {
815815 free (cmor_vars [var_id ].values );
816816 }
817+ if (cmor_vars [var_id ].chunking_dimensions != NULL ) {
818+ free (cmor_vars [var_id ].chunking_dimensions );
819+ }
817820
818821 cmor_vars [var_id ].values = NULL ;
822+ cmor_vars [var_id ].chunking_dimensions = NULL ;
819823 cmor_vars [var_id ].first_time = -999. ;
820824 cmor_vars [var_id ].last_time = -999. ;
821825 cmor_vars [var_id ].first_bound = 1.e20 ;
@@ -3654,8 +3658,7 @@ void cmor_define_dimensions(int var_id, int ncid,
36543658 int ncafid , double * time_bounds ,
36553659 int * nc_dim ,
36563660 int * nc_vars , int * nc_bnds_vars ,
3657- int * nc_vars_af ,
3658- size_t * nc_dim_chunking , int * dim_bnds ,
3661+ int * nc_vars_af , int * dim_bnds ,
36593662 int * zfactors , int * nc_zfactors ,
36603663 int * nc_dim_af , int * nzfactors )
36613664{
@@ -3698,15 +3701,6 @@ void cmor_define_dimensions(int var_id, int ncid,
36983701 if ((i == 0 ) && (cmor_axes [nAxisID ].axis == 'T' ))
36993702 j = NC_UNLIMITED ;
37003703
3701- if ((cmor_axes [nAxisID ].axis == 'X' )
3702- || (cmor_axes [nAxisID ].axis == 'Y' )) {
3703- nc_dim_chunking [i ] = j ;
3704- } else if (cmor_axes [nAxisID ].isgridaxis == 1 ) {
3705- nc_dim_chunking [i ] = j ;
3706- } else {
3707- nc_dim_chunking [i ] = 1 ;
3708- }
3709-
37103704 ierr = nc_def_dim (ncid , cmor_axes [nAxisID ].id , j , & nc_dim [i ]);
37113705 if (ierr != NC_NOERR ) {
37123706 ierr = nc_enddef (ncid );
@@ -4909,7 +4903,6 @@ int cmor_write(int var_id, void *data, char type, char *file_suffix,
49094903 char ctmp2 [CMOR_MAX_STRING ];
49104904 char msg [CMOR_MAX_STRING ];
49114905 char appending_to [CMOR_MAX_STRING ];
4912- size_t nc_dim_chunking [CMOR_MAX_AXES ];
49134906 int nc_vars [CMOR_MAX_VARIABLES ];
49144907 int nc_vars_af [CMOR_MAX_VARIABLES ];
49154908 int nc_dim_af [CMOR_MAX_DIMENSIONS ];
@@ -5159,7 +5152,7 @@ int cmor_write(int var_id, void *data, char type, char *file_suffix,
51595152/* define dimensions in NetCDF file */
51605153/* -------------------------------------------------------------------- */
51615154 cmor_define_dimensions (var_id , ncid , ncafid , time_bounds , nc_dim ,
5162- nc_vars , nc_bnds_vars , nc_vars_af , nc_dim_chunking ,
5155+ nc_vars , nc_bnds_vars , nc_vars_af ,
51635156 & dim_bnds , zfactors , nc_zfactors , nc_dim_af , & nzfactors );
51645157/* -------------------------------------------------------------------- */
51655158/* Store the dimension id for reuse when writing */
@@ -5219,7 +5212,7 @@ int cmor_write(int var_id, void *data, char type, char *file_suffix,
52195212 cmor_create_var_attributes (var_id , ncid , ncafid , nc_vars ,
52205213 nc_bnds_vars , nc_vars_af , nc_associated_vars , nc_singletons ,
52215214 nc_singletons_bnds , nc_zfactors , zfactors , nzfactors ,
5222- nc_dim_chunking , outname );
5215+ outname );
52235216
52245217
52255218 }
@@ -5327,7 +5320,7 @@ void cmor_create_var_attributes(int var_id, int ncid, int ncafid,
53275320 int * nc_vars_af , int * nc_associated_vars ,
53285321 int * nc_singletons , int * nc_singletons_bnds ,
53295322 int * nc_zfactors , int * zfactors , int nzfactors ,
5330- size_t * nc_dim_chunking , char * outname )
5323+ char * outname )
53315324{
53325325
53335326 size_t starts [2 ], counts [2 ];
@@ -5604,41 +5597,47 @@ void cmor_create_var_attributes(int var_id, int ncid, int ncafid,
56045597 }
56055598 }
56065599
5607- size_t bytes_per_elem = 0 ;
5608- if (pVar -> type == 'c' )
5609- bytes_per_elem = sizeof (char );
5610- else if (pVar -> type == 'f' )
5611- bytes_per_elem = sizeof (float );
5612- else if (pVar -> type == 'd' )
5613- bytes_per_elem = sizeof (double );
5614- else if (pVar -> type == 'i' )
5615- bytes_per_elem = sizeof (int );
5616- else if (pVar -> type == 'l' )
5617- bytes_per_elem = sizeof (long );
5618-
56195600 size_t chunking_dims [pVar -> ndims ];
56205601
5621- // Create chunking dimensions where multiple timesteps can fit
5622- // if the chunk size stays under a maximum size.
5623- size_t bytes_per_timestep = bytes_per_elem ;
5624- for (i = 0 ; i < pVar -> ndims ; i ++ ) {
5625- if (cmor_axes [pVar -> axes_ids [i ]].axis != 'T' ) {
5626- bytes_per_timestep *= cmor_axes [pVar -> axes_ids [i ]].length ;
5602+ if (pVar -> chunking_dimensions != NULL ) {
5603+ for (i = 0 ; i < pVar -> ndims ; i ++ ) {
5604+ chunking_dims [i ] = (size_t )pVar -> chunking_dimensions [i ];
56275605 }
5628- }
5629- size_t timesteps_per_chunk = CMOR_TIMESTEP_CHUNK_MAX_BYTES / bytes_per_timestep ;
5630- for (i = 0 ; i < pVar -> ndims ; i ++ ) {
5631- if (cmor_axes [pVar -> axes_ids [i ]].axis == 'T' ) {
5632- if (timesteps_per_chunk > cmor_axes [pVar -> axes_ids [i ]].length ) {
5633- chunking_dims [i ] = cmor_axes [pVar -> axes_ids [i ]].length ;
5634- } else {
5635- chunking_dims [i ] = timesteps_per_chunk ;
5606+ } else {
5607+ size_t bytes_per_elem = 0 ;
5608+ if (pVar -> type == 'c' )
5609+ bytes_per_elem = sizeof (char );
5610+ else if (pVar -> type == 'f' )
5611+ bytes_per_elem = sizeof (float );
5612+ else if (pVar -> type == 'd' )
5613+ bytes_per_elem = sizeof (double );
5614+ else if (pVar -> type == 'i' )
5615+ bytes_per_elem = sizeof (int );
5616+ else if (pVar -> type == 'l' )
5617+ bytes_per_elem = sizeof (long );
5618+
5619+ // Create chunking dimensions where multiple timesteps can fit
5620+ // if the chunk size stays under a maximum size.
5621+ size_t bytes_per_timestep = bytes_per_elem ;
5622+ for (i = 0 ; i < pVar -> ndims ; i ++ ) {
5623+ if (cmor_axes [pVar -> axes_ids [i ]].axis != 'T' ) {
5624+ bytes_per_timestep *= cmor_axes [pVar -> axes_ids [i ]].length ;
56365625 }
5637- } else {
5638- if (timesteps_per_chunk == 0 ) {
5639- chunking_dims [i ] = 0 ;
5626+ }
5627+ size_t timesteps_per_chunk = CMOR_TIMESTEP_CHUNK_MAX_BYTES / bytes_per_timestep ;
5628+ for (i = 0 ; i < pVar -> ndims ; i ++ ) {
5629+ if (cmor_axes [pVar -> axes_ids [i ]].axis == 'T' ) {
5630+ if (timesteps_per_chunk > cmor_axes [pVar -> axes_ids [i ]].length ) {
5631+ chunking_dims [i ] = cmor_axes [pVar -> axes_ids [i ]].length ;
5632+ } else {
5633+ chunking_dims [i ] = timesteps_per_chunk ;
5634+ }
56405635 } else {
5641- chunking_dims [i ] = cmor_axes [pVar -> axes_ids [i ]].length ;
5636+ if (timesteps_per_chunk == 0 ) {
5637+ chunking_dims [i ] = 0 ;
5638+ } else {
5639+ chunking_dims [i ] = cmor_axes [pVar -> axes_ids [i ]].length ;
5640+ }
56425641 }
56435642 }
56445643 }
@@ -6940,6 +6939,10 @@ int cmor_close_variable(int var_id, char *file_name, int *preserve)
69406939 if (cmor_vars [var_id ].values != NULL ) {
69416940 free (cmor_vars [var_id ].values );
69426941 }
6942+ if (cmor_vars [var_id ].chunking_dimensions != NULL ) {
6943+ free (cmor_vars [var_id ].chunking_dimensions );
6944+ cmor_vars [var_id ].chunking_dimensions = NULL ;
6945+ }
69436946 for (i = 0 ; i < cmor_vars [var_id ].nattributes ; i ++ ) {
69446947 if (strcmp (cmor_vars [var_id ].attributes [i ], "cell_methods" )
69456948 == 0 ) {
0 commit comments