Skip to content

Commit 67aaeee

Browse files
Update mppnccombine.c to V2.2.8
1 parent 7135ed3 commit 67aaeee

File tree

1 file changed

+82
-34
lines changed

1 file changed

+82
-34
lines changed

postprocessing/mppnccombine/mppnccombine.c

Lines changed: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
programming interface "mpp_io_mod"
2525
(http://www.gfdl.noaa.gov/~vb/mpp_io.html) by V. Balaji.
2626
27+
V2.2.8: Hans.Vahlenkamp@noaa.gov
28+
If no netCDF format is specified (no -64 or -n4 options) for the
29+
output file then use the netCDF format of the first input file;
30+
use netCDF3 64-bit offset format or netCDF4 classic format.
2731
V2.2.7: Hans.Vahlenkamp@noaa.gov
2832
Synchronize output file before closing and check for errors.
2933
V2.2.6: Seth Underwood <Seth.Underwood@noaa.gov>
@@ -184,8 +188,8 @@ static unsigned long maxrss = 0; /* maximum memory used so far in kilobytes */
184188
static int print_mem_usage = 0;
185189
static unsigned long mem_allocated = 0; /* memory allocated so far */
186190

187-
static const char version[] = "2.2.7";
188-
static const char last_updated[] = "2024-09-25";
191+
static const char version[] = "2.2.8";
192+
static const char last_updated[] = "2024-12-05";
189193

190194
static unsigned long estimated_maxrss = 0; /* see option: -x */
191195
static int mem_dry_run = 0; /* set if -x option is used */
@@ -366,37 +370,38 @@ int main(int argc, char *argv[])
366370
/* Disable fatal returns from netCDF library functions */
367371
ncopts=0;
368372

369-
if (!mem_dry_run) {
370-
/* Create a new netCDF output file */
371-
if ((ncoutfile=(struct fileinfo *)malloc(sizeof(struct fileinfo)))==NULL)
372-
{
373-
fprintf(stderr,"Error: cannot allocate enough memory!\n"); return(1);
374-
}
375-
if (!appendnc)
376-
{
377-
if (stat(outfilename,&statbuf)==0)
378-
{
379-
fprintf(stderr,"Error: output file seems to exist already!\n");
380-
free(ncoutfile); return(1);
381-
}
382-
status = nc__create(outfilename, format, 0, &blksz, &ncoutfile->ncfid);
383-
if (status==(-1))
384-
{
385-
fprintf(stderr,"Error: cannot create the output netCDF file!\n");
386-
free(ncoutfile); return(1);
387-
}
388-
ncsetfill(ncoutfile->ncfid,NC_NOFILL);
389-
}
390-
/* Open an existing netCDF file for appending */
391-
else
392-
{
393-
if ((ncoutfile->ncfid=ncopen(outfilename,NC_WRITE))==(-1))
394-
{
395-
fprintf(stderr,"Error: cannot open the output netCDF file for appending!\n");
396-
free(ncoutfile); return(1);
397-
}
398-
}
399-
}
373+
if (!mem_dry_run)
374+
{
375+
/* Create a new netCDF output file */
376+
if ((ncoutfile=(struct fileinfo *)malloc(sizeof(struct fileinfo)))==NULL)
377+
{
378+
fprintf(stderr,"Error: cannot allocate enough memory!\n"); return(1);
379+
}
380+
if (!appendnc)
381+
{
382+
if (stat(outfilename,&statbuf)==0)
383+
{
384+
fprintf(stderr,"Error: output file seems to exist already!\n");
385+
free(ncoutfile); return(1);
386+
}
387+
status = nc__create(outfilename, format, 0, &blksz, &ncoutfile->ncfid);
388+
if (status==(-1))
389+
{
390+
fprintf(stderr,"Error: cannot create the output netCDF file!\n");
391+
free(ncoutfile); return(1);
392+
}
393+
ncsetfill(ncoutfile->ncfid,NC_NOFILL);
394+
}
395+
/* Open an existing netCDF file for appending */
396+
else
397+
{
398+
if ((ncoutfile->ncfid=ncopen(outfilename,NC_WRITE))==(-1))
399+
{
400+
fprintf(stderr,"Error: cannot open the output netCDF file for appending!\n");
401+
free(ncoutfile); return(1);
402+
}
403+
}
404+
}
400405

401406
/* No input files are specified on the command-line */
402407
if (inputarg==(-1))
@@ -740,6 +745,7 @@ int process_file(char *ncname, unsigned char appendnc,
740745
{
741746
struct fileinfo *ncinfile; /* Information about an input netCDF file */
742747
int nfiles2; /* Number of files in the decomposed domain */
748+
int ncinformat, ncoutformat; /* Format of input and output netCDF files */
743749
int d, v, n; /* Loop variables */
744750
int dimid; /* ID of a dimension */
745751
int decomp[4]; /* "domain_decomposition = #0, #1, #2, #3" attribute */
@@ -749,6 +755,7 @@ int process_file(char *ncname, unsigned char appendnc,
749755
/* #3 ending position of decomposed dimension */
750756
char attname[MAX_NC_NAME]; /* Name of a global or variable attribute */
751757
unsigned char ncinfileerror=0; /* Were there any file errors? */
758+
size_t blksz=65536; /* netCDF block size */
752759

753760
if (print_mem_usage) check_mem_usage();
754761

@@ -878,6 +885,48 @@ int process_file(char *ncname, unsigned char appendnc,
878885
{
879886
if (verbose) printf(" Creating output \"%s\"\n",outncname);
880887

888+
/* Determine the format of the input netCDF file */
889+
if (nc_inq_format(ncinfile->ncfid,&ncinformat)==(-1))
890+
{
891+
fprintf(stderr,"Error: cannot read the input file format!\n");
892+
ncclose(ncinfile->ncfid); free(ncinfile); return(1);
893+
}
894+
895+
/* Determine the format of the output netCDF file */
896+
if (nc_inq_format(ncoutfile->ncfid,&ncoutformat)==(-1))
897+
{
898+
fprintf(stderr,"Error: cannot read the output file format!\n");
899+
ncclose(ncinfile->ncfid); free(ncinfile); return(1);
900+
}
901+
902+
if (verbose) printf(" ncinformat=%d, ncoutformat=%d\n",ncinformat,ncoutformat);
903+
904+
/* If the format option (-64 or -n4) for the output netCDF file is not specified then */
905+
/* recreate the output netCDF file based upon the format of the input netCDF file */
906+
if (ncoutformat==NC_FORMAT_CLASSIC)
907+
{
908+
if (ncinformat==NC_FORMAT_CLASSIC || ncinformat==NC_FORMAT_64BIT_OFFSET)
909+
{
910+
ncoutformat=(NC_CLOBBER | NC_64BIT_OFFSET);
911+
if (verbose) printf(" ncoutformat reset to NC_64BIT_OFFSET \n");
912+
}
913+
else
914+
{
915+
if (ncinformat==NC_FORMAT_NETCDF4 || ncinformat==NC_FORMAT_NETCDF4_CLASSIC)
916+
{
917+
ncoutformat=(NC_CLOBBER | NC_NETCDF4 | NC_CLASSIC_MODEL);
918+
if (verbose) printf(" ncoutformat reset to NC_NETCDF4 with NC_CLASSIC_MODEL\n");
919+
}
920+
}
921+
ncclose(ncoutfile->ncfid);
922+
if (nc__create(outncname,ncoutformat,0,&blksz,&ncoutfile->ncfid)==(-1))
923+
{
924+
fprintf(stderr,"Error: cannot create the output netCDF file!\n");
925+
free(ncoutfile); return(1);
926+
}
927+
ncsetfill(ncoutfile->ncfid,NC_NOFILL);
928+
}
929+
881930
/* Define the dimensions */
882931
for (d=0; d < ncinfile->ndims; d++)
883932
{
@@ -948,7 +997,6 @@ int process_file(char *ncname, unsigned char appendnc,
948997
}
949998
}
950999

951-
9521000
/* Definitions done */
9531001
nc__enddef(ncoutfile->ncfid,headerpad,4,0,4);
9541002
}

0 commit comments

Comments
 (0)