load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" ; ; This function removes commas from an array of strings. ; function remove_commas(d) local cdata, cdata2, comma, nlines, ii, jj, i, j begin cdata = stringtochar(d) ; Convert to character array. comma = stringtochar(",") ; This will be 2 chars, so be ; sure to use comma(0). cdata2 = cdata ; Make a copy. This will eventually ; contain characters without commas. ; ; Loop through each character array, and retain ; only the non-comma characters. ; nlines = dimsizes(d) do i=0,nlines-1 jj = 0 do j=0,dimsizes(cdata(i,:))-1 if(cdata(i,j).ne.comma(0)) then cdata2(i,jj) = cdata(i,j) jj = jj + 1 end if end do end do ; Return the new strings that contain no commas. return(chartostring(cdata2)) end ; ; Main code ; begin ; Read in the data as an array of strings. data = asciiread("asc4.txt",-1,"string") nlines = dimsizes(data) ; First 3 lines and last 2 lines are header and ; footer stuff. Remove them. data2 = data(4:nlines-3) nlines = dimsizes(data2) ; Recalculate # of lines ; Remove commas from strings. data_no_commas = remove_commas(data2) ; print(data_no_commas) ; Write to a temporary file so we can read it back in as floating point. asciiwrite("tmp_file",data_no_commas) ; ; The temp data file contains 1 columns of alpha data and 2 columns ; of floating point data. We only care about the 2 floating point ; columns here. The method below only works because the alpha ; column contains no numbers. ; ncols = 2 new_data = asciiread("tmp_file",(/nlines,ncols/),"float") print(new_data) ; No longer need temporary file. system("/bin/rm tmp_file") end