Changeset 594 for trunk/src/series.m


Ignore:
Timestamp:
Mar 26, 2013, 9:56:10 PM (8 years ago)
Author:
sommeria
Message:

bugs corrected. One step further for using compiled fcts and cluster with series

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/series.m

    r591 r594  
    186186
    187187%% Adjust the GUI according to the binaries available in PARAM.xml
    188 path_uvmat=fileparts(which('uvmat')); %path to civ
    189 addpath (path_uvmat) ; %add the path to civ, (useful in case of change of working directory after civ has been s opened in the working directory)
    190 errormsg=[];%default error message
    191 xmlfile='PARAM.xml';
    192 if exist(xmlfile,'file')
    193     try
    194         t=xmltree(xmlfile);
    195         sparam=convert(t);
    196     catch ME
    197         errormsg={' Unable to read the file PARAM.xml defining the  binaries:';ME.message};
    198     end
    199 else
    200     errormsg=[xmlfile ' not found: path to binaries undefined'];
    201 end
    202 if ~isempty(errormsg)
    203     msgbox_uvmat('WARNING',errormsg);
    204 end
    205 test_batch=0;%default: ,no batch mode available
    206 if isfield(sparam,'BatchParam') && isfield(sparam.BatchParam,'BatchMode')
    207     test_batch=strcmp(sparam.BatchParam.BatchMode,'sge'); %sge is currently the only implemented batch mod
    208 end
    209 RUNVal=get(handles.RunMode,'Value');
    210 if test_batch==0
    211    if RUNVal>2
    212        set(handles.RunMode,'Value',1)
    213    end
    214    set(handles.RunMode,'String',{'local';'background'})
    215 else
    216     set(handles.RunMode,'String',{'local';'background';'cluster'})
    217 end
     188% path_uvmat=fileparts(which('uvmat')); %path to civ
     189% addpath (path_uvmat) ; %add the path to civ, (useful in case of change of working directory after civ has been s opened in the working directory)
     190% errormsg=[];%default error message
     191% xmlfile='PARAM.xml';
     192% if exist(xmlfile,'file')
     193%     try
     194%         t=xmltree(xmlfile);
     195%         sparam=convert(t);
     196%     catch ME
     197%         errormsg={' Unable to read the file PARAM.xml defining the  binaries:';ME.message};
     198%     end
     199% else
     200%     errormsg=[xmlfile ' not found: path to binaries undefined'];
     201% end
     202% if ~isempty(errormsg)
     203%     msgbox_uvmat('WARNING',errormsg);
     204% end
     205% test_batch=0;%default: ,no batch mode available
     206% if isfield(sparam,'BatchParam') && isfield(sparam.BatchParam,'BatchMode')
     207%     test_batch=strcmp(sparam.BatchParam.BatchMode,'sge'); %sge is currently the only implemented batch mod
     208% end
     209% RUNVal=get(handles.RunMode,'Value');
     210% if test_batch==0
     211%    if RUNVal>2
     212%        set(handles.RunMode,'Value',1)
     213%    end
     214%    set(handles.RunMode,'String',{'local';'background'})
     215% else
     216%     set(handles.RunMode,'String',{'local';'background';'cluster'})
     217% end
    218218
    219219%% introduce the input file name(s) if defined from input Param
     
    13081308set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
    13091309drawnow
    1310 [h_fun,Series,filexml,errormsg]=prepare_jobs(handles);
     1310[Series,filexml,errormsg]=prepare_jobs(handles);
    13111311if ~isempty(errormsg)
    13121312    msgbox_uvmat('ERROR',errormsg)
     
    13201320ActionList=get(handles.ActionName,'String');
    13211321ActionName=ActionList{get(handles.ActionName,'Value')};
    1322 
     1322path_series=fileparts(which('series'));
     1323
     1324% create the Action fct handle if RunMode option = 'local'
     1325if strcmp(RunMode,'local')
     1326    if ~isequal(ActionPath,path_series)
     1327        eval(['spath=which(''' ActionName ''');']) %spath = current path of the selected function ACTION
     1328        if ~exist(ActionPath,'dir')
     1329            msgbox_uvmat('ERROR',['The prescribed function path ' ActionPath ' does not exist']);
     1330            return
     1331        end
     1332        if ~isequal(spath,ActionPath)
     1333            addpath(ActionPath)% add the prescribed path if not the current one
     1334        end
     1335    end
     1336    eval(['h_fun=@' ActionName ';'])%create a function handle for ACTION
     1337    if ~isequal(ActionPath,path_series)
     1338        rmpath(ActionPath)% add the prescribed path if not the current one
     1339    end
     1340end
     1341
     1342%% Get RunTime code from the file PARAM.xml (needed to run compiled functions)
     1343errormsg='';%default error message
     1344xmlfile=fullfile(path_series,'PARAM.xml');
     1345test_batch=0;%default: ,no batch mode available
     1346if ~exist(xmlfile,'file')
     1347    [success,message]=copyfile(fullfile(path_series,'PARAM.xml.default'),xmlfile);
     1348end
     1349RunTime='';
     1350if strcmp(ActionExt,'.sh')
     1351    if exist(xmlfile,'file')
     1352        s=xml2struct(xmlfile);
     1353        if strcmp(RunMode,'cluster') && isfield(s,'BatchParam')
     1354            if isfield(s.BatchParam,'RunTime')
     1355                RunTime=s.BatchParam.RunTime;
     1356            end
     1357            if isfield(s.BatchParam,'NbCore')
     1358                NbCore=s.BatchParam.NbCore;
     1359            end
     1360        elseif (strcmp(RunMode,'background')||strcmp(RunMode,'local')) && isfield(s,'RunParam')
     1361            if isfield(s.RunParam,'RunTime')
     1362                RunTime=s.RunParam.RunTime;
     1363            end
     1364            if isfield(s.RunParam,'NbCore')
     1365                NbCore=s.RunParam.NbCore;
     1366            end
     1367        end
     1368    end
     1369    if isempty(RunTime) && strcmp(RunMode,'cluster')
     1370        msgbox_uvmat('ERROR','RunTime name not found in PARAM.xml, compiled version .sh cannot run on cluster')
     1371        return
     1372    end
     1373    Series.RunTime=RunTime;
     1374end
     1375   
    13231376%% set nbre of processes
    1324 if isfield(Series,'NbSlice')&&~isempty(Series.NbSlice)
    1325     NbProcess=Series.NbSlice;
    1326 else
     1377if ~isfield(Series,'NbSlice')
     1378    Series.NbSlice=[];
     1379end
     1380if isempty(Series.NbSlice)
    13271381    switch RunMode
    13281382        case {'local','background'}
     
    13381392    end
    13391393    NbProcess=NbCore;% choose one process per core
    1340 end
     1394else
     1395    NbProcess=Series.NbSlice;% the nbre of run processes is equal to the number of slices
     1396end
     1397
     1398%% read index ranges
     1399first_i=1;
     1400last_i=1;
     1401incr_i=1;
     1402first_j=1;
     1403last_j=1;
     1404incr_j=1;
     1405if isfield(Series.IndexRange,'first_i')
     1406    first_i=Series.IndexRange.first_i;
     1407    incr_i=Series.IndexRange.incr_i;
     1408    last_i=Series.IndexRange.last_i;
     1409end
     1410if isfield(Series.IndexRange,'first_j')
     1411    first_j=Series.IndexRange.first_j;
     1412    last_j=Series.IndexRange.last_j;
     1413end
     1414if last_i < first_i || last_j < first_j , msgbox_uvmat('ERROR','last field number must be larger than the first one'),...
     1415    set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return
     1416else
     1417    BlockLength=ceil(numel(first_i:incr_i:last_i)/NbProcess);
     1418end
     1419
     1420%% main processing
    13411421for iprocess=1:NbProcess% TOD0
    13421422%     split the input files
     
    13461426switch RunMode
    13471427    case 'local'
    1348         switch ActionExt
    1349             case '.m'
    1350                 for iprocess=1:NbProcess
    1351                     if ~isempty(filexml)
    1352                         t=struct2xml(Series);
    1353                         t=set(t,1,'name','Series');
    1354                         save(t,filexml);
    1355                     end
    1356                     Series.RUNHandle=handles.RUN;
    1357                     Series.WaitbarHandle=handles.Waitbar;
    1358                     Series=h_fun(Series);
     1428        Series.RUNHandle=handles.RUN;
     1429        Series.WaitbarHandle=handles.Waitbar;
     1430        for iprocess=1:NbProcess
     1431            if isempty(Series.NbSlice)
     1432                Series.IndexRange.first_i=first_i+(iprocess-1)*BlockLength;
     1433                Series.IndexRange.last_i=first_i+(iprocess)*BlockLength-1;
     1434            else
     1435                Series.IndexRange.first_i= first_i+iprocess-1;
     1436                Series.IndexRange.incr_i=incr_i*Series.NbSlice;
     1437            end
     1438            if ~isempty(filexml)
     1439                t=struct2xml(Series);
     1440                t=set(t,1,'name','Series');
     1441                if isequal(Series.IndexRange.last_i,Series.IndexRange.first_i)
     1442                    term_i=num2str(Series.IndexRange.first_i);
     1443                else
     1444                    term_i=[num2str(Series.IndexRange.first_i) '-' num2str(Series.IndexRange.last_i)];
    13591445                end
    1360             case '.sh'
    1361                 for iprocess=1:NbProcess
    1362                     CivmBin=fullfile(ActionPath,[ActionName '.sh']); %path to the source directory of uvmat
     1446                if isequal(Series.IndexRange.last_j,Series.IndexRange.first_j)
     1447                    term_j=num2str(Series.IndexRange.first_j);
     1448                else
     1449                    term_j=[num2str(Series.IndexRange.first_j) '-' num2str(Series.IndexRange.last_j)];
     1450                end
     1451                save(t,[filexml '_' term_i '_' term_j '.xml']);
     1452            end
     1453            switch ActionExt
     1454                case '.m'
     1455                    h_fun(Series);
     1456                case '.sh'
    13631457                    switch computer
    1364                         case {'PCWIN','PCWIN64'}
     1458                        case {'PCWIN','PCWIN64'} %Windows system
    13651459                            filename=regexprep(filename,'\\','\\\\');% add '\' so that '\' are left as characters
    13661460                            % TODO launch command in DOS
    1367                         case {'GLNX86','GLNXA64','MACI64'}
    1368                             cmd=['#!/bin/bash \n '...
    1369                                 '#$ -cwd \n '...
    1370                                 'hostname && date \n '...
    1371                                 'umask 002 \n'...
    1372                                 CivmBin ' ' Param.xml.RunTime ' ' regexprep(filename,'(.+)([/\\])(.+$)','$1$20_XML$2$3.xml') ' ' Param.OutputFile '.nc'];%allow writting access to created files for user group
     1461                        case {'GLNX86','GLNXA64','MACI64'}%Linux  system
     1462%                             cmd=['#!/bin/bash \n '...
     1463%                                 '#$ -cwd \n '...
     1464%                                 'hostname && date \n '...
     1465%                                 'umask 002 \n'...
     1466%                                 fullfile(ActionPath,[ActionName '.sh']) ' ' Series.RunTime ' ' filexml '_' term_i '_' term_j '.xml'];%allow writting access to created files for user group
     1467                             system([fullfile(ActionPath,[ActionName '.sh']) ' ' Series.RunTime ' ' filexml '_' term_i '_' term_j '.xml']);
    13731468                    end
    1374                 end
     1469            end
    13751470        end
    13761471    case 'background'
     
    13841479            t=set(t,1,'name','Series');
    13851480            save(t,filexml);
    1386             path_uvmat=fileparts(which('uvmat'));
    13871481           
    13881482            filename_bat=regexprep(filexml,'.xml$','.bat');% create executable file to run program in background
     
    14011495                        '. /etc/sysprofile \n'...
    14021496                        'matlab -nodisplay -nosplash -nojvm -logfile ''' filelog ''' <<END_MATLAB \n'...
    1403                         'addpath(''' path_uvmat '''); \n'...
     1497                        'addpath(''' path_series '''); \n'...
    14041498                        'addpath(''' Series.Action.ActionPath '''); \n'...
    14051499                        '' Series.Action.ActionName  '( ''' filexml '''); \n'...
     
    14131507                case {'PCWIN','PCWIN64'}
    14141508                    text_matlabscript=['matlab -automation -logfile ' regexprep(filelog,'\\','\\\\')...
    1415                         ' -r "addpath(''' regexprep(path_uvmat,'\\','\\\\') ''');'...
     1509                        ' -r "addpath(''' regexprep(path_series,'\\','\\\\') ''');'...
    14161510                        'addpath(''' regexprep(Series.Action.ActionPath,'\\','\\\\') ''');'...
    14171511                        '' Series.Action.ActionName  '( ''' regexprep(filexml,'\\','\\\\') ''');exit"'];
     
    15771671% --- Main launch command, called by RUN and BATCH
    15781672
    1579 function [h_fun,Series,filexml,errormsg]=prepare_jobs(handles,run)
     1673function [Series,filexml,errormsg]=prepare_jobs(handles)
    15801674%INPUT:
    15811675% handles: handles of graphic objects on the GUI series
     
    15841678
    15851679%------------------------------------------------------------------------
    1586 h_fun=[];
    15871680filexml='';
    15881681errormsg='';
    1589 if ~exist('run','var')
    1590     run=1;
    1591 end
     1682
    15921683%% Read parameters from series
    15931684Series=read_GUI(handles.series);
     
    15961687end
    15971688
    1598 %% read index ranges
    1599 first_i=1;
    1600 last_i=1;
    1601 incr_i=1;
    1602 first_j=1;
    1603 last_j=1;
    1604 incr_j=1;
    1605 if isfield(Series.IndexRange,'first_i')
    1606     first_i=Series.IndexRange.first_i;
    1607     incr_i=Series.IndexRange.incr_i;
    1608     last_i=Series.IndexRange.last_i;
    1609 end
    1610 if isfield(Series.IndexRange,'first_j')
    1611     first_j=Series.IndexRange.first_j;
    1612     incr_j=Series.IndexRange.incr_j;
    1613     last_j=Series.IndexRange.last_j;
    1614 end
    1615 
    1616 %% read input file parameters and set menus
    1617 menu_coord_state=get(handles.TransformName,'Visible');
    1618 if isequal(menu_coord_state,'on')
    1619     menu_index=get(handles.TransformName,'Value');
    1620     transform_list=get(handles.TransformName,'UserData');
    1621     Series.FieldTransform.TransformHandle=transform_list{menu_index};% transform function handles
    1622 end
    1623 
    1624 if last_i < first_i || last_j < first_j , msgbox_uvmat('ERROR','last field number must be larger than the first one'),...
    1625     set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
    16261689
    16271690%% projection object
    1628 if isfield(Series,'CheckObject')
    1629     if Series.CheckObject
    1630         hset_object=findobj(allchild(0),'tag','set_object');
    1631         Series.ProjObject=read_GUI(hset_object);
    1632         CheckObject_Callback([], [], handles)
    1633     end
    1634 else
    1635     Series.CheckObject=0;
    1636 end
     1691% if isfield(Series,'CheckObject')
     1692%     if Series.CheckObject
     1693%         hset_object=findobj(allchild(0),'tag','set_object');
     1694%         Series.ProjObject=read_GUI(hset_object);
     1695%         CheckObject_Callback([], [], handles)
     1696%     end
     1697% else
     1698%     Series.CheckObject=0;
     1699% end
    16371700
    16381701%% get_field GUI
     
    16441707end
    16451708
    1646 if ~run
    1647     return
    1648 end
    1649 
    16501709%% defining the ActionName function handle
    1651 list_action=get(handles.ActionName,'String');% list menu action
    1652 index=get(handles.ActionName,'Value');
    1653 action= list_action{index}; % selected string
    1654 %Series.hseries=handles.series; % handles to the series GUI
    1655 path_series=which('series');
    1656 ActionPathList=get(handles.ActionName,'UserData');
    1657 ActionPath=ActionPathList{index}; %path stored for the function ACTION
    1658 if ~isequal(ActionPath,path_series)
    1659     eval(['spath=which(''' action ''');']) %spath = current path of the selected function ACTION
    1660     if ~exist(ActionPath,'dir')
    1661         errormsg=['The prescribed function path ' ActionPath ' does not exist'];
    1662         return
    1663     end
    1664     if ~isequal(spath,ActionPath)
    1665         addpath(ActionPath)% add the prescribed path if not the current one
    1666     end
    1667 end
    1668 eval(['h_fun=@' action ';'])%create a function handle for ACTION
    1669 if ~isequal(ActionPath,path_series)
    1670         rmpath(ActionPath)% add the prescribed path if not the current one   
    1671 end
     1710% list_action=get(handles.ActionName,'String');% list menu action
     1711% index=get(handles.ActionName,'Value');
     1712% action= list_action{index}; % selected string
     1713% %Series.hseries=handles.series; % handles to the series GUI
     1714% path_series=which('series');
     1715% ActionPathList=get(handles.ActionName,'UserData');
     1716% ActionPath=ActionPathList{index}; %path stored for the function ACTION
     1717% if ~isequal(ActionPath,path_series)
     1718%     eval(['spath=which(''' action ''');']) %spath = current path of the selected function ACTION
     1719%     if ~exist(ActionPath,'dir')
     1720%         errormsg=['The prescribed function path ' ActionPath ' does not exist'];
     1721%         return
     1722%     end
     1723%     if ~isequal(spath,ActionPath)
     1724%         addpath(ActionPath)% add the prescribed path if not the current one
     1725%     end
     1726% end
     1727% eval(['h_fun=@' action ';'])%create a function handle for ACTION
     1728% if ~isequal(ActionPath,path_series)
     1729%         rmpath(ActionPath)% add the prescribed path if not the current one   
     1730% end
    16721731
    16731732%% create the output data directory and write in it the xml file from the GUI config
     
    17121771        end
    17131772    end
    1714     filexml=fullfile(OutputDir,[Series.InputTable{1,3} '.xml']);% name of the parameter xml file set in this directory
     1773    filexml=fullfile(OutputDir,Series.InputTable{1,3});% name of the parameter xml file set in this directory
    17151774end
    17161775%removes redondant information
     
    18161875
    18171876%% check the current ActionPath to the selected function
    1818 PathName=ActionPathList{ActionIndex};%current recorded path
    1819 set(handles.ActionPath,'String',PathName); %show the path to the senlected function
     1877ActionPath=ActionPathList{ActionIndex};%current recorded path
     1878set(handles.ActionPath,'String',ActionPath); %show the path to the senlected function
    18201879
    18211880%% reinitialise the waitbar
     
    18291888set(handles.Mask,'Visible','off')
    18301889
    1831 %% run the current action function to prepare the input GUI
    1832 [h_fun,Series,tild,errormsg]=prepare_jobs(handles);
     1890%% create the function handle for Action
     1891path_series=which('series');
     1892if ~isequal(ActionPath,path_series)
     1893    eval(['spath=which(''' ActionName ''');']) %spath = current path of the selected function ACTION
     1894    if ~exist(ActionPath,'dir')
     1895        errormsg=['The prescribed function path ' ActionPath ' does not exist'];
     1896        return
     1897    end
     1898    if ~isequal(spath,ActionPath)
     1899        addpath(ActionPath)% add the prescribed path if not the current one
     1900    end
     1901end
     1902eval(['h_fun=@' ActionName ';'])%create a function handle for ACTION
     1903if ~isequal(ActionPath,path_series)
     1904        rmpath(ActionPath)% add the prescribed path if not the current one   
     1905end
     1906
     1907%% prepare the input param
     1908[Series,tild,errormsg]=prepare_jobs(handles);
    18331909if ~isempty(errormsg)
    18341910    msgbox_uvmat('ERROR',errormsg)
     
    18771953        end
    18781954    end
     1955else
     1956% check index ranges
     1957first_i=1;last_i=1;first_j=1;last_j=1;
     1958if isfield(Series.IndexRange,'first_i')
     1959    first_i=Series.IndexRange.first_i;
     1960    last_i=Series.IndexRange.last_i;
     1961end
     1962if isfield(Series.IndexRange,'first_j')
     1963    first_j=Series.IndexRange.first_j;
     1964    last_j=Series.IndexRange.last_j;
     1965end
     1966if last_i < first_i || last_j < first_j , msgbox_uvmat('ERROR','last field number must be larger than the first one'),...
     1967    set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
    18791968end
    18801969
     
    22482337function MenuExportConfig_Callback(hObject, eventdata, handles)
    22492338global Series
    2250 [tild,Series,errormsg]=prepare_jobs(handles,0);
     2339[tild,Series,errormsg]=prepare_jobs(handles);
    22512340% Series=read_GUI(handles.series);
    22522341
     
    24922581ActionName=ActionList{get(handles.ActionName,'Value')};
    24932582if strcmp(ActionExt,'.sh')
    2494     ActionFullName=fullfile(get(handles.ActionPath,'String'),[ActionName ActionExt]);
     2583    ActionFullName=fullfile(get(handles.ActionPath,'String'),[ActionName '.sh']);
    24952584    if ~exist(ActionFullName,'file')
    24962585        answer=msgbox_uvmat('INPUT_Y-N','compiled version has not been created: compile now?');
     
    25022591        end
    25032592    end
     2593    currentdir=pwd;
     2594    cd(get(handles.ActionPath,'String'))
     2595    sh_file=dir([ActionName '.sh']);
     2596    m_file=dir([ActionName '.m']);
     2597    if isfield(m_file,'datenum') && m_file.datenum>sh_file.datenum
     2598        set(handles.ActionExt,'BackgroundColor',[1 1 0])
     2599        answer=msgbox_uvmat('INPUT_Y-N',[ActionName '.sh needs to be updated: recompile now?']);
     2600        if strcmp(answer,'Yes')
     2601            compile(ActionName)
     2602        end
     2603        set(handles.ActionExt,'BackgroundColor',[1 1 1])
     2604    end
     2605    cd(currentdir)
    25042606end
    25052607
Note: See TracChangeset for help on using the changeset viewer.