COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
DEM library function calls
Collaboration diagram for DEM library function calls:

Functions

map_dem_bodyplanet_dem (int body)
 Body for DEM. More...
 
void map_dem_close (map_dem_body *body)
 
void map_dem_cache (int body, int num)
 
map_dem_bodymap_dem_open (int body)
 
void map_dem_scale (map_dem_body *body, double vscale, double hscale)
 
int map_dem_tilt (int body, double lon, double lat, double scale, dem_pixel *pixel)
 
dem_pixel map_dem_pixel (int body, double lon, double lat, double res)
 Height in DEM. More...
 
double map_dem_alt (int body, double lon, double lat, double res)
 
int map_dem_init ()
 

Detailed Description

Function Documentation

map_dem_body* planet_dem ( int  body)

Body for DEM.

Provides the basic information used by the DEM engine for the requested planetary body.

Parameters
bodyInteger value of planetary body. See Coordinate conversion constants for values.
Returns
map_dem_body for the requested planetary body.
58 {
59  if (bodies[body-1] == NULL)
60  {
61  if ((bodies[body-1] = map_dem_open(body)) == NULL) { return nullptr; }
62  }
63  return (bodies[body-1]);
64 }
map_dem_body * map_dem_open(int bodynum)
Definition: demlib.cpp:149
map_dem_body * bodies[20]
Definition: demlib.cpp:42
void map_dem_close ( map_dem_body body)
void map_dem_cache ( int  body,
int  num 
)
100 {
101  int indexl, count;
102  uint32_t i, dalloc;
103  double utcl;
104 
105  if (num > 10)
106  num = 10;
107 // sem_wait(bsem);
108  bsem.lock();
109  if (bodies[body-1] == NULL)
110  {
111 // sem_post(bsem);
112  bsem.unlock();
113  return;
114  }
115 
116  do
117  {
118  count = 0;
119  utcl = currentmjd(0.);
120  indexl = -1;
121  for (i=0; i<bodies[body-1]->demcount; i++)
122  {
123  if (bodies[body-1]->dems[i].pixel.size())
124  {
125  count++;
126  if (bodies[body-1]->dems[i].utc <= utcl)
127  {
128  indexl = i;
129  utcl = bodies[body-1]->dems[i].utc;
130  }
131  }
132  }
133  if (count > num)
134  {
135  bodies[body-1]->dems[indexl].utc = 0.;
136  bodies[body-1]->dems[indexl].pixel.clear();
137  dalloc = bodies[body-1]->dems[indexl].ycount * (bodies[body-1]->dems[indexl].xcount*sizeof(dem_pixel) + sizeof(dem_pixel *));
138  if (totalloc >= dalloc) {
139  totalloc -= dalloc;
140  } else {
141  totalloc = 0;
142  }
143  }
144  } while (count > num);
145 // sem_post(bsem);
146  bsem.unlock();
147 }
vector< vector< dem_pixel > > pixel
Definition: demlib.h:105
int i
Definition: rw_test.cpp:37
int count
Definition: rw_test.cpp:36
uint16_t demcount
Definition: demlib.h:123
double utc
Definition: demlib.h:93
static uint32_t totalloc
Definition: demlib.cpp:44
uint32_t xcount
Definition: demlib.h:101
map_dem_body * bodies[20]
Definition: demlib.cpp:42
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
uint32_t ycount
Definition: demlib.h:102
static std::mutex bsem
Definition: demlib.cpp:46
Location value.
Definition: demlib.h:82
map_dem_dem dems[30000]
Definition: demlib.h:124
map_dem_body* map_dem_open ( int  body)
150 {
151  int maxcount=0;
152  // int maxir, maxic;
153  char ttname[200];
154  string tname;
155  FILE *fp, *fp1;
156  map_dem_body *body;
157  int iretn, iretn1, ir, ic, irmin, irmax, icmin, icmax;
158  uint16_t demtype, dc;
159 
160  if (bodies[bodynum-1] != NULL)
161  return (bodies[bodynum-1]);
162 
163  if (!running)
164  {
165  if ((iretn=map_dem_init()) < 0)
166  {
167  errno = -iretn;
168  return nullptr;
169  }
170  }
171 
172  // running: 0 = uninitialized, 1 = ready, 2 = insufficient memory
173  if (running == 2)
174  {
176  return nullptr;
177  }
178 
179  iretn = get_cosmosresources(tname);
180  if (iretn < 0)
181  {
182  errno = -iretn;
183  return nullptr;
184  }
185  tname += "/mapping/";
186  tname += bodynames[bodynum-1];
187  tname += "/body.dat";
188  if ((fp=fopen(tname.c_str(),"r"))==NULL)
189  {
190  errno = -iretn;
191  return nullptr;
192  }
193  body = (map_dem_body *)calloc(1,sizeof(map_dem_body));
194  if (body == NULL)
195  {
197  return nullptr;
198  }
199 
200 
201  iretn = fscanf(fp,"%lf %lf %lf %lf %lf %lf",&body->orbit,&body->radius,&body->highest, &body->alt1, &body->alt2, &body->alt3);
202  if (iretn == 3)
203  {
204  body->alt1 = body->alt2 = body->alt3 = 0.;
205  }
206  body->highest = body->radius + body->highest/1000.;
207  fclose(fp);
208  body->name = bodynames[bodynum-1];
209  body->vscale = body->hscale = body->htov = 1.;
210 
211  iretn = get_cosmosresources(tname);
212  if (iretn < 0)
213  {
214  errno = -iretn;
215  return nullptr;
216  }
217  tname += "/mapping/";
218  tname += bodynames[bodynum-1];
219  tname += "/dems5.dat";
220  if ((fp=fopen(tname.c_str(),"r"))==NULL)
221  {
222  errno = -DEM_ERROR_NOTFOUND;
223  return nullptr;
224  }
225 
226  dc = 0;
227  while ((iretn=fscanf(fp,"%s %lf %lf %u %u %lf %hu",body->dems[dc].name,&body->dems[dc].lonul,&body->dems[dc].latul,&body->dems[dc].xcount,&body->dems[dc].ycount,&body->dems[dc].psize,&demtype)) != EOF)
228  {
229  body->dems[dc].psize = RADOF(body->dems[dc].psize);
230  body->dems[dc].pixel.clear();
231  switch (demtype)
232  {
233  case DEM_TYPE_SINGLE:
234  body->dems[dc].lonul = RADOF(body->dems[dc].lonul)-body->dems[dc].psize/2.;
235  body->dems[dc].lonlr = body->dems[dc].lonul + (body->dems[dc].xcount) * body->dems[dc].psize;
236  body->dems[dc].latul = RADOF(body->dems[dc].latul)+body->dems[dc].psize/2.;
237  body->dems[dc].latlr = body->dems[dc].latul - (body->dems[dc].ycount) * body->dems[dc].psize;
238  dc++;
239  break;
240  case DEM_TYPE_MULTI:
241  string tname;
242  iretn = get_cosmosresources(tname);
243  if (iretn < 0)
244  {
245  errno = -iretn;
246  return nullptr;
247  }
248  tname += "/mapping/";
249  tname += bodynames[bodynum-1];
250  tname += "/";
251  tname += body->dems[dc].name;
252  tname += "/dems5.dat";
253  if ((fp1=fopen(tname.c_str(),"r"))==NULL)
254  {
255  errno = -DEM_ERROR_NOTFOUND;
256  return nullptr;
257  }
258 
259 // strcpy(tname,body->dems[dc].name);
260  tname = body->dems[dc].name;
261 
262  while ((iretn1=fscanf(fp1,"%s %lf %lf %u %u %lf %hu",ttname,&body->dems[dc].lonul,&body->dems[dc].latul,&body->dems[dc].xcount,&body->dems[dc].ycount,&body->dems[dc].psize,&demtype)) != EOF)
263  {
264  sprintf(body->dems[dc].name,"%s/%s",tname.c_str(),ttname);
265  body->dems[dc].psize = RADOF(body->dems[dc].psize);
266  body->dems[dc].pixel.clear();
267  body->dems[dc].lonul = RADOF(body->dems[dc].lonul)-body->dems[dc].psize/2.;
268  body->dems[dc].lonlr = body->dems[dc].lonul + (body->dems[dc].xcount) * body->dems[dc].psize;
269  body->dems[dc].latul = RADOF(body->dems[dc].latul)+body->dems[dc].psize/2.;
270  body->dems[dc].latlr = body->dems[dc].latul - (body->dems[dc].ycount) * body->dems[dc].psize;
271  dc++;
272  }
273  fclose(fp1);
274  break;
275  }
276  }
277  fclose(fp);
278 
279  // Now populate demindex matrix
280 
281  for (uint16_t i=0; i<dc; i++)
282  {
283  irmin = (int)(200.*(DPI2+body->dems[i].latlr)/DPI);
284  if (irmin < 0)
285  irmin = 0;
286  irmax = (int)(200.*(DPI2+body->dems[i].latul)/DPI);
287  if (irmax > 199)
288  irmax = 199;
289  for (ir=irmin; ir<=irmax; ir++)
290  {
291  icmin = (int)(200.*(DPI+body->dems[i].lonul)/DPI);
292  if (icmin < 0)
293  icmin = 0;
294  icmax = (int)(200.*(DPI+body->dems[i].lonlr)/DPI);
295  if (icmax > 399)
296  icmax = 399;
297  for (ic=icmin; ic<=icmax; ic++)
298  {
299  uint16_t j;
300  for (j=0; j<body->demindexc[ir][ic]; j++)
301  {
302  if (body->demindexi[ir][ic][j] == i) { break; }
303  }
304  if (j == body->demindexc[ir][ic] && body->demindexc[ir][ic] < MAX_DEMINDEX)
305  {
306  body->demindexi[ir][ic][body->demindexc[ir][ic]] = i;
307  body->demindexc[ir][ic]++;
308  }
309  if (body->demindexc[ir][ic] > maxcount) { maxcount = body->demindexc[ir][ic]; }
310  }
311  }
312  }
313 
314  body->demcount = dc;
315  bodies[bodynum-1] = body;
316  return (body);
317 }
double alt3
Definition: demlib.h:122
vector< vector< dem_pixel > > pixel
Definition: demlib.h:105
#define MAX_DEMINDEX
Definition: demlib.h:70
int map_dem_init()
Definition: demlib.cpp:66
double radius
Definition: demlib.h:115
double hscale
Definition: demlib.h:118
int i
Definition: rw_test.cpp:37
int iretn
Definition: rw_test.cpp:37
double latul
Definition: demlib.h:96
double alt1
Definition: demlib.h:120
#define DEM_ERROR_INSUFFICIENT_MEMORY
Definition: cosmos-errno.h:136
uint16_t demcount
Definition: demlib.h:123
const double DPI2
Double precision PI/2.
Definition: math/constants.h:18
double lonul
Definition: demlib.h:95
string get_cosmosresources(bool create_flag)
Return COSMOS Resources Directory.
Definition: datalib.cpp:1337
Planetary body support structure.
Definition: demlib.h:111
#define DEM_TYPE_MULTI
Definition: demlib.h:67
uint16_t demindexc[200][400]
Definition: demlib.h:126
char bodynames[20][15]
Definition: demlib.cpp:43
double lonlr
Definition: demlib.h:97
static int running
Definition: demlib.cpp:48
uint32_t xcount
Definition: demlib.h:101
double psize
Definition: demlib.h:99
#define DEM_ERROR_NOTFOUND
Definition: cosmos-errno.h:137
map_dem_body * bodies[20]
Definition: demlib.cpp:42
double highest
Definition: demlib.h:116
double orbit
Definition: demlib.h:114
uint32_t ycount
Definition: demlib.h:102
double htov
Definition: demlib.h:119
double vscale
Definition: demlib.h:117
FILE * fp
Definition: rw_test.cpp:38
double latlr
Definition: demlib.h:98
const double DPI
Double precision PI.
Definition: math/constants.h:14
string name
Definition: demlib.h:113
double alt2
Definition: demlib.h:121
map_dem_dem dems[30000]
Definition: demlib.h:124
char name[50]
Definition: demlib.h:94
int16_t demindexi[200][400][100]
Definition: demlib.h:125
#define DEM_TYPE_SINGLE
Definition: demlib.h:66
#define RADOF(deg)
Radians of a Degree value.
Definition: math/constants.h:29
void map_dem_scale ( map_dem_body body,
double  vscale,
double  hscale 
)
320 {
321  body->vscale = vscale;
322  body->hscale = hscale;
323  body->highest = body->radius + vscale * (body->highest - body->radius);
324 }
double radius
Definition: demlib.h:115
double hscale
Definition: demlib.h:118
double highest
Definition: demlib.h:116
double vscale
Definition: demlib.h:117
int map_dem_tilt ( int  body,
double  lon,
double  lat,
double  scale,
dem_pixel pixel 
)
507 {
508  double tiltrho;
509 
510  if (lat<-DPI2 || lat>DPI || lon<-DPI || lon>DPI || scalekm <=0. || scalekm >1000.)
511  return (-1);
512 
513  tiltrho = scalekm/bodies[body-1]->radius;
514  *pixel = map_dem_pixel(body,lon,lat,tiltrho);
515  bodies[body-1]->htov = (bodies[body-1]->hscale/bodies[body-1]->vscale);
516  return 0;
517 }
double radius
Definition: demlib.h:115
double hscale
Definition: demlib.h:118
dem_pixel map_dem_pixel(int body, double lon, double lat, double res)
Height in DEM.
Definition: demlib.cpp:342
map_dem_body * bodies[20]
Definition: demlib.cpp:42
double htov
Definition: demlib.h:119
double vscale
Definition: demlib.h:117
const double DPI
Double precision PI.
Definition: math/constants.h:14
dem_pixel map_dem_pixel ( int  body,
double  lon,
double  lat,
double  res 
)

Height in DEM.

If the Lat:Lon is within one of the provided DEM's, return the data for that pixel.

Parameters
bodyInteger value of planetary body. See Coordinate conversion constants for values.
lonLongitude in readians.
latLatitude in radians.
resBest resolution required, in radians.
Returns
dem_pixel for that location.
343 {
344  dem_pixel pixel={0., {0., 0., 1.}};
345  double decrow, deccol, cutc;
346  int32_t erow, ecol, dci, cidx, cbdy;
347  uint32_t drow, dcol;
348  uint32_t i, j, dsize, m;
349  map_dem_dem *sdem;
350  FILE *fp;
351 
352  // running: 0 = uninitialized, 1 = ready, 2 = insufficient memory
353  if (running == 2)
354  return (pixel);
355 
356  if (bodies[body-1] == NULL)
357  {
358  if (map_dem_open(body) == NULL) return (pixel);
359  }
360 
361  if (std::isnan(lat) || std::isnan(lon) || lat<-DPI2 || lat>DPI || lon<-DPI || lon>DPI)
362  return (pixel);
363 
364  if (bodies[body-1] == NULL)
365  return (pixel);
366 
367  sdem = NULL;
368 
369  // First: Find which DEM we need
370 
371  ecol = (int32_t)(400.*(lon + DPI)/D2PI);
372  if (ecol < 0)
373  ecol = 0;
374  if (ecol > 399)
375  ecol = 399;
376 
377  erow = (int32_t)(200.*(lat + DPI2)/DPI);
378  if (erow < 0)
379  erow = 0;
380  if (erow > 199)
381  erow = 199;
382 
383  dci = -1;
384  // Search the appropriate demindex for a DEM that fits our needs.
385  for (i=0; i<bodies[body-1]->demindexc[erow][ecol]; i++)
386  {
387  j = bodies[body-1]->demindexi[erow][ecol][i];
388  if (lon >= bodies[body-1]->dems[j].lonul-1e-13 && lon <= bodies[body-1]->dems[j].lonlr+1e-13 && lat >=
389  bodies[body-1]->dems[j].latlr-1e-13 && lat <= bodies[body-1]->dems[j].latul+1e-13)
390  {
391  if (dci < 0)
392  dci = j;
393  if (res/bodies[body-1]->dems[j].psize >= 2.)
394  break;
395  dci = j;
396  }
397  }
398 
399  // Check whether the DEM is already loaded and load if necessary
400 // sem_wait(bsem);
401  bsem.lock();
402  sdem = &bodies[body-1]->dems[dci];
403 // if (sdem->pixel == NULL)
404  if (sdem->pixel.size() == 0)
405  {
406  dsize = sdem->ycount*(sizeof(dem_pixel *) + sdem->xcount*sizeof(dem_pixel));
407  while (totalloc + dsize > maxalloc)
408  {
409  cbdy = cidx = -1;
410  cutc = currentmjd(0.);
411  for (m=0; m<MAX_DEM_BODIES; m++)
412  {
413  if (bodies[m])
414  {
415  for (i=0; i<bodies[m]->demcount; i++)
416  {
417 // if (bodies[m]->dems[i].pixel)
418  if (bodies[m]->dems[i].pixel.size())
419  {
420  if (bodies[m]->dems[i].utc < cutc)
421  {
422  cidx = i;
423  cbdy = m;
424  cutc = bodies[m]->dems[i].utc;
425  }
426  }
427  }
428  if (cidx >= 0)
429  {
430  bodies[cbdy]->dems[cidx].pixel.clear();
431  totalloc -= bodies[cbdy]->dems[cidx].ycount*(sizeof(dem_pixel *) + bodies[cbdy]->dems[cidx].xcount*sizeof(dem_pixel));
432  bodies[cbdy]->dems[cidx].utc = 0.;
433  if (totalloc + dsize <= maxalloc)
434  break;
435  }
436  }
437  }
438  }
439  string fname;
440  int32_t iretn = get_cosmosresources(fname);
441  if (iretn < 0)
442  {
443  errno = -iretn;
444  return pixel;
445  }
446  fname += "/mapping/";
447  fname += bodies[body-1]->name;
448  fname += "/";
449  fname += sdem->name;
450  fp = fopen(fname.c_str(),"rb");
451  sdem->pixel.resize(sdem->ycount);
452  if (sdem->pixel.size() != sdem->ycount)
453  {
454  maxalloc = (uint32_t)(.9 * maxalloc);
455  return (pixel);
456  }
457  for (i=0; i<sdem->ycount; i++)
458  {
459  sdem->pixel[i].resize(sdem->xcount);
460  if (sdem->pixel[i].size() != sdem->xcount)
461  {
462  sdem->pixel.clear();
463  maxalloc = (uint32_t)(.9 * maxalloc);
464  return (pixel);
465  }
466  size_t count = fread(sdem->pixel[i].data(),sizeof(dem_pixel),sdem->xcount,fp);
467  if (!count)
468  {
469  break;
470  }
471  }
472  sdem->utc = currentmjd(0.);
473  totalloc += dsize;
474  if (maxalloc < totalloc + 2 * dsize)
475  {
476  maxalloc += 2 * dsize;
477 
478  }
479  fclose(fp);
480  }
481 
482  decrow = ((sdem->latul - lat) /sdem->psize) - .5;
483  if (decrow < 0.)
484  drow = 0;
485  else
486  drow = (uint32_t)decrow;
487  if (drow >= sdem->ycount)
488  drow = sdem->ycount - 1;
489  deccol = ((lon - sdem->lonul) /sdem->psize) + .5;
490  if (deccol < 0.)
491  dcol = 0;
492  else
493  dcol = (int32_t)deccol;
494  if (dcol >= sdem->xcount)
495  dcol = sdem->xcount - 1;
496 
497  pixel = sdem->pixel[drow][dcol];
498 // sem_post(bsem);
499  bsem.unlock();
500  pixel.alt = (float)(pixel.alt * bodies[body-1]->vscale);
501  pixel.nmap[2] = (float)(pixel.nmap[2] * bodies[body-1]->htov);
502 
503  return (pixel);
504 }
vector< vector< dem_pixel > > pixel
Definition: demlib.h:105
float nmap[3]
Definition: demlib.h:85
Definition: eci2kep_test.cpp:33
int i
Definition: rw_test.cpp:37
int count
Definition: rw_test.cpp:36
int iretn
Definition: rw_test.cpp:37
double latul
Definition: demlib.h:96
map_dem_body * map_dem_open(int bodynum)
Definition: demlib.cpp:149
uint16_t demcount
Definition: demlib.h:123
const double DPI2
Double precision PI/2.
Definition: math/constants.h:18
float alt
Definition: demlib.h:84
double utc
Definition: demlib.h:93
double lonul
Definition: demlib.h:95
string get_cosmosresources(bool create_flag)
Return COSMOS Resources Directory.
Definition: datalib.cpp:1337
static uint32_t maxalloc
Definition: demlib.cpp:44
DEM support structure.
Definition: demlib.h:91
static uint32_t totalloc
Definition: demlib.cpp:44
uint16_t demindexc[200][400]
Definition: demlib.h:126
static char fname[100]
Definition: geomag.cpp:89
static int running
Definition: demlib.cpp:48
uint32_t xcount
Definition: demlib.h:101
double psize
Definition: demlib.h:99
map_dem_body * bodies[20]
Definition: demlib.cpp:42
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
uint32_t ycount
Definition: demlib.h:102
static std::mutex bsem
Definition: demlib.cpp:46
const double D2PI
Double precision 2*PI.
Definition: math/constants.h:16
#define MAX_DEM_BODIES
Definition: demlib.h:71
double htov
Definition: demlib.h:119
double vscale
Definition: demlib.h:117
FILE * fp
Definition: rw_test.cpp:38
const double DPI
Double precision PI.
Definition: math/constants.h:14
Location value.
Definition: demlib.h:82
string name
Definition: demlib.h:113
map_dem_dem dems[30000]
Definition: demlib.h:124
char name[50]
Definition: demlib.h:94
int16_t demindexi[200][400][100]
Definition: demlib.h:125
double map_dem_alt ( int  body,
double  lon,
double  lat,
double  res 
)
327 {
328  dem_pixel pixel;
329  pixel = map_dem_pixel(body,lon,lat,res);
330  return (pixel.alt);
331 }
float alt
Definition: demlib.h:84
dem_pixel map_dem_pixel(int body, double lon, double lat, double res)
Height in DEM.
Definition: demlib.cpp:342
Location value.
Definition: demlib.h:82
int map_dem_init ( )
67 {
68  void *tptr;
69 
70  if (!running)
71  {
72 //#ifdef COSMOS_MAC_OS
73 // if ((bsem=sem_open("demcache",O_CREAT,O_RDWR,1)) != SEM_FAILED)
74 //#else
75 // bsem = &tsem;
76 // if ((sem_init(bsem,0,1)) != 0)
77 //#endif
78 // {
79 // return (DEM_ERROR_SEMINIT);
80 // }
81  }
82  // Determine reasonable maxalloc, and whether we should run at all
83  for (maxalloc=1U<<31; maxalloc>1U<<27; maxalloc=maxalloc>>1)
84  {
85  if ((tptr=malloc(maxalloc)) != NULL)
86  {
87  maxalloc = maxalloc>>4;
88  running = 1;
89  free(tptr);
90  return 0;
91  }
92  }
93  maxalloc = 0;
94  // running: 0 = uninitialized, 1 = ready, 2 = insufficient memory
95  running = 2;
97 }
#define DEM_ERROR_INSUFFICIENT_MEMORY
Definition: cosmos-errno.h:136
static uint32_t maxalloc
Definition: demlib.cpp:44
static int running
Definition: demlib.cpp:48