-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wave.m
1472 lines (1229 loc) · 36.9 KB
/
Wave.m
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
//
// Wave.m
// iGDDS
//
// Created by Roberto Abraham on Tue Dec 31 2002.
// Copyright (c) 2002 __MyCompanyName__. All rights reserved.
//
// Note: Order for this class means the highest exponent in the power law expansion. Thus
// the number of coefficients is order + 1.
#import "Wave.h"
#pragma mark FUNCTIONS
//Sort order function
int compar(const double *a, const double *b){
if(*a < *b){
return(-1);
}
else if(*a > *b){
return(1);
}
else{
return(0);
}
}
//standard definition --- see numerical recipes
void legendreP(long n, double x, double *p)
{
int i;
p[0] = 1.0; if(n == 0) return;
p[1] = x; if(n == 1) return;
for(i=1; i<n; i++ ){
p[i+1] = (2*i + 1.0)*x*p[i] - i*p[i-1];
p[i+1] /= i + 1.0;
}
}
//somewhat modified from numerical recipes in C++
void locate(double *xx, long n, double x, long *j)
{
long ju,jm,jl;
BOOL ascnd;
jl=-1;
ju=n;
ascnd=(xx[n-1] >= xx[0]);
while (ju-jl > 1) {
jm=(ju+jl) >> 1;
if (x >= xx[jm] == ascnd)
jl=jm;
else
ju=jm;
}
if (x == xx[0]) *j=0;
else if (x == xx[n-1]) *j=n-2;
else *j=jl;
}
//reads in a FITS spectrum in MULTISPEC format
double *readspectrum(char *filename, int *nx, long *p0, double *pMin, double *pMax, double cf[NMAXCOEFFS],
long *ncf, int *error_status)
{
double *arr;
fitsfile *fptr; /* pointer to the FITS file, defined in fitsio.h */
long npixels, firstelem;
int anynull, status;
double nullval;
char comment[73]; // Changed to 73 from 72 by RGA June 2010
char filestring[FLEN_FILENAME];
char wat0line[69]; // Changed to 69 from 68 by RGA June 2010
char wat2line[69]; // Changed to 69 from 68 by RGA June 2010
char wat2[1024] = "";
char *wat2stripped;
char wat2final[1024] = "";
char key[9]; // Changed to 9 from 8 by RGA June 2010
int nread;
int i;
BOOL linear = 0;
//Equispec variables
double crval1, cdelt1;
long crpix1;
//Multispec variables
long ap,beam,dtype,nw,ftype,aplow=-1,aphigh=-1;
double w1,dw,z,wt,w0;
//Legendre polynomial variables
long ncoeff;
double pmin,pmax;
double coeff[10] = {0.,0.,0.,0.,0.,0.,0.,0.,0.,0.};
strncpy(filestring,filename,FLEN_FILENAME);
status = 0;
*nx = 0;
if(fits_open_file(&fptr, filestring, READONLY, &status)){
NSLog(@"Error calling fits_open_file()");
fits_close_file(fptr, &status);
*error_status = status;
return (arr);
}
if(fits_read_key(fptr,TLONG,"NAXIS1",nx,comment,&status)){
NSLog(@"Error calling fits_read_key() to get NAXIS1");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
if(fits_read_key(fptr,TSTRING,"WAT0_001",wat0line,comment,&status)){
if (status == KEY_NO_EXIST){
// NSLog(@"Error calling fits_read_key() to get WAT0_001. Assume this is a linear EQUISPEC FILE.");
status = 0;
linear = 1;
}
else{
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
}
else {
if(!strcmp(wat0line,"system=equispec") || !strcmp(wat0line,"SYSTEM=EQUISPEC")) { // NB. 0 == a match for strcmp. Bah.
linear=1;
}
}
if(linear){
//EQUISPEC CASE
if(fits_read_key(fptr,TDOUBLE,"CRVAL1",&crval1,comment,&status)){
NSLog(@"Error calling fits_read_key() to get CRVAL1");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
if(fits_read_key(fptr,TDOUBLE,"CDELT1",&cdelt1,comment,&status)){
NSLog(@"Error calling fits_read_key() to get CDELT1");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
if(fits_read_key(fptr,TLONG,"CRPIX1",&crpix1,comment,&status)){
NSLog(@"Error calling fits_read_key() to get CRPIX1");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
*p0 = crpix1-1;
*pMin = crpix1;
*pMax = *nx+1;
*ncf = 2;
cf[0] = crval1; cf[1] = cdelt1;
cf[1] = cf[1]* *nx/2.0;
cf[0] = cf[0] + cf[1];
}
else{
//MULTISPEC CASE
//Extract the WCS information
for(i=1;i<10;i++){
sprintf(key,"WAT2_00%d",i);
//NSLog(@"Looking for %s",key);
if(fits_read_key(fptr,TSTRING,key,wat2line,comment,&status)){
if (status == KEY_NO_EXIST){
status = 0;
}
else{
NSLog(@"Error calling fits_read_key() to get WAT2_00X");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
break;
}
else{
if(strlen(wat2line)!=68)
strcat(wat2line," "); //FITSIO does not return trailing spaces... and sometimes you need one!
strcat(wat2,wat2line);
}
}
wat2stripped = strstr(wat2,"\""); //remove leading bumpf. This just leaves the stuff in quotes
strncat(wat2final,wat2stripped+1,strlen(wat2stripped)-2); //extract stuff between quotes
//NSLog(@"Final string: %s",wat2final);
//Parse this incredibly long string...
//First try to extract the information that supposedly HAS to be there if this is a WCS file
nread=sscanf(wat2final,"%ld %ld %ld %lf %lf %ld %lf %ld %ld",&ap,&beam,&dtype,&w1,&dw,&nw,&z,&aplow,&aphigh);
if(nread==9){
//NSLog(@"Successfully read in all 9 lead MULTISPEC fields");
}
else if (nread==7){
//NSLog(@"WARNING: Only read in 7 of 9 lead MULTISPEC fields. Are aplow and aphigh set?");
if(aplow==-1)
aplow=1;
if(aphigh==-1)
aphigh=nw;
//NSLog(@"I'm fudging aplow and aphigh if necessary. Check: aplow=%d aphigh=%d",aplow,aphigh);
}
else{
NSLog(@"ERROR. Unable to read in even a basic MULTISPEC WCS wavelength calibration.");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
//Now try to extract the information that supposedly HAS to be there if this a Legendre polynomial
nread=sscanf(wat2final,"%*s %*s %*s %*s %*s %*s %*s %*s %*s %lf %lf %ld %ld %lf %lf",&wt,&w0,&ftype,&ncoeff,&pmin,&pmax);
if(nread==6){
//NSLog(@"Successfully read in wt,w0,ftype,ncoeff,pmin,pmax fields");
}
else{
NSLog(@"ERROR. Unable to read at least one of the MULTISPEC WCS wt,w0,ftype,ncoeff,pmin,pmax fields");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
//Is this a legendre polynomial?
if(ftype !=2){
NSLog(@"ERROR. Wavelength calibration is not given by a legendre polynomial.");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
//Is the number of orders too big for this function??
if(ncoeff > NMAXCOEFFS){
NSLog(@"ERROR. Maximum number of polynomial coefficients exceeded.");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
//Extract the coefficients of the legendre polynomial. We can handle up to NMAXCOEFF coefficients (which is presently set to
//be 10. If you change it you'll want to change the next line too.
nread=sscanf(wat2final,"%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", &coeff[0],&coeff[1],&coeff[2],&coeff[3],&coeff[4],&coeff[5],&coeff[6],&coeff[7],&coeff[8],&coeff[9]);
if(nread != ncoeff){
NSLog(@"ERROR. Could not read coefficients.");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
//Set the physical offset
if(fits_read_key(fptr,TLONG,"LTV1",p0,comment,&status)){
if(status == KEY_NO_EXIST){
//Try to use the CRPIX1 keyword instead
if(fits_read_key(fptr,TLONG,"CRPIX1",p0,comment,&status)){
if(status==KEY_NO_EXIST){
//Keyword doesn't exist. File is probably written by WSPECTEXT. Bodge it.
*p0 = -1;
status = 0;
}
else{
NSLog(@"Error calling fits_read_key() to get LTV1 or CRPIX1");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
}
status = 0;
}
else{
NSLog(@"Error when calling fits_read_key()");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
}
*p0 = -1* *p0;
//Store the other output parameters for the wavelength calibration. Look for a
//potential screw-up in pMin/pMax not being set properly and fix it.
if((fabs(pmin - 1.0)<1e-5) && (fabs(pmax - *nx)<1e-5) ){
*pMin = *p0;
*pMax = *p0 + *nx;
}
else{
*pMin = pmin;
*pMax = pmax;
}
*ncf = ncoeff;
//cf = (double *)malloc(ncoeff*sizeof(double));
for(i=0;i<ncoeff;i++){
*(cf + i)=coeff[i];
}
}
//Extract the spectrum
firstelem = 1;
npixels = *nx; /* number of pixels in the spectrum */
nullval = 0; /* don't check for null values */
arr = (double *) malloc(sizeof(double)*npixels);
if(fits_read_img(fptr,TDOUBLE,firstelem,npixels,&nullval,arr,&anynull,&status)){
NSLog(@"Error calling fits_read_img()");
fits_close_file(fptr, &status);
*error_status = status;
return(arr);
}
if(fits_close_file(fptr, &status)){
NSLog(@"Error calling fits_close_file()");
*error_status = status;
return(arr);
}
*error_status = status;
return(arr);
};
@implementation Wave
#pragma mark
#pragma mark INITIALIZERS
+(void) initialize
{
if (self==[Wave class]){
[self setVersion:1];
}
}
-(id)init
{
self = [super init];
if (self) {
_n = 0;
_attributes = [[NSMutableDictionary alloc] init];
//[_attributes takeValue:[NSNumber numberWithBool:YES] forKey:@"ShowMe"];
[_attributes takeValue:[NSColor blackColor] forKey:@"Color"];
[_attributes takeValue:[NSNumber numberWithBool:YES] forKey:@"Visible"];
[_attributes takeValue:@"CityScape" forKey:@"LineStyle"];
}
return self;
}
-(BOOL) showMe
{
return [[_attributes objectForKey:@"Visible"] boolValue];
}
-(void) setShowMe:(BOOL)val
{
[_attributes takeValue:[NSNumber numberWithBool:val] forKey:@"Visible"];
}
-(id)initWithCoder:(NSCoder *)coder
{
int version;
if (self=[super init]){
version = [coder versionForClassName:@"Wave"];
[self setY:[coder decodeObject]];
[self setC:[coder decodeObject]];
[coder decodeValueOfObjCType:@encode(long) at:&_n];
[coder decodeValueOfObjCType:@encode(long) at:&_order];
[coder decodeValueOfObjCType:@encode(long) at:&_p0];
[coder decodeValueOfObjCType:@encode(double) at:&_pMin];
[coder decodeValueOfObjCType:@encode(double) at:&_pMax];
[coder decodeValueOfObjCType:@encode(double) at:&_xMin];
[coder decodeValueOfObjCType:@encode(double) at:&_xMax];
[self setX:[coder decodeObject]];
if (version>=1){
[self setAttributes:[coder decodeObject]];
}
else{
[self setAttributes:[[NSMutableDictionary alloc] init]];
[_attributes takeValue:[NSColor blackColor] forKey:@"Color"];
}
}
return self;
}
-(id)initWithNonLinearScale:(double *)ypts nData:(int)n coefficients:(double *)legendre order:(long)order offset:(long)p0 pMin:(double)pMin pMax:(double)pMax
{
self = [super init];
if (self) {
[self init];
_n = n;
_order = order;
if(_y) {[_y release]; _y=nil;}
if(_c) {[_c release]; _c=nil;}
if(_x) {[_x release]; _x=nil;}
_y = [[NSMutableData alloc] init];
_c = [[NSMutableData alloc] init];
_p0 = p0;
_pMin = pMin;
_pMax = pMax;
[_y appendBytes:ypts length:n*sizeof(double)];
[_c appendBytes:legendre length:(order+1)*sizeof(double)];
[self cacheAbscissae];
_xMin = [self xAtIndex:_p0];
_xMax = [self xAtIndex:_p0+_n-1];
}
return self;
}
-(id)initWithZerosUsingN:(int)n startX:(double)x0 dX:(double)dx offset:(long)p0
{
double *ypts = (double *) malloc(n*sizeof(double));
double *cbuffer = (double *) malloc(2*sizeof(double));
int i;
for(i=0;i<n;i++){
*(ypts + i) = 0.0;
}
self = [super init];
if (self) {
[self init];
_n = n;
_order = 1;
if(_y) {[_y release]; _y=nil;}
if(_c) {[_c release]; _c=nil;}
if(_x) {[_x release]; _x=nil;}
_y = [[NSMutableData alloc] init];
_c = [[NSMutableData alloc] init];
_p0 = p0;
_pMin = 0.0;
_pMax = (double)(_n-1.0);
[_y appendBytes:ypts length:n*sizeof(double)];
cbuffer[0] = x0; cbuffer[1] = dx;
cbuffer[1] = cbuffer[1]*(n-1)/2.0;
cbuffer[0] = cbuffer[0] + cbuffer[1];
[_c appendBytes:cbuffer length:2*sizeof(double)];
[self cacheAbscissae];
_xMin = [self xAtIndex:_p0];
_xMax = [self xAtIndex:_p0+_n-1];
}
free(cbuffer);
free(ypts);
return self;
}
-(id)initWithLinearScale:(double *)ypts nData:(int)n startX:(double)x0 dX:(double)dx offset:(long)p0
{
double *cbuffer = (double *) malloc(2*sizeof(double));
self = [super init];
if (self) {
[self init];
_n = n;
_order = 1;
if(_y) {[_y release]; _y=nil;}
if(_c) {[_c release]; _c=nil;}
if(_x) {[_x release]; _x=nil;}
_y = [[NSMutableData alloc] init];
_c = [[NSMutableData alloc] init];
_p0 = p0;
_pMin = 0.0;
_pMax = (double)(_n-1.0);
[_y appendBytes:ypts length:n*sizeof(double)];
cbuffer[0] = x0; cbuffer[1] = dx;
cbuffer[1] = cbuffer[1]*(n-1)/2.0;
cbuffer[0] = cbuffer[0] + cbuffer[1];
[_c appendBytes:cbuffer length:2*sizeof(double)];
[self cacheAbscissae];
_xMin = [self xAtIndex:_p0];
_xMax = [self xAtIndex:_p0+_n-1];
}
free(cbuffer);
return self;
}
-(id)initWithGrid:(double *)xpts y:(double *)ypts nData:(int)n offset:(long)p0
{
double *cbuffer = (double *) malloc(1*sizeof(double)); // dummy
self = [super init];
if (self) {
[self init];
_n = n;
_order = 0;
if(_y) {[_y release]; _y=nil;}
if(_c) {[_c release]; _c=nil;}
if(_x) {[_x release]; _x=nil;}
_x = [[NSMutableData alloc] init];
_y = [[NSMutableData alloc] init];
_c = [[NSMutableData alloc] init];
_p0 = p0;
_pMin = 0.0;
_pMax = (double)(_n-1.0);
cbuffer[0] = 0.0; //dummy
[_x appendBytes:xpts length:n*sizeof(double)];
[_y appendBytes:ypts length:n*sizeof(double)];
[_c appendBytes:cbuffer length:1*sizeof(double)];
_xMin = [self xAtIndex:_p0];
_xMax = [self xAtIndex:_p0+_n-1];
}
free(cbuffer);
return self;
}
-(id)initWithTextFile:(NSString *)path xColumn:(int)xcol yColumn:(int)ycol;
{
if (self = [super init]) {
NSArray *lines;
NSArray *headerRowStrings;
NSArray *dataRowStrings;
NSArray *tokens;
NSString *aRowString;
NSEnumerator *enumerator;
int numberOfHeaderRows;
int numberOfDataRows;
int currentDataLine;
double *x;
double *y;
int i;
lines = [[NSString stringWithContentsOfFile:path] componentsSeparatedByCharacter:'\n'];
enumerator = [lines objectEnumerator];
/* break the file up into header lines and data lines */
numberOfHeaderRows=0;
aRowString = [enumerator nextObject];
while([aRowString characterAtIndex:0] == '#') {
aRowString = [enumerator nextObject];
numberOfHeaderRows++;
}
numberOfDataRows = [lines count] - numberOfHeaderRows;
x=(double *)malloc(sizeof(double)*numberOfDataRows);
y=(double *)malloc(sizeof(double)*numberOfDataRows);
/* Create arrays with header rows and data rows */
headerRowStrings = [lines subarrayWithRange:NSMakeRange(0,numberOfHeaderRows)];
dataRowStrings = [lines subarrayWithRange:NSMakeRange(numberOfHeaderRows,numberOfDataRows)];
/* Extract the first relevant columns of the file */
currentDataLine = 0;
for(i=0;i<numberOfDataRows;i++){
aRowString = [dataRowStrings objectAtIndex:i];
tokens = [aRowString componentsSeparatedByCharacter:' '];
*(x + currentDataLine) = [[tokens objectAtIndex:xcol] doubleValue];
*(y + currentDataLine) = [[tokens objectAtIndex:ycol] doubleValue];
currentDataLine++;
}
[self initWithGrid:x y:y nData:numberOfDataRows offset:0];
}
return self;
}
- (id) initWithFITS:(NSString *)file
{
if (self = [super init]) {
double *data;
double coeffs[NMAXCOEFFS];
char *cFileName;
int error_status;
int nx;
long ncoeffs;
long p0;
double pMin, pMax;
cFileName = (char *)[file UTF8String];
[self init];
if(_y) {[_y release]; _y=nil;}
if(_c) {[_c release]; _c=nil;}
if(_x) {[_x release]; _x=nil;}
//NSLog(@"Attempting to read in %@",file);
data = (double *)readspectrum(cFileName, &nx, &p0, &pMin, &pMax, coeffs, &ncoeffs, &error_status);
_n = (long)nx;
_y = [[NSMutableData alloc] init];
[_y appendBytes:data length:_n*sizeof(double)];
_c = [[NSMutableData alloc] init];
[_c appendBytes:coeffs length:ncoeffs*sizeof(double)];
_order = ncoeffs-1;
_p0 = p0+1;
_pMin = pMin;
_pMax = pMax;
[self cacheAbscissae];
_xMin = [self xAtIndex:_p0];
_xMax = [self xAtIndex:_p0+_n-1];
free(data);
}
return self;
}
-(void)setScalarsNData:(long)n order:(long)order p0:(long)p0 pMin:(double)pMin pMax:(double)pMax xMin:(double)xMin xMax:(double)xMax
{
_n = n;
_order = order;
_p0 = p0;
_pMin = pMin;
_pMax = pMax;
_xMin = xMin;
_xMax = xMax;
}
- (id)copyWithZone:(NSZone *)zone
{
Wave *newWave = [Wave allocWithZone:zone];
[newWave setScalarsNData:[self n]
order:[self order]
p0:[self p0]
pMin:[self pMin]
pMax:[self pMax]
xMin:[self xMin]
xMax:[self xMax]];
// Add objects using Key-Value coding
[newWave takeValue:[_y copyWithZone:zone] forKey:@"_y"];
[newWave takeValue:[_c copyWithZone:zone] forKey:@"_c"];
[newWave takeValue:[_x copyWithZone:zone] forKey:@"_x"];
[newWave takeValue:[_attributes mutableCopyWithZone:zone] forKey:@"_attributes"];
return newWave;
}
#pragma mark
#pragma mark INPUT/OUTPUT
-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:_y];
[coder encodeObject:_c];
[coder encodeValueOfObjCType:@encode(long) at:&_n];
[coder encodeValueOfObjCType:@encode(long) at:&_order];
[coder encodeValueOfObjCType:@encode(long) at:&_p0];
[coder encodeValueOfObjCType:@encode(double) at:&_pMin];
[coder encodeValueOfObjCType:@encode(double) at:&_pMax];
[coder encodeValueOfObjCType:@encode(double) at:&_xMin];
[coder encodeValueOfObjCType:@encode(double) at:&_xMax];
[coder encodeObject:_x];
[coder encodeObject:_attributes];
}
- (int) saveAsFITS:(NSString *)file
{
char *cFileName = (char *)[file UTF8String];
int err;
err=savespectrum(cFileName,
(int)_n,
[_y bytes],
TDOUBLE,
(int)_p0-1,
(double)_pMin,
(double)_pMax,
[_c bytes],
(int)_order+1);
return(err);
}
#pragma mark
#pragma mark STATISTICS
-(double)yMin
{
long i;
double ymin, val;
double *p = (double *)[_y bytes];
ymin = 1.E300;
for(i=0;i<_n;i++){
val = *(p+i);
if (val<ymin)
ymin=val;
}
return ymin;
}
-(double)yMax
{
long i;
double ymax, val;
double *p = (double *)[_y bytes];
ymax = -1.E300;
for(i=0;i<_n;i++){
val = *(p+i);
if (val>ymax)
ymax=val;
}
return ymax;
}
-(double) medianInRangeFromX:(double)x0 toX:(double)x1
{
long i0, i1;
long count, i;
double *temp;
double med;
i0 = (int)[self dindexAtX:x0 outOfRangeValue:-1];
i1 = (int)[self dindexAtX:x1 outOfRangeValue:-1];
if (i0<0 || i1<0)
return NAN; //error is flagged by NAN
temp = (double *)malloc((i1-i0+1)*sizeof(double));
count = 0;
for(i=i0;i<=i1;i++){
*(temp+count) = [self yAtIndex:i];
count++;
}
qsort(temp, count, sizeof(double),(void *)*compar);
med = *(temp + count/2);
free(temp);
return med;
}
-(double) sumInRangeFromX:(double)x0 toX:(double)x1
{
long i0, i1;
long count, i;
double sum;
i0 = (int)[self dindexAtX:x0 outOfRangeValue:-1];
i1 = (int)[self dindexAtX:x1 outOfRangeValue:-1];
if (i0<0 || i1<0)
return NAN; //error is flagged by NAN
//calculate total
sum = 0.0;
count = 0;
for(i=i0;i<=i1;i++){
sum += [self yAtIndex:i];
count++;
}
return sum;
}
-(double) meanInRangeFromX:(double)x0 toX:(double)x1
{
long i0, i1;
long count, i;
double sum;
i0 = (int)[self dindexAtX:x0 outOfRangeValue:-1];
i1 = (int)[self dindexAtX:x1 outOfRangeValue:-1];
if (i0<0 || i1<0)
return NAN; //error is flagged by NAN
//calculate mean
sum = 0.0;
count = 0;
for(i=i0;i<=i1;i++){
sum += [self yAtIndex:i];
count++;
}
return sum/count;
}
-(double)signalToNoiseInRangeFromX:(double)x0 toX:(double)x1
{
long i0, i1;
long count, i;
double avg, rms;
i0 = (int)[self dindexAtX:x0 outOfRangeValue:-1];
i1 = (int)[self dindexAtX:x1 outOfRangeValue:-1];
if (i0<0 || i1<0)
return NAN; //error is flagged by NAN
//calculate mean
avg = 0.0;
count = 0;
for(i=i0;i<=i1;i++){
avg += [self yAtIndex:i];
count++;
}
avg /= count;
//calculate standard deviation
rms = 0.0;
for(i=i0;i<=i1;i++){
rms += pow([self yAtIndex:i] - avg,2.0);
}
rms = sqrt(rms/(count-1));
return avg/rms;
}
-(double)standardDeviationInRangeFromX:(double)x0 toX:(double)x1
{
long i0, i1;
long count, i;
double sum, rms;
i0 = (int)[self dindexAtX:x0 outOfRangeValue:-1];
i1 = (int)[self dindexAtX:x1 outOfRangeValue:-1];
if (i0<0 || i1<0)
return NAN; //error is flagged by NAN
//calculate mean
sum = 0.0;
count = 0;
for(i=i0;i<=i1;i++){
sum += [self yAtIndex:i];
count++;
}
sum /= count;
//calculate standard deviation
rms = 0.0;
for(i=i0;i<=i1;i++){
rms += pow([self yAtIndex:i] - sum,2.0);
}
rms = sqrt(rms/(count-1));
return rms;
}
-(void)boxcar:(int)halfWidth
{
double *yorig,*y;
int i,j;
y = (double *)malloc(_n*sizeof(double));
yorig = (double *)malloc(_n*sizeof(double));
//create a copy of the original data
for(i=0;i<_n;i++)
*(yorig + i) = [self yAtIndex:[self p0]+i];
//null out the endpoints where we've not got enough information to smooth
for(i=0;i<halfWidth;i++)
*(y + i) = 0.;
for(i=_n-halfWidth;i<_n;i++)
*(y + i) = 0.;
//do a running boxcar over the rest of the data
for(i=halfWidth;i<_n-halfWidth;i+=1){
*(y + i) = 0.;
if (halfWidth>0){
for(j=-halfWidth;j<=halfWidth;j++){
*(y + i) += *(yorig + i + j);
}
*(y + i) /= 2*halfWidth + 1;
}
else{
*(y + i) = *(yorig + i);
}
}
[_y release];
_y = [[NSMutableData alloc] init];
[_y appendBytes:y length:_n*sizeof(double)];
free(yorig);
free(y);
}
-(void)cacheAbscissae
{
int i;
double *x;
x = (double *)malloc(_n*sizeof(double));
if(_x)
[_x release];
for(i=0;i<_n;i++)
*(x+i)=[self xAtIndexSlow:_p0+i];
_x = [[NSMutableData alloc] init];
[_x appendBytes:x length:_n*sizeof(double)];
free(x);
}
//description
- (NSString *)description{
NSString *desc = [NSString stringWithFormat:@"\nWave Properties\n n:%d\n p0:%ld\n pMin:%lf\n pMax:%lf\n order:%d\n xMin:%f\n xMax:%f\n",
_n,_p0,_pMin,_pMax,_order,_xMin,_xMax];
int i;
for(i=0;i<_order+1;i++)
desc = [desc stringByAppendingFormat:@" Legendre coeff %d: %f\n",i,*((double *)[_c bytes]+i)];
desc = [desc stringByAppendingFormat:@" i:{%ld,%ld,...,%ld}\n",_p0,_p0+1,_p0+_n-1];
desc = [desc stringByAppendingFormat:@" x:{%f,%f,...,%f}\n",[self xAtIndex:_p0],[self xAtIndex:_p0+1],[self xAtIndex:_p0+_n-1]];
desc = [desc stringByAppendingFormat:@" y:{%le,%le,...,%le}",[self yAtIndex:_p0],[self yAtIndex:_p0+1],[self yAtIndex:_p0+_n-1]];
return(desc);
}
-(void)setScaleWithX0:(double)x0 dX:(double)dx p0:(long)p0
{
double *cbuffer = (double *) malloc(2*sizeof(double));
if(_c){
[_c release];
_c = [[NSMutableData alloc] init];
}
cbuffer[0] = x0; cbuffer[1] = dx;
cbuffer[1] = cbuffer[1]*_n/2.0;
cbuffer[0] = cbuffer[0] + cbuffer[1];
[_c appendBytes:cbuffer length:2*sizeof(double)];
_p0=p0;
_pMin = (double)p0;
_pMax = (double)(_n-1.0);
[self cacheAbscissae];
_xMin = [self xAtIndex:_p0];
_xMax = [self xAtIndex:_p0+_n-1];
}
-(void)setScaleWithCoefficients:(NSMutableData *)coeff order:(long) order p0:(long)p0 pMin:(double)pMin pMax:(double)pMax
{
if(_c){
[_c release];
}
_order = order;
_c = [coeff copy];
_p0=p0;
_pMin=pMin;
_pMax=pMax;
[self cacheAbscissae];
_xMin = [self xAtIndex:_p0];
_xMax = [self xAtIndex:_p0+_n-1];
}
-(double)yAtIndex:(long)i
{
double *yValues = (double *)[_y bytes];
return yValues[i-_p0];
}
//This assumes the abscissae have already been cached
-(double)xAtIndex:(long)i
{
double *xValues = (double *)[_x bytes];
return xValues[i-_p0];
}
//This calculates an abscissa from the lagrange coefficients
-(double)xAtIndexSlow:(long)i
{
double *cValues = (double *)[_c bytes];
double p = (i - (_pMax + _pMin)/2.0)/((_pMax - _pMin)/2.0);
double *lg = (double *)malloc((_order+1)*sizeof(double));
long j;
double x;
legendreP(_order,p,lg);
x=0.0;
for(j=0;j<=_order;j++){
x += *(cValues + j) * *(lg + j);
}
return(x);
free(lg);
}
/* //OLD VERSION
-(Wave *)duplicate