-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_currents.f90
462 lines (354 loc) · 13.7 KB
/
get_currents.f90
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
program get_currents
use ModKind, ONLY: Real8_
use ModTimeConvert
use ModNumConst
implicit none
real, parameter :: Re = 6372.0
integer, parameter :: nLons = 48
integer, parameter :: nLats = 30
integer, parameter :: nAlts = 20
real :: dlat = 1.0
real :: dlon = 7.5
real :: StartLat = 60.0 ! deg
real :: StartLon = 1.0 ! deg
real :: StartAlt = 100.0 ! km
real :: EndAlt = 150.0 ! km
real :: NormalPeakAlt = 105.0
real :: NormalPeakWidth = 10.0
real :: NormalIntegral
real :: GeoLats(nLats), cosLats(nLats)
real :: GeoLons(nLons)
real :: GeoAlts(nAlts)
real :: Normalization(nAlts)
real :: dalt
integer :: iLon, iLat, iAlt, iVar
integer :: iOutputUnit_ = 31
real :: B0(nLons, nLats, nAlts,4)
real :: bDir(nLons, nLats, nAlts,3)
real :: ApexLat(nLons, nLats, nAlts)
real :: ApexLon(nLons, nLats, nAlts)
real :: mlt(nLons, nLats, nAlts)
real :: Date
real :: alat, alon, xmag, ymag, zmag, bmag, magpot, lshell
integer :: iError
integer :: iTime(7), jDay
real(Real8_) :: CurrentTime, StartTime
real :: SubSolarLatitude, SubSolarLongitude
real :: MagneticPoleColat, MagneticPoleLon, MagneticPoleStrength
integer, parameter :: iCharLen_ = 100
integer, parameter :: iCharVarLen_ = 40
character (len=iCharLen_), dimension(10) :: Lines
! Here I am setting the file names of the IE files to read:
character (len=iCharLen_) :: cAMIEFileSouth = "test_ideal.bin"
character (len=iCharLen_) :: cAMIEFileNorth = "test_ideal.bin"
!character (len=iCharLen_) :: cAMIEFileSouth = "b20090722s.swmf"
!character (len=iCharLen_) :: cAMIEFileNorth = "b20090722n.swmf"
character (len=iCharLen_) :: outfilePre = "test", outfileNum, outfile
character (len=iCharVarLen_) :: VarName
real :: Version = 0.1
integer, parameter :: nVars = 7
real :: AllData(nLons, nLats, nAlts, nVars)
real :: TempPotential(nLons, nLats)
real :: TempHall(nLons, nLats)
real :: TempPed(nLons, nLats)
! These are the state variables:
real :: Potential(nLons,nLats,nAlts)
real :: Efield(nLons,nLats,nAlts,3)
real :: ExB(nLons,nLats,nAlts,3)
real :: ExBdir(nLons,nLats,nAlts,3)
real :: Hall(nLons,nLats,nAlts)
real :: Ped(nLons,nLats,nAlts)
real :: jPed(nLons,nLats,nAlts,3)
real :: jHall(nLons,nLats,nAlts,3)
real :: jHor(nLons,nLats,nAlts,3)
real :: jDiv(nLons,nLats,nAlts,3)
real :: edotb, ExBmag, EFieldMag, div
real :: eMax, jMax
! --------------------------------------------------------------
! Here I am setting the number of iterations to take and the dt
! between the iterations
! --------------------------------------------------------------
integer :: nTimes = 3, iter
real :: dt = 60.0
! Set the initial time (obviously, you have to change the time
! if you want to change the time to investigate with new files):
!itime(1) = 2009
!itime(2) = 07
!itime(3) = 22
!itime(4) = 06
!itime(5) = 00
!itime(6) = 00
!itime(7) = 00
itime(1) = 2020
itime(2) = 03
itime(3) = 20
itime(4) = 12
itime(5) = 30
itime(6) = 00
itime(7) = 00
call time_int_to_real(itime, StartTime)
jDay = n_day_of_year(iTime(1), iTime(2), iTime(3))
Date = float(iTime(1)) + float(jDay)/365.0
! Set the delta-altitude
dalt = (EndAlt-StartAlt)/nAlts
! This is setting the values for the IE reader:
Lines(1) = "#AMIEFILES"
Lines(2) = cAMIEFileNorth
Lines(3) = cAMIEFileSouth
Lines(4) = ""
Lines(5) = "#DEBUG"
Lines(6) = "10"
Lines(7) = "0"
Lines(8) = ""
Lines(9) = "#END"
Lines(10) = ""
call EIE_set_inputs(Lines,10)
call EIE_Initialize(iError)
! Set the geographic grid to investigate:
do iLon = 1, nLons
GeoLons(iLon) = StartLon + dlon*(iLon-1)
enddo
do iLat = 1, nLats
GeoLats(iLat) = StartLat + dlat*(iLat-1)
cosLats(iLat) = cos(cosLats(iLat)*cDegToRad)
enddo
! This is a total, 100% hack - it just sets the altitude distribution
! of the conductances. This is really not a great thing, and I just made
! it up. We should look up in a book for both a Pedersen and Hall
! given a certain energy input and take down the distribution. But, I
! didn't do that. I made a total hack:
NormalIntegral = 0.0
do iAlt = 1, nAlts
GeoAlts(iAlt) = StartAlt + dalt*(iAlt-1)
Normalization(iAlt) = dalt*(iAlt-1) * &
exp(-(GeoAlts(iAlt)-NormalPeakAlt)**2/ &
NormalPeakWidth**2)
NormalIntegral = NormalIntegral + &
Normalization(iAlt) * dAlt * 1000.0
enddo
Normalization = Normalization/NormalIntegral
! Need a dummy call to initialize the APEX stuff:
call APEX(DATE, 65.0, 250.0, 100.0, &
LShell, alat,alon,bmag,xmag,ymag,zmag,MagPot)
! Calculate the location of the subsolar point
call SUBSOLR(iTime(1),jDay,iTime(4),iTime(5),iTime(6), &
SubsolarLatitude, SubsolarLongitude)
! Calculate the location of the dipole pole
call dypol( MagneticPoleColat, MagneticPoleLon, MagneticPoleStrength)
! Fill in magnetic coordinates for each of the geographic grid centers:
do iLon = 1, nLons
do iLat = 1, nLats
do iAlt = 1, nAlts
alat = 0.0
alon = 0.0
xmag = 0.0
ymag = 0.0
zmag = 0.0
MagPot = 0.0
LShell = 0.0
call APEX(DATE, &
GeoLats(iLat), &
GeoLons(iLon), &
GeoAlts(iAlt), &
LShell, alat,alon,bmag,xmag,ymag,zmag,MagPot)
! 1 = North
! 2 = East
! 3 = vertical
! 4 = magnitude
B0(iLon,iLat,iAlt,1) = xmag * 1.0e-9
B0(iLon,iLat,iAlt,2) = ymag * 1.0e-9
B0(iLon,iLat,iAlt,3) = -zmag * 1.0e-9
B0(iLon,iLat,iAlt,4) = sqrt( &
B0(iLon,iLat,iAlt,1) ** 2 + &
B0(iLon,iLat,iAlt,2) ** 2 + &
B0(iLon,iLat,iAlt,3) ** 2)
! This defines a b-hat (direction of B)
bDir(iLon,iLat,iAlt,1) = &
B0(iLon,iLat,iAlt,1) / B0(iLon,iLat,iAlt,4)
bDir(iLon,iLat,iAlt,2) = &
B0(iLon,iLat,iAlt,2) / B0(iLon,iLat,iAlt,4)
bDir(iLon,iLat,iAlt,3) = &
B0(iLon,iLat,iAlt,3) / B0(iLon,iLat,iAlt,4)
! Store the latitude and londitude
ApexLat(iLon,iLat,iAlt) = alat
ApexLon(iLon,iLat,iAlt) = alon
enddo
enddo
enddo
! This sets the number of longitudes and latitudes that IE will be expecting:
call UA_SetnMLTs(nLons)
call UA_SetnLats(nLats)
call UA_SetNorth
! -----------------------------------------------------------------------
! Looping can start here, since this stuff is all dependent on the time:
! -----------------------------------------------------------------------
do iter = 0,nTimes-1
CurrentTime = StartTime + dt * iter
call time_real_to_int(CurrentTime, iTime)
write(*,*) "Working on Time : ", iTime
jDay = n_day_of_year(iTime(1), iTime(2), iTime(3))
! Calculate the location of the subsolar point
call SUBSOLR(iTime(1),jDay,iTime(4),iTime(5),iTime(6), &
SubsolarLatitude, SubsolarLongitude)
! Calculate the magnetic local time of each grid point
do iLon = 1, nLons
do iLat = 1, nLats
do iAlt = 1, nAlts
call magloctm( &
ApexLon(iLon,iLat,iAlt), &
SubsolarLatitude, &
SubsolarLongitude, &
MagneticPoleColat, &
MagneticPoleLon, &
MLT(iLon,iLat,iAlt))
enddo
enddo
enddo
call get_AMIE_values(CurrentTime)
! Loop through altitudes, since the magnetic coordinates are dependent on
! Altitude:
do iAlt=1,nAlts
! Set the grid for IE:
call UA_SetGrid(MLT(:,:,iAlt), ApexLat(:,:,iAlt), iError)
! Get the potential:
TempPotential = 0.0
call UA_GetPotential(TempPotential, iError)
potential(:,:,iAlt) = TempPotential
! Get height-integrated Hall Conductance:
call UA_GetHall(TempHall, iError)
! Now spread it out over altitude
hall(:,:,iAlt) = TempHall * Normalization(iAlt)
! Get height-integrated Pedersen Conductance:
call UA_GetPed(TempPed, iError)
! Now spread it out over altitude
ped(:,:,iAlt) = TempPed * Normalization(iAlt)
enddo
eMax = 0.0
jMax = 0.0
do iLon = 2, nLons-1
do iLat = 2, nLats-1
do iAlt = 2, nAlts-1
! Longitude Direction
EField(iLon,iLat,iAlt,1) = - &
(Potential(iLon+1,iLat,iAlt) - Potential(iLon-1,iLat,iAlt)) / &
(2*dlon*cDegToRad*((Re+GeoAlts(iAlt))*1000.0)*cosLats(iLat))
! Latitude Direction
EField(iLon,iLat,iAlt,2) = - &
(Potential(iLon,iLat+1,iAlt) - Potential(iLon,iLat-1,iAlt)) / &
(2*dlat*cDegToRad*((Re+GeoAlts(iAlt))*1000.0))
! Altitude Direction
EField(iLon,iLat,iAlt,3) = - &
(Potential(iLon,iLat,iAlt+1) - Potential(iLon,iLat,iAlt-1)) / &
(2*dAlt*1000.0)
! Remove any E in the B-field direction
edotb = &
EField(iLon,iLat,iAlt,1) * bDir(iLon,iLat,iAlt,1) + &
EField(iLon,iLat,iAlt,2) * bDir(iLon,iLat,iAlt,2) + &
EField(iLon,iLat,iAlt,3) * bDir(iLon,iLat,iAlt,3)
EField(iLon,iLat,iAlt,1) = EField(iLon,iLat,iAlt,1) - &
edotb * bDir(iLon,iLat,iAlt,1)
EField(iLon,iLat,iAlt,2) = EField(iLon,iLat,iAlt,2) - &
edotb * bDir(iLon,iLat,iAlt,2)
EField(iLon,iLat,iAlt,3) = EField(iLon,iLat,iAlt,3) - &
edotb * bDir(iLon,iLat,iAlt,3)
ExB(iLon,iLat,iAlt,1) = &
EField(iLon,iLat,iAlt,2) * B0(iLon,iLat,iAlt,3) - &
EField(iLon,iLat,iAlt,3) * B0(iLon,iLat,iAlt,2)
ExB(iLon,iLat,iAlt,2) = - ( &
EField(iLon,iLat,iAlt,1) * B0(iLon,iLat,iAlt,3) - &
EField(iLon,iLat,iAlt,3) * B0(iLon,iLat,iAlt,1))
ExB(iLon,iLat,iAlt,3) = &
EField(iLon,iLat,iAlt,1) * B0(iLon,iLat,iAlt,2) - &
EField(iLon,iLat,iAlt,2) * B0(iLon,iLat,iAlt,1)
EFieldMag = sqrt( &
EField(iLon,iLat,iAlt,1)**2 + &
EField(iLon,iLat,iAlt,2)**2 + &
EField(iLon,iLat,iAlt,3)**2)
eMax = max(EFieldMag, eMax)
ExBmag = sqrt( &
ExB(iLon,iLat,iAlt,1)**2 + &
ExB(iLon,iLat,iAlt,2)**2 + &
ExB(iLon,iLat,iAlt,3)**2)
ExBdir(iLon,iLat,iAlt,:) = &
ExB(iLon,iLat,iAlt,:)/ExBmag
jPed(iLon,iLat,iAlt,:) = &
EField(iLon,iLat,iAlt,:) * &
Ped(iLon,iLat,iAlt)
jHall(iLon,iLat,iAlt,:) = &
EFieldMag * &
ExBdir(iLon,iLat,iAlt,:) * &
Hall(iLon,iLat,iAlt)
jHor(iLon,iLat,iAlt,:) = &
jPed(iLon,iLat,iAlt,:) + &
jHall(iLon,iLat,iAlt,:)
jMax = max(sqrt(sum(jHor(iLon,iLat,iAlt,:)**2)), jMax)
enddo
enddo
enddo
write(*,*) eMax, jMax
do iLon = 3, nLons-2
do iLat = 3, nLats-2
do iAlt = 3, nAlts-2
! Longitude Direction
div = &
(jHor(iLon+1,iLat,iAlt,1) - jHor(iLon-1,iLat,iAlt,1)) / &
(2*dlon*cDegToRad*((Re+GeoAlts(iAlt))*1000.0)*cosLats(iLat))
! Latitude Direction
div = div + &
(jHor(iLon,iLat+1,iAlt,2) - jHor(iLon,iLat-1,iAlt,2)) / &
(2*dlat*cDegToRad*((Re+GeoAlts(iAlt))*1000.0))
! Altitude Direction
div = div + &
(jHor(iLon,iLat,iAlt+1,3) - jHor(iLon,iLat,iAlt-1,3)) / &
(2*dAlt*1000.0)
jDiv(iLon,iLat,iAlt,:) = div * bDir(iLon,iLat,iAlt,:)
enddo
enddo
enddo
! Output data to file
write(outfileNum,'(i4.4)') iter
outfile = trim(outfilePre)//'_'//trim(outfileNum)//'.bin'
open(iOutputUnit_, file=trim(outfile), status="unknown", form="unformatted")
write(iOutputUnit_) Version
write(iOutputUnit_) nLons, nLats, nAlts
write(iOutputUnit_) nVars
VarName = "Longitude (deg)"
write(iOutputUnit_) VarName
do iLat = 1,nLats
do iAlt = 1,nAlts
AllData(:,iLat,iAlt,1) = GeoLons * cDegToRad
enddo
enddo
VarName = "Latitude (deg)"
write(iOutputUnit_) VarName
do iLon = 1,nLons
do iAlt = 1,nAlts
AllData(iLon,:,iAlt,2) = GeoLats * cDegToRad
enddo
enddo
VarName = "Altitude (deg)"
write(iOutputUnit_) VarName
do iLon = 1,nLons
do iLat = 1,nLats
AllData(iLon,iLat,:,3) = GeoAlts * 1000.0
enddo
enddo
VarName = "Potential (V)"
write(iOutputUnit_) VarName
AllData(:,:,:,4) = Potential
VarName = "jDiv-East (A/m)"
write(iOutputUnit_) VarName
AllData(:,:,:,5) = jDiv(:,:,:,1)
VarName = "jDiv-North (A/m)"
write(iOutputUnit_) VarName
AllData(:,:,:,6) = jDiv(:,:,:,2)
VarName = "jDiv-Vertical (A/m)"
write(iOutputUnit_) VarName
AllData(:,:,:,7) = jDiv(:,:,:,3)
write(iOutputUnit_) iTime
do iVar = 1, nVars
write(iOutputUnit_) AllData(:,:,:,iVar)
enddo
close(iOutputUnit_)
enddo
end program get_currents