Home > matpower7.0 > lib > psse_read.m

psse_read

PURPOSE ^

PSSE_READ Reads the data from a PSS/E RAW data file.

SYNOPSIS ^

function [records, sections] = psse_read(rawfile_name, verbose)

DESCRIPTION ^

PSSE_READ  Reads the data from a PSS/E RAW data file.
   [RECORDS, SECTIONS] = PSSE_READ(RAWFILE_NAME)
   [RECORDS, SECTIONS] = PSSE_READ(RAWFILE_NAME, VERBOSE)

   Reads the data from a PSS/E RAW data file into a cell array of
   strings, corresponding to the lines/records in the file. It
   detects the beginning and ending indices of each section as well
   as any Q record used to indicate the end of the data.

   Input:
       RAWFILE_NAME :  name of the PSS/E RAW file to be read
                       (opened directly with FILEREAD)
       VERBOSE      :  1 to display progress info, 0 (default) otherwise

   Output:
       RECORDS :   a cell array of strings, one for each line in
                   the file (new line characters not included)
       SECTIONS  : a struct array with the following fields
           first : index into RECORDS of first line of the section
           last  : index into RECORDS of last line of the section
           name  : name of the section (if available) extracted
                   from the 'END OF <NAME> DATA, BEGIN ... DATA'
                   comment typically found in the terminator line

   See also PSSE2MPC.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [records, sections] = psse_read(rawfile_name, verbose)
0002 %PSSE_READ  Reads the data from a PSS/E RAW data file.
0003 %   [RECORDS, SECTIONS] = PSSE_READ(RAWFILE_NAME)
0004 %   [RECORDS, SECTIONS] = PSSE_READ(RAWFILE_NAME, VERBOSE)
0005 %
0006 %   Reads the data from a PSS/E RAW data file into a cell array of
0007 %   strings, corresponding to the lines/records in the file. It
0008 %   detects the beginning and ending indices of each section as well
0009 %   as any Q record used to indicate the end of the data.
0010 %
0011 %   Input:
0012 %       RAWFILE_NAME :  name of the PSS/E RAW file to be read
0013 %                       (opened directly with FILEREAD)
0014 %       VERBOSE      :  1 to display progress info, 0 (default) otherwise
0015 %
0016 %   Output:
0017 %       RECORDS :   a cell array of strings, one for each line in
0018 %                   the file (new line characters not included)
0019 %       SECTIONS  : a struct array with the following fields
0020 %           first : index into RECORDS of first line of the section
0021 %           last  : index into RECORDS of last line of the section
0022 %           name  : name of the section (if available) extracted
0023 %                   from the 'END OF <NAME> DATA, BEGIN ... DATA'
0024 %                   comment typically found in the terminator line
0025 %
0026 %   See also PSSE2MPC.
0027 
0028 %   MATPOWER
0029 %   Copyright (c) 2014-2016, Power Systems Engineering Research Center (PSERC)
0030 %   by Ray Zimmerman, PSERC Cornell
0031 %
0032 %   This file is part of MATPOWER.
0033 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0034 %   See https://matpower.org for more info.
0035 
0036 %% default args
0037 if nargin < 2
0038     verbose = 0;
0039 end
0040 
0041 %% define some functions needed for use with CELLFUN
0042 end_of_section  = @(s)~isempty(regexp(s, '^(Q|\s*0)[\s]*(/.*)?$', 'once'));
0043 q_record        = @(s)~isempty(regexp(s, '^Q', 'once'));
0044 section_name    = @(s)regexp(s, '/\s*END OF (.*)\s', 'tokens');
0045 
0046 %% check for regexp split support
0047 if ~have_fcn('regexp_split')
0048     error('psse_read: Sorry, but PSSE2MPC requires support for the ''split'' argument to regexp(), so it does not work on versions of MATLAB prior to 7.3 or Octave prior to 3.8.');
0049 end
0050 
0051 %% read in the file, split on newlines
0052 if verbose
0053     spacers = repmat('.', 1, 56-length(rawfile_name));
0054     fprintf('Reading file ''%s'' %s', rawfile_name, spacers);
0055 end
0056 str = fileread(rawfile_name);
0057 if verbose
0058     fprintf(' done.\nSplitting into individual lines ...');
0059 end
0060 records = regexp(str, '\n|\r\n|\r', 'split');
0061 if verbose
0062     str = sprintf('%d lines read', length(records));
0063     spacers = repmat('.', 1, 32-length(str));
0064     fprintf('%s %s ... done.\nAnalyzing sections ...', spacers, str);
0065 end
0066 
0067 %% find end of section and/or Q record
0068 eos = find(cellfun(end_of_section, records));
0069 
0070 %% terminate at Q record
0071 qi = find(cellfun(q_record, records(eos)));
0072 if ~isempty(qi)         %% remove everything beyond Q record ...
0073     if qi(1) > 1 && eos(qi(1)) - eos(qi(1)-1) > 1
0074         %% ... leaving Q record itself, if it is a section terminator
0075         records(eos(qi(1))+1:end) = [];
0076         eos(qi(1)+1:end) = [];
0077     else
0078         %% ... removing Q record too
0079         records(eos(qi(1)):end) = [];
0080         eos(qi(1):end) = [];
0081     end
0082 end
0083 
0084 %% find section extents and names
0085 ns = length(eos) + 1;
0086 i1 = [1 4 eos(1:end-1)+1];
0087 iN = [3 eos-1];
0088 names = cell(1, ns);
0089 names{1} = 'ID';
0090 for k = 2:ns
0091     tmp = regexp(records{eos(k-1)}, 'DATA', 'split');
0092     if isempty(tmp)     %% workaround a bug in MATLAB 7.3 (R2006b) (and possibly earlier)
0093         names{k} = '';
0094     else
0095         tmp2 = section_name(tmp{1});
0096         if isempty(tmp2)
0097             names{k} = '';
0098         else
0099             names{k} = tmp2{1}{1};
0100         end
0101     end
0102 end
0103 if verbose
0104     str = sprintf('%d sections', ns);
0105     spacers = repmat('.', 1, 45-length(str));
0106     fprintf('%s %s ... done.\n', spacers, str);
0107 end
0108 
0109 %% create the sections struct
0110 sections = struct(  'first', num2cell(i1), ...
0111                     'last', num2cell(iN), ...
0112                     'name', names   );

Generated on Mon 24-Jun-2019 15:58:45 by m2html © 2005