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

Last change on this file since 2902 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 9.0 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(string& nom, string& nomout, string dopt);
34
35class sopiamoduleExecutor : public CmdExecutor {
36public:
37 sopiamoduleExecutor();
38 virtual ~sopiamoduleExecutor();
39 virtual int Execute(string& keyw, vector<string>& args, string& toks);
40};
41
42// Classe pour trace de grille en projection spherique Mollweide
43class MollweideGridDrawer : public PIDrawer {
44public:
45 MollweideGridDrawer(int np, int nm, int nx, int ny);
46 MollweideGridDrawer(int np, int nm, double x_larg=0., double x_0=0.,
47 double y_larg=0., double y_0=0.);
48 virtual ~MollweideGridDrawer();
49 virtual void Draw(PIGraphicUC* g, double xmin, double ymin, double xmax, double ymax);
50 virtual void UpdateLimits();
51
52protected:
53 double xlarg, x0;
54 double ylarg, y0;
55 int nbparall, nbmerid;
56 int nbpt;
57 bool fgrevtheta;
58};
59
60/* --Methode-- */
61MollweideGridDrawer::MollweideGridDrawer(int np, int nm, int nx, int ny)
62{
63 x0 = (double)nx/2.;
64 y0 = (double)ny/2.;
65 xlarg = nx;
66 ylarg = ny;
67 nbparall = np;
68 nbmerid = nm;
69 nbpt = ny/5;
70 if ((nbpt % 20) == 0) nbpt++;
71 fgrevtheta = true;
72}
73
74/* --Methode-- */
75MollweideGridDrawer::MollweideGridDrawer(int np, int nm, double x_larg, double x_0,
76 double y_larg, double y_0)
77{
78 if (x_larg > 0.) {
79 xlarg = x_larg; x0 = x_0;
80 }
81 else {
82 xlarg = M_PI*2.; x0 = M_PI;
83 }
84 if (y_larg > 0.) {
85 ylarg = y_larg; y0 = y_0;
86 }
87 else {
88 ylarg = M_PI; y0 = 0.;
89 }
90 nbparall = np;
91 nbmerid = nm;
92 nbpt = 101;
93 fgrevtheta = false;
94}
95
96/* --Methode-- */
97MollweideGridDrawer::~MollweideGridDrawer()
98{
99}
100
101/* --Methode-- */
102void MollweideGridDrawer::Draw(PIGraphicUC* g, double xmin, double ymin, double xmax, double ymax)
103{
104 PIGrCoord * xxl = new PIGrCoord[nbpt];
105 PIGrCoord * xxr = new PIGrCoord[nbpt];
106 PIGrCoord * yy = new PIGrCoord[nbpt];
107
108 char buff[64];
109
110 // Trace des paralleles
111 g->DrawLine(xmin, y0, xmax, y0);
112 g->DrawString(x0+xlarg/100, y0+ylarg/100, "0");
113
114 // PIGrCoord asc, PIGrCoord desc;
115 int i,k;
116
117 int nkp = (nbparall-1)/2;
118 if (nkp > 0) {
119 double dy = 0.5*ylarg/(double)(nkp+1);
120 double dtd = 90./(double)(nkp+1);
121 for(k=1; k<=nkp; k++) {
122 double fx = sin(acos(2.*(double)k*dy/ylarg))*0.5;
123 double yc = k*dy+y0;
124 g->DrawLine(x0-fx*xlarg, yc, x0+fx*xlarg, yc);
125 double ctd = (fgrevtheta) ? -dtd*k : dtd*k;
126 sprintf(buff, "%5.1f", ctd);
127 g->DrawString(x0, yc, buff);
128 g->DrawString(x0-0.3*xlarg, yc, buff);
129 g->DrawString(x0+0.25*xlarg, yc, buff);
130 yc = -k*dy+y0;
131 g->DrawLine(x0-fx*xlarg, yc, x0+fx*xlarg, yc);
132 sprintf(buff, "%5.1f", -ctd);
133 g->DrawString(x0, yc, buff);
134 g->DrawString(x0-0.3*xlarg, yc, buff);
135 g->DrawString(x0+0.25*xlarg, yc, buff);
136 }
137 }
138
139 // Trace des meridiennes
140 g->DrawLine(x0, ymin, x0, ymax);
141
142 int nkm = (nbmerid-1)/2;
143 if (nkm < 1) nkm = 1;
144 double dphi = M_PI/nkm;
145 double dpd = 180./(double)(nkm);
146 double dy = ylarg/(nbpt-1);
147 for(k=0; k<nkm; k++) {
148 double phi0 = k*dphi;
149 double dphimil = fabs(phi0-M_PI);
150 if (dphimil < 1.e-6) continue;
151 for(i=0; i<nbpt; i++) {
152 double yc = (double)i*dy+y0-0.5*ylarg;
153 double fx = (2.*fabs(yc-y0)/ylarg <= 1.) ?
154 sin(acos(2.*fabs(yc-y0)/ylarg)) * dphimil/M_PI * 0.5 : 0.;
155 yy[i] = yc;
156 xxl[i] = x0-fx*xlarg;
157 xxr[i] = x0+fx*xlarg;
158 }
159
160 g->DrawPolygon(xxl, yy, nbpt, false);
161 g->DrawPolygon(xxr, yy, nbpt, false);
162
163 if (k == 0) continue;
164 for(i=-1; i<=1; i++) {
165 double yc = 0.20*i*ylarg+y0;
166 double fx = sin(acos(2.*fabs(yc-y0)/ylarg)) * dphimil/M_PI * 0.5;
167 double xc = x0-fx*xlarg;
168 sprintf(buff, "%4.1f", dpd*k);
169 g->DrawString(xc, yc, buff);
170 xc = x0+fx*xlarg;
171 sprintf(buff, "%4.1f", 360.-dpd*k);
172 g->DrawString(xc, yc, buff);
173 }
174 }
175
176 delete [] xxl;
177 delete [] xxr;
178 delete [] yy;
179}
180
181/* --Methode-- */
182void MollweideGridDrawer::UpdateLimits()
183{
184 SetLimits(x0-xlarg/2., x0+xlarg/2., y0-ylarg/2., y0+ylarg/2.);
185}
186
187/* --Methode-- */
188sopiamoduleExecutor::sopiamoduleExecutor()
189{
190
191PIACmd * mpiac;
192NamedObjMgr omg;
193mpiac = omg.GetImgApp()->CmdInterpreter();
194
195string hgrp = "SophyaCmd";
196string kw = "powerspec";
197string usage = "FFT on a vector -> Plots power spectrum ";
198usage += "\n Usage: fftp vecName vecFFT [graphic_att] ";
199mpiac->RegisterCommand(kw, usage, this, hgrp);
200kw = "mollgridsph";
201usage = "Creates a spherical coordinate grid in Molleweide projection ";
202usage += "\n Usage: mollgridsph NameSphericalMap [Nb_Parallel Nb_Meridien graphic_att] ";
203mpiac->RegisterCommand(kw, usage, this, hgrp);
204kw = "mollgrid";
205usage = "Creates a spherical coordinate grid in Molleweide projection ";
206usage += "\n Usage: mollgrid [Nb_Parallel Nb_Meridien graphic_att] ";
207mpiac->RegisterCommand(kw, usage, this, hgrp);
208kw = "setprjmoldefval";
209usage = "Set default value for Molleweide projection of spherical maps (outside maps)";
210usage += "\n Usage: setprjmoldefval OutOfMapValue ";
211mpiac->RegisterCommand(kw, usage, this, hgrp);
212
213}
214
215/* --Methode-- */
216sopiamoduleExecutor::~sopiamoduleExecutor()
217{
218}
219
220/* --Methode-- */
221int sopiamoduleExecutor::Execute(string& kw, vector<string>& tokens, string& toks)
222{
223
224if (kw == "powerspec") {
225 if (tokens.size() < 2) {
226 cout << "Usage: powerspec nameVec vecFFT [graphic_att]" << endl;
227 return(0);
228 }
229 if (tokens.size() < 3) tokens.push_back((string)"n");
230 SophyaFFT(tokens[0], tokens[1], tokens[2]);
231 }
232else if (kw == "prjmoldefval") {
233 if (tokens.size() < 1) {
234 cout << "Usage: prjmoldefval OutOfMapValue" << endl;
235 return(0);
236 }
237 Set_Project_Mol_DefVal(atof(tokens[0].c_str()) );
238 }
239else if ( (kw == "mollgridsph") || (kw == "mollgrid") ) {
240 NamedObjMgr omg;
241 MollweideGridDrawer * mollgrid = NULL;
242 string dopt="";
243 if (kw == "mollgridsph") {
244 if (tokens.size() < 1) {
245 cout << "Usage: mollgridsph NameSphericalMap [Nb_Parallel Nb_Meridien graphic_att]"
246 << endl;
247 return(0);
248 }
249 int np = 5;
250 int nm = 5;
251 dopt = "same,thinline";
252 if (tokens.size() > 1) np = atoi(tokens[1].c_str());
253 if (tokens.size() > 2) nm = atoi(tokens[2].c_str());
254 if (tokens.size() > 3) dopt = tokens[3];
255 NObjMgrAdapter* obja = omg.GetObjAdapter(tokens[0]);
256 if (obja == NULL) {
257 cout << "mollgridsph Error , No object with name " << tokens[0] << endl;
258 return(0);
259 }
260 P2DArrayAdapter* arr = obja->Get2DArray(dopt);
261 if (arr == NULL) {
262 cout << "mollgridsph Error , object has non 2DArrayAdapter - Not a spherical map "
263 << endl;
264 return(0);
265 }
266 int nx = arr->XSize();
267 int ny = arr->YSize();
268 delete arr;
269 mollgrid = new MollweideGridDrawer(np, nm, nx, ny);
270 cout << " mollgridsph: Creating MollweideGridDrawer() for object" << tokens[0] << endl;
271 }
272 else if (kw == "mollgrid") {
273 int np = 5;
274 int nm = 5;
275 if (tokens.size() > 0) np = atoi(tokens[0].c_str());
276 if (tokens.size() > 1) nm = atoi(tokens[1].c_str());
277 if (tokens.size() > 2) dopt = tokens[2];
278 mollgrid = new MollweideGridDrawer(np, nm);
279 cout << " mollgrid: Creating MollweideGridDrawer(" << np << "," << nm << ")" << endl;
280 }
281
282 if (mollgrid == NULL) {
283 cout << "mollgridsph Error/BUG !!! mollgrid = NULL " << endl;
284 return(0);
285 }
286
287 int wrsid = 0;
288 //bool fgsr = true;
289
290 string name = "mollgrid";
291 wrsid = omg.GetImgApp()->DispScDrawer(mollgrid, name, dopt);
292 }
293
294return(0);
295
296}
297
298static sopiamoduleExecutor * piaerex = NULL;
299/* Nouvelle-Fonction */
300void sopiamodule_init()
301{
302// Fonction d'initialisation du module
303// Appele par le gestionnaire de modules de piapp (PIACmd::LoadModule())
304if (piaerex) delete piaerex;
305piaerex = new sopiamoduleExecutor;
306}
307
308/* Nouvelle-Fonction */
309void sopiamodule_end()
310{
311// Desactivation du module
312if (piaerex) delete piaerex;
313piaerex = NULL;
314}
315
316
317/* --Methode-- */
318void SophyaFFT(string& nom, string& nomout, string dopt)
319{
320Timer tm("powerspec");
321NamedObjMgr omg;
322AnyDataObj* obj=omg.GetObj(nom);
323if (obj == NULL) {
324 cout << "SophyaFFT() Error , Pas d'objet de nom " << nom << endl;
325 return;
326}
327
328Vector* vin = dynamic_cast<Vector *>(obj);
329if (vin == NULL) {
330 cout << "SophyaFFT() Error , Objet n'est pas un vecteur " << endl;
331 return;
332 }
333TVector< complex<double> > * vout = new TVector< complex<double> > ;
334FFTPackServer ffts;
335cout << "SophyaFFT() - Computing FFT of vector size " << vin->NElts() << endl;
336ffts.FFTForward(*vin, *vout);
337// cout << " SophyaFFT - FFT done " << endl;
338 // tm.Split(" FFT done ");
339
340omg.AddObj(vout, nomout);
341omg.DisplayObj(nomout, dopt);
342return;
343
344}
Note: See TracBrowser for help on using the repository browser.