dustemwrap_plot.pro 34.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
PRO dustemwrap_plot,p_dim,st,dustem_sed,dustem_qsed,dustem_used,dustem_polsed,dustem_polfrac,_extra=_extra ;, dustem_polfrac

;NB/ the old POLFRAC treatment from mpfit_run will have to be copied over here when plotting the polarization fraction.  

;JP had this amazing idea about the plotting of the used parameters and plugins in (a) seperate window(s).
;I will definitely do it as soon as I am done with the main display.

;When you replace a command in cgoplot you will need to replace the cgerrplot commands as well. 
;THIS IS VERY IMPORTANT DO NOT FORGET THIS.
;YOU WILL NEEED A COUNTER FOR THE COMMAND INDICES.... You need to find a solution for this. 
;or know which is the index of the command that is targeted.

;The procedure takes the _extra keyword but the plotting inside does not take the full structure
;but rather the needed parts. 
;when the "_extra_filtering" procedure is finished. The structure will be filtered and the routine will be more secure.
    
;NB (VERY IMPORTANT): in order to refresh the cgoplots maybe I should use the replacecmd keyword.
;This line runs but does it do the job?: ,cmdindex=command_index,replacecmd=command_index you ne
;THAT LINE FOR ALL THE CGOPLOTS LINES

;NB: maybe change the position arrays instead of creating two giant loops. 
;THIS IS A REALLY INTERESTING IDEA


;It seems more adequate to execute dustem_compute_sed/polsed/stokes here instead of this block

;especially that dustem_activate_plugins runs the executable already but does not output it.

;NB: There is an implicit assumption that st and dustem_sed and others are set simultaneously. 

;As stated above I do not think that I need this block. This will need to be corrected.
IF not keyword_set(st) THEN BEGIN
  ;Activation of the plugins is needed because of the plotting of the total emission model that includes them. 
  dustem_activate_plugins,p_dim/(*(*!dustem_fit).param_init_values)  ; 0/0 division case?
  st=dustem_run(p_dim)  
ENDIF

;stop
;If the interpolates arrays are not present (dustem_sed, etc...) run: 
;dustem_compute_sed etc to generate them. 

;!const.c ;speed of light in vacuum.

;Generating a first resizable window (for emission or extinction elements)

;if ~windowavailable(cgquery()) then begin

;LIST OF TESTS THAT NEED TO BE RAN SO THAT THE PLOTTING OF THE MODEL SPECTRA OCCURS

test_sed = isa(!dustem_show.sed)
test_ext = isa(!dustem_show.ext)
if !run_pol then begin
    test_polext = isa(!dustem_show.polext)
    test_polsed = isa(!dustem_show.polsed)
    test_polfrac = isa(!dustem_show.polfrac)
    test_psi_em = isa(!dustem_show.psi_em)
    test_psi_ext = isa(!dustem_show.psi_ext)
    test_qsed = isa(!dustem_show.qsed)
    test_used = isa(!dustem_show.used)
    test_qext = isa(!dustem_show.qext)
    test_uext = isa(!dustem_show.uext)
    test_m = test_sed or test_polsed or test_polfrac or test_qsed or test_used or test_psi_em
    test_x = test_ext or test_polext or test_qext or test_uext or test_psi_ext
endif else begin
    test_m = test_sed 
    test_x = test_ext 
endelse


;testing on the fitting of emission vs extinction data

;I could create a procedure for colors for the plugins but I am just going to define an array for now

;clrs_plgns = (colortable(38))(uniq(colortable(38))) ;CT RAINBOW18

n_plgns = n_tags(*!dustem_plugin)

;I have decided to keep track of the command index manually as I did not find any command online that does this.
cmdind_m = 0 ;for emission
cmdind_x = 0 ;for extinction

;#YOU NEED TO REPLACE THE QSED AND USED SCOPES WITH A SINGLE POLSED ONE 

;ADDING PLUGIN(S) TO SPECTRUM----------------

;these following indices will be used to change the keep the same information in the _extra structure
;this is an initial solution until I finish coding the _extra-filtering procedure. 

iswinsed = !dustemcgwin_id.sed EQ la_undef()
iswinext = !dustemcgwin_id.ext EQ la_undef()

;plotstrct = !p
degtorad = !pi/180 ;factor that the arctan will be devided by in order to have an axis in degrees. Talk to JP maybe he wants angle in radians.

;This should probably be moved to 'dustem_mpfit_run'. !!!!!!!
;for now I am coding it like this. 

if !run_pol then dustem_psi_em = 0.5*atan(dustem_used,dustem_qsed)/degtorad

;if iswinsed then begin ;I believe the iswin test should be below the case condition because I am not copying all the first 'HUGE LOOP' but rather replacing the positional arrays.            
if test_m then begin
    ;do you need an if !run_pol condition here?
    ;!run_pol=1 ;test. Needs to be commented asap.
    ;Generating emission window and saving window ID
    
    if iswinsed then begin
        if !run_pol then cgwindow,wxsize=910,wysize=658,wtitle='DUSTEMWRAP v2.0 (EMISSION)' else cgwindow, wtitle='DUSTEMWRAP v2.0 (EMISSION)';, wback='grey';,wobject=winobj_m
        winid_m = cgquery(dimensions=dim_m,/current) ;this has to be changed because the dimensions of this array change with the data sets present (extinction/emission).
        !dustemcgwin_id.sed = winid_m
        
    endif else winid_m = !dustemcgwin_id.sed
    
    
    ;cgWindow_GetDefs,xsize=xsize,ysize=ysize
    ;cgcontrol, resize=[xsize,ysize]
    if !run_pol then begin
        ;,wback='grey'
        
        ;position arrays for plot and normalized graph
        
        
        ;THIS IS HARD-CODED IN A VERY SILLY WAY
        
        p_sed = [0.05,0.67,0.475,0.90]
        p_psed = [0.525,0.67,0.95,0.90]
        p_spsed = [0.05,0.375,0.475,0.575]
        p_psised = [0.525,0.375,0.95,0.575]
        p_qsed = [0.05,0.12,0.475,0.35]  ;FIX THESE POSITIONS. 
        p_used = [0.525,0.12,0.95,0.35]
        
        np_sed = [0.05,0.60,0.475,0.67]
        np_psed = [0.525,0.60,0.95,0.67]
        np_qsed = [0.05,0.05,0.475,0.12] 
        np_used = [0.525,0.05,0.95,0.12]
        
        p_sed[1]+= 0.0125 & p_sed[3]+= 0.0125 
        p_psed[1]+= 0.0125 & p_psed[3]+= 0.0125
        p_spsed[1]+= 0.0125 & p_spsed[3]+= 0.0125
        p_psised[1]+= 0.0125 & p_psised[3]+= 0.0125
        p_qsed[1]+= 0.0125 & p_qsed[3]+= 0.0125
        p_used[1]+= 0.0125 & p_used[3]+= 0.0125
        
        np_sed[1]+= 0.0125 & np_sed[3]+= 0.0125 
        np_psed[1]+= 0.0125 & np_psed[3]+= 0.0125
        np_qsed[1]+= 0.0125 & np_qsed[3]+= 0.0125
        np_used[1]+= 0.0125 & np_used[3]+= 0.0125
        
        
          
    endif else begin
        p_sed  = [0.07,0.30,0.97,0.85]
        np_sed = [0.07,0.10,0.97,0.30]
    
    endelse

    ;Plotting of sed data axes (whether or not data is present)
    plotsym,0,/fill ;you might need to execute this again. hmm...
    
    cgcontrol, execute=0
    
    ;This is not the right test the user might want to show the prediction for sed and fit the stokes parameters
    if ~test_sed then begin ; SED data is not present (For completeness) # This is only valid when !run_pol=1
        
        cgwindow,'dustemcgwin_dataset', st, dataset='SED', /nodata, position=p_sed, /addcmd, winid=winid_m, _extra=_extra 
        cgwindow,'dustemcgwin_dataset', st, dataset='SED', /norm, /nodata, position=np_sed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1

    endif else begin ;SED exists

       if  ~iswinsed then begin
           cmdind_m = !dustemcgwin_ncmds.sed.pl
           cgwindow,'dustemcgwin_dataset', st, dustem_sed, dataset='SED', /refresh ,position=p_sed, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
           cmdind_m = !dustemcgwin_ncmds.sed.nrm
           cgwindow,'dustemcgwin_dataset', st, dustem_sed, dataset='SED', /refresh, /norm,position=np_sed, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
       endif else begin
      ; stop
           cgwindow,'dustemcgwin_dataset', st, dataset='SED', position=p_sed, /addcmd, winid=winid_m, _extra=_extra   
           cgwindow,'dustemcgwin_dataset', st, dustem_sed, dataset='SED', /refresh ,position=p_sed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
           !dustemcgwin_ncmds.sed.pl = cmdind_m
           cgwindow,'dustemcgwin_dataset', st, dataset='SED',/norm, position=np_sed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1 ;I think nodata can also be used too
           cgwindow,'dustemcgwin_dataset', st, dustem_sed, dataset='SED', /refresh, /norm,position=np_sed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
           !dustemcgwin_ncmds.sed.nrm = cmdind_m
       endelse       
    endelse
    
    ;if ~!run_pol then begin;I don't think the order of the  ;goto, end_m
      if !run_pol then begin ;then goto, end_m
        
        if ~test_qsed then begin ;Qsed is not present
            
            cgwindow,'dustemcgwin_dataset', st, dataset='QSED', /nodata, position=p_qsed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
            cgwindow,'dustemcgwin_dataset', st, dataset='QSED', /norm, /nodata, position=np_qsed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
        
        endif else begin ;Qsed data is present
               
            if  ~iswinsed then begin
                cmdind_m = !dustemcgwin_ncmds.qsed.pl
                ;refreshing the positive_only and negative_only plots if data is present in them
                cgwindow,'dustemcgwin_dataset', st, dustem_qsed, dataset='QSED',/positive_only, /refresh, nodata=0 ,position=p_qsed, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
                cgwindow,'dustemcgwin_dataset', st, dustem_qsed, dataset='QSED',/negative_only, /refresh, nodata=0 ,position=p_qsed, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
                cmdind_m = !dustemcgwin_ncmds.qsed.nrm
                cgwindow,'dustemcgwin_dataset', st, dustem_qsed, dataset='QSED', /refresh, nodata=0, /norm,position=np_qsed, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
            endif else begin
            ;stop
                ;Plotting of postive values if they exist (if hidden data is present it is plotted if not an empty plot is rendred)
                cgwindow,'dustemcgwin_dataset', st, dataset='QSED', position=p_qsed, /addcmd, winid=winid_m,/positive_only, _extra=_extra & cmdind_m+=1
                ;Refreshing the positive_only plot ONLY if there is data. If not do nothing
                cgwindow,'dustemcgwin_dataset', st, dustem_qsed, dataset='QSED',/positive_only, /refresh ,position=p_qsed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
                
                cgwindow,'dustemcgwin_dataset', st, dataset='QSED', position=p_qsed, /addcmd, winid=winid_m,/negative_only, _extra=_extra & cmdind_m+=1
                ;Refreshing the negative_only plot ONLY if there is data. If not do nothing
                cgwindow,'dustemcgwin_dataset', st, dustem_qsed, dataset='QSED',/negative_only, /refresh ,position=p_qsed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
                
                !dustemcgwin_ncmds.qsed.pl = cmdind_m
                
                cgwindow,'dustemcgwin_dataset', st, dataset='QSED',/norm, position=np_qsed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1 ;I think nodata can also be uqsed too
                cgwindow,'dustemcgwin_dataset', st, dustem_qsed, dataset='QSED', /refresh, /norm,position=np_qsed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
                !dustemcgwin_ncmds.qsed.nrm = cmdind_m
        endelse       
      endelse
   ;          
        if ~test_used then begin ;used is not present
            
            cgwindow,'dustemcgwin_dataset', st, dataset='USED', /nodata, position=p_used, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
            cgwindow,'dustemcgwin_dataset', st, dataset='USED', /norm, /nodata, position=np_used, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
        
        endif else begin ;used data is present
               
            if  ~iswinsed then begin
                cmdind_m = !dustemcgwin_ncmds.used.pl
                ;refreshing the positive_only and negative_only plots if data is present in them
                cgwindow,'dustemcgwin_dataset', st, dustem_used, dataset='USED',/positive_only, /refresh, nodata=0 ,position=p_used, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
                cgwindow,'dustemcgwin_dataset', st, dustem_used, dataset='USED',/negative_only, /refresh, nodata=0 ,position=p_used, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
                cmdind_m = !dustemcgwin_ncmds.used.nrm
                cgwindow,'dustemcgwin_dataset', st, dustem_used, dataset='USED', /refresh, nodata=0, /norm,position=np_used, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
            endif else begin
            ;stop
                ;Plotting of postive values if they exist (if hidden data is present it is plotted if not an empty plot is rendred)
                cgwindow,'dustemcgwin_dataset', st, dataset='USED', position=p_used, /addcmd, winid=winid_m,/positive_only, _extra=_extra & cmdind_m+=1
                ;Refreshing the positive_only plot ONLY if there is data. If not do nothing
                cgwindow,'dustemcgwin_dataset', st, dustem_used, dataset='USED',/positive_only, /refresh ,position=p_used, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
                
                cgwindow,'dustemcgwin_dataset', st, dataset='USED', position=p_used, /addcmd, winid=winid_m,/negative_only, _extra=_extra & cmdind_m+=1
                ;Refreshing the negative_only plot ONLY if there is data. If not do nothing
                cgwindow,'dustemcgwin_dataset', st, dustem_used, dataset='USED',/negative_only, /refresh ,position=p_used, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
                
                !dustemcgwin_ncmds.used.pl = cmdind_m
                
                cgwindow,'dustemcgwin_dataset', st, dataset='USED',/norm, position=np_used, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1 ;I think nodata can also be uused too
                cgwindow,'dustemcgwin_dataset', st, dustem_used, dataset='USED', /refresh, /norm,position=np_used, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
                !dustemcgwin_ncmds.used.nrm = cmdind_m
        endelse       
      endelse
      
      if ~test_psi_em then begin ; psi_em data is not present (For completeness) # This is only valid when !run_pol=1
          
          cgwindow,'dustemcgwin_dataset', st, dataset='psi_em', /nodata, position=p_psised, /addcmd, winid=winid_m, _extra=_extra  & cmdind_m+=1

      endif else begin ;psi_em exists

         if  ~iswinsed then begin
             cmdind_m = !dustemcgwin_ncmds.psi_em.pl
             cgwindow,'dustemcgwin_dataset', st, dustem_psi_em, dataset='psi_em', /refresh ,position=p_psised, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
             
         endif else begin
         ;stop
             cgwindow,'dustemcgwin_dataset', st, dataset='psi_em', position=p_psised, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
             cgwindow,'dustemcgwin_dataset', st, dustem_psi_em, dataset='psi_em', /refresh ,position=p_psised, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
             !dustemcgwin_ncmds.psi_em.pl = cmdind_m

         endelse       
      endelse
        
      
      if ~test_polsed then begin ; polsed data is not present (For completeness) # This is only valid when !run_pol=1
          
          cgwindow,'dustemcgwin_dataset', st, dataset='polsed', /nodata, position=p_psed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
          cgwindow,'dustemcgwin_dataset', st, dataset='polsed', /norm, /nodata, position=np_psed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1

      endif else begin ;polsed exists

         if  ~iswinsed then begin
             cmdind_m = !dustemcgwin_ncmds.polsed.pl
             cgwindow,'dustemcgwin_dataset', st, dustem_polsed, dataset='polsed', /refresh ,position=p_psed, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
             cmdind_m = !dustemcgwin_ncmds.polsed.nrm
             cgwindow,'dustemcgwin_dataset', st, dustem_polsed, dataset='polsed', /refresh, /norm,position=np_psed, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
         endif else begin
         ;stop
             cgwindow,'dustemcgwin_dataset', st, dataset='polsed', position=p_psed, /addcmd, winid=winid_m, _extra=_extra  & cmdind_m+=1  
             cgwindow,'dustemcgwin_dataset', st, dustem_polsed, dataset='polsed', /refresh ,position=p_psed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
             !dustemcgwin_ncmds.polsed.pl = cmdind_m
             cgwindow,'dustemcgwin_dataset', st, dataset='polsed',/norm, position=np_psed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1 ;I think nodata can also be upolsed too
             cgwindow,'dustemcgwin_dataset', st, dustem_polsed, dataset='polsed', /refresh, /norm,position=np_psed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
             !dustemcgwin_ncmds.polsed.nrm = cmdind_m
         endelse       
      endelse      
      
      
      
      if ~test_polfrac then begin ; polfrac data is not present (For completeness) # This is only valid when !run_pol=1
          
          cgwindow,'dustemcgwin_dataset', st, dataset='polfrac', /nodata, position=p_spsed, /addcmd, winid=winid_m, _extra=_extra  & cmdind_m+=1

      endif else begin ;polfrac exists

         if  ~iswinsed then begin
             cmdind_m = !dustemcgwin_ncmds.polfrac.pl
             cgwindow,'dustemcgwin_dataset', st, dustem_polfrac, dataset='polfrac', /refresh ,position=p_spsed, winid=winid_m,/replacecmd,cmdindex=cmdind_m, _extra=_extra
             
         endif else begin
         ;stop
             cgwindow,'dustemcgwin_dataset', st, dataset='polfrac', position=p_spsed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
             cgwindow,'dustemcgwin_dataset', st, dustem_polfrac, dataset='polfrac', /refresh ,position=p_spsed, /addcmd, winid=winid_m, _extra=_extra & cmdind_m+=1
             !dustemcgwin_ncmds.polfrac.pl = cmdind_m

         endelse       
      endelse  

    endif
    
    end_m:
    
    ;cgset, winid_m
    
    ;cgcontrol, update=1
;     varrr=cgquery(dimensions=dim_m) ;be careful because the dim_m array can be multi-dimensional because of the presence of two windows.
;     cgcontrol, update=1
;     cgcontrol, resize=[dim_m(0),dim_m(1)]
;     
    ;cgcontrol, resize=1
;     cgcontrol,update=1
;     cgcontrol,update=1
        
;     cgcontrol, resize=[!D.X_SIZE,!D.Y_SIZE]
    
    cgcontrol, execute=1
    ;cgcontrol, update=0
    ;cgcontrol, update=1
    ;varr=cgquery(dimensions=dim_m,objectref=winobj_m)
   ;stop
         
endif            
;endif                     


;Putting everything required for the plotting of the legends (column density,model chi2 and rchi2) and the two windows for the parameters and the plugins respectively
    ;p_sed = [0.05,0.67,0.475,0.90]
;     xxpos = 0.03
;     yypos = 0.95
;     ltit  =  'Model: '
;     cgwindow,'xyouts',xxpos,/addcmd,windid=winid_m,yypos,ltit+!dustem_model,color=0,/normal,charsize=legend_charsize
;     xxpos =
;     yypos =
;     ltit = 'N_{H}:'+strmid(string(*!dustem_HCD,format='(1E10.2)'),2)+'(H/cm^2)'
;     ltit = textoidl(ltit)
;     xyouts,xxpos,yypos,ltit,color=0,/normal,charsize=legend_charsize
;     xxpos =
;     yypos =
;     xyouts,xxpos,yypos,_extra.title,color=0,/normal,charsize=legend_charsize


;stop



; ;#COMMENTED BITS OF CODES 
;  
; IF keyword_set(help) THEN BEGIN
;   doc_library,'dustem_plot_fit_sed'
;   goto,the_end
; ENDIF
; 
; IF keyword_set(ps) THEN BEGIN
;     set_plot, 'PS'
;     device, filename=ps, /color,set_character_size=[170,250], /encapsulated
; ENDIF ELSE BEGIN
; 
;   set_plot,'X'
;     IF keyword_set(win) then window,win;,xsize=600,ysize=800
; ENDELSE
; 
; fact = 1.e4*(*!dustem_HCD)/(4.*!pi)/(3.e8/1.e-6/st.sed.wav)*1.e20/1.e7
; ;use_col_data_filt=70
; ;use_col_sed_spec=170
; use_col_data_filt='blue'
; ;use_col_sed_spec='red'
; use_col_sed_spec='grey'
; IF not keyword_set(col_sed) THEN BEGIN
;   ;use_col_sed_filt=250     ;red
;   use_col_sed_filt='red'     ;red
; ENDIF ELSE BEGIN
;   use_col_sed_filt=col_sed
; ENDELSE
; IF not keyword_set(col_tot) THEN BEGIN
;   ;use_col_tot=200
;   use_col_tot='black'
; ENDIF ELSE BEGIN
;   use_col_tot=col_tot
; ENDELSE
; IF not keyword_set(line_tot) THEN BEGIN
;   use_line_tot=0
; ENDIF ELSE BEGIN
;   use_line_tot=line_tot
; ENDELSE
; 
; 
; spec = st.sed.em_tot * fact
; if keyword_set(fpol) then specpol = st.polsed.em_tot * fact
; ;ADDING PLUGIN(S) TO SPECTRUM----------------
; ;if n_tags(!dustem_data.sed) gt 1 then begin 
; scopes=tag_names((*!dustem_scope))
; IF scopes[0] NE 'NONE' THEN BEGIN
; ;IF ptr_valid(!dustem_plugin) THEN BEGIN
;   FOR i=0L,n_tags(*!dustem_scope)-1 DO BEGIN
;     IF total(strsplit((*(*!dustem_scope).(i)),'+',/extract) EQ 'ADD_SED') THEN spec+=(*(*!dustem_plugin).(i))[*,0]
;     if keyword_set(fpol) then begin
;         IF total(strsplit((*(*!dustem_scope).(i)),'+',/extract) EQ 'REPLACE_QSED') THEN specpol=sqrt(((*(*!dustem_plugin).(i))[*,1])^2+((*(*!dustem_plugin).(i))[*,2])^2)
;     endif
;   ENDFOR
;   FOR i=0L,n_tags(*!dustem_scope)-1 DO BEGIN
;     if keyword_set(fpol) then begin    
;         IF total(strsplit((*(*!dustem_scope).(i)),'+',/extract) EQ 'ADD_QSED') THEN specpol+=sqrt(((*(*!dustem_plugin).(i))[*,1])^2+((*(*!dustem_plugin).(i))[*,2])^2)
;     endif  
;   ENDFOR
; ENDIF
; ;endif
; ;------------------------------------------
; 
; ;stop
; 
; ;use_cols=[use_col_pah,use_col_vsg,use_col_bg,use_col_cont]
; ;use_lines=[use_line_pah,use_line_vsg,use_line_bg,use_line_cont]
; ;col_off=30
; ;Ngrains=(*!dustem_params).grain.Ngrains
; Ngrains=(*!dustem_params).Ngrains
; ;use_cols=long(findgen(Ngrains)/(Ngrains-1)*(255-col_off)+col_off)
; use_cols=dustem_grains_colors(Ngrains,/cgplot)
; use_lines=replicate(0,Ngrains)
; 
; norm = dustem_sed * 0. + 1
; 
; ;====== PLOT THE SED
; 
; IF keyword_set(title) THEN title = title ELSE title = 'Spectral Energy Distribution (Running)'
; 
; IF keyword_set(xr) THEN xr = xr ELSE xr = [1.00E+00,6.00E+04]
; 
; IF keyword_set(yr) THEN yr = yr ELSE yr = [1.00E-7,5.00E03]
; 
; IF keyword_set(xtit) THEN xtit = xtit ELSE xtit = textoidl('\lambda (\mum)')
; 
; IF keyword_set(ytit) THEN ytit = ytit ELSE ytit = textoidl('Brightness/N_H (MJy/sr/H)')
; 
; 
; ;deffo define a title variable here. The procedures are not communicating with each other. 
; ;pos=cgLayout()
; ;###############################
; if keyword_set(fpol) then begin
;     if !run_lin then begin
;     cgDisplay, 600, 500
;     cgplot,(*!dustem_data.sed).wav,(*!dustem_data.sed).values/norm,/nodata,xtit='',ytit=ytit,tit='',/ylog,/xlog,/ys,/xs,position=[0.12,0.25,0.96,0.76],xtickformat='(A1)',_extra=_extra,charsize=1.3,/noerase    
;     endif
; endif ELSE cgplot,(*!dustem_data.sed).wav,(*!dustem_data.sed).values/norm,/nodata,xtit='',ytit=ytit,tit=title,/ylog,/xlog,/ys,/xs,position=[0.12,0.35,0.96,0.90],xtickformat='(A1)',_extra=_extra,charsize=1.3
; ;###############################
; 
; 
; ;cgplot,(*!dustem_data.sed).wav,(*!dustem_data.sed).values/norm,/nodata,xtit='',ytit=textoidl('Brightness/N_H (MJy/sr/H)'),tit=title,ylog=1,/xlog,xr=xr,yr=yr,/ys,/xs,position=[0.12,0.35,0.96,0.90],xtickformat='(A1)'
; 
; 
; ;stop
; ;cgplot,st.polsed.wav,Q_sed*fact,xtit='',ytit=ytitstq,tit=titstq,ylog=ylog,/xlog,xr=xr,yr=yr,/ys,/xs,position=[0.17,0.35,0.95,0.95],xtickformat='(A1)'
; 
; ;this is where you will add the normalized sed
; 
; 
; ind_filt=where((*!dustem_data.sed).filt_names NE 'SPECTRUM',count_filt)
; ind_spec=where((*!dustem_data.sed).filt_names EQ 'SPECTRUM',count_spec)
; ;=== Plot the data
; ; following lines for fawlty compatibility
; defsysv,'!psym',exists=pexist
; if pexist eq 0 then defsysv,'!psym',0
; IF count_spec NE 0 THEN BEGIN
;   ;x&
;   xx=((*!dustem_data.sed).wav)[ind_spec]
;   yy=((*!dustem_data.sed).values)[ind_spec]/norm[ind_spec]
;   rms=3.*((*!dustem_data.sed).sigma)[ind_spec]/2./norm[ind_spec]
;   cgoplot,xx,yy,psym    ,syms=0.5,color=use_col_sed_spec
;   IF not keyword_set(no_spec_error) THEN BEGIN
;     cgerrplot,xx,yy-rms,yy+rms,color=use_col_sed_spec
;     ;err_bar,((*!dustem_data.sed).wav)(ind_spec),((*!dustem_data.sed).values)(ind_spec)/norm(ind_spec),yrms=3.*((*!dustem_data.sed).sigma)(ind_spec)/2./norm(ind_spec)
;   ENDIF
; ENDIF
; IF count_filt NE 0 THEN BEGIN
;   ;stop
;   xx=((*!dustem_data.sed).wav)[ind_filt]
;   yy=((*!dustem_data.sed).values)[ind_filt]/norm[ind_filt]
;   rms=3.*((*!dustem_data.sed).sigma)[ind_filt]/2./norm[ind_filt]
;   plotsym,0,/fill
;   cgoplot,xx,yy,psym=8,color='Dodger Blue';use_col_data_filt
;   ;err_bar,((*!dustem_data.sed).wav)(ind_filt),((*!dustem_data.sed).values)(ind_filt)/norm(ind_filt),yrms=3.*((*!dustem_data.sed).sigma)(ind_filt)/2./norm(ind_filt),color=use_col_data_filt
;   cgerrplot,xx,yy-rms,yy+rms,color='Dodger Blue';use_col_data_filt
; ENDIF
; ;=== Plot the computed SED
; IF count_filt NE 0 THEN BEGIN
;   plotsym,8
;   xx=((*!dustem_data.sed).wav)[ind_filt]
;   yy=sed[ind_filt]/norm[ind_filt]
;   cgoplot,xx,yy,color=use_col_sed_filt,psym=6,syms=2
; ENDIF
; IF count_spec NE 0 THEN BEGIN
;   plotsym,0
;   xx=((*!dustem_data.sed).wav)[ind_spec]
;   yy=sed[ind_spec]/norm[ind_spec]
;   cgoplot,xx,yy,color=use_col_sed_filt,psym=6,syms=2
; ENDIF
; 
; 
; IF !dustem_show_plot EQ 2 THEN BEGIN
; 	norm = spec
; ENDIF ELSE BEGIN
; 	norm = spec * 0. + 1
; ENDELSE
; use_cols[1]='Cornflower'
; FOR i=0L,Ngrains-1 DO BEGIN
;   cgoplot,st.sed.wav,st.sed.(i+1)*fact/norm,color=use_cols[i],linestyle=use_lines[i]
; ENDFOR
; 
; ;stop
; 
; ;PLOTTING OF THE PLUGIN(S)--------------- AUTOMATE THIS. QUITE FEASIBLE
; IF tag_exist(*!dustem_scope,'CONTINUUM') THEN BEGIN
;   cgoplot,st.sed.wav,(*(*!dustem_plugin).continuum)[*,0],color='Teal',linestyle=3
; ENDIF
; IF tag_exist(*!dustem_scope,'FREEFREE') THEN BEGIN
;   cgoplot,st.sed.wav,(*(*!dustem_plugin).freefree)[*,0],color='Dark Red',linestyle=3
; ENDIF
; IF tag_exist(*!dustem_scope,'SYNCHROTRON') THEN BEGIN
;   cgoplot,st.sed.wav,(*(*!dustem_plugin).synchrotron)[*,0],color='Crimson',linestyle=3
; ENDIF
; 
; IF tag_exist(*!dustem_scope,'MBBDY_ISRF') THEN BEGIN
;   cgoplot,st.sed.wav,(*(*!dustem_plugin).mbbdy_isrf)[*,0],color='Gold',linestyle=3
; ENDIF
; 
; IF tag_exist(*!dustem_scope,'MBBDY') THEN BEGIN
;   cgoplot,st.sed.wav,(*(*!dustem_plugin).mbbdy)[*,0],color='Cornflower',linestyle=3
; ENDIF
; 
; 
; cgoplot,st.sed.wav,spec/norm,color=use_col_tot,linestyle=use_line_tot
; if keyword_set(fpol) then cgoplot,st.polsed.wav,specpol,color='Teal',linestyle=4
; ;plot the normalized data as well. 
; ;----------------------------------------
; 
; ;==== print the legend
; frmt0='(A36)'
; frmt1='(1E10.2)'
; frmt2='(F7.2)'
; use_legend_xpos=0.50
; if keyword_set(fpol) then use_legend_ypos=0.70 ELSE use_legend_ypos=0.84
; use_legend_offset=0.03             ;This is the offset between lines of the legend in normalized units
; legend_charsize=0.86
; 
; k=0.
; iscond=0
; n_plgns = n_tags(*!dustem_scope)
; inn=n_plgns/2+1
; IF n_plgns mod 2 ne 0 THEN inn=n_plgns/2+2
; 
; IF keyword_set(legend_xpos) THEN use_legend_xpos=legend_xpos
; IF keyword_set(legend_ypos) THEN use_legend_ypos=legend_ypos
; IF keyword_set(legend_offset) THEN use_legend_offset=legend_offset
; 
; IF !d.name NE 'PS' THEN cleanplot
; 
; tg=0
; prv_str=''
; IF keyword_set(res) THEN BEGIN
;   Npar=n_elements(res)
;   ;print,'=============='
;   
;   FOR i=0L,Npar-1 DO BEGIN
;     parameter_description=(*(*!dustem_fit).param_descs)[i]
;     parameter_type=dustem_parameter_description2type(parameter_description,string_name=string_name)
;     str=string(string_name+' = ',format=frmt0)+string(res[i],format=frmt1)
;     IF keyword_set(errors) THEN BEGIN
;       str=str+textoidl(' \pm ')+string(errors(i),format=frmt1)
;     ENDIF
;    
;     xxpos=use_legend_xpos*0.07
;     yypos=use_legend_ypos*0.03
;     xyouts,xxpos,yypos,'Model: '+!dustem_model,color=0,/normal,charsize=legend_charsize
;    
;     IF STRUPCASE(strmid(strtrim(parameter_description,2),0,6)) eq 'DUSTEM' THEN BEGIN
;        
; ;     xxpos=use_legend_xpos*0.07
; ;     yypos=use_legend_ypos*0.03
; ;     xyouts,xxpos,yypos,'Model: '+!dustem_model,color=0,/normal,charsize=legend_charsize
;      
;     xxpos=use_legend_xpos*0.75
;     yypos=use_legend_ypos*1.03
;     xyouts,xxpos,yypos,'--Plugins--',color=0,/normal,charsize=legend_charsize
;     
;     ii = strsplit(string_name,'_',count=countx) & ii = ii(countx-1)-1 ; Locating the last underscore to automate the extraction of the plugin's keyword
;     
;     mm=where(tag_names(*!dustem_scope) eq strupcase(strmid(string_name,7,ii-7)),coun) ; Selecting a plugin through matching the string name of the plugin form the scope system variable with the one read from the parameter description vector
;  
;     ;tg+=1
;     ;if strmid(string_name,7,ii-7) eq prv_str then tg-=1
;     prmtg=(*(*!dustem_paramtag).(mm))
;     indtg=(strmid(string_name,ii+1)) & indtg=strmid(indtg,0,/reverse_offset)   
;     ;stop
;     indtg=fix(indtg)
;     prmtg=prmtg[indtg-1] 
;     
;     str=string(strmid(string_name,7,ii-7)+' ['+strmid(string_name,ii+1)+']: '+prmtg+' = ',format=frmt0)+string(res[i],format=frmt1)
;    
;     prv_str=strmid(string_name,7,ii-7)
;     
;     
;     
;      IF keyword_set(errors) THEN BEGIN
;        str=str+textoidl(' \pm ')+string(errors(i),format=frmt1)
;      ENDIF
;     
;     xxpos=use_legend_xpos*0.46
;     
;     IF n_plgns gt 3 THEN BEGIN ;adapt the display of the plugins if there are more than three (display two columns)
;         IF i eq inn+k THEN BEGIN
;            k+=inn     
;            ;iscond=1
;            inn+=1
;            xxpos=use_legend_xpos*0.06
;         ENDIF
;     ENDIF
;               
; ;     IF iscond THEN BEGIN
; ;     inn+=1
; ;     xxpos=use_legend_xpos*0.06
; ;     ENDIF
; 
;     yypos=use_legend_ypos-(i-k)*use_legend_offset
;     xyouts,xxpos,yypos,str,color=0,/normal,charsize=legend_charsize
;     endif else begin
;     IF STRUPCASE(strmid(strtrim(parameter_description,2),0,24)) eq '(*!DUSTEM_PARAMS).GRAINS' and STRUPCASE(strmid(strtrim(parameter_description,2),3,1,/reverse_offset)) EQ 'O' then begin
;      indpop=fix(STRUPCASE(strmid(strtrim(parameter_description,2),12,1,/reverse_offset)))
;      ;oo+=1
;      ;string_name=(((*!dustem_params).GRAINS).grain_type)[oo-1]
;      string_name=(((*!dustem_params).GRAINS).grain_type)[indpop]
;      ;stop
;     endif
;     k+=1
;     xxpos=use_legend_xpos*1.5
;     yypos=use_legend_ypos*1.03
;     xyouts,xxpos,yypos,'--Parameters--',color=0,/normal,charsize=legend_charsize
;     yypos=use_legend_ypos-(i)*use_legend_offset 
;     xxpos=use_legend_xpos*1.14
;     str=string(string_name+' = ',format=frmt0)+string(res[i],format=frmt1)
;     xyouts,xxpos,yypos,str,color=0,/normal,charsize=legend_charsize    
;     endelse
;   ENDFOR
;   ;stop
; ENDIF
; IF keyword_set(chi2) THEN BEGIN
;   xxpos=1.29*use_legend_xpos
;   yypos=1.13*0.84;use_legend_ypos
;   xyouts,xxpos,yypos,string('chi2=',format=frmt0)+string(chi2,format=frmt2),color=0,/normal,charsize=legend_charsize
; ENDIF
; IF keyword_set(rchi2) THEN BEGIN
;   xxpos=1.29*use_legend_xpos
;   yypos=1.13*0.84-use_legend_offset;use_legend_ypos-use_legend_offset
;   xyouts,xxpos,yypos,string('red. chi2=',format=frmt0)+string(rchi2,format=frmt2),color=0,/normal,charsize=legend_charsize
; ENDIF
; 
; xtit=textoidl('\lambda (\mum)')
; ;cgplot,(*!dustem_data.sed).wav,(*!dustem_data.sed).values/sed,/nodata,xtit=xtit,ytit='Normalized',tit='',/xlog,xr=xr,/ys,/xs,yr=[0,2],ylog=0,position=[0.12,0.14,0.96,0.35],/noerase,yticks=2,ymino=2,xticklen=0.1
; ;stop
; 
; IF keyword_set(_extra) THEN BEGIN
;   extra_kept={XR:[0.,0.]}
;   extra_tags=tag_names(_extra)
;   ind=where(extra_tags EQ 'XR',count)
;   IF count NE 0 THEN extra_kept.XR=_extra.(ind[0]) ;ELSE extra_kept=0
; ENDIF
; 
; ;stop
; ;,position=[0.12,0.14,0.96,0.35] old position before your polfrac display modification
; if keyword_set(fpol) then begin
; cgplot,(*!dustem_data.sed).wav,(*!dustem_data.sed).values/sed,_extra=extra_kept,/nodata,xtit=xtit,ytit='Normalized',tit='',/xlog,/ys,/xs,yr=[0,2],ylog=0,position=[0.12,0.1,0.96,0.25],/noerase,yticks=2,ymino=2,xticklen=0.1,ytickformat='(F6.2)',charsize=1.3
; endif else cgplot,(*!dustem_data.sed).wav,(*!dustem_data.sed).values/sed,_extra=extra_kept,/nodata,xtit=xtit,ytit='Normalized',tit='',/xlog,/ys,/xs,yr=[0,2],ylog=0,position=[0.12,0.14,0.96,0.35],/noerase,yticks=2,ymino=2,xticklen=0.1,ytickformat='(F6.2)',charsize=1.3
; ;plot the normalized data as well. 
; IF count_spec NE 0 THEN BEGIN
;   xx=((*!dustem_data.sed).wav)[ind_spec]
;   yy=((*!dustem_data.sed).values/sed)[ind_spec]
;   cgoplot,xx,yy,psym=16,symsize=1,thick=2,color=use_col_sed_spec
;   trois_sigma=(3.*((*!dustem_data.sed).sigma)/2./sed)[ind_spec]
;   cgerrplot,xx,yy-trois_sigma,yy+trois_sigma,color=use_col_sed_spec
; ENDIF
; IF count_filt NE 0 THEN BEGIN
;   xx=((*!dustem_data.sed).wav)[ind_filt]
;   yy=((*!dustem_data.sed).values/sed)[ind_filt]
;   cgoplot,xx,yy,psym=16,symsize=1,thick=2,color='Dodger Blue'
;   trois_sigma=(3.*((*!dustem_data.sed).sigma)/2./sed)[ind_filt]
;   cgerrplot,xx,yy-trois_sigma,yy+trois_sigma,color='Dodger Blue'
; ENDIF
; 
; ;stop
; ;cgoplot,(*!dustem_data.sed).wav,(*!dustem_data.sed).values/sed,psym=16,symsize=1,thick=2,color='Dodger Blue'
; cgoplot,10^!x.crange,replicate(1.,2),color='black',linestyle=0
; ;cgoplot,st.sed.wav,spec/spec,color='black'
; ;cgerrplot,((*!dustem_data.sed).wav),((*!dustem_data.sed).values)/sed-3.*((*!dustem_data.sed).sigma)/2./sed,((*!dustem_data.sed).values)/sed+3.*((*!dustem_data.sed).sigma)/2./sed,color='Dodger Blue'
; 
; 
; if keyword_set(fpol) then begin
;     plotsym,0,/fill
;     cgplot, (*!dustem_data.polfrac).wav,(*!dustem_data.polfrac).values*100,/nodata,xtit='',tit=title,ytit='',/xlog,/ylog,/ys,/xs,position=[0.12,0.76,0.96,0.91],charsize=1.3,yr=[1.0E-1,30.0],/noerase,xticklen=0.1,xtickformat='(A1)',_extra=extra_kept;,xr=_extra.xr
;     xxpos=use_legend_xpos*0.35
;     yypos=use_legend_ypos*1.25
;     xyouts,xxpos,yypos,'Polarization fraction (%)',color=cgcolor('purple'),charsize=legend_charsize,/normal
;     yypos=use_legend_ypos*1.20
;     stringg='Polarization SED '+'('+textoidl('P_{\nu}')+')'
;     xyouts,xxpos,yypos,stringg,color=cgcolor('teal'),charsize=legend_charsize,/normal
;     pilotfx=fpol(0);((*!dustem_data.polfrac).values)(0);fpol(0);
;     cgoplot, (*!dustem_data.polfrac).wav,(*!dustem_data.polfrac).values*100,charsize=1.3,psym=8,syms=1,thick=2,color='Dodger Blue'   
;     cgoplot, st.polsed.wav,(specpol/spec)*100/pilotfx*((*!dustem_data.polfrac).values)(0),color='purple'
;     cgoplot, (*!dustem_data.polfrac).wav,fpol*100/pilotfx*((*!dustem_data.polfrac).values)(0),color='Red',psym=6,syms=1
;     ;fpol=abs(fpol)
;     ;delp=(*!dustem_data.polfrac).values*100-fpol*100/pilotfx*((*!dustem_data.polfrac).values)(0)
;     ;cgoplot, ((*!dustem_data.polfrac).wav),((*!dustem_data.polfrac).values*100+delp),charsize=1.3,color='black',linestyle=2
;     ;stop
; endif
; 
; IF keyword_set(ps) THEN BEGIN
;   device,/close
;   set_plot,'X'
; ENDIF
; 
; ;stop
; 
; the_end:

END