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

Last change on this file since 2162 was 1972, checked in by ansari, 23 years ago

Ajout flag -hidezswin aux arguments d'appel de piapp - Reza 30/4/2002

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