Reading in date & time from csv file [message #88616] |
Sun, 18 May 2014 13:25  |
justinclouds
Messages: 25 Registered: December 2012
|
Junior Member |
|
|
I want to read a csv file where the first column is date and time in the format YYYY_MM_DD_HH_MM_SS (e.g. 2014_05_12_18_39_33.194). I proceed as follows:
data = READ_CSV(file, N_TABLE_HEADER=30)
time=string(data.field01[*], format='(I4, "_", I2, "_", I2, "_", I2, "_", I2, "_", I2.5)')
When I print the variable time the result I get is:
2014_**_**_**_**_**
Can anybody help with what needs to be changed here to read this correctly or suggest an alternate method.
- D
|
|
|
Re: Reading in date & time from csv file [message #88617 is a reply to message #88616] |
Sun, 18 May 2014 16:31   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
How about StrSplit?
IDL> tstr = '2014_05_12_18_39_33.194'
IDL> time = strsplit(tstr, '_', /EXTRACT)
IDL> help, time
TIME STRING = Array[6]
IDL> print, time
2014 05 12 18 39 33.194
You can then convert them from strings to integers, floats, etc. using the Fix, Long, Float, Double, etc. functions.
|
|
|
|
|
|
Re: Reading in date & time from csv file [message #88621 is a reply to message #88616] |
Mon, 19 May 2014 08:27   |
Phillip Bitzer
Messages: 223 Registered: June 2006
|
Senior Member |
|
|
On Sunday, May 18, 2014 3:25:45 PM UTC-5, justinclouds wrote:
>
> Can anybody help with what needs to be changed here to read this correctly or suggest an alternate method.
>
>
So, the problem seems to be READ_CSV doesn't seem to detect the first column is a string, correct?
IDL> f = 'test.txt'
IDL> data = READ_CSV(f)
IDL> help, data
** Structure <247e2b8>, 2 tags, length=64, data length=64, refs=1:
FIELD1 DOUBLE Array[4]
FIELD2 DOUBLE Array[4]
IDL> print, data.field1
2014.0000 2014.0000 2014.0000 2014.0000
Not sure why - but I don't use READ_CSV much.
Using READCOL works just fine, as I can specify the format of each column easily:
IDL> READCOL, f, date, val, FORMAT='A, F'
IDL> print, date
2014_05_12_18_39_33.194 2014_05_12_19_39_33.194 2014_05_12_20_39_33.194 2014_05_12_21_39_33.194
Then, you can use STR_SPLIT as Matt suggested...if you're on IDL 8. Otherwise, you'll have to get fancy with STREGEX.
----------------------
Here's the test file I used:
----------------------
2014_05_12_18_39_33.194, 1.0
2014_05_12_19_39_33.194, 2.0
2014_05_12_20_39_33.194, 3.0
2014_05_12_21_39_33.194, 4.0
|
|
|
Re: Reading in date & time from csv file [message #88622 is a reply to message #88620] |
Mon, 19 May 2014 08:37   |
justinclouds
Messages: 25 Registered: December 2012
|
Junior Member |
|
|
I changed the code so that time=data.field01
IDL> help, data.field01
<Expression> DOUBLE = Array[157105]
IDL> print, data.field01[0]
2014.0000
So, I did find a work around by using the readcol function from http://idlastro.gsfc.nasa.gov/ftp/pro/misc/readcol.pro
readcol,file,time, FORMAT = 'A', SKIPLINE = 30
year = strmid(time, 0,4)
month = strmid(time, 5,2)
day = strmid(time, 8,2)
hour = strmid(time, 11,2)
minute = strmid(time, 14,2)
second = strmid(time, 17,2)
This is working. However, I still don't understand why the READ_CSV cannot do the job. I also tried READ_ASCII with the same results as READ_CSV.
- D
|
|
|
Re: Reading in date & time from csv file [message #88623 is a reply to message #88621] |
Mon, 19 May 2014 08:39  |
justinclouds
Messages: 25 Registered: December 2012
|
Junior Member |
|
|
On Monday, May 19, 2014 9:27:32 AM UTC-6, Phillip Bitzer wrote:
> On Sunday, May 18, 2014 3:25:45 PM UTC-5, justinclouds wrote:
>
>>
>
>> Can anybody help with what needs to be changed here to read this correctly or suggest an alternate method.
>
>>
>
>>
>
>
>
> So, the problem seems to be READ_CSV doesn't seem to detect the first column is a string, correct?
>
>
>
> IDL> f = 'test.txt'
>
> IDL> data = READ_CSV(f)
>
>
>
> IDL> help, data
>
> ** Structure <247e2b8>, 2 tags, length=64, data length=64, refs=1:
>
> FIELD1 DOUBLE Array[4]
>
> FIELD2 DOUBLE Array[4]
>
> IDL> print, data.field1
>
> 2014.0000 2014.0000 2014.0000 2014.0000
>
>
>
> Not sure why - but I don't use READ_CSV much.
>
>
>
> Using READCOL works just fine, as I can specify the format of each column easily:
>
> IDL> READCOL, f, date, val, FORMAT='A, F'
>
>
>
> IDL> print, date
>
> 2014_05_12_18_39_33.194 2014_05_12_19_39_33.194 2014_05_12_20_39_33.194 2014_05_12_21_39_33.194
>
>
>
> Then, you can use STR_SPLIT as Matt suggested...if you're on IDL 8. Otherwise, you'll have to get fancy with STREGEX.
>
>
>
> ----------------------
>
> Here's the test file I used:
>
> ----------------------
>
> 2014_05_12_18_39_33.194, 1.0
>
> 2014_05_12_19_39_33.194, 2.0
>
> 2014_05_12_20_39_33.194, 3.0
>
> 2014_05_12_21_39_33.194, 4.0
Thanks. I did just what you suggested and we must have been posting this at the same time! Thanks!
|
|
|