Re: tabs vs. spaces with asciiread

From: Saji N Hameed <saji_at_nyahnyahspammersnyahnyah>
Date: Mon Dec 07 2009 - 20:05:57 MST

Hi Jonathan,

  That is a pretty badly written file ;) Here is ruby to your help.
(wish NCL had low level read/write functions and regular expressions)
In your example, the header seems to be delimited in a different way
from the body. The following code, corrects them separately and writes
an output file delimited by "tab".

Hope it helps!

saji

infil="example.txt"
outfil="corrected_"+infil
delimiter="\t"
fin=File.open(infil,"r")
fout=File.open(outfil,"w")

# read header and skip line after that
# before reading in body
body=[]
header=fin.gets.chomp.split(/\t+/)
fin.gets
while line=fin.gets do
  body << line.chomp.split(/\t+/)
end

# Correct the header and body separately, as they are not consistently
# delimited

# header first
first_two_columns=header[0].split
corrected_header=[]
(corrected_header << first_two_columns << header[1..-1]).flatten!.map! {|col|
col.strip}

# write corrected header to outfil
fout.puts corrected_header.join(delimiter)

# body next
body.each do |row|
  first_four_columns=row[0].split
  fourth_column=first_four_columns[3..-1].join(" ")
  corrected_row=[]
  (corrected_row << first_four_columns[0..2] << fourth_column << row[1..-1]).flatten!.map! {|col|
      col.strip}
  # write corrected body to outfil
  fout.puts corrected_row.join(delimiter)
end

* Jonathan Vigh <vigh@atmos.colostate.edu> [2009-12-07 22:03:36 +0000]:

> Hi NCL'ers,
> I have another ASCII reading question. I created a data file by
> hand using nedit. This file has mixed type (strings, logicals, and
> integers). My original plan was to use asciiread to read the file
> into NCL by column position, converting the strings into the
> appropriate type. Unfortunately, when I was creating the file I used
> both spaces and tabs to align the columns, and it seems that
> asciiread is not seeing things in the position they appear within
> nedit.
>
> I'm wondering, first of all, if anyone knows a simple way to save
> such a file to fix this (maybe in a different text editor?).
> Secondly, does anyone have any ideas on an NCL workaround to read a
> file like this (a short version of the file is attached). My idea to
> split the string into tokens and parse each line individually runs
> into a problem because some of the string fields already have
> spaces.
>
> Please don't spend much time on this - I think I could fix the file
> by hand in an hour or two (only a few hundred lines) - but I just
> wanted to ask to see if anyone already knows a solution.
>
> A question for the developers - Is tabs vs. spaces a known issue
> with asciiread?
>
> Thanks!
> Jonathan
>

> STORMID STORMNAME CASE TYPE 1ST AIRCRAFT EYE IR DATA USABLE IR EYE t_OPEN Tbmin t_CLOSED Tbmin t_FIRST_EYE Tbmin t_PERSISTENT Tbmin t_STRONG Tbmin DESCRIPTION AT AIRCRAFT EYE Tbmin FORMATION MORPHOLOGY
> ------- ------------ ------------------- ------------------- ------- ------ ------ ----------- ------ ------------ ----- ------------ ----- ------------ ----- ------------ ----- --------------------------- ----- --------------------
> AL142008 NANA no_aircraft_data XXXXXXXXXXXXXXXXXXX True True False
> AL152008 OMAR intermittant_failure 14 Oct 2008 / 19:53 True True True 200810140545 -90 200810140545 -90 200809160115 -90 200809160115 -90 Very strong, cold CDO with an embedded warm spot -90 Lots of deep convection with primary band to east, was being sheared from west at first - strong convection persisted near center and at one point formed a col-type warm open warm spot. A strong CDO to west started showing signs of rotation, then a prominent and occasional broad warm spot (but not enough delta T to consider this an eye by our definition) - it seems quite likely that this was the eye but it took quite a while before the delta T was enough to call this a true eye.
> AL162008 SIXTEEN no_attempt XXXXXXXXXXXXXXXXXXX True True False
> AL172008 PALOMA complete_success 06 Nov 2008 / 17:07 True True True 200811061515 -80 200811062315 -80 200811062315 -80 200811071725 -75 200811072325 -80 CDO with indentation and inner cold banding and warm spot. -80 Had lots of strong convection which died out somewhat near the center, then two very powerful cells fired off and merged into a CDO with some signs of rotation

> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

-- 
Saji N. Hameed
APEC Climate Center          				
1463 U-dong, Haeundae-gu,                               +82 51 745 3951
BUSAN 612-020, KOREA                    		saji@apcc21.net
Fax: +82-51-745-3999
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Dec 7 20:04:27 2009

This archive was generated by hypermail 2.1.8 : Thu Dec 10 2009 - 11:30:59 MST