Comparison between arrays with different lenght [message #88950] |
Mon, 07 July 2014 04:00  |
arianna.religi
Messages: 1 Registered: July 2014
|
Junior Member |
|
|
Hello everybody!
I'd like to ask you about a simple problem I have with my program.
I have 2 .txt files that are like this:
FILE 1 (all data, day-by-day)
JULIANDAYYEAR MM DD OZONE STD
2448623 1992 1 1 0
2448624 1992 1 2 308.5 0.90
2448624 1992 1 2 306.9 0.80
2448624 1992 1 2 306.6 0.90
2448624 1992 1 2 303.6 0.60
2448624 1992 1 2 300.9 1.40
2448624 1992 1 2 301.3 1.20
2448624 1992 1 2 296.8 1.20
2448624 1992 1 2 298.8 1.10
2448624 1992 1 2 298.5 1.80
2448624 1992 1 2 298.4 0.60
2448624 1992 1 2 299.8 0.50
2448624 1992 1 2 299.9 0.60
2448624 1992 1 2 299.3 0.50
2448625 1992 1 3 304.4 1.20
2448625 1992 1 3 303.2 1.20
...
FILE2 (there are the averages)
JULIANDAY DD MM AVERAGE
2448623 1 1 0.00
2448624 2 1 301.20
2448625 3 1 299.08
2448626 4 1 0.00
2448627 5 1 292.77
2448628 6 1 283.17
2448629 7 1 0.00
2448630 8 1 267.60
...
Now I would like to create a new file like this:
JULIANDAYYEAR MM DD OZONE STD AVERAGE
2448623 1992 1 1 0 0
2448624 1992 1 2 308.5 0.90 301.2
2448624 1992 1 2 306.9 0.80 301.2
2448624 1992 1 2 306.6 0.90 301.2
2448624 1992 1 2 303.6 0.60 301.2
2448624 1992 1 2 300.9 1.40 301.2
2448624 1992 1 2 301.3 1.20 301.2
2448624 1992 1 2 296.8 1.20 301.2
2448624 1992 1 2 298.8 1.10 301.2
2448624 1992 1 2 298.5 1.80 301.2
2448624 1992 1 2 298.4 0.60 301.2
2448624 1992 1 2 299.8 0.50 301.2
2448624 1992 1 2 299.9 0.60 301.2
2448624 1992 1 2 299.3 0.50 301.2
2448625 1992 1 3 304.4 1.20 299.08
2448625 1992 1 3 303.2 1.20 299.08
but I can't because arrays have a different lenght. How can I do this comparison? For the moment I was only able to open the files:
OPENR,5, inputfile3
READF,5, riga
OPENW, 10, output4
WHILE NOT EOF(5) DO BEGIN
READF,5, giuliano_t1, anno_t1, mese_t1, giorno_t1, ozono_t1, stdozono_t1
giuliano=[giuliano,giuliano_t1]
dd=[dd,giorno_t1]
mm=[mm,mese_t1]
anno=[anno,anno_t1]
to3=[to3,ozono_t1]
ENDWHILE
OPENR,4, output3
READF,4, riga
WHILE NOT EOF(4) DO BEGIN
READF,4, giuliano_t2, giorno_t2, mese_t2, mediato_t2
giuliano2=[giuliano2,giuliano_t2]
giorno1=[giorno1,giorno_t2]
mese1=[mese1,mese_t2]
mediato1=[mediato1,mediato_t2]
ENDWHILE
Thank you for your help, I am absolute beginner with IDL!
|
|
|
Re: Comparison between arrays with different lenght [message #88982 is a reply to message #88950] |
Thu, 10 July 2014 01:50  |
skymaxwell@gmail.com
Messages: 127 Registered: January 2007
|
Senior Member |
|
|
Hi
I think something like that. I'm not sure, that it's best way
PRO DIFFSIZEARRAYS
;opening src files
path1='D:\Work\IDL\DiffSizeArrays\data\file1.txt'
path2='D:\Work\IDL\DiffSizeArrays\data\file2.txt'
outPath='D:\Work\IDL\DiffSizeArrays\data\result.txt'
OPENR,lun1,path1,/GET_LUN
OPENR,lun2,path2,/GET_LUN
;init
array1=''
array2=''
line=''
;reading file1
WHILE NOT EOF(lun1) DO BEGIN
READF,lun1, line
array1 = [array1, line]
ENDWHILE
array1=array1[1:*]
;reading file2
WHILE NOT EOF(lun2) DO BEGIN
READF,lun2, line
;cut useless information; we will be have only julianday and average data
tmp=STRSPLIT(line,/EXTRACT)
line=tmp[0]+' '+tmp[3]
array2 = [array2, line]
ENDWHILE
array2=array2[1:*]
;open file for write
OPENW,lun3,outPath,/GET_LUN
;preparing data and merge data.
sz1=SIZE(array1)
sz2=SIZE(array2)
;only AVERAGE
headerFile=STRSPLIT(array2[0],/EXTRACT)
;headerFile[1]='AVERAGE'and print header for result file
PRINT,array1[0],headerFile[1]
PRINTF,lun3,array1[0],headerFile[1]
FOR I=1,sz1[1]-1 DO BEGIN
FOR J=0,sz2[1]-1 DO BEGIN
tmp1=STRSPLIT(array1[I],/EXTRACT)
tmp2=STRSPLIT(array2[J],/EXTRACT)
IF (tmp1[0] EQ tmp2[0]) THEN BEGIN
PRINTF,lun3,array1[i],tmp2[1]
PRINT,array1[i],tmp2[1]
ENDIF
ENDFOR
ENDFOR
;closing files
FREE_LUN,lun1,lun2,lun3
END
regards
|
|
|