Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
Re: Reading into arrays with order > 2
Steven Riley wrote: > Assuming names3Mom is a 1D array of strings (of valid filenames) is there > any reason why the following code will not read many files into a 3d array? > > FOR I=0,1 DO BEGIN > close,1 & openr,1,names3Mom(I) > readf,1,all3MomData(I,*,*) > END > > I get no error messages, but when I look at my array it contains no data...? > I'm quite sure the files being read contain data. Is this some inherent > problem with readf and arrays with order > 2? "Subscripted variable references are passed by value. Parameters passed by value can only be inputs to program units." (Source: IDL Online Help - Parameter Passing Mechanism) The following code will work. sz = SIZE(all3MomData) buffer = ???ARR(sz(2), sz(3)) ; ??? is data type (INT, FLT etc.) FOR I=0,1 DO BEGIN close,1 & openr,1,names3Mom(I) readf,1,buffer all3MomData(I,*,*) = buffer END -------------------------------- May the Force be with you. Osamu Sasaki as Dasaki E-mail : dasaki@pc.highway.ne.jp
> Assuming names3Mom is a 1D array of strings (of valid filenames) is there > any reason why the following code will not read many files into a 3d array? > > FOR I=0,1 DO BEGIN > close,1 & openr,1,names3Mom(I) > readf,1,all3MomData(I,*,*) > END > > I get no error messages, but when I look at my array it contains no data...? > I'm quite sure the files being read contain data. Is this some inherent > problem with readf and arrays with order > 2? > > Steven This is an very common mistake and is one which has cost me literally hours of time cursing at my code only to realise that I've been duped again. The problem lies in the readf line. Unfortunately you can not read into subscripted arrays. This is to do with the way IDL handles passing around subscripted arrays. Essentially when you subscript an array, IDL creates a copy of that small subsection. So, it is a temporary smaller array which gets passed into the readf function, not the actual all3MomData array. This is called passing by value, because it is only the value of all3MomData that is passed in, not the actual memory location, so readf cannot change all3MomData at all. So readf goes along as normal but instead of doing what you want, which is to read the data into the right slice of all3MomData, it reads the data into this smaller temporary variable. This is no use to you as this temp variable is destroyed after readf is exited and nothing goes back into all3MomData. This is why you're left with all 0's. That's not a very good explanation - if you want a nice concise one then you'll have to wait for David Fanning to post his reply. That's all very well but you're now thinking what exactly do I have to change. Well it's quite simple. Instead of reading directly into all3MomData, you're going to go via a smaller variable which is the same size as the slice, i.e temp = all3MomData(0,*,*) FOR i=0, 1 DO BEGIN OpenR, lun, /GET_LUN, names3Mom[i] readf, lun, temp all3MomData[i,*,*] = temp Free_Lun, lun ENDFOR I've taken a bit of a liberty with your code, here. You used specific values for your file numbers (openr, 1, ...). However if for some reason the value of 1 was being used already by some other program, then your code would have mucked up their code. It is better to use the lun, /GET_LUN construction as it basically asks IDL to find a free number and then passes this number into the variable lun. Also try to use Free_Lun instead of just Close as Close does not properly free up the file. Have a look at the IDL help files about this, but they're not crucial points. My explanation has been a bit ropey, I'm sorry, but this help you stop the problem at least. Anyway I've just realised you're at Oxford and well seeing as I'm at Caius College Cambridge, I'm sure I shouldn't have helped you :-) Cheers, Phil Phil Aldis Gonville and Caius College Cambridge CB2 1TA Phone: 01223 520026 E-Mail : philaldis@yahoo.com
Hi Steven, You are trying to read into a subscripted variable which is passed to readf by value and not by reference so when readf finishes all3MomData is unchanged. this should work (assumimg that all3MomData matches the file format, size+data type) temp=reform(all3MomData[0,*,*]) for i=0, n_elements(names3Mom)-1 do begin openr, lun, names3Mom[i], /get_lun readf, lun, temp all3MomData[i,*,*]=temp free_lun, lun endfor good luck, Ivan Steven Riley wrote: > > Assuming names3Mom is a 1D array of strings (of valid filenames) is there > any reason why the following code will not read many files into a 3d array? > > FOR I=0,1 DO BEGIN > close,1 & openr,1,names3Mom(I) > readf,1,all3MomData(I,*,*) > END > > I get no error messages, but when I look at my array it contains no data...? > I'm quite sure the files being read contain data. Is this some inherent > problem with readf and arrays with order > 2? > > Steven > > -- > > Linacre College > Oxford > OX1 3JA > > 01865 - (2)81229 (W) > 01865 - 209295 (H) > > www.linacre.ox.ac.uk/student_pages/steven_riley -- Ivan Zimine Dpt. of Radiology (MRI), Geneva University Hospitals email: ivan.zimine@physics.unige.ch tel. : (+41 22) 372 70 70