tfile_scanner_mcmc_starter.m - cosmo - front and backend for Markov-Chain Monte Carlo inversion of cosmogenic nuclide concentrations
 (HTM) git clone git://src.adamsgaard.dk/cosmo
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tfile_scanner_mcmc_starter.m (5983B)
       ---
            1 %%  file_scanner_mcmc_starter.m
            2 %  This file is started by run.sh.
            3 %  The script watches an input folder (infolder) for input files generated
            4 %  by the web interface. The content of the input files is used to start
            5 %  calls to the MCMC functions.
            6 %
            7 
            8 %% folder and file configuration
            9 
           10 % folder containing input files from web interface ("uploadhistory.php")
           11 % and status file
           12 infolder = '~/cosmo/input';
           13 
           14 % folder containing input files from web interface while their contents are
           15 % being used by matlab
           16 waitfolder = '~/cosmo/wait';
           17 
           18 % outfolder: folder for generated plots
           19 outfolder = '/var/www/html/output/';
           20 
           21 [status, hostname] = system('hostname');
           22 if ~isempty(strfind(hostname, 'flaptop')) || ...
           23         ~isempty(strfind(hostname, 'adc-server')) % laptop or desktop
           24     infolder = '~/src/cosmo/input';
           25     outfolder = 'output/';
           26 end
           27 
           28 % uniquely identifying file name prefix for input files
           29 prefix = 'cosmo_';
           30 
           31 % archivefolder: folder where completed input files are archived. This 
           32 % folder should be outside of the webserver document root so others cannot
           33 % access this information
           34 archivefolder = '~/cosmo/archive';
           35 
           36 % folder containing matlab scripts to path
           37 matlab_scripts_folder = 'm_pakke2014maj11/';
           38 
           39 %% general settings
           40 debug = true; % show debugging output to matlab console
           41 
           42 % output graphics formats
           43 graphics_formats = {'fig', 'png', 'pdf'};
           44 
           45 % show figures after they are generated in addition to saving them,
           46 % values: 'on' or 'off'
           47 %show_figures = 'on';
           48 show_figures = 'off';
           49 
           50 % number of MCMC walkers
           51 %n_walkers = 2;
           52 
           53 %% initialization
           54 addpath(matlab_scripts_folder);
           55 
           56 % make sure multiple matlab processes to not launch at the exact same time
           57 pause(rand()*4.0);
           58 
           59 disp('Entering main loop, waiting for input from web interface...');
           60 
           61 %% main loop
           62 while 1
           63     
           64     try
           65     
           66         % detect all files in infolder starting with prefix
           67         infiles = dir(strcat(infolder, '/', prefix, '*'));
           68 
           69         % sort files according to modification time
           70         [sorteddates, sortidx] = sort([infiles.datenum]);
           71         infiles = infiles(sortidx);
           72 
           73         % process files sequentially
           74         for i = 1:length(infiles);
           75 
           76             % read full file name and path
           77             infile = strcat(infolder, '/', infiles(i).name);
           78 
           79             % move to waitfolder
           80             movefile(infile, waitfolder);
           81             waitfile = strcat(waitfolder, '/', infiles(i).name);
           82 
           83             idstring = strsplit(infile, '_');
           84             id = idstring(2);
           85             statusfile = char(strcat(infolder, '/status_', id));
           86             diary(char(strcat(infolder, '/log_', id)));
           87 
           88             if debug
           89                 disp(infile);
           90                 disp(strcat('Simulation id: ', id));
           91             end
           92 
           93             % read file and save data to local scope
           94             [sample_id, name, email, lat, long, ...
           95                 be_conc,  al_conc,  c_conc,  ne_conc, ...
           96                 be_uncer, al_uncer, c_uncer, ne_uncer, ...
           97                 zobs, ...
           98                 be_prod_spall, al_prod_spall, c_prod_spall, ne_prod_spall, ...
           99                 be_prod_muons, al_prod_muons, c_prod_muons, ne_prod_muons, ...
          100                 rock_density, ...
          101                 epsilon_gla_min, epsilon_gla_max, ...
          102                 epsilon_int_min, epsilon_int_max, ...
          103                 t_degla_min, t_degla_max, ...
          104                 record, ...
          105                 record_threshold_min, record_threshold_max, ...
          106                 nwalkers] ...
          107                 = import_php_file(waitfile, 1, 1); % only read first line
          108 
          109             % run inversion
          110             [Ss, save_file] = mcmc_inversion(matlab_scripts_folder, debug, ...
          111                 nwalkers, outfolder, ...
          112                 be_conc,  al_conc,  c_conc,  ne_conc, ...
          113                 be_uncer, al_uncer, c_uncer, ne_uncer, ...
          114                 zobs, ...
          115                 be_prod_spall, al_prod_spall, c_prod_spall, ne_prod_spall, ...
          116                 be_prod_muons, al_prod_muons, c_prod_muons, ne_prod_muons, ...
          117                 rock_density, ...
          118                 epsilon_gla_min, epsilon_gla_max, ...
          119                 epsilon_int_min, epsilon_int_max, ...
          120                 t_degla_min, t_degla_max, ...
          121                 record, ...
          122                 record_threshold_min, record_threshold_max, ...
          123                 statusfile, id);
          124 
          125             fid = fopen(statusfile, 'w');
          126             fprintf(fid, 'Generating plots and output files');
          127             fclose(fid);
          128 
          129             % generate plots
          130             %CompareWalks2(Ss, save_file)
          131             generate_plots(Ss, matlab_scripts_folder, ...
          132                 save_file, graphics_formats, show_figures, ...
          133                 sample_id, name, email, lat, long, ...
          134                 be_conc,  al_conc,  c_conc,  ne_conc, ...
          135                 be_uncer, al_uncer, c_uncer, ne_uncer, ...
          136                 zobs, ...
          137                 be_prod_spall, al_prod_spall, c_prod_spall, ne_prod_spall, ...
          138                 be_prod_muons, al_prod_muons, c_prod_muons, ne_prod_muons, ...
          139                 rock_density, ...
          140                 epsilon_gla_min, epsilon_gla_max, ...
          141                 epsilon_int_min, epsilon_int_max, ...
          142                 t_degla_min, t_degla_max, ...
          143                 record, ...
          144                 record_threshold_min, record_threshold_max, ...
          145                 nwalkers);
          146 
          147             % close all figures
          148             close all;
          149 
          150             % delete or archive the file so it is not processed again
          151             %delete(infile)
          152             %movefile(infile, archivefolder);
          153             movefile(char(strcat(waitfolder, '/cosmo_', id)), archivefolder);
          154 
          155             fid = fopen(statusfile, 'w');
          156             fprintf(fid, 'Computations complete');
          157             fclose(fid);
          158 
          159             disp(['Computations complete, id = ' id]);
          160 
          161             diary off;
          162 
          163             %keyboard
          164         end
          165         
          166     catch E
          167         disp(E.message)
          168         exit(1) % quit matlab on error
          169     end
          170 
          171     % sleep in order to reduce system load
          172     pause(5)
          173     
          174     % for debugging purposes; ends loop after first iteration
          175     %break
          176 
          177 end