0001 function [Sim, state] = graven(varargin)
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
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116 LIGOGEO = 16384;
0117 VIRGOTAMA = 2e+4;
0118
0119
0120 inLength = length(varargin);
0121
0122
0123 input = varargin;
0124 [gps,skipCalibFlag] = errorcheck(input);
0125
0126 preGen = ~true;
0127
0128
0129 if inLength == 6
0130 [logFileName, gps, detId, inputFileName, sampFreq, seed] = ...
0131 deal(varargin{:});
0132
0133 rand('state', seed);
0134
0135 list = makelist(inputFileName, detId);
0136
0137 elseif inLength == 7
0138 [nSim, logFileName, gps, detId, inputFileName, sampFreq, seed] = ...
0139 deal(varargin{:});
0140
0141 rand('state', seed);
0142
0143 list = makelist(nSim, inputFileName, detId);
0144
0145 elseif inLength == 11
0146 [nSim, logFileName, gps, detId, simId, ampl, startSamp, internal, ...
0147 external, sampFreq, seed] = deal(varargin{:});
0148
0149 rand('state', seed);
0150
0151 list = makelist(nSim, simId, ampl, startSamp, internal, external, detId);
0152
0153 elseif inLength == 9
0154 [nSim, logFileName, gps, detId, inputFileName, startSamp, external, ...
0155 sampFreq, seed] = deal(varargin{:});
0156
0157 rand('state', seed);
0158 list = pregenlist(nSim, inputFileName, startSamp, external, detId);
0159
0160 else
0161 error('graven: Number of input arguments can only be 6, 7, 9 or 11')
0162 end
0163
0164
0165
0166
0167 oldSkipFlag = skipCalibFlag;
0168 testDet = upper(detId);
0169 switch testDet
0170 case 'H1'
0171 skipCalibFlag = oldSkipFlag;
0172 case 'H2'
0173 skipCalibFlag = oldSkipFlag;
0174 case 'L1'
0175 skipCalibFlag = oldSkipFlag;
0176 otherwise
0177 skipCalibFlag = true;
0178 end
0179
0180
0181
0182
0183
0184
0185
0186 listLength = size(list, 1);
0187
0188 if listLength == 0
0189 error('graven: listing error, list has zero length')
0190 end
0191
0192
0193 [Sim(1:listLength)] = deal(struct('name', [], 'startSamp', [], 'h', [], ...
0194 'AS_Q', [], 'DARM_ERR', [], 'gps0', [], 'pkgps' ,[]));
0195 Internal = deal(struct('x', [], 'phi', []));
0196 External = deal(struct('x', [], 'phi', [], 'psi', []));
0197
0198
0199 fid = fopen(logFileName, 'a');
0200 if fid == -1
0201 error('graven: Failed to open log file')
0202 end
0203
0204 date = clock;
0205
0206 fprintf(fid, ['# This simulation was run on %2.0f/%2.0f/%4.0f ' ...
0207 'at %2.0f:%2.0f:%2.2f EDT\n'], date(2), date(3), date(1), ...
0208 date(4), date(5), date(6));
0209 fprintf(fid, ['# (simId) (ampl) (startSamp1) (startSamp2) (Internal.x) '...
0210 '(Internal.phi) (External.x) (External.phi) (External.psi)' ...
0211 ' (gps) (gps0 s) (gps0 ns) (pkampl) (pkgps s) (pkgps ns) '...
0212 '(detId)\n']);
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 RAD2DEG = 180/pi;
0233
0234 for iSim = 1:listLength
0235
0236 simId = list{iSim, 1};
0237
0238 space = find(simId == ' ');
0239 simId(space) = [];
0240 ampl = list{iSim, 2};
0241 startSamp = list{iSim, 3};
0242
0243 if sum(strcmp(upper(detId),{'VIRGO', 'TAMA'})) > 0
0244 startSamp = ((startSamp-1)*(VIRGOTAMA/LIGOGEO))+1;
0245 end
0246 Internal.x = list{iSim, 4};
0247 Internal.phi = list{iSim, 5};
0248 External.x = list{iSim, 6};
0249 External.phi = list{iSim, 7};
0250 External.psi = list{iSim, 8};
0251
0252
0253 if ampl == -1
0254
0255 preGen = true;
0256 end
0257
0258
0259
0260
0261 samp = ifodelay(detId, External.x, External.phi, sampFreq);
0262 if isempty(samp)
0263 error('graven: ifodelay returned empty samp')
0264 end
0265 stimeIfo = startSamp+samp;
0266
0267
0268
0269
0270
0271 if preGen
0272 stimeIfo = round(stimeIfo);
0273 htt = simidfile(simId);
0274 end
0275
0276
0277 if ~preGen
0278 id = simparse(simId);
0279 if ~strcmp(upper(id), 'CHIRPLET')
0280
0281 hij = makeh(simId, ampl, stimeIfo, sampFreq);
0282 if isempty(hij)
0283 error('graven: makeh returned empty hij')
0284 end
0285
0286 htt = makett(hij, Internal);
0287 if isempty(htt)
0288 error('graven: makett returned empty htt')
0289 end
0290 elseif strcmp(upper(id), 'CHIRPLET')
0291
0292 htt = makechirplet(simId, ampl, startSamp, sampFreq)
0293 end
0294 end
0295
0296
0297 h = detproj(htt, detId, External);
0298 if isempty(h)
0299 error('graven: detproj returned empty h')
0300 end
0301
0302
0303 [maxStrain, maxSamp] = max(h);
0304 maxTimeNdx = ceil(stimeIfo)+maxSamp-1;
0305
0306 maxTimeGps = ndx2gps(maxTimeNdx, sampFreq, gps);
0307
0308
0309
0310
0311
0312
0313
0314 if(skipCalibFlag == true)
0315 asqSeries = zeros(size(h'));
0316 darmSeries = asqSeries;
0317 else
0318 [asqSeries] = calibsimfd(detId, fix(gps+(stimeIfo/sampFreq)), h, ...
0319 sampFreq, 'AS_Q');
0320 if isempty(asqSeries)
0321 error('graven: calibsimfd returned empty asqSeries')
0322 end
0323 runName = getcalibrun(gps);
0324 if strcmp(upper(runName), 'S4')
0325 [darmSeries] = calibsimfd(detId, fix(gps+(stimeIfo/sampFreq)), h, ...
0326 sampFreq, 'DARM_ERR');
0327 if isempty(darmSeries)
0328 error('graven: calibsimfd returned empty darmSeries')
0329 end
0330 else
0331 darmSeries = zeros(size(asqSeries));
0332 end
0333 end
0334
0335 Sim(iSim).name = upper(simId);
0336 Sim(iSim).startSamp = ceil(stimeIfo);
0337 Sim(iSim).h = h';
0338 Sim(iSim).AS_Q = asqSeries;
0339 Sim(iSim).DARM_ERR = darmSeries;
0340 Sim(iSim).gps0 = ndx2gps(stimeIfo, sampFreq, gps);
0341 Sim(iSim).pkgps = maxTimeGps;
0342
0343
0344 fprintf(fid, ['%s %0.5g %9.15f %9.15f %d %d %d %d %d %d %d ' ...
0345 '%8.0f %d %d %8.0f %s \n'], simId, ampl, startSamp, startSamp, ...
0346 Internal.x, Internal.phi, External.x, External.phi, ...
0347 External.psi, gps, Sim(iSim).gps0.sec, Sim(iSim).gps0.ns, ...
0348 maxStrain, Sim(iSim).pkgps.sec, Sim(iSim).pkgps.ns, detId);
0349 end
0350
0351
0352 fclose(fid);
0353
0354
0355 state = rand('state');
0356
0357 return
0358
0359
0360
0361 function [gpsVal,skipCalibFlag] = errorcheck(input)
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390 gpsVal = 0;
0391 skipCalibFlag = false;
0392
0393 inputLength=length(input);
0394
0395 if inputLength == 6
0396 [logFileName, gps, detId, inputFileName, sampFreq, seed] = ...
0397 deal(input{:});
0398
0399
0400
0401
0402
0403 if ~strcmp(class(inputFileName), 'char')
0404 inputFileName
0405 error('graven: inputFileName must be a string')
0406 end
0407
0408 elseif inputLength == 7
0409 [nSim, logFileName, gps, detId, inputFileName, sampFreq, seed] = ...
0410 deal(input{:});
0411
0412
0413
0414
0415
0416 if ~strcmp(class(inputFileName), 'char')
0417 inputFileName
0418 error('graven: inputFileName must be a string')
0419 end
0420
0421
0422 if nSim < 1 | mod(nSim, 1) > 0 | isinf(nSim) | isnan(nSim)
0423
0424 nSim
0425 error('graven: nSim must be a real finite integer >= 1')
0426 end
0427
0428 elseif inputLength == 11
0429 [nSim, logFileName, gps, detId, simId, ampl, startSamp, internal, ...
0430 external, sampFreq, seed] = deal(input{:});
0431
0432
0433
0434
0435 if nSim < 1 | mod(nSim, 1) > 0 | isinf(nSim) | isnan(nSim)
0436
0437 nSim
0438 error('graven: nSim must be a real finite integer >= 1')
0439 end
0440
0441
0442 if ~strcmp(class(simId), 'char')
0443 simId
0444 error('graven: simId must be a string')
0445 end
0446
0447
0448 if size(ampl, 1) == 1 & size(ampl, 2) == 1
0449 if ampl < 0 | isinf(ampl) | isnan(ampl) | ~isreal(ampl)
0450
0451 ampl
0452 error('graven: ampl must be a real positive fitite number')
0453 end
0454 elseif size(ampl, 1) == 1 & size(ampl, 2) == 2
0455 if ampl < 0 | isinf(ampl(1)) | isinf(ampl(2)) | isnan(ampl(1)) ...
0456 | isnan(ampl(2)) | ~isreal(ampl(1)) | ~isreal(ampl(2))
0457
0458 ampl
0459 error(['graven: ampl components must be real positive finite '...
0460 'numbers'])
0461 end
0462 elseif size(ampl, 1) == 1 & size(ampl, 2) == 3
0463 if ampl < 0 | isinf(ampl(1)) | isinf(ampl(2)) | isinf(ampl(3)) | ...
0464 isnan(ampl(1)) | isnan(ampl(2)) | isnan(ampl(3)) | ...
0465 ~isreal(ampl(1)) | ~isreal(ampl(2)) | ~isreal(ampl(3))
0466
0467 ampl
0468 error(['graven: ampl components must be real positive finite '...
0469 'numbers'])
0470 end
0471 else
0472 error('graven: ampl must be a scalar or a 1x2 or 1x3 vector')
0473 end
0474
0475
0476 if size(startSamp, 1) ~= 1 | size(startSamp, 2) ~= 2
0477 startSamp
0478 error('graven: startSamp must be a 1x2 vector')
0479 elseif startSamp(1) < 1 | mod(startSamp(1), 1) > 0 | ...
0480 mod(startSamp(2), 1) > 0 | isinf(startSamp(1)) | ...
0481 isinf(startSamp(2)) | isnan(startSamp(1)) | isnan(startSamp(2))
0482
0483
0484 startSamp
0485 error(['graven: startSamp(1) and startSamp(2) must be real '...
0486 'finite integers >= 1'])
0487 end
0488
0489
0490 if strcmp(class(internal), 'double')
0491
0492 if size(internal, 1) ~= 1 | size(internal, 2) ~= 2
0493 internal
0494 error(['graven: internal must be a 1x2 vector when not a ' ...
0495 'struct or string'])
0496 elseif abs(internal(1)) > 1 | ~isreal(internal(1))
0497
0498 internal
0499 error('graven: internal(1) must be real and bewteen -1 and 1')
0500 elseif internal(2) < -pi | internal(2) > pi | ~isreal(internal(2))
0501
0502 internal
0503 error('graven: internal(2) must be real and between -pi and pi')
0504 end
0505 elseif strcmp(class(internal), 'char')
0506
0507 if ~strcmp(upper(internal), 'RANDOM') & ...
0508 ~strcmp(upper(internal), 'OPTIMAL')
0509 internal
0510 error(['graven: internal must be ''RANDOM'' or ''OPTIMAL'' ' ...
0511 'when a string'])
0512 end
0513 elseif strcmp(class(internal), 'struct')
0514
0515 if ~isfield(internal, 'x')
0516 internal
0517 error('graven: internal must contain field x when a struct')
0518 elseif abs(internal.x) > 1 | ~isreal(internal.x)
0519
0520 internal.x
0521 error('graven: internal.x must be real and between -1 and 1')
0522 end
0523 if ~isfield(internal, 'phi')
0524 internal
0525 error('graven: internal must contain field phi when a struct')
0526 elseif internal.phi < -pi | internal.phi > pi | ~isreal(internal.phi)
0527
0528
0529 internal.phi
0530 error(['graven: internal.phi must be real and betweeen -pi '...
0531 'and pi'])
0532 end
0533 end
0534
0535
0536 checkexternal(external);
0537
0538 elseif inputLength == 9
0539 [nSim, logFileName, gps, detId, inputFileName, startSamp, external, ...
0540 sampFreq, seed] = deal(input{:});
0541
0542
0543 if ~strcmp(class(inputFileName), 'char') & ~iscell(inputFileName)
0544 inputFileName
0545 error('graven: inputFileName must be a string')
0546 end
0547
0548
0549 if size(startSamp, 1) ~= 1 | size(startSamp, 2) ~= 2
0550 startSamp
0551 error('graven: startSamp must be a 1x2 vector')
0552 elseif startSamp(1) < 1 | mod(startSamp(1), 1) > 0 | ...
0553 mod(startSamp(2), 1) > 0 | isinf(startSamp(1)) | ...
0554 isinf(startSamp(2)) | isnan(startSamp(1)) | isnan(startSamp(2))
0555
0556
0557 startSamp
0558 error(['graven: startSamp(1) and startSamp(2) must be real '...
0559 'finite integers >= 1'])
0560 end
0561
0562
0563 checkexternal(external);
0564
0565
0566 if nSim < 1 | mod(nSim, 1) > 0 | isinf(nSim) | isnan(nSim)
0567
0568 nSim
0569 error('graven: nSim must be a real finite integer >= 1')
0570 end
0571 end
0572
0573
0574
0575
0576 if ~strcmp(class(logFileName), 'char')
0577 logFileName
0578 error('graven: logFileName must be a string')
0579 end
0580
0581
0582 if gps == 0 | mod(gps, 1) > 0 | isinf(gps) | isnan(gps)
0583
0584 error('graven: gps must be a real finite integer')
0585 end
0586
0587
0588
0589
0590
0591
0592
0593 if (gps < 0)
0594 skipCalibFlag = true;
0595 gpsVal = abs(gps);
0596 else
0597 skipCalibFlag = false;
0598 gpsVal = gps;
0599 end
0600
0601
0602 if ~strcmp(class(detId), 'char')
0603 detId
0604 error('graven: detId must be a string')
0605 end
0606
0607
0608 if sampFreq <= 0 | isinf(sampFreq) | isnan(sampFreq) | ~isreal(sampFreq)
0609 sampFreq
0610 error('graven: sampFreq must be a real finite number > 0')
0611 end
0612
0613 if sampFreq ~= 16384 & (strcmp(upper(detId),'H1') || strcmp(upper(detId),'H2') ...
0614 || strcmp(upper(detId),'L1') || strcmp(upper(detId),'GEO'))
0615 warning(['graven: sampFreq does not match the sampling frequency ' ...
0616 'of the detId entered'])
0617
0618
0619 elseif sampFreq ~= 20000 & (strcmp(upper(detId),'VIRGO') ...
0620 || strcmp(upper(detId),'TAMA'))
0621 warning(['graven: sampFreq does not match the sampling frequency ' ...
0622 'of the detId entered'])
0623
0624
0625 end
0626
0627
0628 if ~strcmp(class(seed), 'double')
0629 seed
0630 error('graven: seed must be a number')
0631 end
0632
0633 return
0634
0635
0636
0637 function checkexternal(external)
0638
0639
0640 if strcmp(class(external), 'double')
0641
0642 if size(external, 1) ~= 1 | size(external, 2) ~= 3
0643 external
0644 error(['graven: external must be a 1x3 vector when not a ' ...
0645 'struct or string'])
0646 elseif abs(external(1)) > 1 | ~isreal(external(1))
0647
0648 external
0649 error('graven: external(1) must be real and bewteen -1 and 1')
0650 elseif external(2) < -pi | external(2) > pi | ~isreal(external(2))
0651
0652 external
0653 error('graven: external(2) must be real and between -pi and pi')
0654 elseif external(3) < 0 | external(3) > 2*pi | ~isreal(external(3))
0655
0656 external
0657 error('graven: external(3) must be real and between 0 and 2*pi')
0658 end
0659 elseif strcmp(class(external), 'char')
0660
0661 if ~strcmp(upper(external), 'RANDOM') & ...
0662 ~strcmp(upper(external), 'OPTIMAL')
0663 external
0664 error(['graven: external must be ''RANDOM'' or ''OPTIMAL'' ' ...
0665 'when a string'])
0666 end
0667 elseif strcmp(class(external), 'struct')
0668
0669 if ~isfield(external, 'x')
0670 external
0671 error('graven: external must contain field x when a struct')
0672 elseif abs(external.x) > 1 | ~isreal(external.x)
0673
0674
0675 external.x
0676 error('graven: external.x must be real and between -1 and 1')
0677 end
0678 if ~isfield(external, 'phi')
0679 external
0680 error('graven: external must contain field phi when a struct')
0681 elseif external.phi < -pi | external.phi > pi | ~isreal(external.phi)
0682
0683
0684 external.phi
0685 error(['graven: external.phi must be real and betweeen -pi '...
0686 'and pi'])
0687 end
0688 if ~isfield(external, 'psi')
0689 external
0690 error('graven: external must contain field psi when a struct')
0691 elseif external.psi < 0 | external.psi > (2*pi) | ...
0692 ~isreal(external.psi)
0693
0694
0695 external.psi
0696 error('graven: external.psi must be real and between 0 and 2*pi')
0697 end
0698 end
0699 return
0700
0701
0702
0703 function htt = simidfile(simId)
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730 if strcmp(class(simId), 'cell')
0731
0732 if length(simId) == 1 | length(simId) == 2
0733 htt = loadpregendata(simId);
0734 elseif length(simId) == 4
0735 plusPolFile = {simId{1}, simId{2}};
0736 hPlus = loadpregendata(plusPolFile);
0737 crossPolFile = {simId{3}, simId{4}};
0738 hcross = loadpregendata(crossPolFile);
0739 htt = [hPlus(1,:), hCross(1,:)];
0740 else
0741 simId
0742 error(['graven -> simidfile: if simId is a cell, it must have ' ...
0743 'length of 1, 2 or 4'])
0744 end
0745 elseif strcmp(class(simId), 'char')
0746
0747
0748
0749 G = find(simId == ';');
0750 if isempty(G)
0751 plusPolFile = simId(1:end);
0752 elseif length(G) == 1
0753 plusPolFile = simId(1:G-1);
0754 crossPolFile = simId(G+1:end);
0755 else
0756 simId
0757 error('graven -> simidfile: more than 2 pregenerated polarizations specified')
0758 end
0759 Iplus = find(plusPolFile == ',');
0760 if length(Iplus) == 0
0761 plusFile = plusPolFile(1:end);
0762 elseif length(Iplus) == 1
0763 plusFile{1,1} = plusPolFile(1:Iplus(1)-1);
0764 plusFile{1,2} = plusPolFile(Iplus(1)+1:end);
0765 else
0766 plusPolFile
0767 error('graven -> simidfile: Invalid plusPolFile')
0768 end
0769 if exist('crossPolFile') == 1
0770 Icross = find(crossPolFile == ',');
0771 if length(Icross) == 0
0772 crossFile = crossPolFile(1:end);
0773 elseif length(Icross) == 1
0774 crossFile{1,1} = crossPolFile(1:Icross(1)-1);
0775 crossFile{1,2} = crossPolFile(Icross(1)+1:end);
0776 else
0777 crossPolFile
0778 error('graven -> simidfile: Invalid crossPolFile')
0779 end
0780 hCross = loadpregendata(crossFile);
0781
0782 hPlus = loadpregendata(plusFile);
0783 htt = [hPlus(1,:); hCross(1,:)];
0784 else
0785 htt = loadpregendata(plusFile);
0786 end
0787 else
0788 simId
0789 error('graven -> simidfile: simId must be cell or string')
0790 end
0791
0792 return
0793
0794
0795
0796 function htt = loadpregendata(inputFileName)
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825 if ischar(inputFileName)
0826 inputFileName = {inputFileName};
0827 end
0828 [dirPath, fileName, ext] = fileparts(inputFileName{1});
0829 dirPath = [dirPath filesep];
0830 if isempty(fileName) | isempty(ext)
0831 error('graven: fileext returned empty file or ext')
0832 end
0833
0834 if strcmp(ext, '.ilwd')
0835 if exist([dirPath fileName ext]) ~= 2
0836 file = [dirPath fileName ext];
0837 error('graven: File does not exist on search path')
0838 end
0839
0840 waveform = ilwdread([dirPath fileName ext]);
0841
0842 if length(inputFileName) == 2
0843 ilwdString = inputFileName{2};
0844 if strcmpi(ilwdString, 'default');
0845
0846
0847 ilwdString = 'm1.real_8';
0848 end
0849 else
0850
0851
0852 ilwdString = 'm1.real_8';
0853 end
0854
0855
0856
0857
0858 I = find(ilwdString == '.');
0859 if ~isempty(I) & I(end) == length(ilwdString)
0860 ilwdString
0861 error('graven: Invalid ilwdString')
0862 end
0863
0864 nFields = length(I)+1;
0865 for iFields = 1:nFields
0866 if iFields == 1 & nFields ~= 1
0867 temp = getfield(waveform, ilwdString(1:I(iFields)-1));
0868 elseif iFields == 1 & nFields == 1
0869 temp = getfield(waveform, ilwdString);
0870 elseif iFields == nFields & nFields ~= 1
0871 temp = getfield(temp, ilwdString(I(iFields-1)+1:end));
0872 else
0873 temp = getfield(temp, ilwdString(I(iFields-1)+1:I(iFields)-1));
0874 end
0875 end
0876 if ~strcmp(class(temp), 'double')
0877 ilwdString
0878 error('graven: Retrieved data not class double')
0879 end
0880 htt(1,:) = temp(:)';
0881
0882
0883
0884 htt(2,:) = zeros(1,length(htt(1,:)));
0885 elseif strcmp(ext, '.txt')
0886 if exist([dirPath fileName ext]) ~= 2;
0887 file = [dirPath fileName ext];
0888 error('graven: File does not exist on search path')
0889 end
0890
0891 waveform = load([dirPath fileName ext]);
0892 htt(1,:) = waveform(:)';
0893 htt(2,:) = zeros(1, length(waveform));
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911 else
0912 ext
0913 error('graven: inputFileName must have extention *.ilwd or *.txt')
0914 end
0915
0916 return