Skip to content

Commit 58d5ece

Browse files
committed
Prototype solution to allow 'append' on datasets (intended for HDF
usage where each dataset becomes a 'root entry' in the file)
1 parent 451df4f commit 58d5ece

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

common/lib/share/mccode-r.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
/** Include header files to avoid implicit declarations (not allowed on LLVM) */
4141
#include <ctype.h>
4242
#include <sys/types.h>
43+
#include <dirent.h>
44+
#include <errno.h>
4345

4446
// UNIX specific headers (non-Windows)
4547
#if defined(__unix__) || defined(__APPLE__)
@@ -61,6 +63,7 @@ static long mcstartdate = 0; /* start simulation time */
6163
static int mcdisable_output_files = 0; /* --no-output-files */
6264
mcstatic int mcgravitation = 0; /* use gravitation flag, for PROP macros */
6365
mcstatic int mcusedefaults = 0; /* assume default value for all parameters */
66+
mcstatic int mcappend = 0; /* flag to allow append mode on datasets/directories */
6467
mcstatic int mcdotrace = 0; /* flag for --trace and messages for DISPLAY */
6568
mcstatic int mcnexus_embed_idf = 0; /* flag to embed xml-formatted IDF file for Mantid */
6669
#pragma acc declare create ( mcdotrace )
@@ -2731,15 +2734,24 @@ mcuse_dir(char *dir)
27312734
#ifdef USE_MPI
27322735
if(mpi_node_rank == mpi_node_root) {
27332736
#endif
2737+
int exists=0;
2738+
DIR* handle = opendir(dirname);
2739+
if (handle) {
2740+
/* Directory exists. */
2741+
closedir(handle);
2742+
exists=1;
2743+
}
27342744
if(mkdir(dirname, 0777)) {
27352745
#ifndef DANSE
2736-
fprintf(stderr, "Error: unable to create directory '%s' (mcuse_dir)\n", dir);
2737-
fprintf(stderr, "(Maybe the directory already exists?)\n");
2746+
if(!mcappend) {
2747+
fprintf(stderr, "Error: unable to create directory '%s' (mcuse_dir)\n", dir);
2748+
fprintf(stderr, "(Maybe the directory already exists?)\n");
27382749
#endif
27392750
#ifdef USE_MPI
2740-
MPI_Abort(MPI_COMM_WORLD, -1);
2751+
MPI_Abort(MPI_COMM_WORLD, -1);
27412752
#endif
2742-
exit(-1);
2753+
exit(-1);
2754+
}
27432755
}
27442756
#ifdef USE_MPI
27452757
}
@@ -4492,6 +4504,7 @@ mchelp(char *pgmname)
44924504
" -s SEED --seed=SEED Set random seed (must be != 0)\n"
44934505
" -n COUNT --ncount=COUNT Set number of particles to simulate.\n"
44944506
" -d DIR --dir=DIR Put all data files in directory DIR.\n"
4507+
" -a --append Append data files to those in directory DIR.\n"
44954508
" -t --trace Enable trace of " MCCODE_PARTICLE "s through instrument.\n"
44964509
" (Use -t=2 or --trace=2 for modernised mcdisplay rendering)\n"
44974510
" -g --gravitation Enable gravitation for all trajectories.\n"
@@ -4745,6 +4758,10 @@ mcparseoptions(int argc, char *argv[])
47454758
usedir=&argv[i][2];
47464759
else if(!strcmp("--dir", argv[i]) && (i + 1) < argc)
47474760
usedir=argv[++i];
4761+
else if(!strncmp("-a", argv[i], 2))
4762+
mcappend = 1;
4763+
else if(!strcmp("--append", argv[i]))
4764+
mcappend = 1;
47484765
else if(!strncmp("--dir=", argv[i], 6))
47494766
usedir=&argv[i][6];
47504767
else if(!strcmp("-h", argv[i]))

tools/Python/mcrun/mccode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def run(self, pipe=False, extra_opts=None, override_mpi=None):
385385
args.extend([f'--{opt}=' + str(val)])
386386

387387
# Handle proxy options without values (flags)
388-
proxy_opts_flags = ['no-output-files', 'info', 'list-parameters', 'meta-list', 'yes']
388+
proxy_opts_flags = ['no-output-files', 'info', 'list-parameters', 'meta-list', 'yes','append']
389389
if mccode_config.configuration["MCCODE"] == 'mcstas':
390390
proxy_opts_flags.append('gravitation')
391391

tools/Python/mcrun/mcrun.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,12 @@ def is_valid(path):
310310

311311
add('-d', '--dir',
312312
metavar='DIR', type=str,
313-
action='callback', callback=check_file(exist=False),
314313
help='Put all data files in directory DIR')
315314

315+
add('-a', '--append',
316+
action='store_true', default=False,
317+
help='Append data files to those already in directory DIR')
318+
316319
add('--format',
317320
metavar='FORMAT', default='McCode',
318321
help='Output data files using format FORMAT, usually McCode or NeXus '
@@ -406,6 +409,7 @@ def expand_options(options):
406409
datetime.strftime(datetime.now(), DATE_FORMAT_PATH))
407410
# alert user
408411
LOG.info('No output directory specified (--dir)')
412+
409413
# Output file
410414
if options.optimise_file is None:
411415
# use mccode.dat when unspecified

0 commit comments

Comments
 (0)