narray 0.5.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/src/ChangeLog +614 -0
  2. data/src/MANIFEST +82 -0
  3. data/src/README.en +54 -0
  4. data/src/README.ja +63 -0
  5. data/src/SPEC.en +300 -0
  6. data/src/SPEC.ja +284 -0
  7. data/src/depend +14 -0
  8. data/src/extconf.rb +111 -0
  9. data/src/lib/narray_ext.rb +211 -0
  10. data/src/lib/nmatrix.rb +244 -0
  11. data/src/mkmath.rb +780 -0
  12. data/src/mknafunc.rb +190 -0
  13. data/src/mkop.rb +638 -0
  14. data/src/na_array.c +644 -0
  15. data/src/na_func.c +1624 -0
  16. data/src/na_index.c +988 -0
  17. data/src/na_linalg.c +616 -0
  18. data/src/na_random.c +409 -0
  19. data/src/narray.c +1308 -0
  20. data/src/narray.def +29 -0
  21. data/src/narray.h +170 -0
  22. data/src/narray_local.h +210 -0
  23. data/src/nimage/README.en +38 -0
  24. data/src/nimage/demo/fits.rb +97 -0
  25. data/src/nimage/demo/fits_convol.rb +28 -0
  26. data/src/nimage/demo/fits_fftdemo.rb +27 -0
  27. data/src/nimage/demo/fitsdemo1.rb +13 -0
  28. data/src/nimage/demo/fitsdemo2.rb +30 -0
  29. data/src/nimage/demo/fitsdemo3.rb +26 -0
  30. data/src/nimage/demo/fitsmorph.rb +39 -0
  31. data/src/nimage/demo/life_na.rb +57 -0
  32. data/src/nimage/demo/mandel.rb +41 -0
  33. data/src/nimage/extconf.rb +12 -0
  34. data/src/nimage/lib/nimage.rb +51 -0
  35. data/src/nimage/nimage.c +328 -0
  36. data/src/speed/add.py +12 -0
  37. data/src/speed/add.rb +8 -0
  38. data/src/speed/add_int.py +12 -0
  39. data/src/speed/add_int.rb +9 -0
  40. data/src/speed/lu.m +14 -0
  41. data/src/speed/lu.rb +22 -0
  42. data/src/speed/mat.m +23 -0
  43. data/src/speed/mat.rb +28 -0
  44. data/src/speed/mul.py +12 -0
  45. data/src/speed/mul.rb +9 -0
  46. data/src/speed/mul2.py +15 -0
  47. data/src/speed/mul2.rb +13 -0
  48. data/src/speed/mul_comp.py +12 -0
  49. data/src/speed/mul_comp.rb +9 -0
  50. data/src/speed/mul_int.py +12 -0
  51. data/src/speed/mul_int.rb +9 -0
  52. data/src/speed/mybench.py +15 -0
  53. data/src/speed/mybench.rb +31 -0
  54. data/src/speed/solve.m +18 -0
  55. data/src/speed/solve.py +16 -0
  56. data/src/speed/solve.rb +21 -0
  57. data/src/test/statistics.rb +22 -0
  58. data/src/test/testarray.rb +20 -0
  59. data/src/test/testbit.rb +27 -0
  60. data/src/test/testcast.rb +14 -0
  61. data/src/test/testcomplex.rb +35 -0
  62. data/src/test/testfftw.rb +16 -0
  63. data/src/test/testindex.rb +11 -0
  64. data/src/test/testindexary.rb +26 -0
  65. data/src/test/testindexset.rb +55 -0
  66. data/src/test/testmask.rb +40 -0
  67. data/src/test/testmath.rb +48 -0
  68. data/src/test/testmath2.rb +46 -0
  69. data/src/test/testmatrix.rb +13 -0
  70. data/src/test/testmatrix2.rb +33 -0
  71. data/src/test/testmatrix3.rb +19 -0
  72. data/src/test/testminmax.rb +46 -0
  73. data/src/test/testobject.rb +29 -0
  74. data/src/test/testpow.rb +19 -0
  75. data/src/test/testrandom.rb +23 -0
  76. data/src/test/testround.rb +11 -0
  77. data/src/test/testsort.rb +37 -0
  78. data/src/test/teststr.rb +13 -0
  79. data/src/test/testtrans.rb +18 -0
  80. data/src/test/testwhere.rb +27 -0
  81. metadata +127 -0
data/src/narray.def ADDED
@@ -0,0 +1,29 @@
1
+ EXPORTS
2
+ Init_narray
3
+ na_sizeof
4
+ na_make_object
5
+ na_make_scalar
6
+ na_make_empty
7
+ na_get_typecode
8
+ na_clear_data
9
+ na_clone
10
+ na_fill
11
+ na_copy_nary
12
+ na_to_array
13
+ na_ary_to_nary
14
+ na_object_type
15
+ na_cast_object
16
+ na_cast_unless_narray
17
+ na_cast_unless_array
18
+ na_upcast_object
19
+ na_dup_w_type
20
+ na_change_type
21
+ na_upcast_type
22
+ na_to_narray
23
+ na_aset
24
+ na_aref
25
+ na_slice
26
+ na_count_true
27
+ na_count_false
28
+ na_aref_mask
29
+ na_aset_mask
data/src/narray.h ADDED
@@ -0,0 +1,170 @@
1
+ /*
2
+ narray.h
3
+ Numerical Array Extention for Ruby
4
+ (C) Copyright 1999-2003 by Masahiro TANAKA
5
+
6
+ This program is free software.
7
+ You can distribute/modify this program
8
+ under the same terms as Ruby itself.
9
+ NO WARRANTY.
10
+ */
11
+ #ifndef NARRAY_H
12
+ #define NARRAY_H
13
+
14
+ #include <math.h>
15
+
16
+ #include "narray_config.h"
17
+
18
+ #ifdef HAVE_SYS_TYPES_H
19
+ # include <sys/types.h>
20
+ #endif
21
+
22
+ #define NARRAY_VERSION "0.5.9p4"
23
+ #define NARRAY_VERSION_CODE 594
24
+
25
+ /*
26
+ Data types used in NArray :
27
+ Please modify these types if your system has any different type.
28
+ */
29
+ #ifndef HAVE_U_INT8_T
30
+ typedef unsigned char u_int8_t; /* NA_BYTE */
31
+ #endif
32
+
33
+ #ifndef HAVE_INT16_T
34
+ # if SIZEOF_SHORT == 2
35
+ typedef short int16_t; /* NA_SINT */
36
+ # else
37
+ ---->> Please define int16_t manually because sizeof(short) != 2. <<----
38
+ # endif
39
+ #endif /* HAVE_INT16_T */
40
+
41
+ #ifndef HAVE_INT32_T
42
+ # if SIZEOF_LONG == 4
43
+ typedef long int32_t; /* NA_LINT */
44
+ # else
45
+ # if SIZEOF_INT == 4
46
+ typedef int int32_t; /* NA_LINT */
47
+ # else
48
+ ---->> Please define int32_t manually because sizeof(long) != 4. <<----
49
+ # endif
50
+ # endif
51
+ #endif /* HAVE_INT32_T */
52
+
53
+ #ifndef HAVE_U_INT32_T
54
+ # if SIZEOF_LONG == 4
55
+ typedef unsigned long u_int32_t; /* NA_LINT */
56
+ # else
57
+ # if SIZEOF_INT == 4
58
+ typedef unsigned int u_int32_t; /* NA_LINT */
59
+ # else
60
+ ---->> Please define u_int32_t manually because sizeof(long) != 4. <<----
61
+ # endif
62
+ # endif
63
+ #endif /* HAVE_U_INT32_T */
64
+
65
+ typedef struct { float r,i; } scomplex;
66
+ typedef struct { double r,i; } dcomplex;
67
+
68
+ enum NArray_Types {
69
+ NA_NONE,
70
+ NA_BYTE, /* 1 */
71
+ NA_SINT, /* 2 */
72
+ NA_LINT, /* 3 */
73
+ NA_SFLOAT, /* 4 */
74
+ NA_DFLOAT, /* 5 */
75
+ NA_SCOMPLEX, /* 6 */
76
+ NA_DCOMPLEX, /* 7 */
77
+ NA_ROBJ, /* 8 */
78
+ NA_NTYPES /* 9 */
79
+ };
80
+
81
+ /* struct for Numerical Array */
82
+ struct NARRAY {
83
+ int rank; /* # of dimension */
84
+ int total; /* # of total element */
85
+ int type; /* data type */
86
+ int *shape;
87
+ char *ptr; /* pointer to data */
88
+ VALUE ref; /* NArray object wrapping this structure */
89
+ };
90
+
91
+ #ifndef NARRAY_C
92
+ extern VALUE cNArray;
93
+
94
+ extern const int na_sizeof[NA_NTYPES+1];
95
+ #endif
96
+
97
+ #define NA_MAX_RANK 15
98
+
99
+ #define GetNArray(obj,var) Data_Get_Struct(obj, struct NARRAY, var)
100
+ #define IsNArray(obj) (rb_obj_is_kind_of(obj,cNArray)==Qtrue)
101
+
102
+ #define NA_PTR(a,p) ((a)->ptr+(p)*na_sizeof[(a)->type])
103
+ #define NA_STRUCT(val) ((struct NARRAY*)DATA_PTR(val))
104
+ #define NA_PTR_TYPE(val,type) (type)(((struct NARRAY*)DATA_PTR(val))->ptr)
105
+ #define NA_RANK(val) (((struct NARRAY*)DATA_PTR(val))->rank)
106
+ #define NA_TYPE(val) (((struct NARRAY*)DATA_PTR(val))->type)
107
+ #define NA_TOTAL(val) (((struct NARRAY*)DATA_PTR(val))->total)
108
+ #define NA_SHAPE0(val) (((struct NARRAY*)DATA_PTR(val))->shape[0])
109
+ #define NA_SHAPE1(val) (((struct NARRAY*)DATA_PTR(val))->shape[1])
110
+
111
+ #define NA_IsNArray(obj) \
112
+ (rb_obj_is_kind_of(obj,cNArray)==Qtrue)
113
+ #define NA_IsArray(obj) \
114
+ (TYPE(obj)==T_ARRAY || rb_obj_is_kind_of(obj,cNArray)==Qtrue)
115
+ #define NA_IsROBJ(d) ((d)->type==NA_ROBJ)
116
+ #define NA_IsINTEGER(a) \
117
+ ((a)->type==NA_BYTE || (a)->type==NA_SINT || (a)->type==NA_LINT )
118
+ #define NA_IsCOMPLEX(a) \
119
+ ((a)->type==NA_SCOMPLEX || (a)->type==NA_DCOMPLEX)
120
+ #define NA_MAX(a,b) (((a)>(b))?(a):(b))
121
+ #define NA_SWAP(a,b,tmp) {(tmp)=(a);(a)=(b);(b)=(tmp);}
122
+
123
+ #define na_class_dim(klass) NUM2INT(rb_const_get(klass, na_id_class_dim))
124
+
125
+ #define NUM2REAL(v) NUM2DBL( rb_funcall((v),na_id_real,0) )
126
+ #define NUM2IMAG(v) NUM2DBL( rb_funcall((v),na_id_imag,0) )
127
+
128
+ #define NA_ALLOC_SLICE(slc,nc,shp,np) \
129
+ { slc = (struct slice*)xmalloc( sizeof(struct slice)*(nc) + \
130
+ sizeof(int)*(np) );\
131
+ shp = (int*)&( (slc)[nc] ); }
132
+
133
+
134
+ /* Function Prototypes */
135
+
136
+ /* narray.c */
137
+ VALUE na_make_object(int type, int rank, int *shape, VALUE klass);
138
+ VALUE na_make_scalar(VALUE obj, int type);
139
+ VALUE na_make_empty(int type, VALUE klass);
140
+ int na_get_typecode(VALUE v);
141
+ void na_clear_data(struct NARRAY *ary);
142
+ VALUE na_clone(VALUE self);
143
+ VALUE na_fill(VALUE self, volatile VALUE obj);
144
+ void na_copy_nary(struct NARRAY *dst, struct NARRAY *src);
145
+
146
+ /* na_array.c */
147
+ VALUE na_to_array(VALUE obj);
148
+ VALUE na_make_inspect(VALUE self);
149
+ VALUE na_ary_to_nary(VALUE ary, VALUE klass);
150
+ int na_object_type(VALUE v);
151
+
152
+ VALUE na_cast_object(VALUE obj, int type);
153
+ VALUE na_cast_unless_narray(VALUE obj, int type);
154
+ VALUE na_cast_unless_array(VALUE obj, int type);
155
+ VALUE na_upcast_object(VALUE obj, int type);
156
+ VALUE na_dup_w_type(VALUE obj, int type);
157
+ VALUE na_change_type(VALUE obj, int type);
158
+ VALUE na_upcast_type(VALUE obj, int type);
159
+ VALUE na_to_narray(VALUE obj);
160
+
161
+ /* na_index.c */
162
+ VALUE na_aset(int argc, VALUE *argv, VALUE self);
163
+ VALUE na_aref(int argc, VALUE *argv, VALUE self);
164
+ VALUE na_slice(int argc, VALUE *argv, VALUE self);
165
+ VALUE na_count_true(VALUE self);
166
+ VALUE na_count_false(VALUE self);
167
+ VALUE na_aref_mask(VALUE self, VALUE mask);
168
+ void na_aset_mask(VALUE self, VALUE mask, VALUE v);
169
+
170
+ #endif /* ifndef NARRAY_H */
@@ -0,0 +1,210 @@
1
+ /*
2
+ narray_local.h
3
+ Numerical Array Extention for Ruby
4
+ (C) Copyright 1999-2003 by Masahiro TANAKA
5
+
6
+ This program is free software.
7
+ You can distribute/modify this program
8
+ under the same terms as Ruby itself.
9
+ NO WARRANTY.
10
+ */
11
+
12
+ typedef int32_t na_index_t;
13
+
14
+ struct slice {
15
+ char *p; /* pointer to data --- used in loop */
16
+ int n; /* n of indices of this rank */
17
+ int pstep; /* = step * stride * elmsz --- set in na_init_slice */
18
+ int pbeg; /* = beg * stride * elmsz --- set in na_init_slice */
19
+ int stride; /* = shape[0]*shape[1]*...*shape[r-1]
20
+ --- set in na_init_slice */
21
+ int step;
22
+ int beg;
23
+ na_index_t *idx; /* NULL if normal step */
24
+ };
25
+
26
+ typedef void (*na_setfunc_t[NA_NTYPES][NA_NTYPES]) ();
27
+ typedef void (*na_func_t[NA_NTYPES]) ();
28
+ typedef void (*na_ufunc_t[NA_NTYPES]) ();
29
+ typedef void (*na_bifunc_t[NA_NTYPES]) ();
30
+ typedef void (*na_mathfunc_t[NA_NTYPES]) ();
31
+ typedef int (*na_sortfunc_t[NA_NTYPES]) (const void *, const void *);
32
+
33
+ /* function arrays */
34
+ extern na_setfunc_t SetFuncs;
35
+ extern na_ufunc_t SwpFuncs;
36
+ extern na_ufunc_t H2NFuncs;
37
+ extern na_ufunc_t H2VFuncs;
38
+ extern na_ufunc_t NegFuncs;
39
+ extern na_ufunc_t RcpFuncs;
40
+ extern na_ufunc_t AbsFuncs;
41
+ extern na_ufunc_t RealFuncs;
42
+ extern na_ufunc_t ImagFuncs;
43
+ extern na_ufunc_t AnglFuncs;
44
+ extern na_ufunc_t ImagMulFuncs;
45
+ extern na_ufunc_t ConjFuncs;
46
+ extern na_ufunc_t FloorFuncs;
47
+ extern na_ufunc_t CeilFuncs;
48
+ extern na_ufunc_t RoundFuncs;
49
+ extern na_ufunc_t ToStrFuncs;
50
+ extern na_ufunc_t InspFuncs;
51
+ extern na_ufunc_t IndGenFuncs;
52
+ extern na_ufunc_t AddUFuncs;
53
+ extern na_ufunc_t SbtUFuncs;
54
+ extern na_ufunc_t MulUFuncs;
55
+ extern na_ufunc_t DivUFuncs;
56
+ extern na_bifunc_t AddBFuncs;
57
+ extern na_bifunc_t SbtBFuncs;
58
+ extern na_bifunc_t MulBFuncs;
59
+ extern na_bifunc_t DivBFuncs;
60
+ extern na_bifunc_t MulAddFuncs;
61
+ extern na_bifunc_t MulSbtFuncs;
62
+ extern na_bifunc_t ModBFuncs;
63
+ extern na_bifunc_t BAnFuncs;
64
+ extern na_bifunc_t BOrFuncs;
65
+ extern na_bifunc_t BXoFuncs;
66
+ extern na_ufunc_t BRvFuncs;
67
+ extern na_bifunc_t ImgSetFuncs;
68
+ extern na_setfunc_t PowFuncs;
69
+ extern na_bifunc_t atan2Funcs;
70
+ extern na_bifunc_t CmpFuncs;
71
+ extern na_bifunc_t EqlFuncs;
72
+ extern na_ufunc_t AndFuncs;
73
+ extern na_ufunc_t Or_Funcs;
74
+ extern na_ufunc_t XorFuncs;
75
+ extern na_ufunc_t NotFuncs;
76
+ extern na_ufunc_t MinFuncs;
77
+ extern na_ufunc_t MaxFuncs;
78
+ extern na_sortfunc_t SortFuncs;
79
+ extern na_sortfunc_t SortIdxFuncs;
80
+ extern na_bifunc_t RefMaskFuncs;
81
+ extern na_bifunc_t SetMaskFuncs;
82
+
83
+ /* variables */
84
+
85
+ extern VALUE rb_mNMath;
86
+ extern ID na_id_beg, na_id_end, na_id_exclude_end;
87
+ extern ID na_id_minus, na_id_abs, na_id_power;
88
+ extern ID na_id_compare, na_id_and, na_id_or;
89
+ extern ID na_id_equal;
90
+ extern ID na_id_class_dim;
91
+ extern ID na_id_add, na_id_sbt, na_id_mul, na_id_div, na_id_mod;
92
+ extern ID na_id_real, na_id_imag;
93
+ extern ID na_id_coerce_rev;
94
+ extern ID na_id_new;
95
+
96
+ extern const int na_upcast[NA_NTYPES][NA_NTYPES];
97
+ extern const int na_no_cast[NA_NTYPES];
98
+ extern const int na_cast_real[NA_NTYPES];
99
+ extern const int na_cast_comp[NA_NTYPES];
100
+ extern const int na_cast_round[NA_NTYPES];
101
+ extern const int na_cast_byte[NA_NTYPES];
102
+
103
+ extern const char *na_typestring[];
104
+
105
+ extern VALUE cNArrayScalar, cComplex;
106
+
107
+ /* narray.c */
108
+ VALUE na_newdim_ref(int argc, VALUE *argv, VALUE self);
109
+
110
+ /* na_func.c */
111
+ int na_max3(int a, int b, int c);
112
+ void na_shape_max3(int ndim, int *max_shp, int *shp1, int *shp2, int *shp3);
113
+ void na_shape_copy( int ndim, int *shape, struct NARRAY *a );
114
+
115
+ void na_init_slice(struct slice *s, int rank, int *shape, int elmsz);
116
+ void na_set_slice_1obj(int ndim, struct slice *slc, int *shape);
117
+ int na_set_slice_3obj( int ndim,
118
+ struct slice *s1, struct slice *s2, struct slice *s3,
119
+ int *shp1, int *shp2, int *shp3, int *shape );
120
+ void na_loop_general(struct NARRAY *a1, struct NARRAY *a2,
121
+ struct slice *s1, struct slice *s2, void (*func)());
122
+ void na_loop_index_ref(struct NARRAY *a1, struct NARRAY *a2,
123
+ struct slice *s1, struct slice *s2, void (*func)());
124
+
125
+ /* na_index.c */
126
+ void na_aset_slice(struct NARRAY *dst, struct NARRAY *src, struct slice *s1);
127
+ int na_shrink_class(int class_dim, int *shrink);
128
+ VALUE na_shrink_rank(VALUE obj, int class_dim, int *shrink);
129
+
130
+ #define rb_complex_new(r,i) \
131
+ rb_funcall(cComplex, na_id_new, 2, rb_float_new(r), rb_float_new(i))
132
+
133
+
134
+ typedef union {
135
+ u_int8_t b[2];
136
+ int16_t s;
137
+ } na_size16_t;
138
+
139
+ typedef union {
140
+ u_int8_t b[4];
141
+ int32_t i;
142
+ float f;
143
+ } na_size32_t;
144
+
145
+ typedef union {
146
+ u_int8_t b[8];
147
+ float f[2];
148
+ double d;
149
+ } na_size64_t;
150
+
151
+ typedef union {
152
+ u_int8_t b[16];
153
+ double d[2];
154
+ } na_size128_t;
155
+
156
+
157
+ #define swap16(d,s) \
158
+ (d).b[0]=(s).b[1];\
159
+ (d).b[1]=(s).b[0];
160
+
161
+ #define swap32(d,s) \
162
+ (d).b[0]=(s).b[3];\
163
+ (d).b[1]=(s).b[2];\
164
+ (d).b[2]=(s).b[1];\
165
+ (d).b[3]=(s).b[0];
166
+
167
+ #define swap64(d,s) \
168
+ (d).b[0]=(s).b[7];\
169
+ (d).b[1]=(s).b[6];\
170
+ (d).b[2]=(s).b[5];\
171
+ (d).b[3]=(s).b[4];\
172
+ (d).b[4]=(s).b[3];\
173
+ (d).b[5]=(s).b[2];\
174
+ (d).b[6]=(s).b[1];\
175
+ (d).b[7]=(s).b[0];
176
+
177
+ #define swap64c(d,s) \
178
+ (d).b[0]=(s).b[3];\
179
+ (d).b[1]=(s).b[2];\
180
+ (d).b[2]=(s).b[1];\
181
+ (d).b[3]=(s).b[0];\
182
+ (d).b[4]=(s).b[7];\
183
+ (d).b[5]=(s).b[6];\
184
+ (d).b[6]=(s).b[5];\
185
+ (d).b[7]=(s).b[4];
186
+
187
+ #define swap128c(d,s) \
188
+ (d).b[0]=(s).b[7];\
189
+ (d).b[1]=(s).b[6];\
190
+ (d).b[2]=(s).b[5];\
191
+ (d).b[3]=(s).b[4];\
192
+ (d).b[4]=(s).b[3];\
193
+ (d).b[5]=(s).b[2];\
194
+ (d).b[6]=(s).b[1];\
195
+ (d).b[7]=(s).b[0];\
196
+ (d).b[8]=(s).b[15];\
197
+ (d).b[9]=(s).b[14];\
198
+ (d).b[10]=(s).b[13];\
199
+ (d).b[11]=(s).b[12];\
200
+ (d).b[12]=(s).b[11];\
201
+ (d).b[13]=(s).b[10];\
202
+ (d).b[14]=(s).b[9];\
203
+ (d).b[15]=(s).b[8];
204
+
205
+ #if !defined RSTRING_LEN
206
+ #define RSTRING_LEN(a) RSTRING(a)->len
207
+ #endif
208
+ #if !defined RSTRING_PTR
209
+ #define RSTRING_PTR(a) RSTRING(a)->ptr
210
+ #endif
@@ -0,0 +1,38 @@
1
+
2
+ NAME
3
+
4
+ NImage --- A Ruby Extension Library for displaying Images on X11
5
+ by Masahiro Tanaka 2000/07/05
6
+
7
+ DESCRIPTION
8
+
9
+ NImage is a simple Ruby class for displaying 2-D Data as images
10
+ on X11 display.
11
+
12
+ REQUIREMENT
13
+
14
+ Ruby (ver 1.4.4 or later)
15
+ NArray (ver 0.3.1 or later)
16
+
17
+ INSTALLATION
18
+
19
+ ruby extconf.rb
20
+ make
21
+ make install
22
+
23
+ USAGE
24
+
25
+ require "nimage"
26
+ image = NArray.float(100,100) # some 2-D array
27
+ win = NImage.show image
28
+ win.close
29
+
30
+ ACKNOWLEDGMENT
31
+
32
+ moriq-san for Ruby/X11 which is useful for prototyping
33
+
34
+ PS
35
+
36
+ FITS image file is not included. Sorry.
37
+
38
+ END
@@ -0,0 +1,97 @@
1
+ # A simple class for handling FITS images
2
+ # by Masahiro TANAKA 1999/12/19
3
+ # No document, sorry.
4
+ #
5
+ # Require: Ruby/NArray
6
+ # http://koala.astro.isas.ac.jp/~masa/ruby/
7
+ #
8
+ # Example:
9
+ # f = Fits.read('somthing.fits')
10
+ # f.data # => 2-dimentional NArray data
11
+
12
+ require 'narray'
13
+
14
+ class Fits
15
+ attr_accessor :data
16
+ attr_accessor :header
17
+
18
+ def initialize
19
+ @data = ""
20
+ @header = []
21
+ end
22
+
23
+ def data_to_narray(data,type,shape)
24
+ NArray.to_na( data, type, *shape ).ntoh
25
+ end
26
+
27
+
28
+ def read(fname)
29
+ f = File.open(fname, "r")
30
+ @header = []
31
+ nbytes = 0
32
+ foo = {}
33
+
34
+ while
35
+ line = f.read(80)
36
+ nbytes += 80;
37
+
38
+ @header.push line
39
+
40
+ name = line[0,8].strip!
41
+ break if name=="END"
42
+
43
+ rest = line[8..-1]
44
+ if rest =~ /^= +('[^']*')/ #'
45
+ foo[name] = $1
46
+ elsif rest =~ /^= +([^\s\/]+)/
47
+ foo[name] = $1
48
+ end
49
+ end
50
+
51
+ # skip extra space
52
+ nbytes %= 2880
53
+ f.read(2880-nbytes) if nbytes > 0
54
+
55
+ naxis = foo["NAXIS"].to_i
56
+
57
+ case foo["BITPIX"]
58
+ when "8"; elm_size = 1; type = NArray::BYTE
59
+ when "16"; elm_size = 2; type = NArray::SINT
60
+ when "32"; elm_size = 4; type = NArray::LINT
61
+ when "-32"; elm_size = 4; type = NArray::SFLOAT
62
+ when "-64"; elm_size = 8; type = NArray::DFLOAT
63
+ end
64
+
65
+ size = 1
66
+ dims = []
67
+ for i in 1..naxis
68
+ key = "NAXIS#{i}"
69
+ if foo.key? key
70
+ n = foo[key].to_i
71
+ size = size * n
72
+ dims.push n
73
+ #printf "%s=%i\n", key,n
74
+ end
75
+ end
76
+
77
+ @data = data_to_narray( f.read(size*elm_size), type, dims )
78
+ f.close
79
+
80
+ if foo.key? "BSCALE"
81
+ s = foo["BSCALE"].to_f
82
+ @data = @data.to_f.mul!(s) if s != 1
83
+ end
84
+
85
+ if foo.key? "BZERO"
86
+ s = foo["BZERO"].to_f
87
+ @data = @data.to_f.add!(s) if s != 0
88
+ end
89
+
90
+ self
91
+ end
92
+
93
+ end
94
+
95
+ def Fits.read(*a)
96
+ Fits.new.read(*a)
97
+ end
@@ -0,0 +1,28 @@
1
+ # FITS-FFT demo
2
+ require 'fits'
3
+ require 'nimage'
4
+
5
+ f = Fits.new
6
+ f.read( $*.shift || 'dss_m51.fits' )
7
+ nx,ny = f.data.shape
8
+
9
+ NImage.show f.data #, 'M51'
10
+
11
+ # FFT low-pass filtering
12
+ z = 50.0/nx
13
+ x = NArray.float(nx).indgen! * z
14
+ y = NArray.float(ny).indgen! * z
15
+ x[-(nx/2)..-1] = x[nx/2..1]
16
+ y[-(ny/2)..-1] = y[ny/2..1]
17
+ y.reshape!(1,ny)
18
+ # Operation between [nx,1] and [1,ny] arrays makes [nx,ny].
19
+ filt = NMath::exp(-(x**2)-(y**2))
20
+ filt /= filt.sum
21
+
22
+ print "Now executing FFT...\n"
23
+ lowpass = FFTW::convol(f.data,filt)
24
+
25
+ NImage.show lowpass #, 'M51 (FFT low-pass)'
26
+
27
+ print "Hit return key..."
28
+ STDIN.getc
@@ -0,0 +1,27 @@
1
+ # FITS-FFT demo
2
+ require 'fits'
3
+ require 'nimage'
4
+
5
+ f = Fits.new
6
+ f.read( $*.shift || 'dss_m51.fits' )
7
+ nx,ny = f.data.shape
8
+
9
+ NImage.show f.data #, 'M51'
10
+
11
+ # FFT low-pass filtering
12
+ x = NArray.float(nx).indgen! / (nx*0.1)
13
+ y = NArray.float(ny).indgen! / (nx*0.1)
14
+ x[-nx/2] = x[nx/2-1..0]
15
+ y[-ny/2] = y[ny/2-1..0]
16
+ y.reshape!(1,ny)
17
+ # Operation between [nx,1] and [1,ny] arrays makes [nx,ny].
18
+ filt = 1 / (x**2 + y**2 + 1)
19
+
20
+ print "Now executing FFT...\n"
21
+ spec = FFTW.fftw(f.data,-1)
22
+ lowpass = FFTW.fftw(spec*filt,1).real / (nx*ny)
23
+
24
+ NImage.show lowpass #, 'M51 (FFT low-pass)'
25
+
26
+ print "Hit return key..."
27
+ STDIN.getc
@@ -0,0 +1,13 @@
1
+ # FITS demo
2
+ require 'fits'
3
+ require 'nimage'
4
+
5
+ f = Fits.new
6
+ f.read( $*.shift || 'dss_m51.fits' )
7
+ p f.data
8
+
9
+ v = NImage.show f.data
10
+
11
+ print "Hit return key..."
12
+ STDIN.getc
13
+ v.close
@@ -0,0 +1,30 @@
1
+ # FITS demo
2
+ require 'fits'
3
+ require 'nimage'
4
+
5
+ f = Fits.new
6
+ f.read( $*.shift || 'dss_m51.fits' )
7
+
8
+ NImage.show f.data #, 'M51'
9
+
10
+ nx = f.data.shape[0]
11
+ ny = f.data.shape[1]
12
+
13
+ x = NArray.int(nx/2).indgen!*2
14
+ y = NArray.int(ny/2).indgen!*2
15
+ NImage.show f.data[x,y] #, 'M51 (x0.5)'
16
+
17
+ x = NArray.float(nx*1.5).indgen!/1.5
18
+ y = NArray.float(ny*1.5).indgen!/1.5
19
+ NImage.show f.data[x,y] #, 'M51 (x1.5)'
20
+
21
+ x = NArray.float(nx).indgen!
22
+ y = NArray.float(ny).indgen!.newdim!(0)
23
+ i = y / (ny/2) - 1
24
+ i = (x-nx/2) * NMath.sqrt(i.abs) + nx/2 + y*nx
25
+ d = f.data.clone.fill!(0)
26
+ d[i] = f.data
27
+ NImage.show d #, 'M51 (x1.5)'
28
+
29
+ print "Hit return key..."
30
+ STDIN.getc
@@ -0,0 +1,26 @@
1
+ # FITS demo
2
+ require 'fits'
3
+ require 'nimage'
4
+
5
+ f = Fits.new
6
+ f.read( $*.shift || 'dss_m51.fits' )
7
+ image = f.data
8
+
9
+ NImage.show image
10
+
11
+ nx = image.shape[0]
12
+ ny = image.shape[1]
13
+
14
+ # re-sampling
15
+ x = NArray.int(nx/2).indgen!*2
16
+ y = NArray.int(ny/2).indgen!*2
17
+ NImage.show image[x,y]
18
+ # 'M51 (x0.5, resample)'
19
+
20
+ # averaging
21
+ NImage.show image[0...nx/2*2,0...ny/2*2].to_f.
22
+ reshape!(2,nx/2,2,ny/2).sum(0,2).div!(4).reshape!
23
+ # 'M51 (x0.5, average)'
24
+
25
+ print "Hit return key..."
26
+ STDIN.getc
@@ -0,0 +1,39 @@
1
+ # FITS demo
2
+ require 'fits'
3
+ require 'nimage'
4
+
5
+ def triang x, px
6
+ y = NArray.float(*x.shape)
7
+ i1,i2 = (x>px).where2
8
+ y[i1] = NMath.sin((1-x[i1]) * (Math::PI/2/(1-px)) )
9
+ y[i2] = NMath.sin((1+x[i2]) * (Math::PI/2/(1+px)) )
10
+ p y.min
11
+ y
12
+ end
13
+
14
+ # Read-in Image
15
+ f = Fits.new
16
+ f.read( $*.shift || 'dss_m51.fits' )
17
+
18
+ NImage.show f.data
19
+
20
+ nx,ny = f.data.shape
21
+
22
+ # Morphing parameters
23
+ px,py = [ 0.3, -0.6]
24
+ dx,dy = [-0.3, 0.7]
25
+
26
+ indx = NArray.float(nx,1).indgen!
27
+ indy = NArray.float(1,ny).indgen!
28
+
29
+ ix = triang indx/(nx-1)*2-1, px
30
+ iy = triang indy/(ny-1)*2-1, py
31
+
32
+ ii = ix * iy
33
+ ix = (indx + ii*(dx*(nx-1)/2)).round
34
+ iy = (indy + ii*(dy*(ny-1)/2)).round
35
+
36
+ NImage.show f.data[ix+iy*nx]
37
+
38
+ print "Hit return key..."
39
+ STDIN.getc