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
0079 rev = '$Revision: 1.25 $';
0080
0081
0082 if nargin < 1
0083 cdf_file = input('Please enter IEEE Common Data Format file name : ', 's');
0084 end
0085 if nargin < 2
0086 matp_file = input('Please enter MATPOWER file name : ', 's');
0087 end
0088 if nargin < 3
0089 mpcver = '2';
0090 end
0091
0092
0093 [cdf_path cdf_name cdf_ext] = fileparts(cdf_file);
0094 if isempty(cdf_ext)
0095 cdf_ext = '.cdf';
0096 cdf_file = strcat(cdf_name , cdf_ext);
0097 end
0098
0099
0100 [fid, msg] = fopen(cdf_file, 'r');
0101 if fid < 0
0102 disp(msg);
0103 error('cdf2matp: Can not read the input file: %s', cdf_file )
0104 end
0105
0106
0107 if isempty(matp_file)
0108 matp_file = strcat(cdf_name, '.m');
0109 rootname = cdf_name;
0110 else
0111 [matp_path matp_name matp_ext] = fileparts(matp_file);
0112 rootname = matp_name;
0113 matp_file = strcat(matp_name, matp_ext);
0114 end
0115
0116
0117 comments = { sprintf('%s', upper(rootname)), ...
0118 ' Please see ''help caseformat'' for details on the case file format.', ...
0119 ' This data was converted from IEEE Common Data Format', ...
0120 sprintf(' (%s) on %s by cdf2matp, rev. %s', cdf_file, date, rev(12:end-2)), ...
0121 ' See end of file for warnings generated during conversion.', ...
0122 '' };
0123 warnings = {};
0124
0125
0126 title_cdf = fgetl(fid);
0127 if isnumeric(str2num(title_cdf(32:37))) && not(isempty(str2num(title_cdf(32:37))))
0128 baseMVA = str2num(title_cdf(32:37));
0129 if length(findstr(title_cdf(2:9), '/')) == 2
0130 warnings{end+1} = sprintf('***** check the title format in the first line of the cdf file.');
0131 end
0132 else
0133 error('cdf2matp:argChk','Error getting the Base MVA, check the title format in the first line of the file.')
0134 end
0135
0136
0137 comments{end+1} = title_cdf;
0138
0139
0140 while 1
0141 line = fgetl(fid);
0142 if line(1:16) == 'BUS DATA FOLLOWS', break, end
0143 end
0144
0145
0146 ibus = 0;
0147 igen = 0;
0148 iarea = 0;
0149
0150 while 1
0151 line = fgetl(fid);
0152 if line(1:4) == '-999', break, end
0153
0154
0155 ibus = ibus + 1;
0156 bus(ibus, BUS_I) = str2num(line(1:4));
0157 busnames(ibus,:) = line(6:17);
0158 bus(ibus, BUS_TYPE) = str2num(line(25:26));
0159 if bus(ibus, BUS_TYPE) == 0
0160 bus(ibus, BUS_TYPE) = 1;
0161 end
0162 if (bus(ibus, BUS_TYPE) < 2)
0163 bus(ibus, PD) = str2num(line(41:49)) - str2num(line(60:67));
0164 elseif (bus(ibus, BUS_TYPE) >= 2)
0165 bus(ibus, PD) = str2num(line(41:49));
0166 end
0167 bus(ibus, QD) = str2num(line(50:59));
0168 bus(ibus, GS) = baseMVA*str2num(line(107:114));
0169 bus(ibus, BS) = baseMVA*str2num(line(115:122));
0170 bus(ibus, BUS_AREA) = str2num(line(19:20));
0171 bus(ibus, VM) = str2num(line(28:33));
0172 bus(ibus, VA) = str2num(line(34:40));
0173 bus(ibus, BASE_KV) = str2num(line(77:83));
0174 bus(ibus, ZONE) = str2num(line(21:23));
0175 bus(ibus, VMAX) = 1.06;
0176 bus(ibus, VMIN) = 0.94;
0177
0178
0179 Pg = str2num(line(60:67));
0180 Qg = str2num(line(68:75));
0181 Qmax = str2num(line(91:98));
0182 Qmin = str2num(line(99:106));
0183 if bus(ibus, BUS_TYPE) >= 2
0184 igen = igen + 1;
0185 if bus(ibus, BUS_TYPE) == 3, refgen = igen; end
0186 gen(igen, GEN_BUS) = bus(ibus, BUS_I);
0187 gen(igen, PG) = Pg;
0188 if gen(igen, PG) < 0
0189 bus(ibus, PD) = bus(ibus, PD) - gen(igen, PG);
0190 warnings{end+1} = sprintf('***** negative Pg at bus %g treated as Pd', bus(ibus, BUS_I));
0191 fprintf('\n %s', warnings{end});
0192 gen(igen, PG) = 0;
0193 end
0194 gen(igen, QG) = Qg;
0195 gen(igen, QMAX) = Qmax;
0196 gen(igen, QMIN) = Qmin;
0197 if Qmax - Qmin < 0.01
0198 gen(igen, QMAX) = Qmin + 0.1 * baseMVA;
0199 warnings{end+1} = sprintf('***** Qmax = Qmin at generator at bus %4i (Qmax set to Qmin + %g)', bus(ibus, BUS_I), baseMVA/10);
0200 fprintf('\n %s', warnings{end});
0201 end
0202 gen(igen, VG) = str2num(line(85:90));
0203 gen(igen, MBASE) = baseMVA;
0204 gen(igen, GEN_STATUS) = 1;
0205 gen(igen, PMAX) = gen(igen, 2) + baseMVA;
0206 gen(igen, PMIN) = 0;
0207
0208 gencost(igen, MODEL) = POLYNOMIAL;
0209 gencost(igen, STARTUP) = 0;
0210 gencost(igen, SHUTDOWN) = 0;
0211 gencost(igen, NCOST) = 3;
0212
0213
0214
0215 end
0216 end
0217
0218 totload = sum(bus(:, PD));
0219 totgen = sum(gen(:, PG));
0220 if totgen < 1.04 * totload
0221 gen(refgen, PMAX) = gen(refgen, PG) + 1.1 * totload - totgen;
0222 warnings{end+1} = sprintf('***** Insufficient generation, setting Pmax at slack bus (bus %d) to %g', gen(refgen, [GEN_BUS, PMAX]));
0223 fprintf('\n %s', warnings{end});
0224 end
0225
0226
0227 ng = size(gen, 1);
0228
0229
0230
0231 zg = find(gen(:, PG) == 0);
0232 gencost(zg, COST) = 0.01 * ones(size(zg));
0233 gencost(zg, COST+1) = 40 * ones(size(zg));
0234 nzg = find(gen(:, PG) ~= 0);
0235 gencost(nzg, COST) = 10 * ones(size(nzg)) ./ gen(nzg, PG);
0236 gencost(nzg, COST+1) = 20 * ones(size(nzg));
0237 gencost(:, COST+2) = zeros(ng, 1);
0238
0239
0240 while 1
0241 line = fgetl(fid);
0242 if line(1:19) == 'BRANCH DATA FOLLOWS', break, end
0243 end
0244
0245
0246 k = 0;
0247 while 1
0248 line = fgetl(fid);
0249 if line(1:4) == '-999', break, end
0250
0251 k = k + 1;
0252 branch(k, F_BUS) = str2num(line(1:4));
0253 branch(k, T_BUS) = str2num(line(6:9));
0254 branch(k, BR_R) = str2num(line(20:29));
0255 branch(k, BR_X) = str2num(line(30:40));
0256 branch(k, BR_B) = str2num(line(41:50));
0257 branch(k, RATE_A) = str2num(line(51:55));
0258 if branch(k, RATE_A) < 0.000001
0259 branch(k, RATE_A) = 99 * baseMVA;
0260 warnings{end+1} = sprintf('***** MVA limit of branch %d - %d not given, set to %g', branch(k, [1, 2, 6]));
0261 fprintf('\n %s', warnings{end});
0262 end
0263 branch(k, RATE_B) = str2num(line(57:61));
0264 branch(k, RATE_C) = str2num(line(63:67));
0265 branch(k, TAP) = str2num(line(77:82));
0266 branch(k, SHIFT) = 0;
0267 branch(k, BR_STATUS) = 1;
0268 end
0269 fprintf('\n');
0270 fclose(fid);
0271
0272
0273 mpc.baseMVA = baseMVA;
0274 mpc.bus = bus;
0275 mpc.branch = branch;
0276 mpc.gen = gen;
0277 mpc.gencost = gencost;
0278 mpc = loadcase(mpc);
0279
0280
0281 matp_file = savecase(matp_file, comments, mpc, mpcver);
0282
0283
0284 busnames = strtrim(busnames);
0285 if length(matp_file) < 4 || ~strcmp(matp_file(end-3:end), '.mat')
0286 fid = fopen(matp_file, 'at');
0287 fprintf(fid, '\n%% bus names\nmpc.busnames = [\n');
0288 for i = 1:size(busnames, 1)
0289 fprintf(fid, '\t''%s''\n', busnames(i, :));
0290 end
0291 fprintf(fid, '];\n');
0292 fclose(fid);
0293 end
0294
0295
0296 if ~isempty(warnings)
0297 if length(matp_file) < 4 || ~strcmp(matp_file(end-3:end), '.mat')
0298 fid = fopen(matp_file, 'at');
0299 fprintf(fid, '\n%% Warnings from cdf2matp conversion:\n%%\n');
0300 for i=1:length(warnings)
0301 fprintf(fid, '%% %s\n', warnings{i});
0302 end
0303 fclose(fid);
0304 end
0305 end
0306
0307
0308