This file is indexed.

/usr/share/gnudatalanguage/astrolib/tsc.pro is in gdl-astrolib 2018.02.16+dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
FUNCTION tsc,value,posx,nx,posy,ny,posz,nz, $
             AVERAGE=average,WRAPAROUND=wraparound,NO_MESSAGE=no_message, $
             ISOLATED=isolated
;+
; NAME:
;       TSC
;
; PURPOSE:
;       Interpolate an irregularly sampled field using a Triangular Shaped Cloud
;
; EXPLANATION:
;       This function interpolates an irregularly sampled field to a
;       regular grid using Triangular Shaped Cloud (nearest grid point
;       gets weight 0.75-dx^2, points before and after nearest grid
;       points get weight 0.5*(1.5-dx)^2, where dx is the distance
;       from the sample to the grid point in units of the cell size).
;
; CATEGORY:
;       Mathematical functions, Interpolation
;
; CALLING SEQUENCE:
;       Result = TSC, VALUE, POSX, NX[, POSY, NY, POSZ, NZ, 
;                     AVERAGE = average, WRAPAROUND =  wraparound,
;                     ISOLATED = isolated, NO_MESSAGE = no_message]
;
; INPUTS:
;       VALUE: Array of sample weights (field values). For e.g. a
;              temperature field this would be the temperature and the
;              keyword AVERAGE should be set. For e.g. a density field
;              this could be either the particle mass (AVERAGE should
;              not be set) or the density (AVERAGE should be set).
;       POSX:  Array of X coordinates of field samples, unit indices: [0,NX>.
;       NX:    Desired number of grid points in X-direction.
;       
; OPTIONAL INPUTS:
;      POSY: Array of Y coordinates of field samples, unit indices: [0,NY>.
;      NY:   Desired number of grid points in Y-direction.
;      POSZ: Array of Z coordinates of field samples, unit indices: [0,NZ>.
;      NZ:   Desired number of grid points in Z-direction.
;
; KEYWORD PARAMETERS:
;       AVERAGE:    Set this keyword if the nodes contain field samples
;                   (e.g. a temperature field). The value at each grid
;                   point will then be the weighted average of all the
;                   samples allocated to it. If this keyword is not
;                   set, the value at each grid point will be the
;                   weighted sum of all the nodes allocated to it
;                   (e.g. for a density field from a distribution of
;                   particles). (D=0). 
;       WRAPAROUND: Set this keyword if you want the first grid point
;                   to contain samples of both sides of the volume
;                   (see below).
;       ISOLATED:   Set this keyword if the data is isolated, i.e. not
;                   periodic. In that case total `mass' is not conserved.
;                   This keyword cannot be used in combination with the
;                   keyword WRAPAROUND.
;       NO_MESSAGE: Suppress informational messages.
;
; Example of default allocation of nearest grid points: n0=4, *=gridpoint.
;
;     0   1   2   3     Index of gridpoints
;     *   *   *   *     Grid points
;   |---|---|---|---|   Range allocated to gridpoints ([0.0,1.0> --> 0, etc.)
;   0   1   2   3   4   posx
;
; Example of ngp allocation for WRAPAROUND: n0=4, *=gridpoint.
;
;   0   1   2   3         Index of gridpoints
;   *   *   *   *         Grid points
; |---|---|---|---|--     Range allocated to gridpoints ([0.5,1.5> --> 1, etc.)
;   0   1   2   3   4=0   posx
;
;
; OUTPUTS:
;       Prints that a TSC interpolation is being performed of x
;       samples to y grid points, unless NO_MESSAGE is set.
;
; RESTRICTIONS:
;       Field data is assumed to be periodic with the sampled volume
;       the basic cell, unless ISOLATED is set.
;       All input arrays must have the same dimensions.
;       Position coordinates should be in `index units' of the
;       desired grid: POSX=[0,NX>, etc.
;       Keywords ISOLATED and WRAPAROUND cannot both be set.
;
; PROCEDURE:
;       Nearest grid point is determined for each sample.
;       TSC weights are computed for each sample.
;       Samples are interpolated to the grid.
;       Grid point values are computed (sum or average of samples).
;
; EXAMPLE:
;       nx=20
;       ny=10
;       posx=randomu(s,1000)
;       posy=randomu(s,1000)
;       value=posx^2+posy^2
;       field=tsc(value,posx*nx,nx,posy*ny,ny,/average)
;       surface,field,/lego
;
; NOTES:
;       Use csc.pro or ngp.pro for lower order interpolation schemes.    A 
;       standard reference for these interpolation methods is:   R.W. Hockney 
;       and J.W. Eastwood, Computer Simulations Using Particles (New York: 
;       McGraw-Hill, 1981).
;
; MODIFICATION HISTORY:
;       Written by Joop Schaye, Feb 1999.
;       Check for overflow for large dimensions  P. Riley/W. Landsman Dec. 1999
;-

nrsamples=n_elements(value)
nparams=n_params()
dim=(nparams-1)/2

IF dim LE 2 THEN BEGIN
    nz=1
    IF dim EQ 1 THEN ny=1
ENDIF
nxny=long(nx)*long(ny)


;---------------------
; Some error handling.
;---------------------

on_error,2  ; Return to caller if an error occurs.

IF NOT (nparams EQ 3 OR nparams EQ 5 OR nparams EQ 7) THEN BEGIN
    message,'Incorrect number of arguments!',/continue
    message,'Syntax: TSC, VALUE, POSX, NX[, POSY, NY, POSZ, NZ,' + $
      ' AVERAGE = average, WRAPAROUND =  wraparound]'
ENDIF 

IF (nrsamples NE n_elements(posx)) OR $
  (dim GE 2 AND nrsamples NE n_elements(posy)) OR $
  (dim EQ 3 AND nrsamples NE n_elements(posz)) THEN $
  message,'Input arrays must have the same dimensions!'

IF keyword_set(isolated) AND keyword_set(wraparound) THEN $
  message,'Keywords ISOLATED and WRAPAROUND cannot both be set!'

IF NOT keyword_set(no_message) THEN $
  print,'Interpolating ' + strtrim(string(nrsamples,format='(i10)'),1) $
  + ' samples to ' + strtrim(string(nxny*nz,format='(i10)'),1) + $
  ' grid points using TSC...'


;-----------------------
; Calculate TSC weights.
;-----------------------

; Compute weights per axis, in order to reduce memory (everything
; needs to be in memory if we compute all nearest grid points first).

;*************
; X-direction.
;*************

; Coordinates of nearest grid point (ngp).
IF keyword_set(wraparound) THEN ngx=fix(posx+0.5) $
ELSE ngx=fix(posx)+0.5

; Distance from sample to ngp.
dngx=ngx-posx

; Index of ngp.
IF keyword_set(wraparound) THEN kx2=temporary(ngx) $
ELSE kx2=temporary(ngx)-0.5
; Weight of ngp.
wx2=0.75-dngx*dngx

; Point before ngp.
kx1=kx2-1  ; Index.
dx=1.0-dngx  ; Distance to sample.
wx1=0.5*(1.5-temporary(dx))^2  ; TSC-weight.

; Point after ngp.
kx3=kx2+1  ; Index.
dx=1.0+temporary(dngx)  ; Distance to sample.
wx3=0.5*(1.5-temporary(dx))^2  ; TSC-weight.

; Periodic boundary conditions.
bad=where(kx2 EQ 0,count)
IF count NE 0 THEN BEGIN       ; Otherwise kx1=-1.
    kx1[bad]=nx-1
    IF keyword_set(isolated) THEN wx1[bad]=0.
ENDIF
bad=where(kx2 EQ nx-1,count)
IF count NE 0 THEN BEGIN       ; Otherwise kx3=nx.
    kx3[bad]=0
    IF keyword_set(isolated) THEN wx3[bad]=0.
ENDIF
IF keyword_set(wraparound) THEN BEGIN
    bad=where(kx2 EQ nx,count)
    IF count NE 0 THEN BEGIN
        kx2[bad]=0
        kx3[bad]=1
    ENDIF
ENDIF
bad=0  ; Free memory.


;*************
; Y-direction.
;*************

IF dim GE 2 THEN BEGIN 
    ; Coordinates of nearest grid point (ngp).
    IF keyword_set(wraparound) THEN ngy=fix(posy+0.5) $
    ELSE ngy=fix(posy)+0.5

    ; Distance from sample to ngp.
    dngy=ngy-posy

    ; Index of ngp.
    IF keyword_set(wraparound) THEN ky2=temporary(ngy) $
    ELSE ky2=temporary(ngy)-0.5
    ; Weight of ngp.
    wy2=0.75-dngy*dngy

    ; Point before ngp.
    ky1=ky2-1  ; Index.
    dy=1.0-dngy  ; Distance to sample.
    wy1=0.5*(1.5-temporary(dy))^2  ; TSC-weight.

    ; Point after ngp.
    ky3=ky2+1  ; Index.
    dy=1.0+temporary(dngy)  ; Distance to sample.
    wy3=0.5*(1.5-temporary(dy))^2  ; TSC-weight.

    ; Periodic boundary conditions.
    bad=where(ky2 EQ 0,count)
    IF count NE 0 THEN BEGIN       ; Otherwise ky1=-1.
        ky1[bad]=ny-1
        IF keyword_set(isolated) THEN wy1[bad]=0.
    ENDIF
    bad=where(ky2 EQ ny-1,count)
    IF count NE 0 THEN BEGIN       ; Otherwise ky3=ny.
        ky3[bad]=0
        IF keyword_set(isolated) THEN wy3[bad]=0.
    ENDIF
    IF keyword_set(wraparound) THEN BEGIN
        bad=where(ky2 EQ ny,count)
        IF count NE 0 THEN BEGIN
            ky2[bad]=0
            ky3[bad]=1
        ENDIF
    ENDIF
    bad=0  ; Free memory.
ENDIF ELSE BEGIN
    ky1=0
    ky2=0
    wy1=1
    wy2=1
ENDELSE


;*************
; Z-direction.
;*************

IF dim EQ 3 THEN BEGIN
    ; Coordinates of nearest grid point (ngp).
    IF keyword_set(wraparound) THEN ngz=fix(posz+0.5) $
    ELSE ngz=fix(posz)+0.5

    ; Distance from sample to ngp.
    dngz=ngz-posz

    ; Index of ngp.
    IF keyword_set(wraparound) THEN kz2=temporary(ngz) $
    ELSE kz2=temporary(ngz)-0.5
    ; Weight of ngp.
    wz2=0.75-dngz*dngz

    ; Point before ngp.
    kz1=kz2-1  ; Index.
    dz=1.0-dngz  ; Distance to sample.
    wz1=0.5*(1.5-temporary(dz))^2  ; TSC-weight.

    ; Point after ngp.
    kz3=kz2+1  ; Index.
    dz=1.0+temporary(dngz)  ; Distance to sample.
    wz3=0.5*(1.5-temporary(dz))^2  ; TSC-weight.

    ; Periodic boundary conditions.
    bad=where(kz2 EQ 0,count)
    IF count NE 0 THEN BEGIN       ; Otherwise kz1=-1.
        kz1[bad]=nz-1
        IF keyword_set(isolated) THEN wz1[bad]=0.
    ENDIF
    bad=where(kz2 EQ nz-1,count)
    IF count NE 0 THEN BEGIN       ; Otherwise kz3=nz.
        kz3[bad]=0
        IF keyword_set(isolated) THEN wz3[bad]=0.
    ENDIF
    IF keyword_set(wraparound) THEN BEGIN
        bad=where(kz2 EQ nz,count)
        IF count NE 0 THEN BEGIN
            kz2[bad]=0
            kz3[bad]=1
        ENDIF
    ENDIF
    bad=0  ; Free memory.
ENDIF ELSE BEGIN
    kz1=0
    kz2=0
    wz1=1
    wz2=1
ENDELSE
    

;-----------------------------
; Interpolate samples to grid.
;-----------------------------

field=fltarr(nx,ny,nz)
IF keyword_set(average) THEN tottscweight=fltarr(nx,ny,nz)

; tscweight adds up all tsc weights allocated to a grid point, we need
; to keep track of this in order to compute the temperature.
; Note that total(tscweight) is equal to nrsamples and that
; total(ifield)=n0^3 if sph.plot NE 'sph,temp' (not 1 because we use
; xpos=posx*n0 --> cube length different from EDFW paper).

index=kx1+ky1*nx+kz1*nxny
tscweight=wx1*wy1*wz1
IF keyword_set(average) THEN BEGIN
    FOR j=0l,nrsamples-1l DO BEGIN
        field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
    ENDFOR
ENDIF ELSE FOR j=0l,nrsamples-1l DO $
  field[index[j]]=field[index[j]]+tscweight[j]*value[j]
index=kx2+ky1*nx+kz1*nxny
tscweight=wx2*wy1*wz1
IF keyword_set(average) THEN BEGIN
    FOR j=0l,nrsamples-1l DO BEGIN
        field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
    ENDFOR
ENDIF ELSE FOR j=0l,nrsamples-1l DO $
  field[index[j]]=field[index[j]]+tscweight[j]*value[j]
index=kx3+ky1*nx+kz1*nxny
tscweight=wx3*wy1*wz1
IF keyword_set(average) THEN BEGIN
    FOR j=0l,nrsamples-1l DO BEGIN
        field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
    ENDFOR
ENDIF ELSE FOR j=0l,nrsamples-1l DO $
  field[index[j]]=field[index[j]]+tscweight[j]*value[j]

IF dim GE 2 THEN BEGIN
    index=kx1+ky2*nx+kz1*nxny
    tscweight=wx1*wy2*wz1
    IF keyword_set(average) THEN BEGIN
        FOR j=0l,nrsamples-1l DO BEGIN
            field[index[j]]=field[index[j]]+tscweight[j]*value[j]
            tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
        ENDFOR
    ENDIF ELSE FOR j=0l,nrsamples-1l DO $
      field[index[j]]=field[index[j]]+tscweight[j]*value[j]
    index=kx2+ky2*nx+kz1*nxny
    tscweight=wx2*wy2*wz1
    IF keyword_set(average) THEN BEGIN
        FOR j=0l,nrsamples-1l DO BEGIN
            field[index[j]]=field[index[j]]+tscweight[j]*value[j]
            tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
        ENDFOR
    ENDIF ELSE FOR j=0l,nrsamples-1l DO $
      field[index[j]]=field[index[j]]+tscweight[j]*value[j]
    index=kx3+ky2*nx+kz1*nxny
    tscweight=wx3*wy2*wz1
    IF keyword_set(average) THEN BEGIN
        FOR j=0l,nrsamples-1l DO BEGIN
            field[index[j]]=field[index[j]]+tscweight[j]*value[j]
            tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
        ENDFOR
    ENDIF ELSE FOR j=0l,nrsamples-1l DO $
      field[index[j]]=field[index[j]]+tscweight[j]*value[j]
    index=kx1+ky3*nx+kz1*nxny
    tscweight=wx1*wy3*wz1
    IF keyword_set(average) THEN BEGIN
        FOR j=0l,nrsamples-1l DO BEGIN
            field[index[j]]=field[index[j]]+tscweight[j]*value[j]
            tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
        ENDFOR
    ENDIF ELSE FOR j=0l,nrsamples-1l DO $
      field[index[j]]=field[index[j]]+tscweight[j]*value[j]
    index=kx2+ky3*nx+kz1*nxny
    tscweight=wx2*wy3*wz1
    IF keyword_set(average) THEN BEGIN
        FOR j=0l,nrsamples-1l DO BEGIN
            field[index[j]]=field[index[j]]+tscweight[j]*value[j]
            tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
        ENDFOR
    ENDIF ELSE FOR j=0l,nrsamples-1l DO $
      field[index[j]]=field[index[j]]+tscweight[j]*value[j]
    index=kx3+ky3*nx+kz1*nxny
    tscweight=wx3*wy3*wz1
    IF keyword_set(average) THEN BEGIN
        FOR j=0l,nrsamples-1l DO BEGIN
            field[index[j]]=field[index[j]]+tscweight[j]*value[j]
            tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
        ENDFOR
    ENDIF ELSE FOR j=0l,nrsamples-1l DO $
      field[index[j]]=field[index[j]]+tscweight[j]*value[j]

    IF dim EQ 3 THEN BEGIN
        index=kx1+ky1*nx+kz2*nxny
        tscweight=wx1*wy1*wz2
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx2+ky1*nx+kz2*nxny
        tscweight=wx2*wy1*wz2
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx3+ky1*nx+kz2*nxny
        tscweight=wx3*wy1*wz2
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx1+ky2*nx+kz2*nxny
        tscweight=wx1*wy2*wz2
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx2+ky2*nx+kz2*nxny
        tscweight=wx2*wy2*wz2
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx3+ky2*nx+kz2*nxny
        tscweight=wx3*wy2*wz2
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx1+ky3*nx+kz2*nxny
        tscweight=wx1*wy3*wz2
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx2+ky3*nx+kz2*nxny
        tscweight=wx2*wy3*wz2
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx3+ky3*nx+kz2*nxny
        tscweight=wx3*wy3*wz2
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx1+ky1*nx+kz3*nxny
        tscweight=wx1*wy1*wz3
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx2+ky1*nx+kz3*nxny
        tscweight=wx2*wy1*wz3
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx3+ky1*nx+kz3*nxny
        tscweight=wx3*wy1*wz3
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx1+ky2*nx+kz3*nxny
        tscweight=wx1*wy2*wz3
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx2+ky2*nx+kz3*nxny
        tscweight=wx2*wy2*wz3
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx3+ky2*nx+kz3*nxny
        tscweight=wx3*wy2*wz3
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx1+ky3*nx+kz3*nxny
        tscweight=wx1*wy3*wz3
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx2+ky3*nx+kz3*nxny
        tscweight=wx2*wy3*wz3
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
        index=kx3+ky3*nx+kz3*nxny
        tscweight=wx3*wy3*wz3
        IF keyword_set(average) THEN BEGIN
            FOR j=0l,nrsamples-1l DO BEGIN
                field[index[j]]=field[index[j]]+tscweight[j]*value[j]
                tottscweight[index[j]]=tottscweight[index[j]]+tscweight[j]
            ENDFOR
        ENDIF ELSE FOR j=0l,nrsamples-1l DO $
          field[index[j]]=field[index[j]]+tscweight[j]*value[j]
    ENDIF

ENDIF

; Free memory (no need to free any more local arrays, will not lower
; maximum memory usage).
index=0
weight=0


;--------------------------
; Compute weighted average.
;--------------------------

IF keyword_set(average) THEN BEGIN
    good=where(tottscweight NE 0,nrgood)
    field[good]=temporary(field[good])/temporary(tottscweight[good])
ENDIF

return,field

END  ; End of procedure tsc.