kat_read.c 453 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
			read_shot(s);
696 697 698
		} else if (strncasecmp(s, "qshot ", 6) == 0 
                        || strncasecmp(s, "qshotS ", 7) == 0
                        || strncasecmp(s, "qshotN ", 7) == 0) {
Daniel Brown's avatar
Daniel Brown committed
699 700 701 702 703 704 705 706 707 708 709 710
			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);
711 712 713
        } 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);
714 715
        } else if (check_mhomodyne(s) > 0){
            read_mhd_out(s);
716 717
        } else if (strncasecmp(s, "sd ", 3) == 0 || strncasecmp(s, "sd* ", 4) == 0) {
            read_sd_out(s);
Daniel Brown's avatar
Daniel Brown committed
718 719
		} else if (strncasecmp(s, "ad ", 3) == 0) {
			read_amp_out(s);
720
                } else if (strncasecmp(s, "qd ", 3) == 0) {
721
                    read_qd_out(s);
722 723
                } else if (strncasecmp(s, "xlink ", 6) == 0) {
                    read_motion_link(s);
724
                } else if (strncasecmp(s, "xd ", 3) == 0) {
725
                    read_motion_out(s);
Daniel Brown's avatar
Daniel Brown committed
726
                } else if (strncasecmp(s, "pgaind ", 7) == 0) {
727
                    read_openloopTF_out(s);
728 729
                } else if (strncasecmp(s, "fd ", 3) == 0) {
                    read_force_out(s);
Daniel Brown's avatar
Daniel Brown committed
730 731
                } else if (strncasecmp(s, "slink ", 6) == 0) {
                    read_slink(s);
Daniel Brown's avatar
Daniel Brown committed
732 733
                } else if (strncasecmp(s, "vacuum ", 7) == 0) {
                    read_vacuum(s, false);
734 735 736
		} else if (strncasecmp(s, "qnoised ", 8) == 0
                        || strncasecmp(s, "qnoisedS ", 9) == 0
                        || strncasecmp(s, "qnoisedN ", 9) == 0) {
737
                    read_qnoised_cmd(s);
Daniel Brown's avatar
Daniel Brown committed
738
		} else if (strncasecmp(s, "bp ", 3) == 0) {
739
                    read_beamparam_out(s);
Daniel Brown's avatar
Daniel Brown committed
740
		}
adf's avatar
 
adf committed
741
#ifdef DEVELOP
Daniel Brown's avatar
Daniel Brown committed
742 743 744 745 746
		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
747
#endif
Daniel Brown's avatar
Daniel Brown committed
748 749 750
		else if (strncasecmp(s, "gouy ", 5) == 0) {
			// if (!inter.beam.set)
			read_gouy_out(s);
751
		}else if (strncasecmp(s, "map ", 4) == 0) {
Daniel Brown's avatar
Daniel Brown committed
752
			read_maps(s);
753 754
		} else if (strncasecmp(s, "smotion ", 8) == 0) {
			read_surface_motion_map(s);
755
        } else if (strncasecmp(s, "beam ", 5) == 0) {
Daniel Brown's avatar
Daniel Brown committed
756 757
			read_beam(s);
		} else if (strncasecmp(s, "cav ", 4) == 0 ||
758
			strncasecmp(s, "cavity ", 7) == 0) {
Daniel Brown's avatar
Daniel Brown committed
759
			read_cavity(s);
760 761 762
		} else if (strncasecmp(s, "gauss** ", 8) == 0) {
			read_gaussRc(s);
        } else if (strncasecmp(s, "gauss* ", 7) == 0) {
Daniel Brown's avatar
Daniel Brown committed
763 764 765
			read_gauss2(s);
		} else if (strncasecmp(s, "gauss ", 6) == 0) {
			read_gauss(s);
766
    } else if (strncasecmp(s, "tem ", 4) == 0) {
767 768 769
			read_ltem(s, false);
		} else if (strncasecmp(s, "tem* ", 5) == 0) {
			read_ltem(s, true);
Daniel Brown's avatar
Daniel Brown committed
770 771
		} else if (strncasecmp(s, "retrace ", 8) == 0) {
			read_retrace(s);
772
		}     
Daniel Brown's avatar
Daniel Brown committed
773 774 775 776 777 778
        else if (strncasecmp(s, "conf ", 5) == 0) {
            read_conf(s);
        } else if (strncasecmp(s, "/*", 2) == 0) {
            read_init_dummy(fp, 1);
        }
    }
779
    
Daniel Brown's avatar
Daniel Brown committed
780
    rewind(fp);
781

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

787

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

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

        if (strncasecmp(s, "set ", 4) == 0) {
            read_set_command(s);
798 799
        } else if (strncasecmp(s, "cp ", 3) == 0) {
            read_cavity_param_out(s);
800
        } else if (strncasecmp(s, "gnuplot", 7) == 0) {
Daniel Brown's avatar
Daniel Brown committed
801
            read_init_dummy(fp, 0);
802 803
        } else if (strncasecmp(s, "python", 6) == 0) {
            read_init_dummy(fp, 0);
Daniel Brown's avatar
Daniel Brown committed
804 805 806 807 808 809
        } 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);
810
        } 
Daniel Brown's avatar
Daniel Brown committed
811
    }
812

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

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

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

Daniel Brown's avatar
Daniel Brown committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
        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);
845 846
        } else if (strncasecmp(s, "python", 6) == 0) {
            read_init_dummy(fp, 0);
Daniel Brown's avatar
Daniel Brown committed
847 848 849 850 851 852 853 854
        } 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);
        }
    }
855

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

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

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

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

Daniel Brown's avatar
Daniel Brown committed
869 870
        insert_constants_values(s);

871
        if (strncasecmp(s, "showiterate ", 12) == 0) {
Daniel Brown's avatar
Daniel Brown committed
872 873 874
            read_showiterate(s);
        } else if (strncasecmp(s, "gnuplot", 7) == 0) {
            read_init_dummy(fp, 0);
875 876
        } else if (strncasecmp(s, "python", 6) == 0) {
            read_init_dummy(fp, 0);
Daniel Brown's avatar
Daniel Brown committed
877 878 879 880 881 882
        } 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);
883 884 885 886 887 888
        } 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, "func ", 5) == 0) {
            read_func_command(s);
Daniel Brown's avatar
Daniel Brown committed
889 890
        }
    }
Daniel Brown's avatar
Daniel Brown committed
891

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

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

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

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

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

920 921 922 923 924 925 926 927 928 929 930 931 932 933
        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;
934 935
        } else if (strncasecmp(s, "noscripts", 9) == 0) {
                inter.noscripts = 1;
936 937
        } else if (strncasecmp(s, "multi", 5) == 0) {
                inter.multi = true;
938 939
        } else if (strncasecmp(s, "subplot", 7) == 0) {
                inter.subplot = true;
940 941
        } else if (strncasecmp(s, "pause", 5) == 0) {
                inter.pause = 1;
942 943
        } else if (strncasecmp(s, "printnoises", 11) == 0) {
                inter.printqnoiseinputs = true;
944 945
        } else if (strncasecmp(s, "printmatrix", 11) == 0) {
                inter.printmatrix = 1;
946
        } else if (strncasecmp(s, "mismatches", 10) == 0) {
Daniel Brown's avatar
Daniel Brown committed
947
            read_mismatches_command(s);
948 949
        } else if (strncasecmp(s, "frequency", 9) == 0) {
            read_frequency_command(s);
950 951
        } else if (strncasecmp(s, "powers", 6) == 0) {
                read_powers(s);
952 953 954
        } 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
955 956 957 958 959 960 961
            //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
962 963
        } else if (strncasecmp(s, "pyterm", 6) == 0) {
            read_pyterm(s);
Daniel Brown's avatar
Daniel Brown committed
964 965
        } else if (strncasecmp(s, "gnuplot", 7) == 0) {
            ri_gnuplot(fp);
966 967
        } else if (strncasecmp(s, "python", 6) == 0) {
            ri_python(fp);
Daniel Brown's avatar
Daniel Brown committed
968 969 970 971 972 973 974 975 976 977 978 979 980 981
        } 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);
982 983
        } else if (strncasecmp(s, "maximize ", 9) == 0 || strncasecmp(s, "minimize ", 9) == 0) { 
            read_minimize(s);
Daniel Brown's avatar
Daniel Brown committed
984 985 986 987
        } else if (strncasecmp(s, "/*", 2) == 0) {
            read_init_dummy(fp, 1);
        }
    }
Daniel Brown's avatar
Daniel Brown committed
988

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

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

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

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

1004
    assert(inter.num_blocks == mem.num_blocks);
Daniel Brown's avatar
Daniel Brown committed
1005 1006 1007 1008 1009
    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);
1010
    assert(inter.num_sagnacs == mem.num_sagnacs);
Daniel Brown's avatar
Daniel Brown committed
1011 1012 1013 1014 1015 1016
    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);
1017
    assert(inter.num_transfer_funcs == mem.num_transfer_funcs);
Daniel Brown's avatar
Daniel Brown committed
1018
    assert(inter.num_quad_outputs == mem.num_quad_outputs);
Daniel Brown's avatar
Daniel Brown committed
1019
    assert(inter.num_slinks == mem.num_feedbacks);
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    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)
1032
        gerror("Line `%s':\ndof usage is 'dof name parameter comp1 factor [comp2 factor [...]]'\n", command_string);
1033 1034 1035
    
    dof_t *dof = &inter.dof_list[inter.num_dof];
    
1036
    check_name(command_string, dof_name);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    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) {
1052
            gerror("Line `%s':\nCould not process '%s' of the DOF inputs\n", command_string, tmp);
1053 1054 1055 1056 1057 1058
        } 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) {
1059
                gerror("Line `%s':\nDOF only supports mirrors and beamsplitters currently\n", command_string);        
1060 1061 1062
            }
                
            if(atod(factor, &(dof->factors[dof->num_components])))
1063
                gerror("Line `%s':\ncould not factor '%s' as a number\n", command_string, factor);        
1064 1065 1066 1067 1068 1069 1070 1071
            
            dof->num_components++;
        }

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

Daniel Brown's avatar
Daniel Brown committed
1074
void read_slink(const char* command_string) {
Daniel Brown's avatar
Daniel Brown committed
1075
    char command_name[MAX_TOKEN_LEN] = {0};
Daniel Brown's avatar
Daniel Brown committed
1076
    char slink_name[MAX_TOKEN_LEN] = {0};
Daniel Brown's avatar
Daniel Brown committed
1077 1078 1079 1080 1081 1082
    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
1083
    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
1084 1085
    
    if(n != 6 && n != 7)
1086
        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
1087
    
Daniel Brown's avatar
Daniel Brown committed
1088
    int idx = inter.num_slinks;
Daniel Brown's avatar
Daniel Brown committed
1089
    
Daniel Brown's avatar
Daniel Brown committed
1090
    slink_t *fb = &inter.slink_list[idx];
Daniel Brown's avatar
Daniel Brown committed
1091 1092 1093
    
    fb->list_index = idx;
    
Daniel Brown's avatar
Daniel Brown committed
1094
    strcpy(fb->name, slink_name);
Daniel Brown's avatar
Daniel Brown committed
1095 1096