0001 function cdf2matp(cdf_file, matp_file, mpcver)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0070 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0071 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0072 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0073 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0074 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0075 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0076 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0077 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0078 [AREA_I, PRICE_REF_BUS] = idx_area;
0079
0080 rev = '$Revision: 1.23 $';
0081
0082
0083 if nargin < 1
0084 cdf_file = input('Please enter IEEE Common Data Format file name : ', 's');
0085 end
0086 if nargin < 2
0087 matp_file = input('Please enter MATPOWER file name : ', 's');
0088 end
0089 if nargin < 3
0090 mpcver = '2';
0091 end
0092
0093
0094 l = length(matp_file);
0095 rootname = [];
0096 if l > 2
0097 if strcmp(matp_file(l-1:l), '.m')
0098 rootname = matp_file(1:l-2);
0099 extension = '.m';
0100 elseif l > 4
0101 if strcmp(matp_file(l-3:l), '.mat')
0102 rootname = matp_file(1:l-4);
0103 extension = '.mat';
0104 end
0105 end
0106 end
0107 if isempty(rootname)
0108 rootname = matp_file;
0109 extension = '.m';
0110 matp_file = [rootname, extension];
0111 end
0112
0113
0114 comments = { sprintf('%s', upper(rootname)), ...
0115 ' Please see ''help caseformat'' for details on the case file format.', ...
0116 ' This data was converted from IEEE Common Data Format', ...
0117 sprintf(' (%s) on %s by cdf2matp, rev. %s', cdf_file, date, rev(12:end-2)), ...
0118 ' See end of file for warnings generated during conversion.', ...
0119 '' };
0120 warnings = {};
0121
0122 fid = fopen(cdf_file, 'r');
0123
0124
0125 while 1
0126 title = fgetl(fid);
0127 if length(findstr(title, '/'))==2
0128 comments{end+1} = title;
0129 baseMVA = str2num(title(32:37)); break;
0130 end
0131 end
0132
0133
0134 while 1
0135 line = fgetl(fid);
0136 if line(1:16) == 'BUS DATA FOLLOWS', break, end
0137 end
0138
0139
0140 ibus = 0;
0141 igen = 0;
0142 iarea = 0;
0143
0144 while 1
0145 line = fgetl(fid);
0146 if line(1:4) == '-999', break, end
0147
0148
0149 ibus = ibus + 1;
0150 bus(ibus, BUS_I) = str2num(line(1:4));
0151 bus(ibus, BUS_TYPE) = str2num(line(25:26));
0152 if bus(ibus, BUS_TYPE) == 0
0153 bus(ibus, BUS_TYPE) = 1;
0154 end
0155 if (bus(ibus, BUS_TYPE) < 2)
0156 bus(ibus, PD) = str2num(line(41:49)) - str2num(line(60:67));
0157 elseif (bus(ibus, BUS_TYPE) >= 2)
0158 bus(ibus, PD) = str2num(line(41:49));
0159 end
0160 bus(ibus, QD) = str2num(line(50:59));
0161 bus(ibus, GS) = baseMVA*str2num(line(107:114));
0162 bus(ibus, BS) = baseMVA*str2num(line(115:122));
0163 bus(ibus, BUS_AREA) = str2num(line(19:20));
0164 bus(ibus, VM) = str2num(line(28:33));
0165 bus(ibus, VA) = str2num(line(34:40));
0166 bus(ibus, BASE_KV) = str2num(line(77:83));
0167 bus(ibus, ZONE) = str2num(line(21:23));
0168 bus(ibus, VMAX) = 1.06;
0169 bus(ibus, VMIN) = 0.94;
0170
0171
0172 Pg = str2num(line(60:67));
0173 Qg = str2num(line(68:75));
0174 Qmax = str2num(line(91:98));
0175 Qmin = str2num(line(99:106));
0176 if bus(ibus, BUS_TYPE) >= 2
0177 igen = igen + 1;
0178 if bus(ibus, BUS_TYPE) == 3, refgen = igen; end
0179 gen(igen, GEN_BUS) = bus(ibus, BUS_I);
0180 gen(igen, PG) = Pg;
0181 if gen(igen, PG) < 0
0182 bus(ibus, PD) = bus(ibus, PD) - gen(igen, PG);
0183 warnings{end+1} = sprintf('***** negative Pg at bus %g treated as Pd', bus(ibus, BUS_I));
0184 fprintf('\n %s', warnings{end});
0185 gen(igen, PG) = 0;
0186 end
0187 gen(igen, QG) = Qg;
0188 gen(igen, QMAX) = Qmax;
0189 gen(igen, QMIN) = Qmin;
0190 if Qmax - Qmin < 0.01
0191 gen(igen, QMAX) = Qmin + 0.1 * baseMVA;
0192 warnings{end+1} = sprintf('***** Qmax = Qmin at generator at bus %4i (Qmax set to Qmin + %g)', bus(ibus, BUS_I), baseMVA/10);
0193 fprintf('\n %s', warnings{end});
0194 end
0195 gen(igen, VG) = str2num(line(85:90));
0196 gen(igen, MBASE) = baseMVA;
0197 gen(igen, GEN_STATUS) = 1;
0198 gen(igen, PMAX) = gen(igen, 2) + baseMVA;
0199 gen(igen, PMIN) = 0;
0200
0201 gencost(igen, MODEL) = POLYNOMIAL;
0202 gencost(igen, STARTUP) = 0;
0203 gencost(igen, SHUTDOWN) = 0;
0204 gencost(igen, NCOST) = 3;
0205
0206
0207
0208 end
0209 end
0210
0211 totload = sum(bus(:, PD));
0212 totgen = sum(gen(:, PG));
0213 if totgen < 1.04 * totload
0214 gen(refgen, PMAX) = gen(refgen, PG) + 1.1 * totload - totgen;
0215 warnings{end+1} = sprintf('***** Insufficient generation, setting Pmax at slack bus (bus %d) to %g', gen(refgen, [GEN_BUS, PMAX]));
0216 fprintf('\n %s', warnings{end});
0217 end
0218
0219
0220 ng = size(gen, 1);
0221
0222
0223
0224 zg = find(gen(:, PG) == 0);
0225 gencost(zg, COST) = 0.01 * ones(size(zg));
0226 gencost(zg, COST+1) = 40 * ones(size(zg));
0227 nzg = find(gen(:, PG) ~= 0);
0228 gencost(nzg, COST) = 10 * ones(size(nzg)) ./ gen(nzg, PG);
0229 gencost(nzg, COST+1) = 20 * ones(size(nzg));
0230 gencost(:, COST+2) = zeros(ng, 1);
0231
0232
0233 while 1
0234 line = fgetl(fid);
0235 if line(1:19) == 'BRANCH DATA FOLLOWS', break, end
0236 end
0237
0238
0239 k = 0;
0240 while 1
0241 line = fgetl(fid);
0242 if line(1:4) == '-999', break, end
0243
0244 k = k + 1;
0245 branch(k, F_BUS) = str2num(line(1:4));
0246 branch(k, T_BUS) = str2num(line(6:9));
0247 branch(k, BR_R) = str2num(line(20:29));
0248 branch(k, BR_X) = str2num(line(30:40));
0249 branch(k, BR_B) = str2num(line(41:50));
0250 branch(k, RATE_A) = str2num(line(51:55));
0251 if branch(k, RATE_A) < 0.000001
0252 branch(k, RATE_A) = 99 * baseMVA;
0253 warnings{end+1} = sprintf('***** MVA limit of branch %d - %d not given, set to %g', branch(k, [1, 2, 6]));
0254 fprintf('\n %s', warnings{end});
0255 end
0256 branch(k, RATE_B) = str2num(line(57:61));
0257 branch(k, RATE_C) = str2num(line(63:67));
0258 branch(k, TAP) = str2num(line(77:82));
0259 branch(k, SHIFT) = 0;
0260 branch(k, BR_STATUS) = 1;
0261 end
0262 fprintf('\n');
0263 fclose(fid);
0264
0265
0266 mpc.baseMVA = baseMVA;
0267 mpc.bus = bus;
0268 mpc.branch = branch;
0269 mpc.gen = gen;
0270 mpc.gencost = gencost;
0271 mpc = loadcase(mpc);
0272
0273
0274 matp_file = savecase(matp_file, comments, mpc, mpcver);
0275
0276
0277 if ~isempty(warnings)
0278 if length(matp_file) < 4 || ~strcmp(matp_file(end-3:end), '.mat')
0279 fid = fopen(matp_file, 'at');
0280 fprintf(fid, '\n%% Warnings from cdf2matp conversion:\n%%\n');
0281 for i=1:length(warnings)
0282 fprintf(fid, '%% %s\n', warnings{i});
0283 end
0284 fclose(fid);
0285 end
0286 end
0287
0288
0289