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

Last change on this file since 2595 was 2595, checked in by ansari, 21 years ago

ajout commande prjmoldefval ds sopiamodule pour valeur par defaut projection molleweide - Reza , 9 Aout 2004

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