This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfat.pro
executable file
·6981 lines (6608 loc) · 368 KB
/
fat.pro
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Pro fat,SUPPORT=supportdir,CONFIGURATION_FILE=configfile,DEBUG=debug,INSTALLATION_CHECK=installation_check,LVHIS_TEST=lvhistest,PAPER_TEST=papertest,RES_TEST=restest,AGC=agctest,SMALL_AGC=smallagctest,PROBLEMS=problems,NEW_AGC=new_agc
;+
; NAME:
; FAT
; PURPOSE:
; Fit Tilted Ring Models with Tirific in a fully automated manner
; CATEGORY:
; Main for fitting galaxies. Tirific still requires interactive fitting this code attempts
; to remedy that
;
; CALLING SEQUENCE:
; FAT,support='supportdir',configuration_file='configfile'
;
; INPUTS:
; -
; OPTIONAL INPUTS:
; SUPPORT = path to the directory where FAT's support
; routines are located. The default location is ./Support/
; CONFIGURATION_FILE = A configuration file for FAT. This file
; should contain the locations of the galaxies to be fitted. See
; readme for more detailed info.
;
; OPTIONAL INPUT KEYWORDS
; /INSTALLATION_CHECK = Flag to run the Installation check.
; ---------------------------------------------------------------------------------
; The following input keywords are only meant to be used by
; developers. Except for the /debug flag they will not work for
; the common user. If you want to know about these please contact Peter
; Kamphuis.
; ---------------------------------------------------------------------------------
; /DEBUG = Flag to print debugging information in several routines
; /LVHIS_TEST = Flag to run the LVHIS Test.
; /PAPER_TEST = Flag to run the paper artificial galaxies.
; /RESOLUTION_TEST = Flag to run the additional resolution tests
; OPTIONAL KEYWORD OUTPUT:
; -
;
; OUTPUTS:
; See Readme or just run the code
;
; EXAMPLE:
; IDL> FAT,CONFIGURATION_FILE='~/Myfittingdirectory/FAT.config'
;
; PROCEDURES CALLED
; BEAM_PLOT, BOOK_KEEPING, BUILDAXII, CALC_EDGE, CHANGERADII, CHECK_CFLUX, CLEAN_HEADER, CLEANUP, COLUMNDENSITY,
; CONVERTRADEC, CONVERTSKYANGLEFUNCTION(), EXTRACT_PV, FIT_ELLIPSE(),FAT_SMOOTH()
; GETDHI, GET_FIXEDRINGSV9, GET_NEWRINGSV9, GET_PROGRESS,
; INT_PROFILEV2, INTERPOLATE, ISNUMERIC(), LINENUMBER(), MOMENTSV2,
; OBTAIN_PA_INCL, OBTAIN_PAV2, OBTAIN_VELPA, OBTAIN_W50, OVERVIEW_PLOT
; PARAMETERREGUV87, PREPROCESSING, READ_TEMPLATE, SBR_CHECK, SET_SBR, SET_VROTV6,
; SET_WARP_SLOPEV3, TOTAL(), WRITEFITTINGVARIABLES, WRITENEWTOTEMPLATES,
; RESOLVE_ROUTINE, STRLOWCASE, STDDEV and likely more.
;
; MODIFICATION HISTORY:
; 01-01-2019 P.Kamphuis; Modification history is now tracked
; through Github Commits for all files.
; 30-11-2018 P.Kamphuis; Added the ring size parameter such that
; now the ring sizes in beams can be give
; in input file. The default is 1 and it
; is given in the input file as ring_size
; = . AGC should be tested at 1.1 beam rings.
; 30-10-2018 P.Kamphuis: Cleaned up the boudary adjustment parameters.
; 25-10-2018 P.Kamphuis; Fixed a bug where VROT[0] was not kept
; at 0 before regularisation.
; 29-09-2018 P.Kamphuis; Increased velconstused to 2.5*cutoff
; 14-09-2018 P.Kamphuis; Intoducing ring by ring SDIS
; fitting. Migrated to v6.0
; 14-09-2018 P.Kamphuis; Allowing 8% fractional change of center when
; rings > 25
; 13-09-2018 P.Kamphuis; Increased the VROT slope range for large
; galaxies (> 25 rings).
; 06-08-2018 P.Kamphuis; Added line to ensure that integer cubes
; are scaled.
; 03-12-2017 P.Kamphuis; Made sure that in the end _opt is
; stripped from the current fit cube. Else
; the PV-Diagrams are not found.
; 17-11-2017 P.Kamphuis; Fixed a bug where FAT could go into an
; infinite loop by doing PA boundary adjustments.
; 15-11-2017 P.Kamphuis; Added flag for easy running of the tests
; 29-06-2017 P.Kamphuis; Added a condition that the centre in the
; second fit cannot vary by more than 10%
; from the maximum radius. Also made the
; outputcatalogue messages more uniform by
; always including the fit results.
;
; 16-06-2017 P.Kamphuis; Upgraded to v5.0.2, added hanning
; smoothing of the SBR profile.
;
; 20-05-2015 P.Kamphuis; Added a condition for real massive
; galaxies to always be declining.
;
; 19-05-2017 P.Kamphuis; It appears that the offset centre was
; caused by a mismatch in translating CRPIX
; when doing the regridding. Oddly a factor
; of 1-1/ratio should be added.
; 17-05-2017 P.Kamphuis; Added the option to check the
; installation of FAT against my own runs
; in IDL and GDL.
; 17-05-2017 P.Kamphuis; Added a coherent error message for when
; the code crashes.
; 11-05-2017 P.Kamphuis; If the warp was sloped the code would
; take that number for the rotation
; curve. Now checks for VROT in VARINDX
; 10-05-2017 P.Kamphuis; The boundary conditions check demanded
; 2 rings to hit the boundary on the
; receding side and more then 2 on the
; approaching side. Have now set both
; sides to 2.
; 06-05-2017 P.Kamphuis; Added the warp output possibility.
; 28-03-2017 P.Kamphuis; Added a check for the use of ~ in the
; directory paths. If used the append
; function in gdl does not work hence they
; will be replaced with whatever directory
; cd finds under ~
; 27-03-2017 P.Kamphuis; The possibility to use a self ran SoFiA
; input had been lost in recent
; changes. Made sure it is functioning again.
; 22-03-2017 P.Kamphuis; Added condition for very massive
; galaxies to not be rising
; 07-03-2017 P.Kamphuis; Removed the debug parameter from the
; NGC_2903_2.0Beams_3.0SNR regularisation calls.
; 06-03-2017 P.Kamphuis; Added a part such that central continuum
; sources can be blanked no matter what
; way SoFiA swings on it.
; 23-02-2017 P.Kamphuis; reduced all instances of velconstused
; and set 2 instead of 3.5 times cutoff level
; 23-02-2017 P.Kamphuis; Lowered the satisfaction size of ypos
; and xpos and expressed values in
; pixelsize in order to ensure a
; better determination of the center and
; that SoFiA offsets are compensated for.
; 22-02-2017 P.Kamphuis; Compressed the sofia run in a single
; module called run SoFiA.
; 21-06-2016 P.Kamphuis; Modified the sofia run to use kernels
; with 0,0.5,1,2 the beam at a threshold
; of 7.5 and no reliability check.
; 15-06-2016 P.Kamphuis; Adapted the cutoff correction to be
; applying to the inclination+1/10th of
; the upper limit.
; 09-06-2016 P.Kamphuis; Removed the /CENTER keyword from the
; CONGRID call as it does not work in GDL.
; This means a +1/ratio addition to the
; CRPIX values. Additionally, divided the
; verror based on resolution by 2 as it is
; the 1 sigma error it should be half a channel.
; 20-04-2016 P.Kamphuis; Replaced revised_regulation with
; revised_regulation_com and
; revised_regulation_rot thus splitting
; the procedures used for the rotation
; curve (revised_regulation_rot) and PA and INCL
; (revised_regulation_com). revised_regulation_com
; now looks at both PA and Inclination to
; determine the sawtooth pattern and
; significant outliers. The error
; estimation has been completely
; revised. leading to much better fits.
; 05-03-2016 P.Kamphuis; Replaced parameterreguv87 with
; revised_regulation for increased
; compatibility with GDL as well as
; increased control over the fitting
; process. !This is a major change and
; version becomes 4.0. The new routine
; takes the amount of fixed rings into
; account when doing the chisqr
; minimization. As well as doing the mc
; chain to a specific accuracy this
; increases the stability between fits enormously.
; 03-03-2016 P.Kamphuis; The PA regularisation had a bug which is
; removed, also replaced the poly_fit
; routine with fat_fit for increased GDL compatibility
; 27-02-2016 P.Kamphuis; Added overviewplot to be produced for
; every galaxy and introduced the variable
; gdlidl which indentifies wether we are
; running idl or gdl
; 16-02-2016 P.Kamphuis,; Replaced the eigenql function
; N.Giese ; in fit_ellipse with a normal calculation
; as the function is not avalaible in
; GDL. Additionally adapted the use of
; Array_Indices to be GDL compatible.
; 05-01-2016 P.Kamphuis; Restructered the whole preprocessing
; into a more logical flow.
; 04-01-2016 P.Kamphuis; Added fail conditions when final model
; is outside the boundaries set in Kamphuis et al. 2015.
; 04-01-2016 P.Kamphuis; Removed the usage of the rename command
; and replaced it with a tirific specific
; idl routine.
; 04-01-2016 P.Kamphuis; Extensive editing of output messages to
; keep log clear and readable.
; 04-01-2016 P.Kamphuis; Minor bug fixes.
; 29-12-2016 P.Kamphuis: Replaced GAUSS_SMOOTH and SMOOTH with FAT_SMOOTH
; for increased compatibility with IDL 7.0 and GDL
; 29-12-2015 P.Kamphuis; Updated interaction to the latest
; version of SoFiA. Also introduced column
; recognition in order to facilitate
; future changes more easily. Additionally
; this allows the user to add extra output
; to the SofiA file. S+C threshold is
; changed from 4.0 to 5.5 to get similar
; masks dues to SoFiA moving back to their
; initial way of defining the
; threshold. SNR values no longer present in SoFiA.
; 29-12-2015 P.Kamphuis; There is an issue with the usage of
; NAXIS3 as the commit history show going
; back and forth between making this an
; integer or Double precision. I do not
; remember the reason for going to double
; precision but it has been reversed now
; because SoFiA requires and integer (As
; it should be)
; 12-08-2015 P.Kamphuis; Improved Bookkeeping for failed fits,
; improved noise determination and fixed
; an issue with spaces in the config file
; 10-08-2015 P.Kamphuis; On line 4012 it was possible to set the
; rotation curve to zero by having the
; inner INCL and PA hit the border. Made
; sure that if taking ring 0 for PA and
; INCL that the mean is taken for VROT.
; 30-07-2015 P.Kamphuis; Added a set of routines for
; bookkeeping. Still need to implement
; proper removal.
; 24-07-2015 P.Kamphuis; Added routine for the creation of
; residual files
; 24-07-2015 P.Kamphuis; Replaced the usage of SNR from Sofia
; with a proper flux from the integrated
; intensity map. Improved clean up and
; cflux handling. Also improved treatment
; of small galaxies and a better
; determination of CONDISP.
; Written by P.Kamphuis 01-01-2015
; NOTES:
; please note that this pipeline uses the following command line
; commands. Ensure that these work
; under IDL
;the original unix rename working as
;---> rename string replacestring inputfiles
; Standard unix commands pwd,mkdir,rm,
; cp, ls, python
; Tirific (any standalone version) should be properly installed
; and callable from the command line
; as tirific.
; SoFIA (The pipeline is updated to be
; compatible with the latest version,
; if there is any problem please let
; me know.) should be properly installed and the
; python file sofia_pipeline.py should
; be copied or linked into the Support
; directory
;A standard up to date IDL (v7.0 or
;higher) installation with ASTRO LIB
;installed should be enough to run the
;program
;All Fully Automated Tirific (FAT) specific routines are availaible in the
;Support directory
COMPILE_OPT IDL2
;If you have to run a specific config
;file needs to be run over and over
;again you could create a abbreviated
;name here. In this case the test file
;directories are listed
DEFSYSV, '!GDL', EXISTS = gdlidl ;is 1 when running GDL
spawn,'pwd',originaldir
goto,skipcatch
CATCH,Error_status
IF Error_status NE 0. THEN BEGIN
print, ' '
print, 'Oops the following went wrong:'
print,'-------------------------------------------'
help,/Last_Message, Output = theerrmess
;This gives trace back information in
;IDL but unfortunately not in GDL
for j=0,n_elements(theerrmess)-1 do print,theerrmess[j]
print,'-------------------------------------------'
if gdlidl then begin
print, ' '
print,'Unfortunately GDL does not provide trace back information in help'
print,'In order to fix this error please open FAT.pro and '
print,'uncomment line 273 (goto,skipcatch).'
print, ' '
print,'Then reproduce the error with trace back information '
print, 'and submit an issue at:'
print, ' '
print,' https://github.com/PeterKamphuis/FAT/issues'
print, ' '
print,'If the error occured while fitting a galaxy, please attach you fitting'
print,'log as well.'
endif else begin
print, ' '
print,'If this message completely baffles you then please submit the last 20 lines of output as a bug report to:'
print, ' '
print,' https://github.com/PeterKamphuis/FAT/issues'
print, ' '
print,'If the error occured while fitting a galaxy, please attach you fitting'
print,'log as well.'
endelse
cd,originaldir
stop
ENDIF
skipcatch:
version='V2.0.2'
;First thing we do is to check whether we run IDL or GDL
DEFSYSV, '!GDL', EXISTS = gdlidl ;is 1 when running GDL
if n_elements(supportdir) EQ 0 then supportdir='Support'
CD,supportdir,CURRENT=old_dir
;The directory where the support
;routines for the program are located
;Python needs this to be a full path
;on my mac so we'll set that
;with a pwd just in case
spawn,'pwd',supportdir
cd,old_dir
supportdir=supportdir[0]+'/'
;Extension name for a basic information (e.g HI diameter/mass central
;position) file
basicinfo='BasicInfo'
spacecheck=strtrim(str_sep(supportdir,' '),2)
supportdirchecked=STRJOIN(spacecheck,'\ ')
;Location of the input configuration file
if keyword_set(installation_check) then $
configfile='Installation_Check/FAT_INPUT.config'
if keyword_set(lvhistest) then begin
spawn,'printenv FAT_TEST_DIR',fatmaintest
configfile=fatmaintest+'/LVHIS-26_3/Input.config'
endif
if keyword_set(papertest) then begin
spawn,'printenv FAT_TEST_DIR',fatmaintest
configfile=fatmaintest+'/SmallCat_Warps/Input_1.config'
endif
if keyword_set(restest) then begin
spawn,'printenv FAT_TEST_DIR',fatmaintest
configfile=fatmaintest+'/Test_Res_v5.0/FAT_INPUT.config'
endif
if keyword_set(agctest) then begin
spawn,'printenv FAT_TEST_DIR',fatmaintest
configfile=fatmaintest+'/Better_Base/FAT_INPUT.config'
endif
if keyword_set(smallagctest) then begin
spawn,'printenv FAT_TEST_DIR',fatmaintest
configfile=fatmaintest+'/Small_Base/FAT_INPUT.config'
endif
if keyword_set(problems) then begin
spawn,'printenv FAT_TEST_DIR',fatmaintest
configfile=fatmaintest+'/Problems/FAT_INPUT.config'
endif
if keyword_set(new_agc) then begin
spawn,'printenv FAT_TEST_DIR',fatmaintest
configfile=fatmaintest+'/Full_Database/FAT_INPUT.config'
endif
IF n_elements(configfile) EQ 0 then begin
configfile='FAT_INPUT.config'
endif
;dumb resolve routine doesn't accept paths so cd to support dir
CD,supportdir,current=old_dir
RESOLVE_ROUTINE, 'beam_plot'
RESOLVE_ROUTINE, 'book_keeping'
RESOLVE_ROUTINE, 'buildaxii'
RESOLVE_ROUTINE, 'calc_edge'
RESOLVE_ROUTINE, 'changeradii'
RESOLVE_ROUTINE, 'check_cflux'
RESOLVE_ROUTINE, 'cleanup'
RESOLVE_ROUTINE, 'clean_header'
RESOLVE_ROUTINE, 'colour_bar'
RESOLVE_ROUTINE, 'colormaps'
RESOLVE_ROUTINE, 'columndensity'
RESOLVE_ROUTINE, 'convertradec'
RESOLVE_ROUTINE, 'convertskyanglefunction',/IS_FUNCTION
RESOLVE_ROUTINE, 'create_residuals'
RESOLVE_ROUTINE, 'dec_names'
RESOLVE_ROUTINE, 'extract_pv'
RESOLVE_ROUTINE, 'fat_arctan',/IS_FUNCTION
RESOLVE_ROUTINE, 'fatarctan',/IS_FUNCTION
RESOLVE_ROUTINE, 'fat_fit',/IS_FUNCTION
IF gdlidl then RESOLVE_ROUTINE,'fat_gdlgauss',/IS_FUNCTION,/COMPILE_FULL_FILE
RESOLVE_ROUTINE, 'fat_savgol',/IS_FUNCTION
RESOLVE_ROUTINE, 'fat_ploterror'
RESOLVE_ROUTINE, 'fat_smooth',/IS_FUNCTION
RESOLVE_ROUTINE, 'fat_spline',/IS_FUNCTION
RESOLVE_ROUTINE, 'getdhi'
RESOLVE_ROUTINE, 'get_fixedringsv9'
RESOLVE_ROUTINE, 'get_newringsv9'
RESOLVE_ROUTINE, 'get_progress'
RESOLVE_ROUTINE, 'int_profilev2'
RESOLVE_ROUTINE, 'interpolate'
RESOLVE_ROUTINE, 'install_check',/IS_FUNCTION
RESOLVE_ROUTINE, 'isnumeric',/IS_FUNCTION
RESOLVE_ROUTINE, 'linenumber',/IS_FUNCTION
RESOLVE_ROUTINE, 'momentsv2'
RESOLVE_ROUTINE, 'obtain_pa_incl'
RESOLVE_ROUTINE, 'obtain_ratios',/IS_FUNCTION
RESOLVE_ROUTINE, 'obtain_velpa'
RESOLVE_ROUTINE, 'obtain_w50'
RESOLVE_ROUTINE, 'organize_output'
RESOLVE_ROUTINE, 'overview_plot'
RESOLVE_ROUTINE, 'pre_ran_sofia'
RESOLVE_ROUTINE, 'preprocessing'
RESOLVE_ROUTINE, 'ra_names'
RESOLVE_ROUTINE, 'read_template'
RESOLVE_ROUTINE, 'regularisation_sdis'
RESOLVE_ROUTINE, 'rename'
RESOLVE_ROUTINE, 'revised_regularisation_com'
RESOLVE_ROUTINE, 'revised_regularisation_rot'
RESOLVE_ROUTINE, 'rottab'
RESOLVE_ROUTINE, 'run_sofia'
RESOLVE_ROUTINE, 'sbr_check'
RESOLVE_ROUTINE, 'set_sbr'
RESOLVE_ROUTINE, 'set_sdis'
RESOLVE_ROUTINE, 'set_vrotv6'
RESOLVE_ROUTINE, 'set_warp_slopev3'
RESOLVE_ROUTINE, 'showpixelsmap'
RESOLVE_ROUTINE, 'writefittingvariables'
RESOLVE_ROUTINE, 'writenewtotemplate'
CD,old_dir
tryconfigagain:
;open the configuration file and get the proper parameters
fileexist=FILE_TEST(configfile)
;however sometimes the config is not found
IF strlen(configfile) NE 0 AND fileexist EQ 0 then begin
print,"You have provided a config file but it can't be found"
print,"If you want to provide a config file please give the name"
print,"Else press enter to continue"
disp=''
WHILE (disp EQ '') DO BEGIN
read,disp
IF disp EQ '' then begin
configfile=''
disp='hh'
ENDIF else begin
configfile=disp
ENDELSE
ENDWHILE
goto,tryconfigagain
ENDIF
IF fileexist EQ 0 then goto,noconfig
close,1
h=' '
openr,1,configfile
filelength=FILE_LINES(configfile)-1
WHILE ~ EOF(1) DO BEGIN
readf,1,h
tmp=str_sep(strtrim(strcompress(h),2),'=')
IF n_elements(tmp) GT 1 then begin
case STRLOWCASE(strtrim(tmp[0],2)) of
;thefirst galaxy to be fitted
'startgalaxy':startgalaxy=double(tmp[1])
;thelast galaxy to be fitted
;if set to -1 the whole catalogue will be fitted
'endgalaxy':endgalaxy=double(tmp[1])
;parameters to skip certain fits for testing
;Files need to be present as it will only skip the tirific fitting part
;if you want to run only part of the code use testing remainder and will go on
;to the next galaxy
'testing':testing=double(tmp[1])
;Remove all previous pre-processing as cutting the cube 0 no 1 yes 2
;use provided preprocessing
'allnew':allnewin=double(tmp[1])
;Parameters for finishing the fitting process early
;IF set to one the program finishes after this loop (The first 1 it encounters)
'finishafter':finishafter=double(tmp[1])
;Parameter to set the size of the rings (the default is 1)
'ring_size': ring_spacing_in = double(tmp[1])
;Parameter to fix the inclination to
;being flat (the default is 1, not fixed)
'fix_incl': fix_incl_in = double(tmp[1])
;Parameter to fix the PA to
;being flat (the default is 1, not fixed)
'fix_pa': fix_pa_in = double(tmp[1])
;Parameter to fix the PA to
;being flat (the default is 1, not fixed)
'fix_sdis': fix_sdis_in = double(tmp[1])
;Input catalogue for the pipeline
'catalogue':catalogue=tmp[1]
;Directory with all the directories of the galaxies to be fitted
'maindir':maindir=tmp[1]
;Output file for the fitting results
'outputcatalogue':outputcatalogue=tmp[1]
;Output file for the log of the fit
'outputlog':olog=tmp[1]
;trigger for creating new output
;results file 'y' or append the old one 'n'
'new_output':newresult=tmp[1]
;trigger for creating a new output
;log file 'y' or append the old one 'n'
'new_log':newlog=tmp[1]
;Optimal number of pixels per maj axis beam
'opt_pixelbeam':optpixelbeam=fix(tmp[1])
;Hanning smoothed or not (y=1, n=0)
'velocity_resolution':vresolution=double(tmp[1])
;Clean up after the fitting is complete.
'maps_output':bookkeeping=double(tmp[1])
;Do we want warp info
'warp_output':warpoutput=double(tmp[1])
else:begin
end
endcase
endif
ENDWHILE
close,1
;if we are checking the installation
;then the maindir, outputcatalogue and
;Log go into the
;originaldir+installationcheck.
if keyword_set(Installation_Check) then begin
catalogue=originaldir[0]+'/Installation_Check/FAT_Input_Catalogue.txt'
maindir=originaldir[0]+'/Installation_Check/'
outputcatalogue=originaldir[0]+'/Installation_Check/Output_N2903.txt'
endif
;If maindir, outputcatalogue or
;catalog start with a ~ then replace
;it with the full path as append does
;not work in gdl otherwise.
tildepresent=STRMID(maindir,0,1)
thisishome=' '
IF tildepresent EQ '~' then begin
cd,'~',CURRENT=old_dir
spawn,'pwd',thisishome
cd,old_dir
maindir= STRJOIN(STRSPLIT(maindir, '~',/regex,/extract,/preserve_null),thisishome)
ENDIF
tildepresent=STRMID(catalogue,0,1)
IF tildepresent EQ '~' then begin
IF thisishome EQ ' ' then begin
cd,'~',CURRENT=old_dir
spawn,'pwd',thisishome
cd,old_dir
print,'are we doing this?'
ENDIF
catalogue= STRJOIN(STRSPLIT(catalogue, '~',/regex,/extract,/preserve_null),thisishome)
ENDIF
tildepresent=STRMID(outputcatalogue,0,1)
IF tildepresent EQ '~' then begin
IF thisishome EQ ' ' then begin
cd,'~',CURRENT=old_dir
spawn,'pwd',thisishome
cd,old_dir
print,'are we doing this?'
ENDIF
outputcatalogue= STRJOIN(STRSPLIT(outputcatalogue, '~',/regex,/extract,/preserve_null),thisishome)
ENDIF
noconfig:
;Make idiot failsafe standards for the config files
IF size(maindir,/TYPE) NE 7 then begin
print,'You have not provided the maindirectory for the data cubes'
disp=''
WHILE (disp EQ '') DO BEGIN
print,'Please provide the maindir for the data (Default = home)'
read,disp
IF disp EQ '' then disp='~'
filetest=FILE_TEST(disp)
IF filetest EQ 0 then begin
print,'That is not a valid directory please try again'
disp=''
ENDIF
ENDWHILE
maindir=disp
ENDIF
IF size(catalogue,/TYPE) NE 7 then begin
print,'You have not provided a catalogue with directories and initial estimates'
disp=''
WHILE (disp EQ '') DO BEGIN
print,'Please provide the catalogue with initial estimates (No Default)'
read,disp
filetest=FILE_TEST(disp)
IF filetest EQ 0 then begin
print,'That is not a valid file, please try again'
disp=''
ENDIF
ENDWHILE
catalogue=disp
ENDIF
IF size(outputcatalogue,/TYPE) NE 7 then begin
disp=''
WHILE (disp EQ '') DO BEGIN
print,'Please provide the file for results (Default = '+maindir+'/output_dir/fit_result.txt)'
read,disp
if disp EQ '' then begin
wehaveadir=FILE_TEST(maindir+'/output_dir')
IF wehaveadir EQ 0 then begin
spawn,'mkdir '+maindir+'/output_dir/',isthere
ENDIF
disp=maindir+'output_dir/fit_results.txt'
endif
ENDWHILE
outputcatalogue=disp
ENDIF
;defaults for configuration file
IF size(finishafter,/TYPE) EQ 0 then finishafter=2
IF size(testing,/TYPE) EQ 0 then testing=0
IF size(startgalaxy,/TYPE) EQ 0 then startgalaxy=0
IF size(endgalaxy,/TYPE) EQ 0 then endgalaxy=-1
IF size(newlog,/TYPE) EQ 0 then newlog='y'
IF size(newresult,/TYPE) EQ 0 then newresult='y'
IF n_elements(vresolution) EQ 0 then vresolution=1.
IF n_elements(ring_spacing_in) EQ 0. then ring_spacing_in = 1.
IF n_elements(fix_incl_in) EQ 0. then fix_incl_in = 1.
IF n_elements(fix_pa_in) EQ 0. then fix_pa_in = 1.
IF n_elements(fix_sdis_in) EQ 0. then fix_sdis_in = 1.
IF n_elements(optpixelbeam) EQ 0 then optpixelbeam=4.
IF n_elements(allnewin) EQ 0 then allnewin=1
IF n_elements(bookkeeping) EQ 0 then bookkeeping=3
IF n_elements(warpoutput) EQ 0 then warpoutput=0
IF ring_spacing_in LT 0.5 then ring_spacing_in = 0.5
IF bookkeeping EQ 5. then bookkeeping=4
; And adding the Hublle constant
H_0 = 70. ;km/s/Mpc
bookkeepingin=bookkeeping
finishafterold=finishafter
;start a log with current program name
help,calls=cc
print,linenumber()+"This is version "+version[0]+" of the program"
;read the input catalogue
openr,1,catalogue
filelength=FILE_LINES(catalogue)-1
catnumber=strarr(filelength)
catRA=strarr(filelength)
catDEC=strarr(filelength)
epoch=strarr(filelength)
catPA=dblarr(filelength)
catPAdev=dblarr(filelength)
catinc=dblarr(filelength)
catincdev=dblarr(filelength)
catvsys=dblarr(filelength)
catmaxrot=dblarr(filelength)
catmaxrotdev=dblarr(filelength)
catDistance=dblarr(filelength)
catnoise=dblarr(filelength)
catmajbeam=dblarr(filelength)
catminbeam=dblarr(filelength)
catbpa=dblarr(filelength)
catDirname=strarr(filelength)
catCubename=strarr(filelength)
catMom0name=strarr(filelength)
catMom1name=strarr(filelength)
catMom2name=strarr(filelength)
catMaskname=strarr(filelength)
catCatalogname=strarr(filelength)
h=' '
readf,1,h
tmp=str_sep(strtrim(strcompress(h),2),'|')
;IF we have a new catalog we need to
;determine what each column is
;referring to
IF n_elements(tmp) LT 16 then begin
;When using the Sofia input it is not
;necessary to have a specific ordering
;of the input catalogue just the
;correct column names
tmpnumber=WHERE(STRLOWCASE(tmp) EQ 'number')
tmpdistance=WHERE(STRLOWCASE(tmp) EQ 'distance')
tmpdir=WHERE(STRLOWCASE(tmp) EQ 'directoryname')
tmpcube=WHERE(STRLOWCASE(tmp) EQ 'cubename')
IF n_elements(tmp) GT 4 then begin
test=WHERE(STRLOWCASE(tmp) EQ 'basename')
IF test[0] EQ -1 then begin
tmpmom2=WHERE(STRLOWCASE(tmp) EQ 'moment2')
tmpmom1=WHERE(STRLOWCASE(tmp) EQ 'moment1')
tmpmom0=WHERE(STRLOWCASE(tmp) EQ 'moment0')
tmpmask=WHERE(STRLOWCASE(tmp) EQ 'mask')
tmpcatalog=WHERE(STRLOWCASE(tmp) EQ 'catalog')
;If we want to use preprocessed input
;than we need moment0 moment1 a mask
;and a catalog
IF allnewin GT 1 AND (tmpmom1[0] EQ -1 OR tmpmom2[0] EQ -1 OR tmpmom0[0] EQ -1 OR tmpmask[0] EQ -1 OR tmpcatalog[0] EQ -1) then begin
print,'Your catalog file is not configured properly. Aborting'
stop
ENDIF
ENDIF
ENDIF ELSE BEGIN
;If we want to use preprocessed input
;than we need moment0 moment1 a mask
;and a catalog
IF allnewin GT 1 then begin
print,'Your catalog file is not configured properly. Aborting'
stop
ENDIF
ENDELSE
ENDIF
;Then read the actual input from the catalo
h=' '
counter=0
for i=0,filelength-1 do begin
readf,1,h
tmp=str_sep(strtrim(strcompress(h),2),'|')
if n_elements(tmp) LT 3 then begin
counter++
ENDIF
IF n_elements(tmp) GE 16 then begin
;Old fashioned input of initial
;estimates from the literature
;Better to use the Sofia estimates as
;the program is fine tuned to them
catnumber[i]=tmp[0]
catRA[i]=tmp[1]
catDEC[i]=tmp[2]
epoch[i]=tmp[3]
catPA[i]=double(tmp[4])
catinc[i]=double(tmp[5])
catincdev[i]=double(tmp[6])
catvsys[i]=double(tmp[7])
catmaxrot[i]=double(tmp[8])
catDistance[i]=double(tmp[9])
;IF catDistance[i] LT 1. AND catDistance[i] NE -1 THEN catDistance[i]=1.
catnoise[i]=double(tmp[10])
catmajbeam[i]=double(tmp[11])
catminbeam[i]=double(tmp[12])
catbpa[i]=double(tmp[13])
catDirname[i]=tmp[14]
tmpagain=str_sep(tmp[15],'.')
IF n_elements(tmpagain) GT 1 and STRLOWCASE(tmpagain[n_elements(tmpagain)-1]) EQ 'fits' then begin
IF n_elements(tmpagain) GT 2 then catCubename[i]=STRJOIN(tmpagain[0:n_elements(tmpagain)-2],'.') else catCubename[i]=tmpagain[0]
endif else catCubename[i]=tmp[15]
IF n_elements(tmp) GT 16 then begin
catMom0name[i]=tmp[16]
catMom1name[i]=tmp[17]
catMom2name[i]=tmp[18]
ENDIF ELSE BEGIN
catMom0name[i]='youshouldnotnameyourfilesthisway'
catMom1name[i]='youshouldnotnameyourfilesthisway'
catMom2name[i]='youshouldnotnameyourfilesthisway'
ENDELSE
counter=0
ENDIF ELSE BEGIN
IF n_elements(tmp) GT 3 then begin
counter=0 ;IF we have a new catalog we use the determined order
catnumber[i]=tmp[tmpnumber]
catDirname[i]=tmp[tmpdir]
tmpagain=str_sep(tmp[tmpcube],'.')
IF n_elements(tmpagain) GT 1 and STRLOWCASE(tmpagain[n_elements(tmpagain)-1]) EQ 'fits' then begin
IF n_elements(tmpagain) GT 2 then catCubename[i]=STRJOIN(tmpagain[0:n_elements(tmpagain)-2],'.') else catCubename[i]=tmpagain[0]
endif else catCubename[i]=tmp[tmpcube]
catDistance[i]=double(tmp[tmpdistance])
;IF we have preprocessed data we read the file names
IF allnewin GT 1 then begin
IF test[0] EQ -1 then begin
catMom0name[i]=tmp[tmpmom0]
catMom1name[i]=tmp[tmpmom1]
catMom2name[i]=tmp[tmpmom2]
catMaskname[i]=tmp[tmpmask]
catCatalogname[i]=tmp[tmpcatalog]
tmpagain=str_sep(tmp[tmpcatalog],'.')
IF n_elements(tmpagain) GT 1 and STRLOWCASE(tmpagain[n_elements(tmpagain)-1]) NE 'ascii' then catCatalogname[i]=tmp[tmpcatalog]+'.ascii'
ENDIF ELSE BEGIN
catMom0name[i]=tmp[test]+'_mom0'
catMom1name[i]=tmp[test]+'_mom1'
catMom2name[i]=tmp[test]+'_mom2'
catMaskname[i]=tmp[test]+'_mask'
catCatalogname[i]=tmp[test]+'_cat.ascii'
ENDELSE
; else we make sure they have a
; silly name so they don't
; exist in the directory
ENDIF ELSE BEGIN
catMom0name[i]='youshouldnotnameyourfilesthisway'
catMom1name[i]='youshouldnotnameyourfilesthisway'
catMom2name[i]='youshouldnotnameyourfilesthisway'
ENDELSE
ENDIF
ENDELSE
endfor
close,1
;Let's get the longest dirname
;in the catalogue so we make
;sure it writes ok.
maxdirlen = 14
for i =0,n_elements(catDirname)-1 do begin
tmplen=strlen(catDirname[i])
if tmplen GT maxdirlen then maxdirlen=tmplen
endfor
dirformat='A-'+strtrim(string(maxdirlen),2)
;Create a file to write the results to
If not FILE_TEST(outputcatalogue) OR strupcase(newresult) EQ 'Y' then begin
comment='Comments on Fit Result'
commentlen='A'+strtrim(string(strlen(comment)),2)
openw,1,outputcatalogue
printf,1,'Directory Name','AC1','AC2',comment,format='("",('+dirformat+')," ",2(A6)," ",('+commentlen+'))'
close,1
endif
;if our end galaxy is -1 then we want
;to fit the full catalog
IF startgalaxy LT 0 then startgalaxy = 0
IF endgalaxy EQ -1 then begin
endgalaxy=filelength-1-counter
ENDIF
;start the main galaxy fitting loop
for i=startgalaxy,endgalaxy do begin
;To ensure using the right template we
;read them everytime we start a new galaxy
finishafter=finishafterold
allnew=allnewin
bookkeeping=bookkeepingin
ring_spacing = ring_spacing_in
fix_incl= fix_incl_in
fix_pa= fix_pa_in
fix_sdis= fix_sdis_in
doubled=0
ini_mode_factor=25.
currentfitcube=catcubename[i]
noisemapname=currentfitcube+'_noisemap'
close,1
;Start a galaxy specific fitting log
IF size(olog,/TYPE) EQ 7 then begin
if catdirname[i] EQ './' then log=maindir+'/'+olog else $
log=maindir+'/'+catdirname[i]+'/'+olog
prevlog=maindir+'/'+catdirname[i]+'/Prev_Log.txt'
spawn,'mv '+log+' '+prevlog
IF not FILE_TEST(log) OR strupcase(newlog) EQ 'Y' then begin
openw,66,log
printf,66,linenumber()+"This file is a log of the fitting process run at "+systime()
printf,66,linenumber()+"This is version "+version[0]+" of the program."
IF gdlidl then begin
printf,66,linenumber()+"You are using GDL"
endif else begin
printf,66,linenumber()+"You are using IDL"
endelse
close,66
ENDIF ELSE BEGIN
openu,66,log,/APPEND
printf,66,linenumber()+"This file is a log of the fitting process continued at "+systime()
printf,66,linenumber()+"This is version "+version[0]+" of the program."
close,66
ENDELSE
ENDIF
;Read the template for the first
;tirific fit
read_template,supportdir+'/1stfit.def', tirificfirst, tirificfirstvars
;Read the template for the second
;tirific fit
read_template,supportdir+'/2ndfit.def', tirificsecond, tirificsecondvars
;Read the template sofia input file
;print which galaxy we are at
print,linenumber()+"We're at galaxy number "+strtrim(string(i,format='(I10)'),2)+". Which is catalogue id number "+catnumber[i]
IF size(log,/TYPE) EQ 7 then begin
openu,66,log,/APPEND
printf,66,linenumber()+"We're at galaxy number "+strtrim(string(i,format='(I10)'),2)+". Which is catalogue id number "+catnumber[i]
close,66
ENDIF
; And the directory
new_dir=maindir+'/'+catdirname[i]+'/'
CD,new_dir,CURRENT=old_dir
;To avoid confusion we remove all
;initial fits and subsequent fits that
;are present if we want to start all
;over again
IF allnewin EQ 1 then begin
IF testing LT 1 then cleanup,currentfitcube
ENDIF
CD,OLD_DIR
;See if we already cut the initial cube to something more accesible
;Let's go to the right directory
IF size(log,/TYPE) EQ 7 then begin
openu,66,log,/APPEND
printf,66,linenumber()+"Working in Directory "+new_dir
close,66
ENDIF ELSE BEGIN
print,linenumber()+"Working in Directory "+new_dir
ENDELSE
;Break if the cube cannot be found
cubeext='.fits'
fitsexists=FILE_TEST(maindir+'/'+catdirname[i]+'/'+currentfitcube+cubeext)
IF fitsexists EQ 0 then begin
cubext='.FITS'
fitsexists=FILE_TEST(maindir+'/'+catdirname[i]+'/'+currentfitcube+cubeext)
ENDIF
if fitsexists EQ 0 then begin
comment='This galaxy has no fits cube to work with, it is skipped.'
commentlen='A'+strtrim(string(strlen(comment)),2)
openu,1,outputcatalogue,/APPEND
printf,1,catDirname[i],strtrim(string(0),2),strtrim(string(0),2),comment,format='("",('+dirformat+')," ",2(A6)," ",('+commentlen+'))'
close,1
IF size(log,/TYPE) EQ 7 then begin
openu,66,log,/APPEND
printf,66,linenumber()+catDirname[i]+" This galaxy has no fits cube to work with, it is skipped."
close,66
ENDIF ELSE BEGIN
print,linenumber()+catDirname[i]+" This galaxy has no fits to work with, it is skipped."
ENDELSE
bookkeeping=5
optimized=0
goto,finishthisgalaxy
endif
;set some triggers at 0
maxrings=0.
propermom0=0
propermom1=0
propermom2=0
propermomnoise=0
optimized=0
wroteblank=0
;Let's see if a pre-processed
;cube exists and if we want to
;use that
precube=FILE_TEST(maindir+'/'+catdirname[i]+'/'+currentfitcube+'_preprocessed'+cubeext)
IF NOT precube OR allnew EQ -1 then begin
;read in the cube and check
;it's header
dummy=readfits(maindir+'/'+catdirname[i]+'/'+currentfitcube+cubeext,header,/NOSCALE,/SILENT)
;if the cube is in integer format we
;should scale the values in order to
;not mess thing up
IF sxpar(header,'BITPIX') EQ 16 then dummy=readfits(maindir+'/'+catdirname[i]+'/'+currentfitcube+cubeext,header,/SILENT)
IF n_elements(catmajbeam) GT 0 then begin
beam=[catmajbeam[i],catminbeam[i]]
ENDIF ELSE beam=[1,1]
noise=1
clean_header,header,writecube,beam,log=log,catalogue=outputcatalogue,directory=catdirname[i],dir_format=dirformat
IF writecube EQ 2 then begin
bookkeeping=5
GOTO,FINISHTHISGALAXY
ENDIF
preprocessing,dummy,header,writecube,catalogue=outputcatalogue,noise=noise,name=currentfitcube,directory=catdirname[i],log=log,dir_format=dirformat
Case 1 of
writecube EQ 0:begin
IF size(log,/TYPE) EQ 7 then begin
openu,66,log,/APPEND
printf,66,linenumber()+"The input cube is correctly organized and does not need to be modified."
close,66
ENDIF
end
writecube EQ 1 OR writecube EQ 3:begin
IF size(log,/TYPE) EQ 7 then begin
openu,66,log,/APPEND
printf,66,linenumber()+"The input cube is not suitable for fitting we are rewriting it."
close,66
ENDIF
;If we changed something we rewrite the cube
writefits,maindir+'/'+catdirname[i]+'/'+currentfitcube+'_preprocessed'+cubeext,float(dummy),header
currentfitcube=currentfitcube+'_preprocessed'
if allnew EQ 0 then allnew=1
if allnew EQ 2 then begin
;If we have pre-processed sofia we need to make sure our mask is treated in the same way
maskin = readfits(maindir+'/'+catdirname[i]+'/'+catMaskname[i]+cubeext,mask_header,/NOSCALE,/SILENT)
preprocessing,maskin,mask_header,writecube,catalogue=outputcatalogue,noise=noise,name=catMaskname[i],directory=catdirname[i],log=log,dir_format=dirformat,/mask
writefits,maindir+'/'+catdirname[i]+'/'+catMaskname[i]+'_preprocessed'+cubeext,float(maskin),mask_header
catMaskname[i]=catMaskname[i]+'_preprocessed'
ENDIF
end
writecube EQ 2:begin
;If we encountered a problem we abort
bookkeeping=5
GOTO,FINISHTHISGALAXY
end
;writecube EQ 3:begin
;If we have changed more than just the
;header we need new moment maps even
;if they already exists and user does
;not ask for them. Only not when sofia
;pre-prepared output is used.
; writefits,maindir+'/'+catdirname[i]+'/'+currentfitcube+'_preprocessed'+cubeext,float(dummy),header
; currentfitcube=currentfitcube+'_preprocessed'
; if allnew EQ 0 then allnew=1
; end
else:begin
IF size(log,/TYPE) EQ 7 then begin
openu,66,log,/APPEND
printf,66,linenumber()+"This should never happen."
close,66
ENDIF
print,linenumber()+"This should never happen."
stop
end