source: Sophya/trunk/SophyaPI/ProgPI/sopiamodule.cc@ 2977

Last change on this file since 2977 was 2925, checked in by ansari, 19 years ago

Petites correction concernant la gestion de dopt (display option) dans commandes FFT - Reza 28/3/2006

File size: 15.7 KB
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <stdio.h>
4#include <stdlib.h>
5#include <iostream>
6#include <ctype.h>
7#include <math.h>
8
9#include <typeinfo>
10
11#include <vector>
12#include <string>
13
14#include "piacmd.h"
15#include "nobjmgr.h"
16#include "pistdimgapp.h"
17#include "servnobjm.h"
18#include "tvector.h"
19#include "pitvmaad.h"
20#include "fftpserver.h"
21#include "bruit.h"
22#include "piscdrawwdg.h"
23#include "ctimer.h"
24
25#include "pidrawer.h"
26#include "nomgadapter.h"
27#include "nomskymapadapter.h"
28
29extern "C" {
30void sopiamodule_init();
31void sopiamodule_end();
32}
33
34void SophyaFFT_forw(string& nom, string& nomout, string dopt);
35void SophyaFFT_back(string& nom, string& nomout, string dopt, bool forcez8=false);
36// void SophyaFFT_crefilter(string& nomvec, string& nomfilter, string func, string dopt);
37void SophyaFFT_filter(string& nom, string& filt, string& outvec, string dopt, bool fgvec=false);
38
39class sopiamoduleExecutor : public CmdExecutor {
40public:
41 sopiamoduleExecutor();
42 virtual ~sopiamoduleExecutor();
43 virtual int Execute(string& keyw, vector<string>& args, string& toks);
44};
45
46// Classe pour trace de grille en projection spherique Mollweide
47class MollweideGridDrawer : public PIDrawer {
48public:
49 MollweideGridDrawer(int np, int nm, int nx, int ny);
50 MollweideGridDrawer(int np, int nm, double x_larg=0., double x_0=0.,
51 double y_larg=0., double y_0=0.);
52 virtual ~MollweideGridDrawer();
53 virtual void Draw(PIGraphicUC* g, double xmin, double ymin, double xmax, double ymax);
54 virtual void UpdateLimits();
55
56protected:
57 double xlarg, x0;
58 double ylarg, y0;
59 int nbparall, nbmerid;
60 int nbpt;
61 bool fgrevtheta;
62};
63
64/* --Methode-- */
65MollweideGridDrawer::MollweideGridDrawer(int np, int nm, int nx, int ny)
66{
67 x0 = (double)nx/2.;
68 y0 = (double)ny/2.;
69 xlarg = nx;
70 ylarg = ny;
71 nbparall = np;
72 nbmerid = nm;
73 nbpt = ny/5;
74 if ((nbpt % 20) == 0) nbpt++;
75 fgrevtheta = true;
76}
77
78/* --Methode-- */
79MollweideGridDrawer::MollweideGridDrawer(int np, int nm, double x_larg, double x_0,
80 double y_larg, double y_0)
81{
82 if (x_larg > 0.) {
83 xlarg = x_larg; x0 = x_0;
84 }
85 else {
86 xlarg = M_PI*2.; x0 = M_PI;
87 }
88 if (y_larg > 0.) {
89 ylarg = y_larg; y0 = y_0;
90 }
91 else {
92 ylarg = M_PI; y0 = 0.;
93 }
94 nbparall = np;
95 nbmerid = nm;
96 nbpt = 101;
97 fgrevtheta = false;
98}
99
100/* --Methode-- */
101MollweideGridDrawer::~MollweideGridDrawer()
102{
103}
104
105/* --Methode-- */
106void MollweideGridDrawer::Draw(PIGraphicUC* g, double xmin, double ymin, double xmax, double ymax)
107{
108 PIGrCoord * xxl = new PIGrCoord[nbpt];
109 PIGrCoord * xxr = new PIGrCoord[nbpt];
110 PIGrCoord * yy = new PIGrCoord[nbpt];
111
112 char buff[64];
113
114 // Trace des paralleles
115 g->DrawLine(xmin, y0, xmax, y0);
116 g->DrawString(x0+xlarg/100, y0+ylarg/100, "0");
117
118 // PIGrCoord asc, PIGrCoord desc;
119 int i,k;
120
121 int nkp = (nbparall-1)/2;
122 if (nkp > 0) {
123 double dy = 0.5*ylarg/(double)(nkp+1);
124 double dtd = 90./(double)(nkp+1);
125 for(k=1; k<=nkp; k++) {
126 double fx = sin(acos(2.*(double)k*dy/ylarg))*0.5;
127 double yc = k*dy+y0;
128 g->DrawLine(x0-fx*xlarg, yc, x0+fx*xlarg, yc);
129 double ctd = (fgrevtheta) ? -dtd*k : dtd*k;
130 sprintf(buff, "%5.1f", ctd);
131 g->DrawString(x0, yc, buff);
132 g->DrawString(x0-0.3*xlarg, yc, buff);
133 g->DrawString(x0+0.25*xlarg, yc, buff);
134 yc = -k*dy+y0;
135 g->DrawLine(x0-fx*xlarg, yc, x0+fx*xlarg, yc);
136 sprintf(buff, "%5.1f", -ctd);
137 g->DrawString(x0, yc, buff);
138 g->DrawString(x0-0.3*xlarg, yc, buff);
139 g->DrawString(x0+0.25*xlarg, yc, buff);
140 }
141 }
142
143 // Trace des meridiennes
144 g->DrawLine(x0, ymin, x0, ymax);
145
146 int nkm = (nbmerid-1)/2;
147 if (nkm < 1) nkm = 1;
148 double dphi = M_PI/nkm;
149 double dpd = 180./(double)(nkm);
150 double dy = ylarg/(nbpt-1);
151 for(k=0; k<nkm; k++) {
152 double phi0 = k*dphi;
153 double dphimil = fabs(phi0-M_PI);
154 if (dphimil < 1.e-6) continue;
155 for(i=0; i<nbpt; i++) {
156 double yc = (double)i*dy+y0-0.5*ylarg;
157 double fx = (2.*fabs(yc-y0)/ylarg <= 1.) ?
158 sin(acos(2.*fabs(yc-y0)/ylarg)) * dphimil/M_PI * 0.5 : 0.;
159 yy[i] = yc;
160 xxl[i] = x0-fx*xlarg;
161 xxr[i] = x0+fx*xlarg;
162 }
163
164 g->DrawPolygon(xxl, yy, nbpt, false);
165 g->DrawPolygon(xxr, yy, nbpt, false);
166
167 if (k == 0) continue;
168 for(i=-1; i<=1; i++) {
169 double yc = 0.20*i*ylarg+y0;
170 double fx = sin(acos(2.*fabs(yc-y0)/ylarg)) * dphimil/M_PI * 0.5;
171 double xc = x0-fx*xlarg;
172 sprintf(buff, "%4.1f", dpd*k);
173 g->DrawString(xc, yc, buff);
174 xc = x0+fx*xlarg;
175 sprintf(buff, "%4.1f", 360.-dpd*k);
176 g->DrawString(xc, yc, buff);
177 }
178 }
179
180 delete [] xxl;
181 delete [] xxr;
182 delete [] yy;
183}
184
185/* --Methode-- */
186void MollweideGridDrawer::UpdateLimits()
187{
188 SetLimits(x0-xlarg/2., x0+xlarg/2., y0-ylarg/2., y0+ylarg/2.);
189}
190
191/* --Methode-- */
192sopiamoduleExecutor::sopiamoduleExecutor()
193{
194
195PIACmd * mpiac;
196NamedObjMgr omg;
197mpiac = omg.GetImgApp()->CmdInterpreter();
198
199string kw,usage;
200// ------------- Help group FFT
201string hgrp = "FFT";
202
203kw = "fftforw";
204usage = "FFT on a vector -> computes forward Fourier transform";
205usage += "\n Usage: fftforw vecSig vecSpec [graphic_att] ";
206usage += "\n vecSpec = FFTForward(vecSig) ";
207usage += "\n vecSig: Input data vector of type r_8 or complex<r_8> ";
208usage += "\n vecSpec: Output data vector of type complex<r_8> ";
209usage += "\n See also : fftback fftfilter fftfuncfilter ";
210mpiac->RegisterCommand(kw, usage, this, hgrp);
211
212kw = "fftback";
213usage = "FFT on a vector -> computes backward Fourier transform ";
214usage += "\n Usage: fftback vecSpec vecSig [graphic_att] [C/Z] ";
215usage += "\n vecSig = FFTBackward(vecSpec) ";
216usage += "\n vecSpec: Input data vector of type complex<r_8> ";
217usage += "\n vecSig: Output Data vector of type r_8 (default) ";
218usage += "\n or complex<r_8> if C/Z specified or ";
219usage += "\n vecSpec computed by fftforw on a complex vector";
220usage += "\n See also : fftforw fftfilter fftfuncfilter ";
221mpiac->RegisterCommand(kw, usage, this, hgrp);
222
223kw = "fftfilter";
224usage = "Filter (multiply) vecSpec (vector complex<r_8>) by Filter (vector<r_8>)";
225usage += "\n Usage: fftfilter vecSpec FilterVec vecFiltSpec [graphic_att] ";
226usage += "\n vecFiltSpec(i) = vecSpec(i) * complex(FilterVec(i),0) ";
227usage += "\n See also : fftforw fftbackw fftfuncfilter ";
228mpiac->RegisterCommand(kw, usage, this, hgrp);
229
230kw = "fftfuncfilter";
231usage = "Filter (multiply) vecSpec (vector complex<r_8>) by FilterFunc(i) ";
232usage += "\n Usage: fftfilter vecSpec FilterFunc vecFiltSpec [graphic_att] ";
233usage += "\n vecFiltSpec(i) = vecSpec(i) * complex(FilterFunc(i),0) ";
234usage += "\n See also : fftforw fftbackw fftfilter ";
235mpiac->RegisterCommand(kw, usage, this, hgrp);
236
237//--------------------------
238hgrp = "SophyaCmd";
239
240kw = "mollgridsph";
241usage = "Creates a spherical coordinate grid in Molleweide projection ";
242usage += "\n Usage: mollgridsph NameSphericalMap [Nb_Parallel Nb_Meridien graphic_att] ";
243mpiac->RegisterCommand(kw, usage, this, hgrp);
244
245kw = "mollgrid";
246usage = "Creates a spherical coordinate grid in Molleweide projection ";
247usage += "\n Usage: mollgrid [Nb_Parallel Nb_Meridien graphic_att] ";
248mpiac->RegisterCommand(kw, usage, this, hgrp);
249
250kw = "setprjmoldefval";
251usage = "Set default value for Molleweide projection of spherical maps (outside maps)";
252usage += "\n Usage: setprjmoldefval OutOfMapValue ";
253mpiac->RegisterCommand(kw, usage, this, hgrp);
254
255}
256
257/* --Methode-- */
258sopiamoduleExecutor::~sopiamoduleExecutor()
259{
260}
261
262/* --Methode-- */
263int sopiamoduleExecutor::Execute(string& kw, vector<string>& tokens, string& toks)
264{
265
266if (kw == "powerspec" || kw == "fftforw") {
267 if (tokens.size() < 2) {
268 cout << "Usage: fftforw vecSig vecSpec [graphic_att] " << endl;
269 return(0);
270 }
271 if (tokens.size() < 3) tokens.push_back((string)"");
272 SophyaFFT_forw(tokens[0], tokens[1], tokens[2]);
273}
274else if (kw == "fftback") {
275 if (tokens.size() < 2) {
276 cout << "Usage: fftback vecSpec vecSig [graphic_att] [C/Z]" << endl;
277 return(0);
278 }
279 bool forcez8 = false;
280 if (tokens.size() > 3) {
281 char fcz8 = tolower(tokens[3][0]);
282 if ( (fcz8 == 'c') || (fcz8 == 'z') ) forcez8 = true;
283 }
284 if (tokens.size() < 3) tokens.push_back((string)"");
285 SophyaFFT_back(tokens[0], tokens[1], tokens[2], forcez8);
286}
287else if ( (kw == "fftfilter") || (kw == "fftfuncfilter") ) {
288 if (tokens.size() < 3) {
289 cout << "Usage: fftfilter/fftfuncfilter vecSpec Filter vecFiltSpec [graphic_att]" << endl;
290 return(0);
291 }
292 if (tokens.size() < 4) tokens.push_back((string)"");
293 bool fgvec = false;
294 if (kw == "fftfilter") fgvec = true;
295 SophyaFFT_filter(tokens[0], tokens[1], tokens[2], tokens[3], fgvec);
296}
297
298else if (kw == "prjmoldefval") {
299 if (tokens.size() < 1) {
300 cout << "Usage: prjmoldefval OutOfMapValue" << endl;
301 return(0);
302 }
303 Set_Project_Mol_DefVal(atof(tokens[0].c_str()) );
304}
305
306else if ( (kw == "mollgridsph") || (kw == "mollgrid") ) {
307 NamedObjMgr omg;
308 MollweideGridDrawer * mollgrid = NULL;
309 string dopt="";
310 if (kw == "mollgridsph") {
311 if (tokens.size() < 1) {
312 cout << "Usage: mollgridsph NameSphericalMap [Nb_Parallel Nb_Meridien graphic_att]"
313 << endl;
314 return(0);
315 }
316 int np = 5;
317 int nm = 5;
318 dopt = "same,thinline";
319 if (tokens.size() > 1) np = atoi(tokens[1].c_str());
320 if (tokens.size() > 2) nm = atoi(tokens[2].c_str());
321 if (tokens.size() > 3) dopt = tokens[3];
322 NObjMgrAdapter* obja = omg.GetObjAdapter(tokens[0]);
323 if (obja == NULL) {
324 cout << "mollgridsph Error , No object with name " << tokens[0] << endl;
325 return(0);
326 }
327 P2DArrayAdapter* arr = obja->Get2DArray(dopt);
328 if (arr == NULL) {
329 cout << "mollgridsph Error , object has non 2DArrayAdapter - Not a spherical map "
330 << endl;
331 return(0);
332 }
333 int nx = arr->XSize();
334 int ny = arr->YSize();
335 delete arr;
336 mollgrid = new MollweideGridDrawer(np, nm, nx, ny);
337 cout << " mollgridsph: Creating MollweideGridDrawer() for object" << tokens[0] << endl;
338 }
339 else if (kw == "mollgrid") {
340 int np = 5;
341 int nm = 5;
342 if (tokens.size() > 0) np = atoi(tokens[0].c_str());
343 if (tokens.size() > 1) nm = atoi(tokens[1].c_str());
344 if (tokens.size() > 2) dopt = tokens[2];
345 mollgrid = new MollweideGridDrawer(np, nm);
346 cout << " mollgrid: Creating MollweideGridDrawer(" << np << "," << nm << ")" << endl;
347 }
348
349 if (mollgrid == NULL) {
350 cout << "mollgridsph Error/BUG !!! mollgrid = NULL " << endl;
351 return(0);
352 }
353
354 int wrsid = 0;
355 //bool fgsr = true;
356
357 string name = "mollgrid";
358 wrsid = omg.GetImgApp()->DispScDrawer(mollgrid, name, dopt);
359 }
360
361return(0);
362
363}
364
365static sopiamoduleExecutor * piaerex = NULL;
366/* Nouvelle-Fonction */
367void sopiamodule_init()
368{
369// Fonction d'initialisation du module
370// Appele par le gestionnaire de modules de piapp (PIACmd::LoadModule())
371if (piaerex) delete piaerex;
372piaerex = new sopiamoduleExecutor;
373}
374
375/* Nouvelle-Fonction */
376void sopiamodule_end()
377{
378// Desactivation du module
379if (piaerex) delete piaerex;
380piaerex = NULL;
381}
382
383
384/* Nouvelle-Fonction */
385void SophyaFFT_forw(string& nom, string& nomout, string dopt)
386{
387//Timer tm("powerspec");
388NamedObjMgr omg;
389AnyDataObj* obj=omg.GetObj(nom);
390if (obj == NULL) {
391 cout<<"SophyaFFT_forw() Error , Pas d'objet de nom "<<nom<<endl;
392 return;
393}
394
395TVector<r_8>* vin = dynamic_cast<TVector<r_8> *>(obj);
396TVector< complex<r_8> >* vinc = dynamic_cast<TVector< complex<r_8> >*>(obj);
397if (vin == NULL && vinc == NULL) {
398 cout<<"SophyaFFT_forw() Error , Objet n'est pas un vecteur r_8 ou complex<r_8>"<<endl;
399 return;
400}
401TVector< complex<r_8> > * vout = new TVector< complex<r_8> > ;
402FFTPackServer ffts;
403if(vin!=NULL) {
404 cout<<"SophyaFFT_forw() - Computing FFT of TVector<r_8> of size "<<vin->NElts()<< endl;
405 ffts.FFTForward(*vin, *vout);
406 cout<<"...Output TVector<complex<r_8>> of size: "<<vout->NElts()<<endl;
407 vout->Info()["FFTTYPE"]="F:R8->Z8";
408} else {
409 cout<<"SophyaFFT_forw() - Computing FFT of vector TVector<complex<r_8>> of size "<<vinc->NElts()<< endl;
410 ffts.FFTForward(*vinc, *vout);
411 cout<<"...Output TVector<complex<r_8>> of size: "<<vout->NElts()<<endl;
412 vout->Info()["FFTTYPE"] = "F:Z8->Z8";
413}
414// cout << " SophyaFFT - FFT done " << endl;
415 // tm.Split(" FFT done ");
416
417omg.AddObj(vout, nomout);
418omg.DisplayObj(nomout, dopt);
419return;
420
421}
422
423
424/* Nouvelle-Fonction */
425void SophyaFFT_back(string& nom, string& nomout, string dopt, bool forcez8)
426{
427 //Timer tm("powerspec");
428NamedObjMgr omg;
429AnyDataObj* obj=omg.GetObj(nom);
430if (obj == NULL) {
431 cout << "SophyaFFT_back() Error , Pas d'objet de nom " << nom << endl;
432 return;
433}
434
435TVector< complex<r_8> >* vinc = dynamic_cast<TVector< complex<r_8> >*>(obj);
436if (vinc == NULL) {
437 cout << "SophyaFFT_back() Error , Objet n'est pas un vecteur complex<r_8>" << endl;
438 return;
439 }
440
441TVector< r_8 > * vout = NULL;
442TVector< complex<r_8> > * voutc = NULL;
443
444if (forcez8 || (vinc->Info().GetS("FFTTYPE") == "F:Z8->Z8") ) {
445 voutc = new TVector< complex<r_8> >;
446}
447else {
448 vout = new TVector< r_8 >;
449}
450
451FFTPackServer ffts;
452cout << "SophyaFFT_back() - Computing Backward FFT of vector size " << vinc->NElts() << endl;
453if(vout!=NULL) {
454 ffts.FFTBackward(*vinc, *vout);
455 cout<<"...Output TVector<r_8> of size: "<<vout->NElts()<<endl;
456 vout->Info()["FFTTYPE"]="B:Z8->R8";
457 omg.AddObj(vout, nomout);
458} else {
459 ffts.FFTBackward(*vinc, *voutc);
460 cout<<"...Output TVector<complex<r_8>> of size: "<<voutc->NElts()<<endl;
461 voutc->Info()["FFTTYPE"]="B:Z8->Z8";
462 omg.AddObj(voutc, nomout);
463}
464
465omg.DisplayObj(nomout, dopt);
466return;
467}
468
469/* Nouvelle-Fonction */
470void SophyaFFT_crefilter(string& nomvec, string& nomfilter, string func, string dopt)
471{
472NamedObjMgr omg;
473AnyDataObj* obj=omg.GetObj(nomvec);
474if(obj == NULL) {
475 cout<<"SophyaFFT_crefilter() Error , Pas d'objet de nom "<<nomvec<<endl;
476 return;
477}
478
479TVector< complex<r_8> >* vfft = dynamic_cast<TVector< complex<r_8> >*>(obj);
480if(vfft == NULL) {
481 cout<<"SophyaFFT_crefilter() Error , Objet "<<nomvec<<" n'est pas un Tvector<complex<r_8>>"<< endl;
482 return;
483}
484sa_size_t n = vfft->Size();
485if(n<=0) return;
486
487cout<<"Creating filter "<<nomfilter<<"("<<n<<") from vec "<<nomvec<<" with fun "<<func<<endl;
488omg.GetServiceObj()->PlotFunc(func,nomfilter,0.,(double)n,n,dopt);
489}
490
491/* Nouvelle-Fonction */
492void SophyaFFT_filter(string& nom, string& filt, string& outvec, string dopt, bool fgvec)
493{
494NamedObjMgr omg;
495AnyDataObj* obj=omg.GetObj(nom);
496if(obj == NULL) {
497 cout<<"SophyaFFT_filter() Error , Pas d'objet de nom "<<nom<<endl;
498 return;
499}
500TVector< complex<r_8> >* vfft = dynamic_cast<TVector< complex<r_8> >*>(obj);
501if(vfft == NULL) {
502 cout<<"SophyaFFT_filter() Error , Objet "<<nom<<" n'est pas un Tvector<complex<r_8>>"<< endl;
503 return;
504}
505int n = vfft->Size();
506if(n<=0) {
507 cout<<"SophyaFFT_filter() Error , vector size = 0 " << endl;
508 return;
509}
510
511string nomfilter = "/autoc/fft_filter";
512if (fgvec) {
513 nomfilter = filt;
514 cout<<"SophyaFFT_filter(): using vector " << filt << " for filtering spectrum ..." << endl;
515}
516else {
517 cout<<"SophyaFFT_filter(): Creating filter vector "<<nomfilter
518 <<" from function " << filt << endl;
519 string fcrdopt = "nodisp";
520 omg.GetServiceObj()->PlotFunc(filt, nomfilter, 0., (double)n, n, fcrdopt);
521}
522AnyDataObj* objfilter=omg.GetObj(nomfilter);
523if(objfilter == NULL) {
524 cout<<"SophyaFFT_filter() Error , Pas d'objet de nom "<<nomfilter<<endl;
525 return;
526}
527
528TVector<r_8>* vfilter = dynamic_cast<TVector<r_8>*>(objfilter);
529if(vfft == NULL) {
530 cout<<"SophyaFFT_filter() Error , Objet "<<nomfilter<<" n'est pas un Tvector<r_8>"<<endl;
531 return;
532}
533
534n = (vfilter->Size()<vfft->Size()) ? vfilter->Size(): vfft->Size();
535
536cout<<" SophyaFFT_filter(): filtering from 0 to "<<n<<endl;
537TVector< complex<r_8> > * filtfft = new TVector< complex<r_8> >(*vfft, false);
538for(int i=0;i<n;i++) (*filtfft)(i) *= (*vfilter)(i);
539
540cout<<"SophyaFFT_filter(): Adding obj: " << outvec << endl;
541omg.AddObj(filtfft, outvec);
542omg.DisplayObj(outvec, dopt);
543return;
544}
Note: See TracBrowser for help on using the repository browser.