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 indexes 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.
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 indexes 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-2015 by Power System Engineering Research Center (PSERC) 0030 % by Ray Zimmerman, PSERC Cornell 0031 % 0032 % $Id: psse_read.m 2644 2015-03-11 19:34:22Z ray $ 0033 % 0034 % This file is part of MATPOWER. 0035 % Covered by the 3-clause BSD License (see LICENSE file for details). 0036 % See http://www.pserc.cornell.edu/matpower/ for more info. 0037 0038 %% default args 0039 if nargin < 2 0040 verbose = 0; 0041 end 0042 0043 %% define some functions needed for use with CELLFUN 0044 end_of_section = @(s)~isempty(regexp(s, '^(Q|\s*0)[\s]*(/.*)?$', 'once')); 0045 q_record = @(s)~isempty(regexp(s, '^Q', 'once')); 0046 section_name = @(s)regexp(s, '/\s*END OF (.*)\s', 'tokens'); 0047 0048 %% check for regexp split support 0049 if ~have_fcn('regexp_split') 0050 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.'); 0051 end 0052 0053 %% read in the file, split on newlines 0054 if verbose 0055 spacers = repmat('.', 1, 56-length(rawfile_name)); 0056 fprintf('Reading file ''%s'' %s', rawfile_name, spacers); 0057 end 0058 str = fileread(rawfile_name); 0059 if verbose 0060 fprintf(' done.\nSplitting into individual lines ...'); 0061 end 0062 records = regexp(str, '\n|\r\n|\r', 'split'); 0063 if verbose 0064 str = sprintf('%d lines read', length(records)); 0065 spacers = repmat('.', 1, 32-length(str)); 0066 fprintf('%s %s ... done.\nAnalyzing sections ...', spacers, str); 0067 end 0068 0069 %% find end of section and/or Q record 0070 eos = find(cellfun(end_of_section, records)); 0071 0072 %% terminate at Q record 0073 qi = find(cellfun(q_record, records(eos))); 0074 if ~isempty(qi) %% remove everything beyond Q record ... 0075 if qi(1) > 1 && eos(qi(1)) - eos(qi(1)-1) > 1 0076 %% ... leaving Q record itself, if it is a section terminator 0077 records(eos(qi(1))+1:end) = []; 0078 eos(qi(1)+1:end) = []; 0079 else 0080 %% ... removing Q record too 0081 records(eos(qi(1)):end) = []; 0082 eos(qi(1):end) = []; 0083 end 0084 end 0085 0086 %% find section extents and names 0087 ns = length(eos) + 1; 0088 i1 = [1 4 eos(1:end-1)+1]; 0089 iN = [3 eos-1]; 0090 names = cell(1, ns); 0091 names{1} = 'ID'; 0092 for k = 2:ns 0093 tmp = regexp(records{eos(k-1)}, 'DATA', 'split'); 0094 if isempty(tmp) %% workaround a bug in Matlab 7.3 (R2006b) (and possibly earlier) 0095 names{k} = ''; 0096 else 0097 tmp2 = section_name(tmp{1}); 0098 if isempty(tmp2) 0099 names{k} = ''; 0100 else 0101 names{k} = tmp2{1}{1}; 0102 end 0103 end 0104 end 0105 if verbose 0106 str = sprintf('%d sections', ns); 0107 spacers = repmat('.', 1, 45-length(str)); 0108 fprintf('%s %s ... done.\n', spacers, str); 0109 end 0110 0111 %% create the sections struct 0112 sections = struct( 'first', num2cell(i1), ... 0113 'last', num2cell(iN), ... 0114 'name', names );