| [722] | 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 |  | 
|---|
| [1540] | 23 | #include "pidrawer.h" | 
|---|
|  | 24 | #include "nomgadapter.h" | 
|---|
|  | 25 |  | 
|---|
| [722] | 26 | extern "C" { | 
|---|
|  | 27 | void sopiamodule_init(); | 
|---|
|  | 28 | void sopiamodule_end(); | 
|---|
|  | 29 | } | 
|---|
|  | 30 |  | 
|---|
|  | 31 | void SophyaFFT(string& nom, string& nomout, string dopt); | 
|---|
|  | 32 |  | 
|---|
|  | 33 | class sopiamoduleExecutor : public CmdExecutor { | 
|---|
|  | 34 | public: | 
|---|
|  | 35 | sopiamoduleExecutor(); | 
|---|
|  | 36 | virtual       ~sopiamoduleExecutor(); | 
|---|
| [1267] | 37 | virtual int   Execute(string& keyw, vector<string>& args, string& toks); | 
|---|
| [722] | 38 | }; | 
|---|
|  | 39 |  | 
|---|
| [1540] | 40 | //  Classe pour trace de grille en projection spherique Mollweide | 
|---|
|  | 41 | class MollweideGridDrawer : public PIDrawer { | 
|---|
|  | 42 | public: | 
|---|
|  | 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 |  | 
|---|
|  | 50 | protected: | 
|---|
|  | 51 | double xlarg, x0; | 
|---|
|  | 52 | double ylarg, y0; | 
|---|
|  | 53 | int nbparall, nbmerid; | 
|---|
|  | 54 | int nbpt; | 
|---|
|  | 55 | bool fgrevtheta; | 
|---|
|  | 56 | }; | 
|---|
|  | 57 |  | 
|---|
| [722] | 58 | /* --Methode-- */ | 
|---|
| [1540] | 59 | MollweideGridDrawer::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-- */ | 
|---|
|  | 73 | MollweideGridDrawer::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-- */ | 
|---|
|  | 95 | MollweideGridDrawer::~MollweideGridDrawer() | 
|---|
|  | 96 | { | 
|---|
|  | 97 | } | 
|---|
|  | 98 |  | 
|---|
|  | 99 | /* --Methode-- */ | 
|---|
|  | 100 | void 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-- */ | 
|---|
|  | 180 | void MollweideGridDrawer::UpdateLimits() | 
|---|
|  | 181 | { | 
|---|
|  | 182 | SetLimits(x0-xlarg/2., x0+xlarg/2., y0-ylarg/2., y0+ylarg/2.); | 
|---|
|  | 183 | } | 
|---|
|  | 184 |  | 
|---|
|  | 185 | /* --Methode-- */ | 
|---|
| [722] | 186 | sopiamoduleExecutor::sopiamoduleExecutor() | 
|---|
|  | 187 | { | 
|---|
|  | 188 |  | 
|---|
|  | 189 | PIACmd * mpiac; | 
|---|
|  | 190 | NamedObjMgr omg; | 
|---|
|  | 191 | mpiac = omg.GetImgApp()->CmdInterpreter(); | 
|---|
|  | 192 |  | 
|---|
|  | 193 | string hgrp = "SophyaCmd"; | 
|---|
|  | 194 | string kw = "powerspec"; | 
|---|
|  | 195 | string usage = "FFT on a vector -> Plots power spectrum "; | 
|---|
|  | 196 | usage += "\n Usage: fftp vecName vecFFT [graphic_att] "; | 
|---|
|  | 197 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| [1540] | 198 | kw = "mollgridsph"; | 
|---|
|  | 199 | usage = "Creates a spherical coordinate grid in Molleweide projection "; | 
|---|
|  | 200 | usage += "\n Usage: mollgridsph NameSphericalMap [Nb_Parallel Nb_Meridien graphic_att] "; | 
|---|
|  | 201 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
|  | 202 | kw = "mollgrid"; | 
|---|
|  | 203 | usage = "Creates a spherical coordinate grid in Molleweide projection "; | 
|---|
|  | 204 | usage += "\n Usage: mollgrid [Nb_Parallel Nb_Meridien graphic_att] "; | 
|---|
|  | 205 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| [722] | 206 |  | 
|---|
|  | 207 | } | 
|---|
|  | 208 |  | 
|---|
|  | 209 | /* --Methode-- */ | 
|---|
|  | 210 | sopiamoduleExecutor::~sopiamoduleExecutor() | 
|---|
|  | 211 | { | 
|---|
|  | 212 | } | 
|---|
|  | 213 |  | 
|---|
|  | 214 | /* --Methode-- */ | 
|---|
| [1267] | 215 | int sopiamoduleExecutor::Execute(string& kw, vector<string>& tokens, string& toks) | 
|---|
| [722] | 216 | { | 
|---|
|  | 217 |  | 
|---|
|  | 218 | if (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 | } | 
|---|
| [1540] | 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 | } | 
|---|
| [722] | 268 |  | 
|---|
| [1540] | 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 | int opt = omg.GetServiceObj()->DecodeDispOption(dopt, fgsr); | 
|---|
|  | 277 |  | 
|---|
|  | 278 | string name = "mollgrid"; | 
|---|
|  | 279 | wrsid = omg.GetImgApp()->DispScDrawer(mollgrid, name, opt); | 
|---|
|  | 280 | if (fgsr) omg.GetImgApp()->RestoreGraphicAtt(); | 
|---|
|  | 281 | } | 
|---|
|  | 282 |  | 
|---|
| [722] | 283 | return(0); | 
|---|
|  | 284 |  | 
|---|
|  | 285 | } | 
|---|
|  | 286 |  | 
|---|
|  | 287 | static sopiamoduleExecutor * piaerex = NULL; | 
|---|
|  | 288 | /* Nouvelle-Fonction */ | 
|---|
|  | 289 | void sopiamodule_init() | 
|---|
|  | 290 | { | 
|---|
|  | 291 | // Fonction d'initialisation du module | 
|---|
|  | 292 | // Appele par le gestionnaire de modules de piapp (PIACmd::LoadModule()) | 
|---|
|  | 293 | if (piaerex) delete piaerex; | 
|---|
|  | 294 | piaerex = new sopiamoduleExecutor; | 
|---|
|  | 295 | } | 
|---|
|  | 296 |  | 
|---|
|  | 297 | /* Nouvelle-Fonction */ | 
|---|
|  | 298 | void sopiamodule_end() | 
|---|
|  | 299 | { | 
|---|
|  | 300 | // Desactivation du module | 
|---|
|  | 301 | if (piaerex) delete piaerex; | 
|---|
|  | 302 | piaerex = NULL; | 
|---|
|  | 303 | } | 
|---|
|  | 304 |  | 
|---|
|  | 305 |  | 
|---|
|  | 306 | /* --Methode-- */ | 
|---|
|  | 307 | void SophyaFFT(string& nom, string& nomout, string dopt) | 
|---|
|  | 308 | { | 
|---|
| [1481] | 309 | Timer tm("powerspec"); | 
|---|
| [722] | 310 | NamedObjMgr omg; | 
|---|
|  | 311 | AnyDataObj* obj=omg.GetObj(nom); | 
|---|
|  | 312 | if (obj == NULL) { | 
|---|
|  | 313 | cout << "SophyaFFT() Error , Pas d'objet de nom " << nom << endl; | 
|---|
|  | 314 | return; | 
|---|
|  | 315 | } | 
|---|
|  | 316 |  | 
|---|
|  | 317 | Vector* vin = dynamic_cast<Vector *>(obj); | 
|---|
|  | 318 | if (vin == NULL) { | 
|---|
|  | 319 | cout << "SophyaFFT() Error , Objet n'est pas un vecteur " <<  endl; | 
|---|
|  | 320 | return; | 
|---|
|  | 321 | } | 
|---|
| [1481] | 322 | TVector< complex<double> > * vout = new TVector< complex<double> >  ; | 
|---|
| [722] | 323 | FFTPackServer ffts; | 
|---|
| [1481] | 324 | cout << "SophyaFFT() - Computing FFT of vector size " << vin->NElts() << endl; | 
|---|
| [722] | 325 | ffts.FFTForward(*vin, *vout); | 
|---|
| [1481] | 326 | // cout << " SophyaFFT - FFT done " << endl; | 
|---|
|  | 327 | // tm.Split(" FFT done "); | 
|---|
| [722] | 328 |  | 
|---|
|  | 329 | omg.AddObj(vout, nomout); | 
|---|
|  | 330 | omg.DisplayObj(nomout, dopt); | 
|---|
|  | 331 | return; | 
|---|
|  | 332 |  | 
|---|
|  | 333 | } | 
|---|