File: hdrl_response.c

package info (click to toggle)
cpl-plugin-xshoo 3.5.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 20,920 kB
  • sloc: ansic: 170,001; sh: 4,369; python: 2,391; makefile: 1,211
file content (1878 lines) | stat: -rw-r--r-- 67,639 bytes parent folder | download | duplicates (12)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
/*
 * This file is part of the HDRL
 * Copyright (C) 2017 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*-----------------------------------------------------------------------------
                                   Includes
 -----------------------------------------------------------------------------*/

#include "hdrl_response.h"
#include "hdrl_spectrum_resample.h"
#include "hdrl_efficiency.h"
#include "hdrl_utils.h"
#include <math.h>
#include <cpl.h>


/*-----------------------------------------------------------------------------
                          Private Functions
 -----------------------------------------------------------------------------*/
static inline  cpl_array *
get_uniform_wavs(const hdrl_spectrum1D * s, const hdrl_data_t w_step,
        const hdrl_data_t lmin, const hdrl_data_t lmax);

static inline hdrl_xcorrelation_result *
correlate_obs_with_telluric(const hdrl_spectrum1D * obs_s,
                                            const hdrl_spectrum1D * telluric_s,
                                            const hdrl_data_t w_step,
                                            cpl_size half_win,
                                            const cpl_boolean normalize,
                                            const hdrl_data_t lmin,
                                            const hdrl_data_t lmax);

static inline hdrl_spectrum1D *
shift_and_convolve_telluric_model(const hdrl_spectrum1D * obs,
                                              const hdrl_spectrum1D * telluric,
                                              const hdrl_data_t w_step,
                                              const cpl_size half_win,
                                              const cpl_boolean normalize,
                                              const hdrl_data_t lmin,
                                              const hdrl_data_t lmax,
                                              double * telluric_shift);

static inline cpl_size get_lower_odd(const cpl_size sz);

static inline hdrl_spectrum1D *
convolve_with_kernel_symmetrically(const hdrl_spectrum1D * s,
                                     const double sigma,
                                     const hdrl_data_t w_step);

static inline cpl_matrix *
create_symmetrical_gaussian_kernel(const double  slitw, const double  fwhm,
		const cpl_size max_sz);

static inline hdrl_spectrum1D *
convolve_spectrum_with_kernel(const hdrl_spectrum1D * s,
                         const cpl_matrix * kernel);

static inline double
erf_antideriv(const double x, const double sigma);


/*calculate ratio between obs spectrum and convolved and shifted telluric model*/
static inline hdrl_spectrum1D *
compute_corrected_obs_spectrum(
                              const hdrl_spectrum1D * obs_s_arg,
                              const hdrl_spectrum1D * telluric_s_arg,
                              const hdrl_data_t w_step,
                              const cpl_size half_win,
                              const cpl_boolean normalize,
                              const cpl_boolean shift_in_log_scale,
                              const hdrl_data_t lmin, const hdrl_data_t lmax,
                              double * telluric_shift);

static inline hdrl_spectrum1D *
compute_interpolated_spectrum(const hdrl_spectrum1D * wlength_source,
        const hdrl_spectrum1D * sampled_points,
		const hdrl_spectrum1D_interpolation_method method);

static inline void
compute_quality(const hdrl_spectrum1D * s,
        const cpl_bivector * quality_areas, double * mean_abs_difference_from_1,
		double * stddev);

static inline void
free_spectrum_array(hdrl_spectrum1D ** s, const cpl_size sz);

static inline cpl_error_code
get_first_error_code(const cpl_error_code * codes, const cpl_size sz);

static inline hdrl_spectrum1D *
select_win(const hdrl_spectrum1D * s, const hdrl_data_t wmin,
        const hdrl_data_t wmax);

static inline hdrl_spectrum1D *
select_obs_wlen( const hdrl_spectrum1D * s,
		const hdrl_spectrum1D * wlens_source);

static inline hdrl_spectrum1D *
correct_spectrum_for_doppler_shift(const hdrl_spectrum1D * s,
					const hdrl_data_t offset);

static inline hdrl_spectrum1D *
filter_spectrum_median(const hdrl_spectrum1D * resp, const cpl_size radius);

static inline hdrl_image *
compute_median_on_hdrl_image(const hdrl_image * img, const cpl_size radius);

static inline hdrl_spectrum1D *
resample_on_medians_skip_abs_regions(const hdrl_spectrum1D * s,
		const cpl_array * fit_points, const cpl_bivector * high_abs_regions,
		const hdrl_data_t wrange);

static inline cpl_array *
remove_regions_and_outliers_from_array(const cpl_array * fit_points,
		const cpl_bivector * high_abs_regions,
		const hdrl_data_t wmin, const hdrl_data_t wmax);

static inline cpl_boolean
contained_in_any_region(const hdrl_data_t w, const cpl_bivector * high_abs_regions);

static inline hdrl_spectrum1D *
get_median_on_fit_points(const hdrl_spectrum1D * s_input,
		const cpl_array * fit_points, const hdrl_data_t wrange);

static inline hdrl_spectrum1D *
remove_bad_data(const hdrl_spectrum1D * s);

static inline const hdrl_spectrum1Dlist *
hdrl_response_telluric_evaluation_parameter_get_telluric_models(
		const hdrl_parameter * par);

static inline hdrl_data_t
hdrl_response_telluric_evaluation_parameter_get_w_step(
		const hdrl_parameter * par);

static inline cpl_size
hdrl_response_telluric_evaluation_parameter_get_half_win(
		const hdrl_parameter * par);

static inline cpl_boolean
hdrl_response_telluric_evaluation_parameter_get_normalize(
		const hdrl_parameter * par);

static inline cpl_boolean
hdrl_response_telluric_evaluation_parameter_get_shift_in_log_scale(
		const hdrl_parameter * par);

static inline const cpl_bivector *
hdrl_response_telluric_evaluation_parameter_get_quality_areas(
		const hdrl_parameter * par);

static inline const cpl_bivector *
hdrl_response_telluric_evaluation_parameter_get_fit_areas(
		const hdrl_parameter * par);

static inline hdrl_data_t
hdrl_response_telluric_evaluation_parameter_get_lmin(
		const hdrl_parameter * par);

static inline hdrl_data_t
hdrl_response_telluric_evaluation_parameter_get_lmax(
		const hdrl_parameter * par);

static inline const cpl_array *
hdrl_response_parameter_get_fit_points(
		const hdrl_parameter * par);

static inline const cpl_bivector *
hdrl_response_parameter_get_high_abs_regions(
		const hdrl_parameter * par);

static inline cpl_size
hdrl_response_parameter_get_radius(
		const hdrl_parameter * par);

static inline hdrl_data_t
hdrl_response_parameter_get_wrange(
		const hdrl_parameter * par);

static inline hdrl_spectrum1D *
	hdrl_spectrum1D_extract_fit_regions(const hdrl_spectrum1D * s,
			const cpl_bivector * areas);

/*Private data structures*/

/*Parameter used for the telluric evaluation*/
typedef struct {
     HDRL_PARAMETER_HEAD;
	 hdrl_spectrum1Dlist * telluric_models;
	 hdrl_data_t w_step;
	 cpl_size half_win;
	 cpl_boolean normalize;
	 cpl_boolean shift_in_log_scale;
	 cpl_bivector * quality_areas;
	 cpl_bivector * fit_areas;
	 hdrl_data_t lmin;
	 hdrl_data_t lmax;
}hdrl_response_telluric_evaluation_parameter;

static inline void
hdrl_response_telluric_evaluation_parameter_delete(
		hdrl_parameter * ev){

	if(ev == NULL) return;

	if(hdrl_parameter_get_parameter_enum(ev)
			!= HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION)
		return;

	hdrl_response_telluric_evaluation_parameter * par =
			(hdrl_response_telluric_evaluation_parameter *)ev;

	hdrl_spectrum1Dlist_delete(par->telluric_models);
	cpl_bivector_delete(par->quality_areas);
	cpl_bivector_delete(par->fit_areas);
	cpl_free(par);
}

static hdrl_parameter_typeobj
hdrl_telluric_eval_parameters_type = {
		HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,      			/* type */
    (hdrl_alloc *)&cpl_malloc,                							/* fp_alloc */
    (hdrl_free *)&hdrl_response_telluric_evaluation_parameter_delete, /* fp_free */
    NULL,                                     							/* fp_destroy */
    sizeof(hdrl_response_telluric_evaluation_parameter),  			/* obj_size */
};

/*Parameter used for the response*/
typedef struct {
     HDRL_PARAMETER_HEAD;
     cpl_size radius;
     cpl_array * fit_points;
     cpl_bivector * high_abs_regions;
     hdrl_data_t wrange;
}response_fit_parameter;

static inline void
hdrl_response_fit_parameter_delete(
		hdrl_parameter * ev){

	if(ev == NULL) return;

	if(hdrl_parameter_get_parameter_enum(ev)
			!= HDRL_PARAMETER_RESPONSE_FIT)
		return;

	response_fit_parameter * par =
			(response_fit_parameter *)ev;

	cpl_bivector_delete(par->high_abs_regions);
	cpl_array_delete(par->fit_points);
	cpl_free(par);
}

static hdrl_parameter_typeobj
hdrl_response_parameters_type = {
	HDRL_PARAMETER_RESPONSE_FIT,      			/* type */
    (hdrl_alloc *)&cpl_malloc,                			/* fp_alloc */
    (hdrl_free *)&hdrl_response_fit_parameter_delete, /* fp_free */
    NULL,                                     			/* fp_destroy */
    sizeof(response_fit_parameter),  		/* obj_size */
};


static inline hdrl_spectrum1D *
hdrl_spectrum1D_create_from_buffers(double * flux, double * wlens, cpl_size sz,
		hdrl_spectrum1D_wave_scale scale);

static inline hdrl_response_result *
hdrl_response_result_wrap(hdrl_spectrum1D * final_response,
		  	  	  	      hdrl_spectrum1D * selected_response,
						  hdrl_spectrum1D * raw_response,

						  hdrl_spectrum1D * corrected_observed_spectrum,
						  cpl_size best_telluric_model_idx,
                          hdrl_data_t telluric_shift,
						  hdrl_data_t avg_diff_from_1,
						  hdrl_data_t stddev,

						  hdrl_data_t doppler_shift);

/**
 *
 * @addtogroup hdrl_response
 */
/*----------------------------------------------------------------------------*/

/**@{*/

/*-----------------------------------------------------------------------------
                                   Functions
 -----------------------------------------------------------------------------*/

/* ---------------------------------------------------------------------------*/
/**
 * @brief ctor for the hdrl_parameter for the telluric evaluation
 * @param telluric_models		The available telluric models
 * @param w_step				Sampling step to use when upsampling model and
 * 								observed spectrum to calculate the cross
 * 								correlations
 * @param half_win				Half the search window to be used to find the
 * 								peak of the cross correlation
 * @param normalize				CPL_TRUE if the cross correlation should be
 * 								normalized, CPL_FALSE otherwise
 * @param shift_in_log_scale	CPL_TRUE if the cross correlation has to be
 * 								calculated	in logarithmic scale.
 * 								CPL_FALSE otherwise.
 * @param quality_areas			Areas where the quality of the fit of the
 * 								telluric model has to be evaluated.
 * @param fit_areas				Areas where the median points are extracted from,
 * 								in order to generate the final quality parameters
 * 								of the telluric model
 * @param lmin					Minimum wavelength used to calculate the
 * 								cross-correlation (in log scale if
 * 								shift_in_log_scale = TRUE)
 * @param lmax					Maximum wavelength used to calculate the
 * 								cross-correlation (in log scale if
 * 								shift_in_log_scale = TRUE)
 * @return hdrl_parameter or NULL in case of error, see below.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: if any of the pointers are NULL
 * - CPL_ERROR_ILLEGAL_INPUT: if w_step <= 0 or half_win <= 0 or lmin >= lmax
 */
/* ---------------------------------------------------------------------------*/

hdrl_parameter *
hdrl_response_telluric_evaluation_parameter_create(
										const hdrl_spectrum1Dlist * telluric_models,
										hdrl_data_t w_step,
										cpl_size half_win,
										cpl_boolean normalize,
										cpl_boolean shift_in_log_scale,
										const cpl_bivector * quality_areas,
										const cpl_bivector * fit_areas,
										hdrl_data_t lmin,
										hdrl_data_t lmax){

    cpl_ensure(quality_areas != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(telluric_models != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(fit_areas != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(w_step > 0.0, CPL_ERROR_ILLEGAL_INPUT, NULL);
    cpl_ensure(half_win > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
    cpl_ensure(lmin < lmax, CPL_ERROR_ILLEGAL_INPUT, NULL);

    hdrl_response_telluric_evaluation_parameter * ev
        = (hdrl_response_telluric_evaluation_parameter *)
           hdrl_parameter_new(&hdrl_telluric_eval_parameters_type);

    ev->telluric_models = hdrl_spectrum1Dlist_duplicate(telluric_models);
    ev->w_step = w_step;
    ev->half_win = half_win;
    ev->normalize = normalize;
    ev->shift_in_log_scale = shift_in_log_scale;
    ev->quality_areas = cpl_bivector_duplicate(quality_areas);
    ev->fit_areas = cpl_bivector_duplicate(fit_areas);
    ev->lmin = lmin;
    ev->lmax = lmax;

    return (hdrl_parameter *)ev;
}


/* ---------------------------------------------------------------------------*/
/**
 * @brief ctor for the hdrl_parameter for the final interpolation of the response
 * @param radius				Radius of the median filter used to smooth the
 * 								response
 * 								before the final interpolation
 * @param fit_points			Median points where the fit will be calculated
 * @param wrange				Range around the median point where the median
 * 								is calculated
 * @param high_abs_regions		High absorption regions that should be skipped
 * 								when calculating the fit. If NULL no skipping is done.
 *
 * @return hdrl_parameter or NULL in case of error, see below.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: if any of the required pointers are NULL
 * - CPL_ERROR_ILLEGAL_INPUT: radius or wrange is less or equal 0
 */
/* ---------------------------------------------------------------------------*/
hdrl_parameter *
hdrl_response_fit_parameter_create(
		const cpl_size radius, const cpl_array * fit_points,
		const hdrl_data_t wrange, const cpl_bivector * high_abs_regions){

	cpl_ensure(radius > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
	cpl_ensure(wrange > 0.0, CPL_ERROR_ILLEGAL_INPUT, NULL);
	cpl_ensure(fit_points != NULL, CPL_ERROR_NULL_INPUT, NULL);

	response_fit_parameter * p
	        = (response_fit_parameter *)
	           hdrl_parameter_new(&hdrl_response_parameters_type);

	p->fit_points = cpl_array_duplicate(fit_points);
	p->high_abs_regions =  NULL;

	if(high_abs_regions)
		p->high_abs_regions = cpl_bivector_duplicate(high_abs_regions);

	p->radius = radius;
	p->wrange = wrange;

	return (hdrl_parameter *) p;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Computation of the response
 * @param obs_s				Observed spectrum
 * @param ref_s				Reference std star spectrum
 * @param E_x				Atmospheric Extinction
 * @param telluric_par		Telluric correction parameter,
 * 							NULL if telluric correction is skipped.
 * 							See hdrl_response_telluric_evaluation_parameter_create
 * 							for details.
 * @param velocity_par		Doppler shift estimation and compensation. NULL if
 * 							compensation has to be skipped.
 * 							See hdrl_spectrum1D_shift_fit_parameter_create for
 * 							more details.
 * @param calc_par			Parameter for the core computation of the response,
 * 							e.g. exposure time.
 * 							See hdrl_response_parameter_create for more details.
 * @param fit_par			Parameter for the final interpolation of the response,
 * 							see hdrl_response_fit_parameter_create for more details.
 *
 * @return hdrl_response_result or NULL in case of error, see below.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: if any among calc_par, fit_par  or the spectra
 * 						   are NULL
 * - CPL_ERROR_ILLEGAL_OUTPUT: Any of the algorithmical steps can fail.
 * 							   This usually means that the output  of that step
 * 							   can be NULL, if that is the case we abort triggering
 * 							   a CPL_ERROR_ILLEGAL_OUTPUT
 */
/* ---------------------------------------------------------------------------*/
hdrl_response_result *
hdrl_response_compute(
        const hdrl_spectrum1D * obs_s,
		const hdrl_spectrum1D * ref_s,
		const hdrl_spectrum1D * E_x,
		const hdrl_parameter * telluric_par,
		const hdrl_parameter * velocity_par,
		const hdrl_parameter * calc_par,
		const hdrl_parameter * fit_par){

	cpl_ensure(calc_par != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(hdrl_parameter_get_parameter_enum(calc_par) ==
			HDRL_PARAMETER_EFFICIENCY, CPL_ERROR_ILLEGAL_INPUT, NULL);

	if(telluric_par)
		cpl_ensure(hdrl_parameter_get_parameter_enum(telluric_par) ==
				HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, NULL);

	if(velocity_par)
		cpl_ensure(hdrl_parameter_get_parameter_enum(velocity_par) ==
				HDRL_PARAMETER_SPECTRUM1D_SHIFT,
				CPL_ERROR_ILLEGAL_INPUT, NULL);

	cpl_ensure(fit_par != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(hdrl_parameter_get_parameter_enum(fit_par) ==
				HDRL_PARAMETER_RESPONSE_FIT, CPL_ERROR_ILLEGAL_INPUT, NULL);

	cpl_ensure(obs_s != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(ref_s != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(E_x != NULL, CPL_ERROR_NULL_INPUT, NULL);

	hdrl_data_t best_mean_minus1 = 0, best_stddev = 0;
	hdrl_data_t best_telluric_shift = 0;
	cpl_size best_idx = -1;

	hdrl_spectrum1D * corrected_obs =
			hdrl_response_evaluate_telluric_models(obs_s, telluric_par,
			        &best_telluric_shift, &best_mean_minus1, &best_stddev, &best_idx);

	cpl_ensure(best_idx >= 0, CPL_ERROR_ILLEGAL_OUTPUT, NULL);
	cpl_ensure(corrected_obs != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

	cpl_ensure(cpl_error_get_code() == CPL_ERROR_NONE,
			CPL_ERROR_ILLEGAL_OUTPUT, NULL);

	hdrl_data_t velocity_shift = 0.0;
	if(velocity_par != NULL){
		velocity_shift =
				hdrl_spectrum1D_compute_shift_fit(corrected_obs, velocity_par);
	}

	cpl_ensure(cpl_error_get_code() == CPL_ERROR_NONE,
			CPL_ERROR_ILLEGAL_OUTPUT, NULL);

	hdrl_spectrum1D * ref_shifted =
			correct_spectrum_for_doppler_shift(ref_s,
					velocity_shift);

	cpl_ensure(ref_shifted != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);
	cpl_ensure(cpl_error_get_code() == CPL_ERROR_NONE,
			CPL_ERROR_ILLEGAL_OUTPUT, NULL);

	hdrl_spectrum1D * resp_raw =
			hdrl_response_core_compute(corrected_obs, ref_shifted, E_x, calc_par);

	cpl_ensure(resp_raw != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);
	cpl_ensure(cpl_error_get_code() == CPL_ERROR_NONE,
			CPL_ERROR_ILLEGAL_OUTPUT, NULL);

	const cpl_size radius = hdrl_response_parameter_get_radius(fit_par);
	const cpl_bivector * high_abs_regions =
			hdrl_response_parameter_get_high_abs_regions(fit_par);
	const cpl_array * fit_points =
				hdrl_response_parameter_get_fit_points(fit_par);
	const hdrl_data_t wrange =
			hdrl_response_parameter_get_wrange(fit_par);

	cpl_ensure(cpl_error_get_code() == CPL_ERROR_NONE,
				CPL_ERROR_ILLEGAL_OUTPUT, NULL);

	hdrl_spectrum1D * resp_smoothed =
			filter_spectrum_median(resp_raw, radius);

	cpl_ensure(resp_smoothed != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);
	cpl_ensure(cpl_error_get_code() == CPL_ERROR_NONE, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

	hdrl_spectrum1D * resp_on_fit_points =
			resample_on_medians_skip_abs_regions(resp_smoothed, fit_points,
					high_abs_regions, wrange);

	cpl_ensure(resp_on_fit_points != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);
	cpl_ensure(cpl_error_get_code() == CPL_ERROR_NONE,
			CPL_ERROR_ILLEGAL_OUTPUT, NULL);


	hdrl_parameter * par =
			hdrl_spectrum1D_resample_interpolate_parameter_create(hdrl_spectrum1D_interp_akima);

	hdrl_spectrum1D * resp_final =
			hdrl_spectrum1D_resample_on_array(resp_on_fit_points,
					hdrl_spectrum1D_get_wavelength(resp_smoothed).wavelength,
					par);

	hdrl_parameter_delete(par);
	hdrl_spectrum1D_delete(&resp_smoothed);
	hdrl_spectrum1D_delete(&ref_shifted);


	return hdrl_response_result_wrap(resp_final, resp_on_fit_points,
			resp_raw, corrected_obs, best_idx, best_telluric_shift,
			best_mean_minus1, best_stddev, velocity_shift);
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter for the final response contained inside the hdrl_response_result
 * @param res			 hdrl_response_result
 *
 * @return response or NULL in case of error. The final response is the final
 * product of the algorithm.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: res is NULL
 */
/* ---------------------------------------------------------------------------*/

const hdrl_spectrum1D *
hdrl_response_result_get_final_response(const hdrl_response_result * res){
	cpl_ensure(res != NULL, CPL_ERROR_NULL_INPUT, NULL);
	return res->final_response;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter for the selected response contained inside the hdrl_response_result
 * @param res			 hdrl_response_result
 *
 * @return  selected response or NULL in case of error. The selected response is
 * the raw response sampled in the fit points. This response is going then to be
 * interpolated, creating the final response.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: res is NULL
 */
/* ---------------------------------------------------------------------------*/

const hdrl_spectrum1D *
hdrl_response_result_get_selected_response(const hdrl_response_result * res){
	cpl_ensure(res != NULL, CPL_ERROR_NULL_INPUT, NULL);
	return res->selected_response;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter for the raw response contained inside the hdrl_response_result
 * @param res			 hdrl_response_result
 *
 * @return the raw response or NULL in case of error. The raw response is
 * the ratio between the observed spectrum and the reference one, corrected for
 * e.g. gain, atmospheric extinction, etc.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: res is NULL
 */
/* ---------------------------------------------------------------------------*/
const hdrl_spectrum1D *
hdrl_response_result_get_raw_response(const hdrl_response_result * res){
	cpl_ensure(res != NULL, CPL_ERROR_NULL_INPUT, NULL);
	return res->raw_response;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter for the corrected observed spectrum contained in hdrl_response_result
 * @param res			 hdrl_response_result
 *
 * @return the observed spectrum corrected by the telluric model. If telluric
 * correction was disabled the output of this function is not defined.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: res is NULL
 */
/* ---------------------------------------------------------------------------*/
const hdrl_spectrum1D *
hdrl_response_result_get_corrected_obs_spectrum(const hdrl_response_result * res){
	cpl_ensure(res != NULL, CPL_ERROR_NULL_INPUT, NULL);
	return res->corrected_observed_spectrum;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter of the index of the telluric model used for telluric correction
 * contained in hdrl_response_result
 * @param res			 hdrl_response_result
 *
 * @return the index (0-based) of the telluric model used for telluric correction.
 * If telluric correction was disabled the output of this function is not defined.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: res is NULL
 */
/* ---------------------------------------------------------------------------*/
cpl_size
hdrl_response_result_get_best_telluric_model_idx(const hdrl_response_result * res){
	cpl_ensure(res != NULL, CPL_ERROR_NULL_INPUT, -1);
	return res->best_telluric_model_idx;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter of the value |mean - 1|, where mean is the average of the ratio
 * between the corrected observed spectrum and its smoothed fit.
 * @param res			 hdrl_response_result
 *
 * @return the value |mean - 1|, where mean is the average of the ratio
 * between the corrected observed spectrum and its smoothed fit.
 * This value can be used to assess the quality of the match of the telluric model
 * with the provided observed spectrum.
 * If telluric correction was disabled the output of this function is not defined.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: res is NULL
 */
/* ---------------------------------------------------------------------------*/
hdrl_data_t
hdrl_response_result_get_avg_diff_from_1(const hdrl_response_result * res){
	cpl_ensure(res != NULL, CPL_ERROR_NULL_INPUT, NAN);
	return  res->avg_diff_from_1;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter of the standard deviation of the ratio between the corrected
 * observed spectrum and its smoothed fit.
 * @param res			 hdrl_response_result
 *
 * @return the standard deviation of the ratio between the corrected observed
 * spectrum and its smoothed fit.
 * This value can be used to assess the quality of the match of the telluric model
 * with the provided observed spectrum.
 * If telluric correction was disabled the output of this function is not defined.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: res is NULL
 */
/* ---------------------------------------------------------------------------*/
hdrl_data_t
hdrl_response_result_get_stddev(const hdrl_response_result * res){
	cpl_ensure(res != NULL, CPL_ERROR_NULL_INPUT, NAN);
	return res->stddev;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter of the shift applied to the telluric model.
 * @param res            hdrl_response_result
 *
 * @return shift applied to the selected telluric model.
 * This value can be used to assess the quality of the match of the telluric model
 * with the provided observed spectrum.
 * If telluric correction was disabled the output of this function is not defined.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: res is NULL
 */
/* ---------------------------------------------------------------------------*/
hdrl_data_t
hdrl_response_result_get_telluric_shift(const hdrl_response_result * res){
    cpl_ensure(res != NULL, CPL_ERROR_NULL_INPUT, NAN);
    return res->telluric_shift;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter of the doppler shift used to correct the model.
 * @param res			 hdrl_response_result
 *
 * @return the doppler shift used to correct the model.
 * If doppler corection was disabled the output of this function is not defined.
 *
 * Possible cpl-error-code set in this function:
 * - CPL_ERROR_NULL_INPUT: res is NULL
 */
/* ---------------------------------------------------------------------------*/
hdrl_data_t
hdrl_response_result_get_doppler_shift(const hdrl_response_result * res){
	cpl_ensure(res != NULL, CPL_ERROR_NULL_INPUT, NAN);
	return res->doppler_shift;
}


/* ---------------------------------------------------------------------------*/
/**
 * @brief Destructor for hdrl_response_result
 * @param res			 hdrl_response_result to be destroyed
 */
/* ---------------------------------------------------------------------------*/
void
hdrl_response_result_delete(hdrl_response_result * res){

	if(!res) return;

	hdrl_spectrum1D_delete(&(res->final_response));
	hdrl_spectrum1D_delete(&(res->selected_response));
	hdrl_spectrum1D_delete(&(res->raw_response));

	hdrl_spectrum1D_delete(&(res->corrected_observed_spectrum));

	cpl_free(res);
}

/* This function evaluates all the telluric models inside the hdrl_parameter ev,
 * picks the best model, returns its index (best_model_index), some quality parameter
 * (mean_minus_1 and stddev) and obs_s corrected with the best model.
 * If ev == NULL the function returns a copy of obs_s.
 * ev is created using hdrl_response_telluric_evaluation_parameter_create. */
hdrl_spectrum1D *
hdrl_response_evaluate_telluric_models(
                                     const hdrl_spectrum1D * obs_s,
                                     const hdrl_parameter * ev,
                                     hdrl_data_t * telluric_shift,
                                     hdrl_data_t * mean_minus_1, hdrl_data_t * stddev,
                                     cpl_size * best_model_index){

	cpl_ensure(mean_minus_1 != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(stddev != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(best_model_index != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(obs_s != NULL, CPL_ERROR_NULL_INPUT, NULL);

	*mean_minus_1 = (hdrl_data_t)0.0;
    *stddev = (hdrl_data_t)0.0;
    *best_model_index = -1;

	if(ev == NULL){
		*best_model_index  = 0;
		*mean_minus_1 = NAN;
		*stddev = NAN;
		*telluric_shift = NAN;
		return hdrl_spectrum1D_duplicate(obs_s);
	}

	cpl_ensure(hdrl_parameter_get_parameter_enum(ev)
			== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
			CPL_ERROR_ILLEGAL_INPUT, NULL);

	 const hdrl_spectrum1Dlist * telluric_models =
			 hdrl_response_telluric_evaluation_parameter_get_telluric_models(ev);
	 const hdrl_data_t w_step =
			 hdrl_response_telluric_evaluation_parameter_get_w_step(ev);
	 const cpl_size half_win =
			 hdrl_response_telluric_evaluation_parameter_get_half_win(ev);
	 const cpl_boolean normalize =
			 hdrl_response_telluric_evaluation_parameter_get_normalize(ev);
	 const cpl_boolean shift_in_log_scale =
			 hdrl_response_telluric_evaluation_parameter_get_shift_in_log_scale(ev);
	 const cpl_bivector * quality_areas =
			 hdrl_response_telluric_evaluation_parameter_get_quality_areas(ev);
	 const cpl_bivector * fit_areas =
			 hdrl_response_telluric_evaluation_parameter_get_fit_areas(ev);
	 const hdrl_data_t lmin =
			 hdrl_response_telluric_evaluation_parameter_get_lmin(ev);
	 const hdrl_data_t lmax =
			 hdrl_response_telluric_evaluation_parameter_get_lmax(ev);

    hdrl_spectrum1D * to_ret = NULL;

    const cpl_size sz = hdrl_spectrum1Dlist_get_size(telluric_models);
    cpl_ensure(sz > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);

    cpl_array * calc_std_devs = cpl_array_new(sz, CPL_TYPE_DOUBLE);
    cpl_array * calc_means_minus_1 = cpl_array_new(sz, CPL_TYPE_DOUBLE);
    cpl_array * calc_telluric_shift = cpl_array_new(sz, CPL_TYPE_DOUBLE);

    cpl_array_fill_window(calc_std_devs, 0, sz, 0);
    cpl_array_fill_window(calc_means_minus_1, 0, sz, 0);
    cpl_array_fill_window(calc_telluric_shift, 0, sz, 0);

    double * p_stddevs = cpl_array_get_data_double(calc_std_devs);
    double * p_means = cpl_array_get_data_double(calc_means_minus_1);
    double * p_telluric_shift = cpl_array_get_data_double(calc_telluric_shift);

    hdrl_spectrum1D ** l = cpl_calloc(sz, sizeof(hdrl_spectrum1D*));
    cpl_error_code * codes = cpl_calloc(sz, sizeof(cpl_error_code));

    HDRL_OMP(omp parallel for)
    for(cpl_size i = 0; i < sz; ++i){
        const hdrl_spectrum1D * this_model =
                hdrl_spectrum1Dlist_get_const(telluric_models, i);

        l[i] = hdrl_response_evaluate_telluric_model(obs_s, this_model, w_step,
                half_win, normalize, shift_in_log_scale, quality_areas, fit_areas,
                lmin, lmax, p_means + i, p_stddevs + i,p_telluric_shift + i);
        codes[i] = cpl_error_get_code();

        if(l[i] == NULL && codes[i] == CPL_ERROR_NONE)
            codes[i] = CPL_ERROR_ILLEGAL_OUTPUT;
    }

    cpl_error_code err = get_first_error_code(codes, sz);

    if(!err){
    	cpl_size best_idx = 0;
        err = cpl_array_get_minpos(calc_means_minus_1, &best_idx);
        if(!err){
        	*stddev = cpl_array_get(calc_std_devs, best_idx, NULL);
        	*mean_minus_1 = cpl_array_get(calc_means_minus_1, best_idx, NULL);
        	*telluric_shift = cpl_array_get(calc_telluric_shift, best_idx, NULL);
        	to_ret = l[best_idx];
        	l[best_idx] = NULL;
        	*best_model_index = best_idx;
        }
    }

    cpl_array_delete(calc_std_devs);
    cpl_array_delete(calc_means_minus_1);
    cpl_array_delete(calc_telluric_shift);
    cpl_free(codes);
    free_spectrum_array(l, sz);

    cpl_ensure(err == CPL_ERROR_NONE, err, NULL);

    return to_ret;
}



/*This function evaluates how well a telluric model corrects an observed spectrum.
 * For the parameters see hdrl_response_telluric_evaluation_parameter_create.*/
hdrl_spectrum1D *
hdrl_response_evaluate_telluric_model(const hdrl_spectrum1D * obs_s_arg,
                                        const hdrl_spectrum1D * telluric_s_arg,
                                        const hdrl_data_t w_step,
                                        const cpl_size half_win,
                                        const cpl_boolean normalize,
                                        const cpl_boolean shift_in_log_scale,
                                        const cpl_bivector * quality_areas,
                                        const cpl_bivector * fit_areas,
                                        const hdrl_data_t lmin, const hdrl_data_t lmax,
                                        double * mean_minus_1, double * stddev,
                                        double * telluric_shift){

    cpl_ensure(obs_s_arg != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(telluric_s_arg != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(quality_areas != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(fit_areas != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(mean_minus_1 != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(stddev != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(w_step > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
    cpl_ensure(half_win > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);

    *mean_minus_1 = 0.0;
    *stddev  = 0.0;
    *telluric_shift = 0.0;

    hdrl_spectrum1D * corrected_spectrum =
            compute_corrected_obs_spectrum(obs_s_arg,
                    telluric_s_arg, w_step, half_win, normalize,
                    shift_in_log_scale, lmin, lmax, telluric_shift);

    cpl_ensure(corrected_spectrum != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

    hdrl_spectrum1D * corr_spectrum_extracted =
    		hdrl_spectrum1D_extract_fit_regions(corrected_spectrum, fit_areas);

    if(corr_spectrum_extracted == NULL) {
        hdrl_spectrum1D_delete(&corrected_spectrum);
        cpl_ensure(CPL_FALSE, CPL_ERROR_ILLEGAL_OUTPUT, NULL);
    }

    hdrl_spectrum1D * smoothed_fit =
            compute_interpolated_spectrum(corrected_spectrum,
            		corr_spectrum_extracted, hdrl_spectrum1D_interp_akima);

    hdrl_spectrum1D * quality_ratio =
            hdrl_spectrum1D_div_spectrum_create(corrected_spectrum, smoothed_fit);

    compute_quality(quality_ratio, quality_areas, mean_minus_1, stddev);

    hdrl_spectrum1D_delete(&corr_spectrum_extracted);
    hdrl_spectrum1D_delete(&smoothed_fit);
    hdrl_spectrum1D_delete(&quality_ratio);

    return corrected_spectrum;
}

/*-----------------------------------------------------------------------------
                          Private Functions
 -----------------------------------------------------------------------------*/

/* Given a spectrum the function calculates:
 * mean_abs_difference_from_1 = |mean - 1| and standard deviation.
 * The flux defined on wavelengths outside the quality_areas are ignored.*/
static inline void
compute_quality(const hdrl_spectrum1D * s,
        const cpl_bivector * quality_areas, double * mean_abs_difference_from_1,
		double * stddev){

    hdrl_spectrum1D * s_new =
            hdrl_spectrum1D_select_wavelengths(s, quality_areas, CPL_TRUE);

    const hdrl_image * flux = hdrl_spectrum1D_get_flux(s_new);

    *mean_abs_difference_from_1 = fabs(hdrl_image_get_mean(flux).data - 1.0);
    *stddev = hdrl_image_get_stdev(flux);

    hdrl_spectrum1D_delete(&s_new);
}

/* Interpolates using a cspline the points in sampled_points to obtain a spectrum
 * defined on the wavelengths of wlength_source*/
static inline hdrl_spectrum1D *
compute_interpolated_spectrum(const hdrl_spectrum1D * wlength_source,
        const hdrl_spectrum1D * sampled_points,
		const hdrl_spectrum1D_interpolation_method method){

    hdrl_parameter * par =
            hdrl_spectrum1D_resample_interpolate_parameter_create(method);

    hdrl_spectrum1D_wavelength waves = hdrl_spectrum1D_get_wavelength(wlength_source);

    hdrl_spectrum1D * continuum_fit =
            hdrl_spectrum1D_resample(sampled_points, &waves, par);

    hdrl_parameter_delete(par);
    return continuum_fit;
}

/* This function correct the observed spectrum by the telluric spectrum. In order
 * to do so, the function aligns the two spectra, convolves the telluric model by
 * a gaussian kernel. The observed spectrum is then divided by the shifted and
 * convolved telluric model.
 * NOTE: the output spectrum is defined on the wavelengths of obs_s_arg.*/
static inline hdrl_spectrum1D *
compute_corrected_obs_spectrum(
                              const hdrl_spectrum1D * obs_s_arg,
                              const hdrl_spectrum1D * telluric_s_arg,
                              const hdrl_data_t w_step, const cpl_size half_win,
                              const cpl_boolean normalize,
                              const cpl_boolean shift_in_log_scale,
                              const hdrl_data_t lmin, const hdrl_data_t lmax,
                              double * telluric_shift){

    cpl_ensure(obs_s_arg != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(telluric_s_arg != NULL, CPL_ERROR_NULL_INPUT, NULL);

    hdrl_spectrum1D_wavelength obs_wavs = hdrl_spectrum1D_get_wavelength(obs_s_arg);

    hdrl_spectrum1D * obs_s = hdrl_spectrum1D_duplicate(obs_s_arg);
    hdrl_spectrum1D * telluric_s_cp = hdrl_spectrum1D_duplicate(telluric_s_arg);

    if(shift_in_log_scale){
        hdrl_spectrum1D_wavelength_convert_to_log(obs_s);
        hdrl_spectrum1D_wavelength_convert_to_log(telluric_s_cp);
    }

    hdrl_spectrum1D * telluric_s_shifted_convolved=
            shift_and_convolve_telluric_model(obs_s, telluric_s_cp, w_step,
                    half_win, normalize, lmin, lmax, telluric_shift);


    if(telluric_s_shifted_convolved != NULL){
        hdrl_spectrum1D_wavelength_convert_to_linear(telluric_s_shifted_convolved);
    }

    hdrl_parameter * pars = hdrl_spectrum1D_resample_integrate_parameter_create();
    hdrl_spectrum1D * telluric_s_shifted_convolved_downsampled =
                hdrl_spectrum1D_resample(telluric_s_shifted_convolved, &obs_wavs, pars);
    hdrl_spectrum1D * corrected =
            hdrl_spectrum1D_div_spectrum_create(obs_s_arg , telluric_s_shifted_convolved_downsampled);

    hdrl_spectrum1D_delete(&obs_s);
    hdrl_spectrum1D_delete(&telluric_s_cp);
    hdrl_spectrum1D_delete(&telluric_s_shifted_convolved);
    hdrl_spectrum1D_delete(&telluric_s_shifted_convolved_downsampled);
    hdrl_parameter_delete(pars);

    return corrected;
}

/*Trims s so that its minimum and maximum wavelenghts do not exceed wlens_source*/
static inline hdrl_spectrum1D *
select_obs_wlen( const hdrl_spectrum1D * s,
		const hdrl_spectrum1D * wlens_source){

	const cpl_array * wlens = hdrl_spectrum1D_get_wavelength(wlens_source).wavelength;

	const hdrl_data_t wmin = cpl_array_get_min(wlens);
	const hdrl_data_t wmax = cpl_array_get_max(wlens);

	return select_win(s, wmin, wmax);
}

/*The telluric model is correlated with the observed spectrum to compute the
 * relative shift between the two. Then the telluric model is shifted to match the
 * observed spectrum. Then the shifted model is convolved with a gaussian kernel. */
static inline hdrl_spectrum1D *
shift_and_convolve_telluric_model(const hdrl_spectrum1D * obs,
								  const hdrl_spectrum1D * telluric,
								  const hdrl_data_t w_step,
								  const cpl_size half_win,
								  const cpl_boolean normalize,
								  const hdrl_data_t lmin,
 								  const hdrl_data_t lmax,
 								  double * telluric_shift){

    hdrl_spectrum1D * telluric_s = select_win(telluric, lmin, lmax);

	cpl_ensure(telluric_s != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

    hdrl_xcorrelation_result * res =
            correlate_obs_with_telluric(obs, telluric_s,
                    w_step, half_win,  normalize, lmin, lmax);
    hdrl_spectrum1D_delete(&telluric_s);

    cpl_ensure(res != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

    hdrl_data_t shift = (hdrl_data_t)
            (hdrl_xcorrelation_result_get_peak_subpixel(res) -
             hdrl_xcorrelation_result_get_half_window(res) * w_step);

    *telluric_shift = shift;

    hdrl_spectrum1D * telluric_selected_obs =
    		select_obs_wlen(telluric, obs);

    hdrl_spectrum1D * telluric_s_shifted =
            hdrl_spectrum1D_wavelength_shift_create(telluric_selected_obs, shift);
    const double sigma = hdrl_xcorrelation_result_get_sigma(res);

    hdrl_xcorrelation_result_delete(res);

    cpl_ensure(telluric_s_shifted != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

    hdrl_spectrum1D * telluric_s_shifted_convolved =
            convolve_with_kernel_symmetrically(telluric_s_shifted, sigma, w_step);

    hdrl_spectrum1D_delete(&telluric_s_shifted);
    hdrl_spectrum1D_delete(&telluric_selected_obs);

    return telluric_s_shifted_convolved;
}

/*The function: 1. resamples uniformly obs_s and telluric_s, so they're defined
 * on the same wavelengths. 2. correlates the two spectra to find out the relative
 * shift of one wrt the other.
 *
 * w_step, lmin and lmax are used for the resampling, all the other inputs
 * are used for the cross correlation.*/
static inline hdrl_xcorrelation_result *
correlate_obs_with_telluric(const hdrl_spectrum1D * obs_s,
                                            const hdrl_spectrum1D * telluric_s,
                                            const hdrl_data_t w_step,
                                            const cpl_size half_win,
                                            const cpl_boolean normalize,
                                            const hdrl_data_t lmin,
                                            const hdrl_data_t lmax){

    cpl_ensure(obs_s != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(telluric_s != NULL, CPL_ERROR_NULL_INPUT, NULL);

    hdrl_data_t wmin = cpl_array_get_min(hdrl_spectrum1D_get_wavelength(obs_s).wavelength);
    hdrl_data_t wmax = cpl_array_get_max(hdrl_spectrum1D_get_wavelength(obs_s).wavelength);

    hdrl_spectrum1D * tell_for_sel = select_win(telluric_s, wmin, wmax);

    hdrl_spectrum1D * telluric_s_res = NULL;
    hdrl_spectrum1D * obs_s_res = NULL;
    {
        cpl_array * new_lambdas = get_uniform_wavs(tell_for_sel, w_step, lmin, lmax);

        /*
         * We need to make sure that telluric_s and obs_s are
         * sampled uniformly.
        */
        hdrl_parameter * par =
                hdrl_spectrum1D_resample_interpolate_parameter_create
                (hdrl_spectrum1D_interp_akima);

        telluric_s_res =
                hdrl_spectrum1D_resample_on_array(telluric_s, new_lambdas, par);

        obs_s_res =
                hdrl_spectrum1D_resample_on_array(obs_s, new_lambdas, par);

        hdrl_parameter_delete(par);
        cpl_array_delete(new_lambdas);
    }

    cpl_ensure(obs_s_res != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);
    cpl_ensure(telluric_s_res != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

    hdrl_xcorrelation_result * gshift =
            hdrl_spectrum1D_compute_shift_xcorrelation(telluric_s_res, obs_s_res,
                    half_win, normalize);

    hdrl_spectrum1D_delete(&telluric_s_res);
    hdrl_spectrum1D_delete(&obs_s_res);
    hdrl_spectrum1D_delete(&tell_for_sel);
    return gshift;
}


/*This function returns a uniformly sampled sequence of wavelengths. The distance
 * between 2 elements is w_step, the starting point is max(lmin, min_wavelengths_s)
 * the end point is min(lmax, max_wavelegths_s)*/
static inline  cpl_array *
get_uniform_wavs(const hdrl_spectrum1D * s, const hdrl_data_t w_step,
        const hdrl_data_t lmin, const hdrl_data_t lmax){

    const hdrl_data_t w_min = CPL_MAX(lmin,
            cpl_array_get_min(hdrl_spectrum1D_get_wavelength(s).wavelength));
    const hdrl_data_t w_max = CPL_MIN(lmax,
            cpl_array_get_max(hdrl_spectrum1D_get_wavelength(s).wavelength));

    const cpl_size sz_new_spectrum = (w_max - w_min)/w_step;
    cpl_array * new_w_lengths = cpl_array_new(sz_new_spectrum, HDRL_TYPE_DATA);

    for(cpl_size i = 0; i < sz_new_spectrum; ++i){
        cpl_array_set(new_w_lengths, i, i * w_step + w_min);
    }

   return new_w_lengths;
}

/*This function convolves a kernel with the flux of a spectrum. The output
 * spectrum is without error and it is defined on the same wavelengths of the input
 * spectrum. The convolution one the borders is done using a reduced number of samples.*/
static inline hdrl_spectrum1D *
convolve_spectrum_with_kernel(const hdrl_spectrum1D * s,
                         const cpl_matrix * kernel){

    const cpl_size sz = hdrl_spectrum1D_get_size(s);
    const hdrl_image * h_img = hdrl_spectrum1D_get_flux(s);
    const cpl_image * img = hdrl_image_get_image_const(h_img);
    cpl_image * dest = cpl_image_new(sz, 1, HDRL_TYPE_DATA);

    const cpl_error_code cd =
            cpl_image_filter(dest, img, kernel, CPL_FILTER_LINEAR, CPL_BORDER_FILTER);


    hdrl_spectrum1D * to_ret = NULL;
    if(cd == CPL_ERROR_NONE){
        hdrl_spectrum1D_wavelength s_wav = hdrl_spectrum1D_get_wavelength(s);

        to_ret =
                hdrl_spectrum1D_create_error_free(dest, s_wav.wavelength, s_wav.scale);
    }

    cpl_image_delete(dest);

    cpl_ensure(cd == CPL_ERROR_NONE, cd, NULL);

    return to_ret;
}

static inline cpl_size get_lower_odd(const cpl_size sz){

	if(sz == 0) return 0;

	if(sz % 2 == 1) return sz;
	return sz - 1;
}

/*This function convolves the spectrum s with a symmetrical gaussian kernel having
 * std deviation sigma. The wavelength step of the kernel is w_step.*/
static inline hdrl_spectrum1D *
convolve_with_kernel_symmetrically(const hdrl_spectrum1D * s,
                                   const double sigma,
                                   const hdrl_data_t w_step){

	const double fwhm = CPL_MATH_FWHM_SIG * sigma;
    int fwhm_pix = (int) (fwhm / w_step + 0.5);

    cpl_matrix * kernel =
            create_symmetrical_gaussian_kernel(fwhm_pix / CPL_MATH_FWHM_SIG,
                                        fwhm_pix / CPL_MATH_FWHM_SIG,
										get_lower_odd(hdrl_spectrum1D_get_size(s)));

    hdrl_spectrum1D * convolved =
    		convolve_spectrum_with_kernel(s, kernel);
    cpl_matrix_delete(kernel);

    cpl_ensure(convolved != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

    return convolved;
}

/* Creates a gaussian symmetrical kernel for a given slit width (slitw) and a given
 * fwhm. The function ALWAYS returns a kernel with a odd number of elements.*/
static inline cpl_matrix *
create_symmetrical_gaussian_kernel(const double  slitw, const double  fwhm,
		const cpl_size max_sz){

    cpl_ensure(slitw > 0.0,  CPL_ERROR_ILLEGAL_INPUT, NULL);
    cpl_ensure(fwhm  > 0.0,  CPL_ERROR_ILLEGAL_INPUT, NULL);

    const double   sigma  = fwhm * CPL_MATH_SIG_FWHM;
    cpl_size size   = 1 + (cpl_size)(5.0 * sigma + 0.5*slitw);

    size *= 2;
    /* filter need an odd number of elements */
    size ++;

    size = CPL_MIN(size, max_sz);

    cpl_matrix * kernel = cpl_matrix_new(1, size);

    /* Special case for i = 0 */
    cpl_matrix_set(kernel, 0, size/2,
                         (erf_antideriv(0.5*slitw + 0.5, sigma) -
                          erf_antideriv(0.5*slitw - 0.5, sigma)) / slitw);

    for (cpl_size i = 1; i < size / 2; i++) {

        const double x1p = (double)i + 0.5 * slitw + 0.5;
        const double x1n = (double)i - 0.5 * slitw + 0.5;
        const double x0p = (double)i + 0.5 * slitw - 0.5;
        const double x0n = (double)i - 0.5 * slitw - 0.5;
        const double val = 0.5 / slitw *
            (erf_antideriv(x1p, sigma) - erf_antideriv(x1n, sigma) -
             erf_antideriv(x0p, sigma) + erf_antideriv(x0n, sigma));

        cpl_matrix_set(kernel, 0, size/2 + i, val);
        cpl_matrix_set(kernel, 0, size/2 - i, val);
    }

    return kernel;
}

/* The antiderivative of erx(x/sigma/sqrt(2)) with respect to x */
static inline double
erf_antideriv(const double x, const double sigma)
{
    return x * erf( x / (sigma * CPL_MATH_SQRT2))
       + 2.0 * sigma/CPL_MATH_SQRT2PI * exp(-0.5 * x * x / (sigma * sigma));
}

/* Given an array of spectra the function takes care of freeing each spectra
 * and the buffer containing the pointers.*/
static inline void
free_spectrum_array(hdrl_spectrum1D ** s, const cpl_size sz){
    hdrl_spectrum1Dlist * l = hdrl_spectrum1Dlist_wrap(s, sz);
    hdrl_spectrum1Dlist_delete(l);
}

/*Get the first element of the array that is not CPL_ERROR_NONE. If all the
 * elements are CPL_ERROR_NONE the function returns CPL_ERROR_NONE.*/
static inline cpl_error_code
get_first_error_code(const cpl_error_code * codes, const cpl_size sz){
    for(cpl_size i = 0; i < sz; ++i){
        if(codes[i]) return codes[i];
    }
    return CPL_ERROR_NONE;
}

/*Selects all the wavelengths between wmin and wmax*/
static inline hdrl_spectrum1D *
select_win(const hdrl_spectrum1D * s, const hdrl_data_t wmin,
        const hdrl_data_t wmax){

    cpl_bivector * bv = cpl_bivector_new(1);

    cpl_vector_set(cpl_bivector_get_x(bv), 0, wmin);
    cpl_vector_set(cpl_bivector_get_y(bv), 0, wmax);

    hdrl_spectrum1D * to_ret = hdrl_spectrum1D_select_wavelengths(s, bv, CPL_TRUE);

    cpl_bivector_delete(bv);

    return to_ret;
}
/*Corrects the spectrum s by the doppler offset offset.*/
static inline hdrl_spectrum1D *
correct_spectrum_for_doppler_shift(const hdrl_spectrum1D * s,
					const hdrl_data_t offset){

	if(offset == 0.0)
		return hdrl_spectrum1D_duplicate(s);

	const hdrl_image * flux = hdrl_spectrum1D_get_flux(s);
	cpl_array * wavs =
			cpl_array_duplicate(hdrl_spectrum1D_get_wavelength(s).wavelength);

	for(cpl_size i = 0; i < cpl_array_get_size(wavs); ++i){
		const double d = cpl_array_get(wavs, i, NULL) * (1. + offset);
		cpl_array_set(wavs, i, d);
	}

	hdrl_spectrum1D * to_ret = hdrl_spectrum1D_create(
		hdrl_image_get_image_const(flux),
		hdrl_image_get_error_const(flux),
		wavs,
		hdrl_spectrum1D_get_scale(s));
	cpl_array_delete(wavs);
	return to_ret;
}
/*Median filters the flux, with error propagation*/
static inline hdrl_spectrum1D *
filter_spectrum_median(const hdrl_spectrum1D * resp, const cpl_size radius){
	const hdrl_image  * flx_total = hdrl_spectrum1D_get_flux(resp);
	hdrl_image * flx_smoothed = compute_median_on_hdrl_image(flx_total, radius);

	hdrl_spectrum1D * to_ret = hdrl_spectrum1D_create(
			hdrl_image_get_image(flx_smoothed),
			hdrl_image_get_error(flx_smoothed),
			hdrl_spectrum1D_get_wavelength(resp).wavelength,
			hdrl_spectrum1D_get_scale(resp));

	hdrl_image_delete(flx_smoothed);

	return to_ret;
}

/*Median filters an HDRL image, with error propagation*/
static inline hdrl_image *
compute_median_on_hdrl_image(const hdrl_image * img, const cpl_size radius){

	hdrl_image * to_ret = hdrl_image_duplicate(img);
	cpl_size sz = hdrl_image_get_size_x(img);
	for(cpl_size i = 1; i <= sz; ++i){
		cpl_size start = CPL_MAX(i - radius, 1);
		cpl_size stop = CPL_MIN(i + radius, sz);
		hdrl_image * ex_img = hdrl_image_extract(img, start, 1, stop, 1);
		hdrl_value m = hdrl_image_get_median(ex_img);
		hdrl_image_delete(ex_img);
		hdrl_image_set_pixel(to_ret, i, 1, m);
	}
	return to_ret;
}

/*Remove rejected values or values being NAN or INF*/
static inline hdrl_spectrum1D *
remove_bad_data(const hdrl_spectrum1D * s){

	const cpl_size sz = hdrl_spectrum1D_get_size(s);
	double * flx = cpl_calloc(sz, sizeof(double));
	double * flx_e = cpl_calloc(sz, sizeof(double));
	double * wlen = cpl_calloc(sz, sizeof(double));

	cpl_size true_size = 0;
	for(cpl_size i = 0; i < sz; ++i){
		int rej = 0;
		hdrl_value v = hdrl_spectrum1D_get_flux_value(s, i, &rej);
		if(rej || isnan(v.data) || isinf(v.data)) continue;

		flx[true_size] = v.data;
		flx_e[true_size] = v.error;
		wlen[true_size] = hdrl_spectrum1D_get_wavelength_value(s, i, &rej);
		true_size++;
	}

	if(true_size == 0){
		cpl_free(flx);
		cpl_free(flx_e);
		cpl_free(wlen);
		return NULL;
	}

	hdrl_spectrum1D_wave_scale scale = hdrl_spectrum1D_get_scale(s);
	cpl_image * img_flx = cpl_image_wrap_double(true_size, 1, flx);
	cpl_image * img_flx_e = cpl_image_wrap_double(true_size, 1, flx_e);
	cpl_array * arr_wlens= cpl_array_wrap_double(wlen, true_size);
	hdrl_spectrum1D * good_spectrum = hdrl_spectrum1D_create(img_flx, img_flx_e,
			arr_wlens, scale);

	cpl_image_delete(img_flx);
	cpl_image_delete(img_flx_e);
	cpl_array_delete(arr_wlens);
	return good_spectrum;
}

/* For each point p in fit_points generates a new spectrum whose wavelengths are
 * fit_points and whose flux are the medians taken in the range
 * [p - wrange, p + wrange]. */
static inline hdrl_spectrum1D *
get_median_on_fit_points(const hdrl_spectrum1D * s_input,
		const cpl_array * fit_points, const hdrl_data_t wrange){

	cpl_size final_fit_points_size = cpl_array_get_size(fit_points);

	cpl_array * wlens_fit = cpl_array_new(final_fit_points_size,
			HDRL_TYPE_DATA);
	hdrl_image * flux_fit = hdrl_image_new(final_fit_points_size, 1);

	for(cpl_size i = 0; i < final_fit_points_size; ++i){
		const double w_fit = cpl_array_get(fit_points, i, NULL);
		cpl_array_set(wlens_fit, i, w_fit);
		hdrl_spectrum1D * f_s =
				select_win(s_input, w_fit - wrange, w_fit + wrange);

		if(f_s == NULL){
			cpl_error_reset();
			hdrl_image_reject(flux_fit, i + 1, 1);
			continue;
		}

		const hdrl_value v = hdrl_image_get_median(hdrl_spectrum1D_get_flux(f_s));
		hdrl_image_set_pixel(flux_fit, i + 1, 1, v);
		hdrl_spectrum1D_delete(&f_s);
	}

	const hdrl_spectrum1D_wave_scale scale = hdrl_spectrum1D_get_scale(s_input);

	hdrl_spectrum1D * to_ret = hdrl_spectrum1D_create(
			hdrl_image_get_image(flux_fit),
			hdrl_image_get_error(flux_fit),
			wlens_fit,
			scale
			);
	cpl_array_delete(wlens_fit);
	hdrl_image_delete(flux_fit);
	return to_ret;
}
/*removes high abs regions and values that are NAN or INF or rejected*/
static inline hdrl_spectrum1D *
select_regions_and_good_value(const hdrl_spectrum1D * s, const cpl_bivector * areas){

	hdrl_spectrum1D * filted = NULL;
	if(areas != NULL)
			filted = hdrl_spectrum1D_select_wavelengths(s, areas, CPL_FALSE);
	else
			filted = hdrl_spectrum1D_duplicate(s);

	hdrl_spectrum1D * to_ret = remove_bad_data(filted);
	hdrl_spectrum1D_delete(&filted);
	return to_ret;
}

/*This function:
 * 1. Removes all the wavelengths contained inside high_abs_regions, from both s
 * 	  and fit_points
 * 2. For each surviving point in fit_points take the median flux on the filtered s
*/
static inline hdrl_spectrum1D *
resample_on_medians_skip_abs_regions(const hdrl_spectrum1D * s,
		const cpl_array * fit_points, const cpl_bivector * high_abs_regions,
		const hdrl_data_t wrange){

	cpl_ensure(s != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(fit_points != NULL, CPL_ERROR_NULL_INPUT, NULL);

	hdrl_spectrum1D * filter_s =
			select_regions_and_good_value(s, high_abs_regions);

	cpl_ensure(filter_s != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

	const hdrl_data_t wmin=
				cpl_array_get_min(hdrl_spectrum1D_get_wavelength(filter_s).wavelength);
	const hdrl_data_t wmax=
				cpl_array_get_max(hdrl_spectrum1D_get_wavelength(filter_s).wavelength);

	cpl_array * filter_fit_points =
			remove_regions_and_outliers_from_array(fit_points, high_abs_regions, wmin, wmax);

	if(filter_fit_points == NULL || cpl_array_get_size(filter_fit_points) == 0){
		hdrl_spectrum1D_delete(&filter_s);
		cpl_array_delete(filter_fit_points);
		cpl_ensure(CPL_FALSE, CPL_ERROR_ILLEGAL_OUTPUT, NULL);
	}

	hdrl_spectrum1D * selected_s_median_points =
			get_median_on_fit_points(filter_s, filter_fit_points, wrange);

	cpl_array_delete(filter_fit_points);
	hdrl_spectrum1D_delete(&filter_s);

	return selected_s_median_points;
}

/*Checks if w is contained in any of the windows in high_abs_regions.*/
static inline cpl_boolean
contained_in_any_region(const hdrl_data_t w, const cpl_bivector * high_abs_regions){

	if(!high_abs_regions) return CPL_FALSE;

	const cpl_size sz = cpl_bivector_get_size(high_abs_regions);

	for(cpl_size i = 0; i < sz; ++i){
		const double wmin =
				cpl_vector_get(cpl_bivector_get_x_const(high_abs_regions), i);
		const double wmax =
				cpl_vector_get(cpl_bivector_get_y_const(high_abs_regions), i);
		if(w >= wmin && w <= wmax) return CPL_TRUE;
	}
	return CPL_FALSE;
}

/*Removes each element in the array that is outside the range [wmin, wmax] or that
 * is contained inside the high_abs_regions*/
static inline cpl_array *
remove_regions_and_outliers_from_array(const cpl_array * fit_points,
		const cpl_bivector * high_abs_regions,
		const hdrl_data_t wmin, const hdrl_data_t wmax){

	const cpl_size sz = cpl_array_get_size(fit_points);
	double * filter = cpl_calloc(sz, sizeof(*filter));
	cpl_size k = 0;
	for(cpl_size i = 0; i < sz; ++i){
		const double w = cpl_array_get(fit_points, i, NULL);
		if(w > wmax) continue;
		if(w < wmin) continue;
		if(contained_in_any_region(w, high_abs_regions)) continue;

		filter[k] = w;
		k++;
	}

	if(k == 0){
		cpl_free(filter);
		return NULL;
	}

	return cpl_array_wrap_double(filter, k);
}

static inline const hdrl_spectrum1Dlist *
hdrl_response_telluric_evaluation_parameter_get_telluric_models(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, NULL);

	const hdrl_response_telluric_evaluation_parameter * p =
			(const hdrl_response_telluric_evaluation_parameter *)par;
	return p->telluric_models;
}

static inline hdrl_data_t
hdrl_response_telluric_evaluation_parameter_get_w_step(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, 0.0);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, 0.0);

	const hdrl_response_telluric_evaluation_parameter * p =
			(const hdrl_response_telluric_evaluation_parameter *)par;
	return p->w_step;
}

static inline cpl_size
hdrl_response_telluric_evaluation_parameter_get_half_win(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, 0);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, 0);

	const hdrl_response_telluric_evaluation_parameter * p =
			(const hdrl_response_telluric_evaluation_parameter *)par;
	return p->half_win;
}

static inline cpl_boolean
hdrl_response_telluric_evaluation_parameter_get_normalize(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);

	const hdrl_response_telluric_evaluation_parameter * p =
			(const hdrl_response_telluric_evaluation_parameter *)par;
	return p->normalize;
}

static inline cpl_boolean
hdrl_response_telluric_evaluation_parameter_get_shift_in_log_scale(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);

	const hdrl_response_telluric_evaluation_parameter * p =
			(const hdrl_response_telluric_evaluation_parameter *)par;
	return p->shift_in_log_scale;
}

static inline const cpl_bivector *
hdrl_response_telluric_evaluation_parameter_get_quality_areas(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, NULL);

	const hdrl_response_telluric_evaluation_parameter * p =
			(const hdrl_response_telluric_evaluation_parameter *)par;
	return p->quality_areas;
}

static inline const cpl_bivector *
hdrl_response_telluric_evaluation_parameter_get_fit_areas(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, NULL);

	const hdrl_response_telluric_evaluation_parameter * p =
			(const hdrl_response_telluric_evaluation_parameter *)par;
	return p->fit_areas;
}

static inline hdrl_data_t
hdrl_response_telluric_evaluation_parameter_get_lmin(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, 0.0);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, 0.0);

	const hdrl_response_telluric_evaluation_parameter * p =
			(const hdrl_response_telluric_evaluation_parameter *)par;
	return p->lmin;
}

static inline hdrl_data_t
hdrl_response_telluric_evaluation_parameter_get_lmax(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, 0.0);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_TELLURIC_EVALUATION,
				CPL_ERROR_ILLEGAL_INPUT, 0.0);

	const hdrl_response_telluric_evaluation_parameter * p =
			(const hdrl_response_telluric_evaluation_parameter *)par;
	return p->lmax;
}

static inline const cpl_array *
hdrl_response_parameter_get_fit_points(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_FIT,
				CPL_ERROR_ILLEGAL_INPUT, NULL);

	const response_fit_parameter * p =
			(const response_fit_parameter *)par;
	return p->fit_points;
}

static inline const cpl_bivector *
hdrl_response_parameter_get_high_abs_regions(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, NULL);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_FIT,
				CPL_ERROR_ILLEGAL_INPUT, NULL);

	const response_fit_parameter * p =
			(const response_fit_parameter *)par;
	return p->high_abs_regions;
}

static inline cpl_size
hdrl_response_parameter_get_radius(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, 0);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_FIT,
				CPL_ERROR_ILLEGAL_INPUT, 0);

	const response_fit_parameter * p =
			(const response_fit_parameter *)par;
	return p->radius;
}

static inline hdrl_data_t
hdrl_response_parameter_get_wrange(
		const hdrl_parameter * par){

	cpl_ensure(par != NULL, CPL_ERROR_NULL_INPUT, 0.0);
	cpl_ensure(hdrl_parameter_get_parameter_enum(par)
				== HDRL_PARAMETER_RESPONSE_FIT,
				CPL_ERROR_ILLEGAL_INPUT, 0.0);

	const response_fit_parameter * p =
			(const response_fit_parameter *)par;
	return p->wrange;
}

/*wrapper around the hdrl_spectrum1D constructor that accepts double arrays*/
static inline hdrl_spectrum1D *
hdrl_spectrum1D_create_from_buffers(double * flux, double * wlens, cpl_size sz,
		hdrl_spectrum1D_wave_scale scale){

	cpl_array * w = cpl_array_wrap_double(wlens, sz);
	cpl_image * fl = cpl_image_wrap_double(sz, 1, flux);

	hdrl_spectrum1D * to_ret = hdrl_spectrum1D_create_error_free(fl, w, scale);

	cpl_array_unwrap(w);
	cpl_image_unwrap(fl);
	return to_ret;
}
/* For every window in "areas", extract a flux point having as wavelength the
 * middle point of the window and as flux the median of the flux value defined
 * on the window.*/
static inline hdrl_spectrum1D *
	hdrl_spectrum1D_extract_fit_regions(const hdrl_spectrum1D * s,
			const cpl_bivector * areas){

	const double step = 1.0;

	cpl_size sz = cpl_bivector_get_size(areas);
	const cpl_vector * l_min = cpl_bivector_get_x_const(areas);
	const cpl_vector * l_max = cpl_bivector_get_y_const(areas);

	double * flux = cpl_calloc(sz + 2, sizeof(double));
	double * wlens = cpl_calloc(sz + 2, sizeof(double));

	const hdrl_data_t wmin =
			cpl_array_get_min(hdrl_spectrum1D_get_wavelength(s).wavelength);
	const hdrl_data_t wmax =
			cpl_array_get_max(hdrl_spectrum1D_get_wavelength(s).wavelength);
	{
		hdrl_spectrum1D * s_sel =
				select_win(s, wmin - step, wmin + step);

		const hdrl_image * sel_flux = hdrl_spectrum1D_get_flux(s_sel);
		flux[0] = hdrl_image_get_median(sel_flux).data;
		wlens[0] = wmin;

		hdrl_spectrum1D_delete(&s_sel);
	}

	cpl_size size_sel = 1;
	for(cpl_size i = 0; i < sz; ++i){
		const double lambda_min = cpl_vector_get(l_min, i);
		const double lambda_max = cpl_vector_get(l_max, i);

		hdrl_spectrum1D * s_sel =
				select_win(s, lambda_min, lambda_max);

		if(s_sel == NULL){
			cpl_error_reset();
			continue;
		}
		wlens[size_sel] = .5 * (lambda_max + lambda_min);
		const hdrl_image * sel_flux = hdrl_spectrum1D_get_flux(s_sel);
		flux[size_sel] = hdrl_image_get_median(sel_flux).data;

		hdrl_spectrum1D_delete(&s_sel);
		size_sel++;
	}

	{
		hdrl_spectrum1D * s_sel =
				select_win(s, wmax - step, wmax + step);
		const hdrl_image * sel_flux = hdrl_spectrum1D_get_flux(s_sel);
		flux[size_sel] = hdrl_image_get_median(sel_flux).data;
		wlens[size_sel] = wmax;
		hdrl_spectrum1D_delete(&s_sel);
		size_sel++;
	}

	hdrl_spectrum1D * sel_s = NULL;
	if(size_sel > 0)
	{
		const hdrl_spectrum1D_wave_scale scale = hdrl_spectrum1D_get_scale(s);
		sel_s = hdrl_spectrum1D_create_from_buffers(flux, wlens, size_sel, scale);
	}

	cpl_free(flux);
	cpl_free(wlens);

	return sel_s;
}
/*ctor for hdrl_response_result.
 * NOTE: hdrl_response_result gets ownership of all the pointers provided.
 * DO NOT deallocate them, they will be de-allocated by hdrl_response_result dtor.
 * */
static inline hdrl_response_result *
hdrl_response_result_wrap(hdrl_spectrum1D * final_response,
                          hdrl_spectrum1D * selected_response,
                          hdrl_spectrum1D * raw_response,

                          hdrl_spectrum1D * corrected_observed_spectrum,
                          cpl_size best_telluric_model_idx,
                          hdrl_data_t telluric_shift,
                          hdrl_data_t avg_diff_from_1,
                          hdrl_data_t stddev,

                          hdrl_data_t doppler_shift){

	hdrl_response_result * to_ret = cpl_calloc(1, sizeof(*to_ret));

	to_ret->final_response = final_response;
	to_ret->raw_response = raw_response;
	to_ret->selected_response = selected_response;

	to_ret->corrected_observed_spectrum = corrected_observed_spectrum;
	to_ret->best_telluric_model_idx = best_telluric_model_idx;
	to_ret->telluric_shift = telluric_shift;
	to_ret->avg_diff_from_1 = avg_diff_from_1;
	to_ret->stddev = stddev;

	to_ret->doppler_shift = doppler_shift;

	return to_ret;
}


/**@}*/