Re: SPLIT numbers into sub-numbers [message #80577] |
Sun, 24 June 2012 01:36 |
lecacheux.alain
Messages: 325 Registered: January 2008
|
Senior Member |
|
|
On 22 juin, 21:52, k_ghr...@yahoo.com wrote:
> Dear all
> I would like to split the following number 20100115 into year, month
> and day
> any suggestions
> Thanks
The "IDL way" for extracting dates from such a coded integer array
might be:
IDL> dates = transpose([[x/10000],[(x mod 10000)/100],[x mod 100]])
in which dates will be an array of dimensions 3 x N_elements(x).
for instance:
IDL> x = [20100401, 20110501, 20120601]
IDL> print, transpose([[x/10000],[(x mod 10000)/100],[x mod 100]])
2010 4 1
2011 5 1
2012 6 1
A maybe more "readable", but slower way would be:
IDL> dates = intarr(3,N_elements(x)) ;you must define your output
first
IDL> reads, string(x, FORMAT='(i8)'), dates, FORMAT='(i4,i2,i2)'
or, if you like Julian days:
IDL> jd = dblarr(3,N_elements(x)) ;you must define your output
first
IDL> reads, string(x, FORMAT='(i8)'), jd,
FORMAT='(C(CYI4,CMOI2,CDI2))'
IDL> print,jd
2455288.0 2455683.0 2456080.0
alx.
|
|
|
Re: SPLIT numbers into sub-numbers [message #80580 is a reply to message #80577] |
Sat, 23 June 2012 15:08  |
k_ghreep
Messages: 19 Registered: May 2012
|
Junior Member |
|
|
On Jun 23, 11:13 pm, Carsten Lechte <c...@toppoint.de> wrote:
> On 23/06/12 03:45, Kenneth P. Bowman wrote:
>
>> year = 0
>> month = 0
>> day = 0
>> READS, ts, year, month, day, FORMAT = "(I4,2I2)"
>
> Nice, I had not thought of that.
>
> My first idea was:
>
> num = 20100115L ; very important: force LONG integer type!
> y = num/10000
> m = (num-y*10000)/100
> d = num - y*10000 - m*100
> print, num, y, m, d
>
> ==> 20100115 2010 1 15
>
> chl
for the following array
20100101 20100101 20100101 20100101 20100102
20100102 20100102 20100102 20100103 20100103
20100103 20100103 20100104
I wrote
y=lonarr( n_elements(num))
m =lonarr( n_elements(num))
d =lonarr( n_elements(num))
for i = 0, n_elements(num)-1 do begin
num(i)=num(i)+0L
y(i) = num(i)/10000
m(i) = (num(i)-y(i)*10000)/100
d(i) = num(i) - y(i)*10000 - m(i)*100
print, num(i), y(i), m(i), d(i)
endfor
and now ran correctly with the following output
20100101 2010 1 1
20100101 2010 1 1
20100101 2010 1 1
20100101 2010 1 1
20100102 2010 1 2
20100102 2010 1 2
20100102 2010 1 2
20100102 2010 1 2
20100103 2010 1 3
20100103 2010 1 3
20100103 2010 1 3
20100103 2010 1 3
20100104 2010 1 4
thank you for your helping
|
|
|
Re: SPLIT numbers into sub-numbers [message #80582 is a reply to message #80580] |
Sat, 23 June 2012 14:13  |
Carsten Lechte
Messages: 124 Registered: August 2006
|
Senior Member |
|
|
On 23/06/12 03:45, Kenneth P. Bowman wrote:
> year = 0
> month = 0
> day = 0
> READS, ts, year, month, day, FORMAT = "(I4,2I2)"
Nice, I had not thought of that.
My first idea was:
num = 20100115L ; very important: force LONG integer type!
y = num/10000
m = (num-y*10000)/100
d = num - y*10000 - m*100
print, num, y, m, d
==> 20100115 2010 1 15
chl
|
|
|
Re: SPLIT numbers into sub-numbers [message #80583 is a reply to message #80582] |
Sat, 23 June 2012 11:59  |
k_ghreep
Messages: 19 Registered: May 2012
|
Junior Member |
|
|
On Jun 22, 11:02 pm, Michael Galloy <mgal...@gmail.com> wrote:
> On 6/22/12 1:52 PM, k_ghr...@yahoo.com wrote:
>
>> Dear all
>> I would like to split the following number 20100115 into year, month
>> and day
>> any suggestions
>> Thanks
>
> Something like this:
>
> IDL> ts = '20100115'
> IDL> year = long(strmid(ts, 0, 4))
> IDL> month = long(strmid(ts, 4, 2))
> IDL> day = long(strmid(ts, 6, 2))
> IDL> help, year, month, day
> YEAR LONG = 2010
> MONTH LONG = 1
> DAY LONG = 15
>
> Mike
> --
> Michael Galloywww.michaelgalloy.com
> Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
> Research Mathematician
> Tech-X Corporation
Thanks for your help
first you convert a long number to a string by put it between ' '
but if i have a long array like this
20100101 20100101 20100101 20100101 20100102
20100102 20100102 20100102 20100103 20100103
20100103 20100103 20100104
and I cann't put each sub group between ' '
how I can split this directly?
Best wishes
Khaled
|
|
|
Re: SPLIT numbers into sub-numbers [message #80584 is a reply to message #80583] |
Sat, 23 June 2012 11:59  |
k_ghreep
Messages: 19 Registered: May 2012
|
Junior Member |
|
|
On Jun 23, 3:45 am, "Kenneth P. Bowman" <k-bow...@null.edu> wrote:
> In article <js2mga$jh...@dont-email.me>, Michael Galloy <mgal...@gmail.com>
> wrote:
>
>
>
>
>
>
>
>
>
>> On 6/22/12 1:52 PM, k_ghr...@yahoo.com wrote:
>>> Dear all
>>> I would like to split the following number 20100115 into year, month
>>> and day
>>> any suggestions
>>> Thanks
>
>> Something like this:
>
>> IDL> ts = '20100115'
>> IDL> year = long(strmid(ts, 0, 4))
>> IDL> month = long(strmid(ts, 4, 2))
>> IDL> day = long(strmid(ts, 6, 2))
>> IDL> help, year, month, day
>> YEAR LONG = 2010
>> MONTH LONG = 1
>> DAY LONG = 15
>
> Or
>
> year = 0
> month = 0
> day = 0
> READS, ts, year, month, day, FORMAT = "(I4,2I2)"
>
> Cheers, Ken
Thanks for your help
first you convert a long number to a string by put it between ' '
but if i have a long array like this
20100101 20100101 20100101 20100101 20100102
20100102 20100102 20100102 20100103 20100103
20100103 20100103 20100104
and I cann't put each sub group between ' '
how I can split this directly?
Best wishes
Khaled
|
|
|
Re: SPLIT numbers into sub-numbers [message #80587 is a reply to message #80584] |
Fri, 22 June 2012 18:45  |
Kenneth P. Bowman
Messages: 585 Registered: May 2000
|
Senior Member |
|
|
In article <js2mga$jh0$1@dont-email.me>, Michael Galloy <mgalloy@gmail.com>
wrote:
> On 6/22/12 1:52 PM, k_ghreep@yahoo.com wrote:
>> Dear all
>> I would like to split the following number 20100115 into year, month
>> and day
>> any suggestions
>> Thanks
>
> Something like this:
>
> IDL> ts = '20100115'
> IDL> year = long(strmid(ts, 0, 4))
> IDL> month = long(strmid(ts, 4, 2))
> IDL> day = long(strmid(ts, 6, 2))
> IDL> help, year, month, day
> YEAR LONG = 2010
> MONTH LONG = 1
> DAY LONG = 15
Or
year = 0
month = 0
day = 0
READS, ts, year, month, day, FORMAT = "(I4,2I2)"
Cheers, Ken
|
|
|
Re: SPLIT numbers into sub-numbers [message #80590 is a reply to message #80587] |
Fri, 22 June 2012 14:02  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 6/22/12 1:52 PM, k_ghreep@yahoo.com wrote:
> Dear all
> I would like to split the following number 20100115 into year, month
> and day
> any suggestions
> Thanks
Something like this:
IDL> ts = '20100115'
IDL> year = long(strmid(ts, 0, 4))
IDL> month = long(strmid(ts, 4, 2))
IDL> day = long(strmid(ts, 6, 2))
IDL> help, year, month, day
YEAR LONG = 2010
MONTH LONG = 1
DAY LONG = 15
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
|
|
|