PRO read_model_tm,init_model,tp,mp,modin,flag
;
; Reading scaled models and calculation of geometrical height h [km]
; and pressures Pt (total), Pg (gas).
; All quantities, except h [km], are in cgs units.
;
; INPUT:
;  init_model (string) in the form 'C' defines the folder name
;   'Models/VAL_'+init_model
;  tp - temperature parameter in the range -0.03 to 0.03
;  mp - column-mass parameter in the range 0.2 to 5
; OUTPUTS:
;  modin - model array DOUBLE(10,103) [h,m,T,vt,nel,rho,Pg,Pt,z,tau]
;  flag = 1 when the model is ok, flag = 0 when the model contains NaN

tp=tp<0.03>(-0.03)
mp=mp<5.>0.2
id=49*round((tp+0.03)*1000)+round((mp-0.2)*10)
print,'Model T-par, m-par, ID:  ',tp,mp,id

model_name='Models/VAL_'+init_model+'/MODEL_INP'+string(id,format='(I05)')+'.DAT'
modin=dblarr(10,103)
g=27398.   ; gravitational acceleration [cm/s^2]
flag=1

openr,1,model_name
for j=0,102 do begin
  readf,1,dum,m,t,vt,nel,rho,z,tau
  ; Pressures
  ptot=g*m
  pgas=ptot-0.5*rho*vt^2
  ; output array
  modin(1,j)=m
  modin(2,j)=t
  modin(3,j)=vt
  modin(4,j)=nel
  modin(5,j)=rho
  modin(6,j)=pgas
  modin(7,j)=ptot
  modin(8,j)=z
  modin(9,j)=tau
endfor
close,1

; Geometric height
modin[0,*]=(modin[8,102]-7.5e6-modin[8,*])/1.e5

; Presence of NaNs
if total(finite(modin)) ne n_elements(modin) then flag=0

END