open file text with special format

74 views
Skip to first unread message

Khaled Tahoon

unread,
May 15, 2024, 7:49:42 AM5/15/24
to idl-pvwave
Hello, I' m a new with idl programming. 
attach includes a file of regional climate model. 
Does anyone help me to open the file and read the data of Lon., Lat., and Ps for contour plot using IDL under windows operating system.
Thanks 
ps_200605 - Copy.txt

markus.sc...@gmail.com

unread,
May 15, 2024, 2:06:31 PM5/15/24
to idl-pvwave
Hi Khaled, 
try the code below. Please loom up the documentation of the function and methods you don't know.
I hope this helps, Markus

function extract_value, lines, start, ends
  val = strsplit(lines[start:ends],' ,;',/extract)
  val = val.map('float')
  return, val.reduce(lambda('x,y:[x,y]'))
end

filename='Downloads/ps_200605.txt'
print, file_test(filename,/read)
n_lines =file_lines(filename)
lines=strarr(n_lines)
openr, lun, filename,/get_lun
readf, lun, lines
free_lun, lun

posi = lonarr(3)
foreach  var,['xlon','xlat','ps'],i do posi[i] = $
    where(stregex(lines,' *'+var+' *= *',/boolean))
semicolon = where(stregex(lines,'.* *; *',/boolean))

xlon = extract_value(lines, posi[-3]+1, semicolon[-3])
xlat = extract_value(lines, posi[-2]+1, semicolon[-2])
ps   = extract_value(lines, posi[-1]+1, semicolon[-1])

c=contour(ps,xlon,xlat)

Khaled Tahoon

unread,
May 17, 2024, 7:35:56 AM5/17/24
to idl-pvwave
Hi Markus,
Thanks a lot for your kindly reply.
unfortunately,  the foreach statement is not run under my IDL version.
I modified your code as follows:

filename='downloads\ps_200605.txt'

n_lines =file_lines(filename)
lines=strarr(n_lines)
openr, lun, filename,/get_lun
readf, lun, lines
free_lun, lun

var=['xlon','xlat','ps']
posi=lonarr(3)
for i=0, n_elements(Var) - 1 do begin
posi[i] = where(stregex(lines,' *'+var[i]+' *= *',/boolean))

semicolon = where(stregex(lines,'.* *; *',/boolean))
endfor
when I run your code, I get that POSI is Out of range subscript encountered
and then the following statement will be incorrect. 
xlon = extract_value(lines, posi[-3]+1, semicolon[-3])
could you please explain for me what do you means -3 in both posi[-3] and semicolon[-3].
I would like to read the ps parameter as function in Jx, Iy. 
So, PS become a 2-D array or matrix with rank of Jx * Iy 
regards 
Khaled
please, attach file is the model output. 
In addition, for example, in this file one read the 
dimensions:
time = UNLIMITED ; // (1 currently)
bnds = 2 ;
jx = 237 ;
iy = 197 
variables:
double time(time) ;
time:standard_name = "time" ;
time:long_name = "time" ;
time:bounds = "time_bnds" ;
time:units = "hours since 1949-12-01 00:00:00 UTC" ;
time:calendar = "gregorian" ;
time:axis = "T" ;
how one can read each parameter.   
ps_200605.txt

Khaled Tahoon

unread,
May 18, 2024, 6:42:40 AM5/18/24
to idl-pvwave
Hello Markus,
I modified your code as follows:-  
;==================================
file='downloads\ps_200605.txt'
n_lines =file_lines(file)
lines=strarr(n_lines)
openr, lun, file,/get_lun

readf, lun, lines
free_lun, lun

var_k=['xlon','xlat','Ps']
posi=lonarr(3)
for i=0L, n_elements(Var_k) - 1L do begin
semicolon = where(stregex(lines,'.* *; *',/boolean))  ; this command is correct
posi[i] = where(stregex(lines,' *'+var_k[i]+' *= *',/boolean))    
endfor

n_semicolon=n_elements(semicolon)
Inv_Ind_semicolom= (n_semicolon-1)-Indgen(n_semicolon)

print, posi[0]+1, semicolon[Inv_Ind_semicolom[2]]
print, posi[1]+1, semicolon[Inv_Ind_semicolom[1]]
print, posi[2]+1, semicolon[Inv_Ind_semicolom[0]]
xlon = extract_value(lines, posi[0]+1, semicolon[Inv_Ind_semicolom[2]])
xlat = extract_value(lines, posi[1]+1,  semicolon[Inv_Ind_semicolom[1]])
ps   = extract_value(lines, posi[2]+1,  semicolon[Inv_Ind_semicolom[0]])
print, xlon.val
print, xlat.val
print, ps.val
end

function extract_value, lines, start, ends
  val = strsplit(lines[start:ends],' ,;',/extract)
  val = val.map('float')
  dims = Image_Dimensions(Val, XSize=XSize, YSize=YSize, TrueIndex=trueindex)

  return, val.reduce(lambda('x,y:[x,y]'))
end
;====================
I still have some errors in my code such as. 
STRTOK: Expression must be a scalar or 1 element array in this context: STRINGIN. related to Xlon, xlat, and Ps
what means  val.map('float') in the subroutine.
In addition, I need an alternative statements of reduce(lambda('x,y:[x,y]')).
Best regards
Khaled
Reply all
Reply to author
Forward
0 new messages