kat_read.c 451 KB
Newer Older
1 2
// $Id$

3
/*! 
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * \file kat_read.c
 * \brief Input file reading routines.
 * 
 * @section Copyright notice
 *
 *  This file is part of the interferometer simulation Finesse
 *  http://www.gwoptics.org/finesse
 *
 *  Copyright (C) 1999 onwards Andreas Freise
 *  with parts of the code written by Daniel Brown, Paul Cochrane
 *  and Gerhard Heinzel.
 *
 *  This program is free software; you can redistribute it and/or modify it under
 *  the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Daniel Brown's avatar
Daniel Brown committed
22
 *  See the GNU General Public License for more details.
23
 *
Daniel Brown's avatar
Daniel Brown committed
24
 *  You should have received a copy of the GNU General Public License along with
25 26
 *  this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 *  Suite 330, Boston, MA 02111-1307 USA
Daniel Brown's avatar
Daniel Brown committed
27
 */
28

29
#define _GNU_SOURCE
30
#include <sys/unistd.h>
31
#include "kat.h"
32
#include "kat_mem.h"
33
#include "kat_config.h"
34
#include "kat_read.h"
35
#include "kat_optics.h"
36
#include "kat_inline.c"
37
#include "kat_io.h"
38
#include "kat_aux.h"
39
#include "kat_init.h"
cochrane's avatar
In u():  
cochrane committed
40
#include "kat_aa.h"
41
#include "kat_check.h"
42
#include "base64.h"
43
#include "fmemopen.h"
44
#include "uthash.h"
45

46
extern transfer_func_t mtf_free_mass;
47 48
extern double *f_s; //!< Frequency value list [frequency]
extern complex_t ***a_s; //!< Amplitude list [field] [output] [frequeny]
49
extern int *t_s; //!< Type_of_Signal list [frequency]
Daniel Brown's avatar
Daniel Brown committed
50
int *transfer; //!< ???
51

52
extern bool bCygserverRunning;
53
extern init_variables_t init;
54
extern interferometer_t inter;
55
extern options_t options;
56 57
extern memory_t mem;
extern local_var_t vlocal;
58
extern char functokens[];
59

60 61
extern const int MAX_NEWTON_WEIGHT;

Daniel Brown's avatar
Daniel Brown committed
62 63
int endcomp; //!< ???
int lastparam; //!< ???
64

65 66 67 68 69 70 71 72 73 74 75
extern char *__merged_kat_buffer;
extern FILE *__merged_kat_file;

bool check_for_include_kat(FILE **ptr_ifp){
    
    assert(ptr_ifp != NULL);
    
    FILE *ifp = *ptr_ifp;
    
    rewind(ifp);
    
76 77 78 79 80 81 82 83 84 85 86
    int num_includes = 0;
    
    char tmp[LINE_LEN] = {0};
    char include_command_str[MAX_INCLUDES][LINE_LEN] = {{0}};
    char include_filenames[MAX_INCLUDES][LINE_LEN] = {{0}};
    FILE* include_files[MAX_INCLUDES] = {0};
    size_t include_filesize[MAX_INCLUDES] = {0};
    int include_num_blocks[MAX_INCLUDES] = {0};
    char include_blocklist[MAX_INCLUDES][LINE_LEN] = {{0}};
    char include_blocks[MAX_INCLUDES][MAX_BLOCKS][LINE_LEN] = {{{0}}};
   
87
    // include can only be used on the very first line
88 89 90 91 92 93 94 95
    while(fgets(tmp, LINE_LEN - 1, ifp) != NULL) {
        char *s = tmp + strspn(tmp, WHITESPACE); /* skip whitespace */
        
        if(s[strlen(s) - 1] == '\n')
            s[strlen(s) - 1] = '\0';
        
        if (strncasecmp(s, "!include ", 9) == 0){
            if(num_includes >= MAX_INCLUDES){
96
                gerror("Line `%s':\n   Maximum number of includes has been reached\n", s);
97 98 99 100
            }
            
            strcpy(include_command_str[num_includes], s);
            num_includes++;
101 102 103
        } 
    }
    
104 105
    if(!num_includes)
        return false;
106
    
107
    int k=0;
108 109 110 111 112 113 114 115 116
    
    size_t kat_size = 0, include_size = 0;
    
    // get the kat file size
    fseek(ifp, 0L, SEEK_END);
    kat_size = ftell(ifp);
    rewind(ifp);
    assert(kat_size > 0);
    
117 118 119 120 121 122 123
    for(k=0; k<num_includes; k++){
        char command[MAX_TOKEN_LEN] = {0};

        // if we get this far the fist line is an include command
        int n = sscanf(include_command_str[k], "%s %s %[^\n]\n", command, include_filenames[k], include_blocklist[k]);

        if(!(n == 2 || n == 3))
124
            gerror("Line `%s':\n   include usage is '!include kat_file [block1 block2 block3 ...]'\n", include_command_str[k]);
125 126 127
        
        // check to see if we can open the include file
        if ((include_files[k] = fopen(include_filenames[k], "r")) == NULL) {
128
            gerror("Line `%s':\n    included kat file %s can't be opened\n", include_command_str[k], include_filenames[k]);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        }
        
        // get the included kat file size
        fseek(include_files[k], 0L, SEEK_END);
        include_filesize[k] = ftell(include_files[k]);
        rewind(include_files[k]);
        assert(include_filesize[k] > 0);
        
        include_size += include_filesize[k];
        
        if(n == 3){
            // if there are 3 inputs the the blocks string should contain space
            // delimited list of block names to include
            char *pch = strtok (include_blocklist[k], " ");

            while (pch != NULL) {
                strcpy(include_blocks[k][include_num_blocks[k]], pch);
                include_num_blocks[k]++;
                pch = strtok (NULL, " ");
            }
        }
    }
    
152 153 154 155
    assert(include_size > 0);
    
    // allocate memory to store the merged file, this is free'd at the
    // end of the main function.
156
    __merged_kat_buffer = (char*) malloc(sizeof(char) * (kat_size + include_size + num_includes + 1));
157
    __merged_kat_file = fmemopen(__merged_kat_buffer, kat_size + include_size + num_includes + 1, "r+");
158
    
159 160
    for(k=0; k<num_includes; k++){
        warn("Using include file `%s` ", include_filenames[k]);
161
        
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        if(include_num_blocks[k] > 0){
            warn("with %i blocks:", include_num_blocks[k]);
            int n;
            for(n=0; n<include_num_blocks[k]; n++){
                message("%s ", include_blocks[k][n]);
            }
        }
        
        message("\n");
        
        // Now we have somewhere to store our merged files add in the included kat first
        if(include_num_blocks[k] == 0){
           
            // if there are no blocks specified we can just load in the whole file
            fread(__merged_kat_buffer+ftell(__merged_kat_file), 1, include_filesize[k], include_files[k]);
            fseek(__merged_kat_file, include_filesize[k], SEEK_CUR);
            // separate the included files with a line ending
            fprintf(__merged_kat_file, "\n");     
        } else {
            int in_block = -1;
            char _s[LINE_LEN] = {0};

            while (fgets(_s, LINE_LEN - 1, include_files[k]) != NULL) {
                char *s = _s + strspn(_s, WHITESPACE); 
                // first check if we have found a block otherwise don't include it
                if(in_block < 0){
                    if(strncasecmp(s, "%%% FTblock", 11) == 0) {
                        int n = 0;
                        for(n=0; n<include_num_blocks[k]; n++){
                            char *ptr_block = strcasestr(s, include_blocks[k][n]);
Daniel Brown's avatar
Daniel Brown committed
192

193 194 195 196
                            if(ptr_block != NULL){
                                in_block = n;
                                break;
                            }
197 198
                        }
                    }
199 200 201 202 203 204
                } else if(in_block > -1) {
                    if(strncasecmp(s, "%%% FTend", 9) == 0) {
                        int n = 0;
                        // search each block and make sure that we find the end
                        // to the block we are in
                        for(n=0; n<include_num_blocks[k]; n++){
205
                            char *ptr_block = (char*)strcasestr(s, include_blocks[k][n]);
206 207 208

                            if(ptr_block != NULL){
                                if(n != in_block) {
209
                                    gerror("Line `%s' of %s:\nFound '%%%%%`% FTend %s' before '%%%%%% FTend %s'\n", s, include_filenames[k], include_blocks[k][n], include_blocks[k][in_block]);
210 211 212 213
                                }

                                in_block = -1;
                                break;
214 215 216 217
                            }
                        }
                    }
                }
218 219 220 221

                if(in_block > -1 && strncasecmp(s, "%%%", 3) != 0){
                    fwrite(s, sizeof(char), strlen(s), __merged_kat_file);
                }
222 223
            }
            
224 225 226
            if(k>0){
                // separate the included files with a line ending
                fprintf(__merged_kat_file, "\n");     
227 228 229 230 231
            }
        }
    }
    
    // Now read in the kat file that has been run
232
    fread(__merged_kat_buffer+ftell(__merged_kat_file), kat_size, 1, ifp);
233
    
234 235 236 237 238
//    rewind(__merged_kat_file);
//    char l[LINE_LEN];
//    while (fgets(l, LINE_LEN - 1, __merged_kat_file) != NULL) {
//        message(l);
//    }
239 240 241 242
    
    // Now that we have merged everything, close the original file handle
    // and set the main file handle to that of our merged one.
    fclose(ifp);
243 244 245 246
    
    for(k=0; k<num_includes; k++){
        fclose(include_files[k]);
    }
247 248 249 250 251 252
    
    *ptr_ifp = __merged_kat_file;
    
    return true;
}

253 254 255 256 257 258 259
bool parse_frequency_input(const char *fstr, double *f, const char* name, const char* variable){
    assert(fstr != NULL);
    assert(f != NULL);
    
    // check if it is just a number
    if (atod(fstr, f)) {
        // If not then check if we are using the signal frequency variables
260 261 262 263 264
        if(string_matches_exactly(fstr, "$fs")){
            *f = inter.fsig;
        } else if(string_matches_exactly(fstr, "$mfs")) {
            *f = -inter.fsig;
        } else {
265 266 267 268 269 270 271 272 273 274
            return true;
        }
    } else {
        return false;
    }
    
    // then this also means we need to add another put command to put the variable
    // there. Will do this by creating a new command and parsing it later once all
    // components are read in
    sprintf(inter.tmp_put_cmds[inter.num_tmp_put_cmds++], "put %s %s %s", name, variable, fstr);
275
    
276 277 278
    return false;
}

279 280 281 282 283 284
int check_mhomodyne(char* s) {
    int i = 0;
    
    char buffer[10] = {0};

    for(i=1; i<=MAX_MHOMODYNE; i++){
285
        sprintf(buffer, "q%ihd", i);
286

287
        if (strncasecmp(s, buffer, strlen(buffer))==0){
288 289 290 291 292 293 294
            return i;
        }
    }
    
    return 0;
}

295
/** Iterates over all commands and checks if they are valid
296
 */
297
void check_all_commands(FILE *fp){
Daniel Brown's avatar
Daniel Brown committed
298
    assert(fp != NULL);
299 300
    char s0[LINE_LEN] = {0};
    char *s = NULL;
301 302 303 304 305 306
    
    while (fgets(s0, LINE_LEN - 1, fp) != NULL) {
        s = s0 + strspn(s0, WHITESPACE); /* skip whitespace */
        if (prepare_line(s, 1)) {
            continue;
        }
Daniel Brown's avatar
Daniel Brown committed
307

308 309
        insert_constants_values(s);

310 311 312 313
        if((strncasecmp(s, "fsig* ", 5) == 0)) {
            gerror("Line '%s':\n'fsig*' is no longer used, to apply force use the force parameter in a normal fsig command, e.g. 'Fz'\n");
        }
        
Daniel Brown's avatar
Daniel Brown committed
314
        if (((strncasecmp(s, "mod", 3) == 0) ||
315 316 317 318 319 320 321 322
                (strncasecmp(s, "m* ", 3) == 0) ||
                (strncasecmp(s, "m1 ", 3) == 0) ||
                (strncasecmp(s, "m2 ", 3) == 0) ||
                (strncasecmp(s, "m ", 2) == 0) ||
                (strncasecmp(s, "mirror ", 7) == 0) ||
                (strncasecmp(s, "mirror* ", 8) == 0) ||
                (strncasecmp(s, "mirror1 ", 8) == 0) ||
                (strncasecmp(s, "mirror2 ", 8) == 0) ||
323
                (strncasecmp(s, "block ", 6) == 0) ||
324 325 326 327 328 329 330 331 332 333 334 335 336
                (strncasecmp(s, "bs* ", 4) == 0) ||
                (strncasecmp(s, "bs1 ", 4) == 0) ||
                (strncasecmp(s, "bs2 ", 4) == 0) ||
                (strncasecmp(s, "bs ", 3) == 0) ||
                (strncasecmp(s, "beamsplitter* ", 14) == 0) ||
                (strncasecmp(s, "beamsplitter1 ", 14) == 0) ||
                (strncasecmp(s, "beamsplitter2 ", 14) == 0) ||
                (strncasecmp(s, "beamsplitter ", 13) == 0) ||
                (strncasecmp(s, "s ", 2) == 0) ||
                (strncasecmp(s, "space ", 6) == 0) ||
                (strncasecmp(s, "sagnac ", 7) == 0) ||
                (strncasecmp(s, "squeezer ", 9) == 0) ||
                (strncasecmp(s, "sq ", 3) == 0) ||
337
                (strncasecmp(s, "sq* ", 4) == 0) ||
338
                (strncasecmp(s, "lens ", 5) == 0) ||
Daniel Brown's avatar
Daniel Brown committed
339
                (strncasecmp(s, "lens* ", 6) == 0) ||
340 341 342 343
                (strncasecmp(s, "l ", 2) == 0) ||
                (strncasecmp(s, "laser ", 6) == 0) ||
                (strncasecmp(s, "light ", 6) == 0) ||
                (strncasecmp(s, "isol ", 5) == 0) ||
344
                (strncasecmp(s, "dbs ", 4) == 0) ||
345 346
                (strncasecmp(s, "diode ", 6) == 0) ||
                (strncasecmp(s, "variable ", 9) == 0) ||
347
                (strncasecmp(s, "var ", 4) == 0) ||
348 349 350 351 352 353 354
                (strncasecmp(s, "fsig ", 4) == 0) ||
                (strncasecmp(s, "gauss ", 6) == 0) ||
                (strncasecmp(s, "gauss* ", 7) == 0) ||
                (strncasecmp(s, "gauss** ", 8) == 0) ||
                (strncasecmp(s, "shot ", 5) == 0) ||
                (strncasecmp(s, "qshot ", 6) == 0) ||
                (strncasecmp(s, "qshotS ", 7) == 0) ||
355
                (strncasecmp(s, "qshotN ", 7) == 0) ||
356 357 358 359
                (strncasecmp(s, "pdS", 3) == 0) ||
                (strncasecmp(s, "pdN", 3) == 0) ||
                (strncasecmp(s, "pd", 2) == 0) ||
                (strncasecmp(s, "ad ", 3) == 0) ||
360
                (strncasecmp(s, "fd ", 3) == 0) ||
Daniel Brown's avatar
Daniel Brown committed
361
                (strncasecmp(s, "pgaind ", 7) == 0) ||
Daniel Brown's avatar
Daniel Brown committed
362
                (strncasecmp(s, "slink ", 6) == 0) ||
Daniel Brown's avatar
Daniel Brown committed
363
                (strncasecmp(s, "sd ", 3) == 0) ||
364
                (strncasecmp(s, "sd* ", 4) == 0) ||
365
                (strncasecmp(s, "xd ", 3) == 0) ||
366
                (strncasecmp(s, "hd ", 3) == 0) ||
367 368
                (strncasecmp(s, "qhdN ", 5) == 0) ||
                (strncasecmp(s, "qhdS ", 5) == 0) ||
369
                (strncasecmp(s, "qhd ", 4) == 0) ||
370
                (check_mhomodyne(s)) ||
371
                (strncasecmp(s, "xlink ", 6) == 0) ||
372
                (strncasecmp(s, "qd ", 3) == 0) ||
373
                (strncasecmp(s, "qnoised ", 8) == 0) ||
374 375
                (strncasecmp(s, "qnoisedS ", 9) == 0) ||
                (strncasecmp(s, "qnoisedN ", 9) == 0) ||
376
                (strncasecmp(s, "attr ", 5) == 0) ||
377
                (strncasecmp(s, "smotion ", 8) == 0) ||
378 379 380 381 382 383 384 385 386
                (strncasecmp(s, "map ", 4) == 0) ||
                //        (strncasecmp(s, "coefficients ", 13) == 0) ||
                (strncasecmp(s, "cav ", 4) == 0) ||
                (strncasecmp(s, "cavity ", 7) == 0) ||
                (strncasecmp(s, "color", 5) == 0) ||
                (strncasecmp(s, "width", 5) == 0) ||
                (strncasecmp(s, "deriv_h", 7) == 0) ||
                (strncasecmp(s, "scale", 5) == 0) ||
                (strncasecmp(s, "time", 4) == 0) ||
387
                (strncasecmp(s, "noscripts", 9) == 0) ||
388 389
                (strncasecmp(s, "noxaxis", 7) == 0) ||
                (strncasecmp(s, "multi", 5) == 0) ||
390
                (strncasecmp(s, "subplot", 7) == 0) ||
391 392
                (strncasecmp(s, "pause", 5) == 0) ||
                (strncasecmp(s, "printmatrix", 11) == 0) ||
393
                (strncasecmp(s, "printnoises", 11) == 0) ||
394
                (strncasecmp(s, "mismatches", 10) == 0) ||
395
                (strncasecmp(s, "trace", 5) == 0) ||
396
                (strncasecmp(s, "powers", 6) == 0) ||
397 398 399 400 401 402 403 404
                (strncasecmp(s, "grating", 7) == 0) ||
                (strncasecmp(s, "gr", 2) == 0) ||
                (strncasecmp(s, "noise", 5) == 0) ||
                (strncasecmp(s, "debug", 5) == 0) ||
                (strncasecmp(s, "phase ", 6) == 0) ||
                (strncasecmp(s, "gnuterm", 7) == 0) ||
                (strncasecmp(s, "pyterm", 6) == 0) ||
                (strncasecmp(s, "gnuplot", 7) == 0) ||
405
                (strncasecmp(s, "python", 6) == 0) ||
406 407 408 409 410 411 412 413
                (strncasecmp(s, "matlabplot", 10) == 0) ||
                (strncasecmp(s, "matlab", 6) == 0) ||
                (strncasecmp(s, "diff", 4) == 0) ||
                (strncasecmp(s, "noplot", 6) == 0) ||
                (strncasecmp(s, "put ", 4) == 0) ||
                (strncasecmp(s, "put*", 4) == 0) ||
                (strncasecmp(s, "set ", 4) == 0) ||
                (strncasecmp(s, "showiterate ", 12) == 0) ||
414 415
                (strncasecmp(s, "minimize ", 9) == 0) ||
                (strncasecmp(s, "maximize ", 9) == 0) ||
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
                (strncasecmp(s, "lock ", 5) == 0) ||
                (strncasecmp(s, "lock*", 5) == 0) ||
                (strncasecmp(s, "func ", 5) == 0) ||
                (strncasecmp(s, "func*", 5) == 0) ||
                (strncasecmp(s, "xaxis*", 6) == 0) ||
                (strncasecmp(s, "xaxis", 5) == 0) ||
                (strncasecmp(s, "x3axis", 6) == 0) ||
                (strncasecmp(s, "x2axis*", 7) == 0) ||
                (strncasecmp(s, "x2axis", 6) == 0) ||
                (strncasecmp(s, "yaxis", 5) == 0) ||
                (strncasecmp(s, "maxtem ", 7) == 0) ||
                (strncasecmp(s, "knm ", 4) == 0) ||
                (strncasecmp(s, "beam ", 5) == 0) ||
                (strncasecmp(s, "tem ", 4) == 0) ||
                (strncasecmp(s, "tem* ", 5) == 0) ||
                (strncasecmp(s, "mask ", 5) == 0) ||
                (strncasecmp(s, "pdtype ", 7) == 0) ||
                (strncasecmp(s, "const ", 6) == 0) ||
                (strncasecmp(s, "bp ", 3) == 0) ||
                (strncasecmp(s, "cp ", 3) == 0) ||
                (strncasecmp(s, "conv ", 5) == 0) ||
                (strncasecmp(s, "mp ", 3) == 0) ||
                (strncasecmp(s, "gouy ", 5) == 0) ||
                (strncasecmp(s, "retrace ", 8) == 0) ||
                (strncasecmp(s, "video ", 5) == 0) ||
                (strncasecmp(s, "lambda ", 5) == 0) ||
                (strncasecmp(s, "intmethod ", 10) == 0) ||
                (strncasecmp(s, "/*", 2) == 0) ||
                (strncasecmp(s, "conf ", 5) == 0) ||
                (strncasecmp(s, "startnode ", 10) == 0) ||
                (strncasecmp(s, "mf ", 3) == 0) ||
Daniel Brown's avatar
Daniel Brown committed
447
                (strncasecmp(s, "fadd ", 5) == 0) ||
448
                (strncasecmp(s, "tf ", 3) == 0) ||
449
                (strncasecmp(s, "tf2 ", 4) == 0) ||
450 451
                (strncasecmp(s, "frequency ", 10) == 0 ) ||
                (strncasecmp(s, "dither ", 7) == 0 ) ||
452 453
                (strncasecmp(s, "vacuum ", 7) == 0 ) ||
                (strncasecmp(s, "!include", 8) == 0 )
454
                ))  {
Daniel Brown's avatar
Daniel Brown committed
455 456 457 458 459

            // found good component !
            if (strncasecmp(s, "gnuplot", 7) == 0) {
                read_init_dummy(fp, 0); // skip GNUPLOT lines
            }
460 461 462
            if (strncasecmp(s, "python", 6) == 0) {
                read_init_dummy(fp, 0); // skip PYTHON lines
            }
Daniel Brown's avatar
Daniel Brown committed
463 464 465 466 467 468 469 470 471 472
            if (strncasecmp(s, "matlabplot", 10) == 0) {
                read_init_dummy(fp, 0); // skip MATLAB lines
            } else if (strncasecmp(s, "matlab", 6) == 0) {
                read_init_dummy(fp, 0); // skip MATLAB lines
            }

            if (strncasecmp(s, "/*", 2) == 0) {
                read_init_dummy(fp, 1); // skip comments lines
            }
        } else {
473 474 475
            gerror("line '%s':\nno such command or component\n", s);
        }
    }
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    
    rewind(fp);
}

//! Read the input file

/*!
 * \param fp the file pointer
 *
 * \todo m1, m2, mirror1, mirror2, bs1, bs2, beamsplitter1, beamsplitter2,
 * squeezer, laser, grating, matlab branches/commands untested
 *
 * \todo qshotS, pdN, x3axis, x3axis*, showiterate, pause, video
 * branches/commands untested
 *
 * \todo break up into smaller routines!!!
 */
void read_file(FILE *fp) {
	char s0[LINE_LEN] = {0};
    char *s;

    // make sure the file pointer is non-null
    assert(fp != NULL);

    endcomp = 0;
    lastparam = -1;

    rewind(fp);
504

505 506 507 508 509 510
    if (inter.debug & 512) {
        message("Reading input file:\n");
        fflush(stdout);
    }
    
    // do initial check to ensure all command names are valid
511
    rewind(fp);
512 513

    check_all_commands(fp);
514
    
Daniel Brown's avatar
Daniel Brown committed
515 516
    while (fgets(s0, LINE_LEN - 1, fp) != NULL) {
        s = s0 + strspn(s0, WHITESPACE); /* skip whitespace */
517
        check_for_long_line(fp,s);
Daniel Brown's avatar
Daniel Brown committed
518 519 520
        if (prepare_line(s, 1)) {
            continue;
        }
521 522 523 524
        
        if (strncasecmp(s, "!include", 8) == 0)
            continue;
            
Daniel Brown's avatar
Daniel Brown committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
        insert_constants_values(s);

        if (strncasecmp(s, "m* ", 3) == 0) {
            read_mirror2(s);
        } else if (strncasecmp(s, "m1 ", 3) == 0) {
            read_mirror(s, 1);
        } else if (strncasecmp(s, "m2 ", 3) == 0) {
            read_mirror(s, 2);
        } else if (strncasecmp(s, "m ", 2) == 0) {
            read_mirror(s, 0);
        } else if (strncasecmp(s, "mirror ", 7) == 0) {
            read_mirror(s, 0);
        } else if (strncasecmp(s, "mirror* ", 8) == 0) {
            read_mirror2(s);
        } else if (strncasecmp(s, "mirror1 ", 8) == 0) {
            read_mirror(s, 1);
        } else if (strncasecmp(s, "mirror2 ", 8) == 0) {
            read_mirror(s, 2);
        } else if (strncasecmp(s, "bs* ", 4) == 0) {
            read_beamsplitter2(s);
        } else if (strncasecmp(s, "bs1 ", 4) == 0) {
            read_beamsplitter(s, 1);
        } else if (strncasecmp(s, "bs2 ", 4) == 0) {
            read_beamsplitter(s, 2);
        } else if (strncasecmp(s, "bs ", 3) == 0) {
            read_beamsplitter(s, 0);
        } else if (strncasecmp(s, "beamsplitter* ", 14) == 0) {
            read_beamsplitter2(s);
        } else if (strncasecmp(s, "beamsplitter1 ", 14) == 0) {
            read_beamsplitter(s, 1);
        } else if (strncasecmp(s, "beamsplitter2 ", 14) == 0) {
            read_beamsplitter(s, 2);
        } else if (strncasecmp(s, "beamsplitter ", 13) == 0) {
            read_beamsplitter(s, 0);
559
        } else if (strncasecmp(s, "sagnac ", 7) == 0) {
Daniel Brown's avatar
Daniel Brown committed
560
            read_sagnac(s);
561 562
        } else if (strncasecmp(s, "block ", 6) == 0) {
            read_block(s);
Daniel Brown's avatar
Daniel Brown committed
563 564 565 566
        } else if (strncasecmp(s, "s ", 2) == 0 ||
                strncasecmp(s, "space ", 6) == 0) {
            read_space(s);
        } else if (strncasecmp(s, "squeezer ", 9) == 0 ||
567 568
                strncasecmp(s, "sq ", 3) == 0 ||
                strncasecmp(s, "sq* ", 4) == 0) {
Daniel Brown's avatar
Daniel Brown committed
569 570
            read_squeezer(s);
        } else if (strncasecmp(s, "lens ", 5) == 0) {
571 572 573
            read_lens(s, false);
        } else if (strncasecmp(s, "lens* ", 6) == 0) {
            read_lens(s, true);
Daniel Brown's avatar
Daniel Brown committed
574 575 576 577 578 579 580 581 582 583
        } else if (strncasecmp(s, "l ", 2) == 0) {
            read_light_in(s);
        } else if (strncasecmp(s, "laser ", 6) == 0) {
            read_light_in(s);
        } else if (strncasecmp(s, "light ", 6) == 0) {
            read_light_in(s);
        } else if (strncasecmp(s, "grating", 7) == 0) {
            read_verbose_grating(s);
        } else if (strncasecmp(s, "gr", 2) == 0) {
            read_grating(s);
584
        } else if (strncasecmp(s, "isol ", 5) == 0 || strncasecmp(s, "diode ", 6) == 0) {
Daniel Brown's avatar
Daniel Brown committed
585
            read_diode(s);
586 587
        } else if (strncasecmp(s, "dbs ", 4) == 0) {
            read_dbs(s);
Daniel Brown's avatar
Daniel Brown committed
588 589
        } else if (strncasecmp(s, "variable ", 9) == 0) {
            read_variable(s);
590 591
        } else if (strncasecmp(s, "var ", 4) == 0) {
            read_variable(s);
Daniel Brown's avatar
Daniel Brown committed
592 593 594 595
        } else if (strncasecmp(s, "intmethod ", 10) == 0) {
            read_intmethod(s);
        } else if (strncasecmp(s, "gnuplot", 7) == 0) {
            read_init_dummy(fp, 0);
596 597
        } else if (strncasecmp(s, "python", 6) == 0) {
            read_init_dummy(fp, 0);
Daniel Brown's avatar
Daniel Brown committed
598 599 600 601 602 603
        } else if (strncasecmp(s, "matlabplot", 10) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "matlab", 6) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "/*", 2) == 0) {
            read_init_dummy(fp, 1);
Daniel Brown's avatar
Daniel Brown committed
604 605
        } else if (strncasecmp(s, "knm ", 4) == 0) {
            read_knm(s);
Daniel Brown's avatar
Daniel Brown committed
606
        } else if (strncasecmp(s, "mf ", 3) == 0 || strncasecmp(s, "fadd ", 3) == 0) {
607
            read_manual_frequency(s,0);
608
        } else if (strncasecmp(s, "tf2 ", 4) == 0) {
609
            read_general_mech_transfer_function(s);
610
        } else if (strncasecmp(s, "tf ", 3) == 0) {
611
            read_QF_mech_transfer_function(s);
Daniel Brown's avatar
Daniel Brown committed
612 613
        }
    }
614

Daniel Brown's avatar
Daniel Brown committed
615 616 617 618
    if (inter.debug & 512) {
        message("  ....1..");
        fflush(stdout);
    }
Daniel Brown's avatar
Daniel Brown committed
619

Daniel Brown's avatar
Daniel Brown committed
620
    rewind(fp);
Daniel Brown's avatar
Daniel Brown committed
621

Daniel Brown's avatar
Daniel Brown committed
622 623
    while (fgets(s0, LINE_LEN - 1, fp) != NULL) {
        s = s0 + strspn(s0, WHITESPACE); /* skip whitespace */
624

Daniel Brown's avatar
Daniel Brown committed
625 626 627
        if (prepare_line(s, 1)) {
            continue;
        }
Daniel Brown's avatar
Daniel Brown committed
628

Daniel Brown's avatar
Daniel Brown committed
629 630 631 632
        insert_constants_values(s);

        if (strncasecmp(s, "mod", 3) == 0) {
            read_modulator(s);
633 634
        } else if (strncasecmp(s, "dof", 3) == 0) {
            read_dof(s);
635 636
        } else if (strncasecmp(s, "dither", 6) == 0) {
            read_dither(s);
Daniel Brown's avatar
Daniel Brown committed
637 638
        } else if (strncasecmp(s, "gnuplot", 7) == 0) {
            read_init_dummy(fp, 0);
639 640
        } else if (strncasecmp(s, "python", 6) == 0) {
            read_init_dummy(fp, 0);
Daniel Brown's avatar
Daniel Brown committed
641 642 643 644 645 646
        } else if (strncasecmp(s, "matlabplot", 10) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "matlab", 6) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "/*", 2) == 0) {
            read_init_dummy(fp, 1);
647 648 649
        }  else if (strncasecmp(s, "attr ", 5) == 0) {
            read_attribute(s);
        } 
Daniel Brown's avatar
Daniel Brown committed
650
    }
Daniel Brown's avatar
Daniel Brown committed
651

Daniel Brown's avatar
Daniel Brown committed
652 653 654 655
    if (inter.debug & 512) {
        message("..2..");
        fflush(stdout);
    }
Daniel Brown's avatar
Daniel Brown committed
656

657 658 659 660 661 662 663 664 665 666 667 668
    rewind(fp);
    
    while (fgets(s0, LINE_LEN - 1, fp) != NULL) {
        s = s0 + strspn(s0, WHITESPACE); /* skip whitespace */

        if (prepare_line(s, 1)) {
            continue;
        }

        insert_constants_values(s);
    }
    
Daniel Brown's avatar
Daniel Brown committed
669
    rewind(fp);
Daniel Brown's avatar
Daniel Brown committed
670

Daniel Brown's avatar
Daniel Brown committed
671 672 673 674 675
    while (fgets(s0, LINE_LEN - 1, fp) != NULL) {
        s = s0 + strspn(s0, WHITESPACE); /* skip whitespace */
        if (prepare_line(s, 1)) {
            continue;
        }
Daniel Brown's avatar
Daniel Brown committed
676

Daniel Brown's avatar
Daniel Brown committed
677
        insert_constants_values(s);
Daniel Brown's avatar
Daniel Brown committed
678

Daniel Brown's avatar
Daniel Brown committed
679
        if (strncasecmp(s, "fsig ", 4) == 0) {
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
					read_fsig(s);
				}
				else if (strncasecmp(s, "gnuplot", 7) == 0) {
					read_init_dummy(fp, 0);
				}
				else if (strncasecmp(s, "python", 6) == 0) {
					read_init_dummy(fp, 0);
				}
				else if (strncasecmp(s, "matlabplot", 10) == 0) {
					read_init_dummy(fp, 0);
				}
				else if (strncasecmp(s, "matlab", 6) == 0) {
					read_init_dummy(fp, 0);
				}
				else if (strncasecmp(s, "shot ", 5) == 0) {
Daniel Brown's avatar
Daniel Brown committed
695 696
			//      if (!inter.beam.set)  // ignoring other detetcors if beam analyser is set!
			read_shot(s);
697 698 699
		} else if (strncasecmp(s, "qshot ", 6) == 0 
                        || strncasecmp(s, "qshotS ", 7) == 0
                        || strncasecmp(s, "qshotN ", 7) == 0) {
Daniel Brown's avatar
Daniel Brown committed
700 701 702 703 704 705 706 707 708 709 710 711
			read_quantum_shot(s);
		} else if (strncasecmp(s, "pdtype ", 7) == 0) {
			continue;
		} else if (strncasecmp(s, "pdS", 3) == 0) {
			//  if (!inter.beam.set)
			read_light_out(s);
		} else if (strncasecmp(s, "pdN", 3) == 0) {
			// if (!inter.beam.set)
			read_light_out(s);
		} else if (strncasecmp(s, "pd", 2) == 0) {
			// if (!inter.beam.set)
			read_light_out(s);
712 713 714
        } else if (strncasecmp(s, "hd ", 3) == 0 || strncasecmp(s, "qhd ", 4) == 0
                    || strncasecmp(s, "qhdN ", 5) == 0 || strncasecmp(s, "qhdS ", 5) == 0) {
            read_hd_out(s);
715 716
        } else if (check_mhomodyne(s) > 0){
            read_mhd_out(s);
717 718
        } else if (strncasecmp(s, "sd ", 3) == 0 || strncasecmp(s, "sd* ", 4) == 0) {
            read_sd_out(s);
Daniel Brown's avatar
Daniel Brown committed
719 720
		} else if (strncasecmp(s, "ad ", 3) == 0) {
			read_amp_out(s);
721
                } else if (strncasecmp(s, "qd ", 3) == 0) {
722
                    read_qd_out(s);
723 724
                } else if (strncasecmp(s, "xlink ", 6) == 0) {
                    read_motion_link(s);
725
                } else if (strncasecmp(s, "xd ", 3) == 0) {
726
                    read_motion_out(s);
Daniel Brown's avatar
Daniel Brown committed
727
                } else if (strncasecmp(s, "pgaind ", 7) == 0) {
728
                    read_openloopTF_out(s);
729 730
                } else if (strncasecmp(s, "fd ", 3) == 0) {
                    read_force_out(s);
Daniel Brown's avatar
Daniel Brown committed
731 732
                } else if (strncasecmp(s, "slink ", 6) == 0) {
                    read_slink(s);
Daniel Brown's avatar
Daniel Brown committed
733 734
                } else if (strncasecmp(s, "vacuum ", 7) == 0) {
                    read_vacuum(s, false);
735 736 737
		} else if (strncasecmp(s, "qnoised ", 8) == 0
                        || strncasecmp(s, "qnoisedS ", 9) == 0
                        || strncasecmp(s, "qnoisedN ", 9) == 0) {
738
                    read_qnoised_cmd(s);
Daniel Brown's avatar
Daniel Brown committed
739
		} else if (strncasecmp(s, "bp ", 3) == 0) {
740
                    read_beamparam_out(s);
Daniel Brown's avatar
Daniel Brown committed
741
		}
adf's avatar
 
adf committed
742
#ifdef DEVELOP
Daniel Brown's avatar
Daniel Brown committed
743 744 745 746 747
		else if (strncasecmp(s, "conv ", 5) == 0) {
			read_convolution_out(s);
		} else if (strncasecmp(s, "mp ", 3) == 0) {
			read_mirror_phase_out(s);
		}
adf's avatar
 
adf committed
748
#endif
Daniel Brown's avatar
Daniel Brown committed
749 750 751
		else if (strncasecmp(s, "gouy ", 5) == 0) {
			// if (!inter.beam.set)
			read_gouy_out(s);
752
		}else if (strncasecmp(s, "map ", 4) == 0) {
Daniel Brown's avatar
Daniel Brown committed
753
			read_maps(s);
754 755
		} else if (strncasecmp(s, "smotion ", 8) == 0) {
			read_surface_motion_map(s);
756
        } else if (strncasecmp(s, "beam ", 5) == 0) {
Daniel Brown's avatar
Daniel Brown committed
757 758
			read_beam(s);
		} else if (strncasecmp(s, "cav ", 4) == 0 ||
759
			strncasecmp(s, "cavity ", 7) == 0) {
Daniel Brown's avatar
Daniel Brown committed
760
			read_cavity(s);
761 762 763
		} else if (strncasecmp(s, "gauss** ", 8) == 0) {
			read_gaussRc(s);
        } else if (strncasecmp(s, "gauss* ", 7) == 0) {
Daniel Brown's avatar
Daniel Brown committed
764 765 766
			read_gauss2(s);
		} else if (strncasecmp(s, "gauss ", 6) == 0) {
			read_gauss(s);
767
    } else if (strncasecmp(s, "tem ", 4) == 0) {
768 769 770
			read_ltem(s, false);
		} else if (strncasecmp(s, "tem* ", 5) == 0) {
			read_ltem(s, true);
Daniel Brown's avatar
Daniel Brown committed
771 772
		} else if (strncasecmp(s, "retrace ", 8) == 0) {
			read_retrace(s);
773
		}     
Daniel Brown's avatar
Daniel Brown committed
774 775 776 777 778 779
        else if (strncasecmp(s, "conf ", 5) == 0) {
            read_conf(s);
        } else if (strncasecmp(s, "/*", 2) == 0) {
            read_init_dummy(fp, 1);
        }
    }
780
    
Daniel Brown's avatar
Daniel Brown committed
781
    rewind(fp);
782

Daniel Brown's avatar
Daniel Brown committed
783 784 785 786
    if (inter.debug & 512) {
        message("..3..");
        fflush(stdout);
    }
Daniel Brown's avatar
Daniel Brown committed
787

788

Daniel Brown's avatar
Daniel Brown committed
789 790 791 792 793
    while (fgets(s0, LINE_LEN - 1, fp) != NULL) {
        s = s0 + strspn(s0, WHITESPACE); /* skip whitespace */
        if (prepare_line(s, 1)) {
            continue;
        }
794

Daniel Brown's avatar
Daniel Brown committed
795 796 797 798
        insert_constants_values(s);

        if (strncasecmp(s, "set ", 4) == 0) {
            read_set_command(s);
799 800
        } else if (strncasecmp(s, "cp ", 3) == 0) {
            read_cavity_param_out(s);
801
        } else if (strncasecmp(s, "gnuplot", 7) == 0) {
Daniel Brown's avatar
Daniel Brown committed
802
            read_init_dummy(fp, 0);
803 804
        } else if (strncasecmp(s, "python", 6) == 0) {
            read_init_dummy(fp, 0);
Daniel Brown's avatar
Daniel Brown committed
805 806 807 808 809 810 811 812
        } else if (strncasecmp(s, "matlabplot", 10) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "matlab", 6) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "/*", 2) == 0) {
            read_init_dummy(fp, 1);
        }
    }
813

Daniel Brown's avatar
Daniel Brown committed
814
    rewind(fp);
cochrane's avatar
cochrane committed
815

Daniel Brown's avatar
Daniel Brown committed
816 817 818 819
    if (inter.debug & 512) {
        message("..(y)..");
        fflush(stdout);
    }
820
    
Daniel Brown's avatar
Daniel Brown committed
821 822
    while (fgets(s0, LINE_LEN - 1, fp) != NULL) {
        s = s0 + strspn(s0, WHITESPACE); /* skip whitespace */
Daniel Brown's avatar
Daniel Brown committed
823

Daniel Brown's avatar
Daniel Brown committed
824 825 826
        if (prepare_line(s, 1)) {
            continue;
        }
cochrane's avatar
cochrane committed
827

Daniel Brown's avatar
Daniel Brown committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
        insert_constants_values(s);

        if (strncasecmp(s, "xaxis*", 6) == 0) {
            read_xaxis2(s);
        } else if (strncasecmp(s, "xaxis", 5) == 0) {
            read_xaxis(s);
        } else if (strncasecmp(s, "x3axis*", 7) == 0) {
            read_x3axis2(s);
        } else if (strncasecmp(s, "x3axis", 6) == 0) {
            read_x3axis(s);
        } else if (strncasecmp(s, "x2axis*", 7) == 0) {
            read_x2axis2(s);
        } else if (strncasecmp(s, "x2axis", 6) == 0) {
            read_x2axis(s);
        } else if (strncasecmp(s, "noxaxis ", 8) == 0) {
            inter.noxaxis = 1;
        } else if (strncasecmp(s, "gnuplot", 7) == 0) {
            read_init_dummy(fp, 0);
846 847
        } else if (strncasecmp(s, "python", 6) == 0) {
            read_init_dummy(fp, 0);
Daniel Brown's avatar
Daniel Brown committed
848 849 850 851 852 853 854 855
        } else if (strncasecmp(s, "matlabplot", 10) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "matlab", 6) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "/*", 2) == 0) {
            read_init_dummy(fp, 1);
        }
    }
856

Daniel Brown's avatar
Daniel Brown committed
857 858 859
    if (inter.x1.xtype == NO_FREQ && inter.noxaxis == 0) {
        gerror("missing 'xaxis' instruction\n");
    }
860

Daniel Brown's avatar
Daniel Brown committed
861
    rewind(fp);
Daniel Brown's avatar
Daniel Brown committed
862

Daniel Brown's avatar
Daniel Brown committed
863 864
    while (fgets(s0, LINE_LEN - 1, fp) != NULL) {
        s = s0 + strspn(s0, WHITESPACE); /* skip whitespace */
Daniel Brown's avatar
Daniel Brown committed
865

Daniel Brown's avatar
Daniel Brown committed
866 867 868
        if (prepare_line(s, 1)) {
            continue;
        }
Daniel Brown's avatar
Daniel Brown committed
869

Daniel Brown's avatar
Daniel Brown committed
870 871 872 873 874 875 876 877 878 879 880 881
        insert_constants_values(s);

        if (strncasecmp(s, "func ", 5) == 0) {
            read_func_command(s);
        } else if (strncasecmp(s, "showiterate ", 12) == 0) {
            read_showiterate(s);
        } else if (strncasecmp(s, "lock ", 5) == 0) {
            read_lock_command(s);
        } else if (strncasecmp(s, "lock* ", 6) == 0) {
            read_lock2(s);
        } else if (strncasecmp(s, "gnuplot", 7) == 0) {
            read_init_dummy(fp, 0);
882 883
        } else if (strncasecmp(s, "python", 6) == 0) {
            read_init_dummy(fp, 0);
Daniel Brown's avatar
Daniel Brown committed
884 885 886 887 888 889 890 891
        } else if (strncasecmp(s, "matlabplot", 10) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "matlab", 6) == 0) {
            read_init_dummy(fp, 0);
        } else if (strncasecmp(s, "/*", 2) == 0) {
            read_init_dummy(fp, 1);
        }
    }
Daniel Brown's avatar
Daniel Brown committed
892

Daniel Brown's avatar
Daniel Brown committed
893
    rewind(fp);
Daniel Brown's avatar
Daniel Brown committed
894

Daniel Brown's avatar
Daniel Brown committed
895 896 897 898
    if (inter.debug & 512) {
        message("..(x)..");
        fflush(stdout);
    }
Daniel Brown's avatar
Daniel Brown committed
899

900 901
    int i;
    for(i=0; i<mem.num_tmp_put_cmds; i++){
Daniel Brown's avatar
Daniel Brown committed
902
        if(strlen(inter.tmp_put_cmds[i]) > 0) {
903
            read_put_command(inter.tmp_put_cmds[i]);
Daniel Brown's avatar
Daniel Brown committed
904
        }
905 906
    }
    
Daniel Brown's avatar
Daniel Brown committed
907
    while (fgets(s0, LINE_LEN - 1, fp) != NULL) {
908 909 910 911
        s = s0 + strspn(s0, WHITESPACE); /* skip whitespace */
        if (prepare_line(s, 1)) {
                continue;
        }
Daniel Brown's avatar
Daniel Brown committed
912

913
        insert_constants_values(s);
Daniel Brown's avatar
Daniel Brown committed
914

915
        if (strncasecmp(s, "noplot ", 7) == 0) {
916
            read_noplot(s);
917
        } else if (strncasecmp(s, "put ", 4) == 0) {
918
            read_put_command(s);
919
        }
Daniel Brown's avatar
Daniel Brown committed
920

921 922 923 924 925 926 927 928 929 930 931 932 933 934
        if (strncasecmp(s, "put* ", 5) == 0) {
                read_put_star_command(s);
        } else if (strncasecmp(s, "color", 5) == 0) {
                read_color(s);
        } else if (strncasecmp(s, "mask ", 5) == 0) {
                read_mask(s);
        } else if (strncasecmp(s, "width", 5) == 0) {
                read_width(s);
        } else if (strncasecmp(s, "deriv_h", 7) == 0) {
                read_deriv_h(s);
        } else if (strncasecmp(s, "scale", 5) == 0) {
                read_scale(s);
        } else if (strncasecmp(s, "time", 4) == 0) {
                inter.time = 1;
935 936
        } else if (strncasecmp(s, "noscripts", 9) == 0) {
                inter.noscripts = 1;
937 938
        } else if (strncasecmp(s, "multi", 5) == 0) {
                inter.multi = true;
939 940
        } else if (strncasecmp(s, "subplot", 7) == 0) {
                inter.subplot = true;
941 942
        } else if (strncasecmp(s, "pause", 5) == 0) {
                inter.pause = 1;
943 944
        } else if (strncasecmp(s, "printnoises", 11) == 0) {
                inter.printqnoiseinputs = true;
945 946
        } else if (strncasecmp(s, "printmatrix", 11) == 0) {
                inter.printmatrix = 1;
947
        } else if (strncasecmp(s, "mismatches", 10) == 0) {
Daniel Brown's avatar
Daniel Brown committed
948
            read_mismatches_command(s);
949 950
        } else if (strncasecmp(s, "frequency", 9) == 0) {
            read_frequency_command(s);
951 952
        } else if (strncasecmp(s, "powers", 6) == 0) {
                read_powers(s);
953 954 955
        } else if (strncasecmp(s, "trace", 5) == 0) {
                read_trace(s);
        }// this should already have been done in pre_scan()
Daniel Brown's avatar
Daniel Brown committed
956 957 958 959 960 961 962
            //else if (strncasecmp(s, "noise", 5) == 0) {
            //read_noise(s);
            //}
        else if (strncasecmp(s, "phase ", 6) == 0) {
            read_phase(s);
        } else if (strncasecmp(s, "gnuterm", 7) == 0) {
            read_gnuterm(s);
Andreas Freise's avatar
Andreas Freise committed
963 964
        } else if (strncasecmp(s, "pyterm", 6) == 0) {
            read_pyterm(s);
Daniel Brown's avatar
Daniel Brown committed
965 966
        } else if (strncasecmp(s, "gnuplot", 7) == 0) {
            ri_gnuplot(fp);
967 968
        } else if (strncasecmp(s, "python", 6) == 0) {
            ri_python(fp);
Daniel Brown's avatar
Daniel Brown committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982
        } else if (strncasecmp(s, "matlabplot", 10) == 0) {
            ri_matlabplot(fp);
        } else if (strncasecmp(s, "matlab", 6) == 0) {
            ri_matlab(fp);
        } else if (strncasecmp(s, "diff", 4) == 0) {
            read_deriv(s);
        } else if (strncasecmp(s, "yaxis", 5) == 0) {
            read_yaxis(s);
        } else if (strncasecmp(s, "video ", 5) == 0) {
            inter.video = 1;
        } else if (strncasecmp(s, "startnode ", 10) == 0) {
            read_startnode(s);
        } else if (strncasecmp(s, "pdtype ", 7) == 0) {
            read_pdtype(s);
983 984
        } else if (strncasecmp(s, "maximize ", 9) == 0 || strncasecmp(s, "minimize ", 9) == 0) { 
            read_minimize(s);
Daniel Brown's avatar
Daniel Brown committed
985 986 987 988
        } else if (strncasecmp(s, "/*", 2) == 0) {
            read_init_dummy(fp, 1);
        }
    }
Daniel Brown's avatar
Daniel Brown committed
989

Daniel Brown's avatar
Daniel Brown committed
990 991 992 993
    if (inter.debug & 512) {
        message("..4..");
        fflush(stdout);
    }
Daniel Brown's avatar
Daniel Brown committed
994

Daniel Brown's avatar
Daniel Brown committed
995
    rewind(fp);
996

997 998
    // TODO - is this actually needed this final check?
    check_all_commands(fp);
Daniel Brown's avatar
Daniel Brown committed
999

Daniel Brown's avatar
Daniel Brown committed
1000 1001 1002 1003
    if (inter.debug & 512) {
        message("..done!\n");
        fflush(stdout);
    }
1004

1005
    assert(inter.num_blocks == mem.num_blocks);
Daniel Brown's avatar
Daniel Brown committed
1006 1007 1008 1009 1010
    assert(inter.num_mirrors == mem.num_mirrors);
    assert(inter.num_spaces == mem.num_spaces);
    assert(inter.num_beamsplitters == mem.num_beamsplitters);
    assert(inter.num_modulators == mem.num_modulators);
    assert(inter.num_light_inputs == mem.num_light_inputs);
1011
    assert(inter.num_sagnacs == mem.num_sagnacs);
Daniel Brown's avatar
Daniel Brown committed
1012 1013 1014 1015 1016 1017
    assert(inter.num_diodes == mem.num_diodes);
    assert(inter.num_variables == mem.num_variables);
    assert(inter.num_lenses == mem.num_lenses);
    assert(inter.num_gratings == mem.num_gratings);
    assert(inter.num_components == mem.num_components);
    assert(inter.num_convolution_outputs == mem.num_convolution_outputs);
1018
    assert(inter.num_transfer_funcs == mem.num_transfer_funcs);
Daniel Brown's avatar
Daniel Brown committed
1019
    assert(inter.num_quad_outputs == mem.num_quad_outputs);
Daniel Brown's avatar
Daniel Brown committed
1020
    assert(inter.num_slinks == mem.num_feedbacks);
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
    assert(inter.num_dof == mem.num_dof);
}

void read_dof(const char* command_string) {
    char command_name[MAX_TOKEN_LEN] = {0};
    char dof_name[MAX_TOKEN_LEN] = {0};
    char parameter[MAX_TOKEN_LEN] = {0};
    char rest[MAX_TOKEN_LEN] = {0};
  
    int n = sscanf(command_string, "%s %s %s %s", command_name, dof_name, parameter, rest);
    
    if(n != 4 && strlen(rest) == 0)
1033
        gerror("Line `%s':\ndof usage is 'dof name parameter comp1 factor [comp2 factor [...]]'\n", command_string);
1034 1035 1036
    
    dof_t *dof = &inter.dof_list[inter.num_dof];
    
1037
    check_name(command_string, dof_name);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    strcpy(dof->name, dof_name);
    
    char tmp[MAX_TOKEN_LEN] = {0};
    
    do{
        char comp_name[MAX_TOKEN_LEN] = {0};
        char factor[MAX_TOKEN_LEN] = {0};
        
        strcpy(tmp, rest);

        n = sscanf(tmp, "%s %s %s", comp_name, factor, rest);

        if(n==0){
            break; // nothing to process
        } if(n != 2 || n != 3) {
1053
            gerror("Line `%s':\nCould not process '%s' of the DOF inputs\n", command_string, tmp);
1054 1055 1056 1057 1058 1059
        } else {   
            dof->component_idx[dof->num_components] = get_component_index_from_name(comp_name);
            dof->component_type[dof->num_components] = get_component_type_decriment_index(&(dof->component_idx[dof->num_components]));
            
            if(dof->component_type[dof->num_components] != MIRROR
                    || dof->component_type[dof->num_components] != BEAMSPLITTER) {
1060
                gerror("Line `%s':\nDOF only supports mirrors and beamsplitters currently\n", command_string);        
1061 1062 1063
            }
                
            if(atod(factor, &(dof->factors[dof->num_components])))
1064
                gerror("Line `%s':\ncould not factor '%s' as a number\n", command_string, factor);        
1065 1066 1067 1068 1069 1070 1071 1072
            
            dof->num_components++;
        }

    }while(n > 2);
    
    dof->list_index = inter.num_dof;
    inter.num_dof++;
Daniel Brown's avatar
Daniel Brown committed
1073 1074
}

Daniel Brown's avatar
Daniel Brown committed
1075
void read_slink(const char* command_string) {
Daniel Brown's avatar
Daniel Brown committed
1076
    char command_name[MAX_TOKEN_LEN] = {0};
Daniel Brown's avatar
Daniel Brown committed
1077
    char slink_name[MAX_TOKEN_LEN] = {0};
Daniel Brown's avatar
Daniel Brown committed
1078 1079 1080 1081 1082 1083
    char output_name[MAX_TOKEN_LEN] = {0};
    char input_name[MAX_TOKEN_LEN] = {0};
    char input_motion[MAX_TOKEN_LEN] = {0};
    char transfer[MAX_TOKEN_LEN] = {0};
    char rest[MAX_TOKEN_LEN] = {0};
   
Daniel Brown's avatar
Daniel Brown committed
1084
    int n = sscanf(command_string, "%s %s %s %s %s %s %s", command_name, slink_name, output_name, input_name, input_motion, transfer, rest);
Daniel Brown's avatar
Daniel Brown committed
1085 1086
    
    if(n != 6 && n != 7)
1087
        gerror("Line `%s':\nslink usage is 'slink name output input motion transfer_function'\n", command_string, output_name);
Daniel Brown's avatar
Daniel Brown committed
1088
    
Daniel Brown's avatar
Daniel Brown committed
1089
    int idx = inter.num_slinks;
Daniel Brown's avatar
Daniel Brown committed
1090
    
Daniel Brown's avatar
Daniel Brown committed
1091
    slink_t *fb = &inter.slink_list[idx];
Daniel Brown's avatar
Daniel Brown committed
1092 1093 1094
    
    fb->list_index = idx;
    
Daniel Brown's avatar
Daniel Brown committed
1095
    strcpy(fb->name, slink_name);
Daniel Brown's avatar
Daniel Brown committed
1096 1097 1098 1099 1100
    strcpy(fb->motion_name, input_motion);
    
    fb->comp_list_idx = get_component_index_from_name(input_name);
    
    if(fb->comp_list_idx == NOT_FOUND)
1101
        gerror("Line `%s':\nCould not find input component '%s'\n", command_string, input_name);
Daniel Brown's avatar
Daniel Brown committed
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    
    fb->comp_type = get_component_type_decriment_index(&fb->comp_list_idx);
    
    if(fb->comp_type == MIRROR){
        mirror_t *m = &inter.mirror_list[fb->comp_list_idx];
        fb->comp_motion_idx = motion_string_to_index(input_motion, m->mass, m->Ix, m->Iy, m->num_surface_motions);
    } else if(fb->comp_type == BEAMSPLITTER){
        beamsplitter_t *bs = &inter.bs_list[fb->comp_list_idx];
        fb->comp_motion_idx = motion_string_to_index(input_motion, bs->mass, bs->Ix, bs->Iy, 0);
    } else {
1112
        gerror("Line `%s':\nInput component '%s' must be a mirror or beamsplitter\n", command_string, input_name);
Daniel Brown's avatar
Daniel Brown committed
1113 1114 1115
    }
    
    if(fb->comp_motion_idx == NOT_FOUND)
1116
        gerror("Line `%s':\nMotion '%s' is not available for component %s\n", command_string, input_motion, input_name);
Daniel Brown's avatar
Daniel Brown committed
1117 1118 1119 1120
    
    fb->output_list_idx = get_component_index_from_name(output_name);
    
    if(fb->output_list_idx == NOT_FOUND)
1121
        gerror("Line `%s':\nCould not find output detector '%s'\n", command_string, output_name);
Daniel Brown's avatar
Daniel Brown committed
1122 1123 1124 1125
    
    int