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

Last change on this file since 2920 was 2920, checked in by cmv, 20 years ago

crefilter,fftfilter,fftback for master env cmv 14/03/2006

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