I have a script in IDL which reads an unformatted binary file (F77) and outputs it as a .sav file but I want to convert this script to python and save as an .npz file and I am having trouble on the read line.
IDL Code:
;create save file for QBO model output
;---------------------------------------------------------------------
;input data here (Only adjust this info)
;---------------------------------------------------------------------
time=7300 ;duration from fortran code
tstep=5 ;time step from fortran code
inputfile='/home/cwilliams/Metr_205/qbo/uvwtom.'; binary file from fortran
outputsavefile='~cwilliams/Metr_205/qbo/qbo_FULLX.sav'; name of save file with variables
;--------------------------------------------------------------------------------
;--------------------------------------------------------------------------------
;==============================================================================
; Do Not Adjust the Following Code
;==============================================================================
fname='dat'
nstep=time/tstep ;time/timestep
nm=nstep-1
um=fltarr(17,34)
vm=fltarr(17,34)
wm=fltarr(17,34)
tm=fltarr(17,34)
pm=fltarr(17,34)
om=fltarr(17,34)
vpm=fltarr(17,34)
vdm=fltarr(17,34)
u=fltarr(17,34,nstep)
v=fltarr(17,34,nstep)
w=fltarr(17,34,nstep)
temp=fltarr(17,34,nstep)
pressure=fltarr(17,34,nstep)
ozone=fltarr(17,34,nstep)
date=findgen(nstep)*(2.*tstep/365.)
ulevels2=[-30, -25, -20, -15, -10, -5, 5, 10, 15, 20, 25, 30]
ulevels3=findgen(15)*3.-12
lab=findgen(20)*0+1
lat=findgen(17)*2.7-1.35
ht=findgen(34)*0.5+17.
openr,37,inputfile+fname,/f77_unformatted & title='base'
for n=0,nm do begin
readu,37,um,vm,wm,tm,om,pm
u(*,*,n)=um/100.
v(*,*,n)=vm
w(*,*,n)=wm
temp(*,*,n)=tm*700000./2.87e6
pressure(*,*,n)=pm/10000.
ozone(*,*,n)=om
endfor
close,37
save,filename=outputsavefile,u,v,w,temp,pressure,ozone,date,ulevels2,ulevels3,lab,$
lat,ht
stop
end
Python Code: #create save file for QBO model output
#---------------------------------------------------------------------
#input data here (Only adjust this info)
#---------------------------------------------------------------------
time=7300 #duration from fortran code
tstep=5 #time step from fortran code
inputfile='/home/cwilliams/metr51/lab12/uvwtom.dat'# binary file from fortran
outputsavefile='~cwilliams/metr51/lab12/qbo_FULLX.sav'# name of save file with variables
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
import numpy as n
#==============================================================================
# Do Not Adjust the Following Code
#==============================================================================
nstep=time/tstep #time/timestep
nm=nstep-1
um=n.empty((17,34))
vm=n.empty((17,34))
wm=n.empty((17,34))
tm=n.empty((17,34))
pm=n.empty((17,34))
om=n.empty((17,34))
vpm=n.empty((17,34))
vdm=n.empty((17,34))
u=n.empty((17,34,nstep))
v=n.empty((17,34,nstep))
w=n.empty((17,34,nstep))
temp=n.empty((17,34,nstep))
pressure=n.empty((17,34,nstep))
ozone=n.empty((17,34,nstep))
date=n.arange(nstep)*(2.*tstep/365.)
ulevels2=n.array([-30, -25, -20, -15, -10, -5, 5, 10, 15, 20, 25, 30])
ulevels3=n.arange(15)*3.-12
lab=n.arange(20)*0+1
lat=n.arange(17)*2.7-1.35
ht=n.arange(34)*0.5+17.
This is where I need some assitance:
f=open(inputfile,'rb')
data=f.read()
for i in range(nm+1):
# readu,37,um,vm,wm,tm,om,pm
# u(:,:,i)=um/100.
# v(:,:,i)=vm
# w(:,:,i)=wm
# temp(:,:,i)=tm*700000./2.87e6
# pressure(:,:,i)=pm/10000.
# ozone(*,*,n)=om
#
#n.savez(filename=outputsavefile,u=u,v=v,w=w,temp=temp,pressure=pressure,ozone=ozone,date=date,ulevels2=ulevels2,ulevels3=ulevels3,lab=lab,lat=lat,ht=ht)
I know there is an issue with the row/column order between IDL and Python but I think I can fix this once I read the code in.