Simplify if statements and for loops inside for loops [message #83677] |
Fri, 29 March 2013 09:07  |
morganlsilverman
Messages: 46 Registered: February 2013
|
Member |
|
|
Hello,
I'm going for loop crazy and can't figure out how to simplify. It just keeps getting longer and more complicated. The gist of what I'm trying to do is match each element of mdate = string[14] to array adatea=string[74] (find matching dates). Once I know where if any place it matches I want to see if criteria are met to actually use that profile. This is where I go crazy. In some cases because there could be more than one profile on a single day, index might equal more than one location. For each of those locations I need to make sure there are at least 15 non-NaN values in the array and if so, calculate the difference between each altitude level and then if those differences do not exceed 500m I can use the profile do more calculations but if not then I just want to move onto the next matching date. Is there anyway to simplify this? Thank you.
Sincerely,
Morgan
for k=2,2 do begin
indexa = where(strmatch(strmid(adatea,0,8),strmid(mdate(k),0,8)) eq 1,num)
s = size(adatea)
ncol = s(1)
col = indexa mod ncol
row = indexa/ncol
if num eq 1 then begin ; If matches then go through profile critera
inda = where(finite(aalta(col,row,*)), count) ; Count number of non-NaN altitudes
l = 0
endif else begin
if num gt 1 then begin
for l=0,n_elements(num)-1 do begin
inda(l) = where(finite(aalta(col(l),row(l),*)), count)
endfor
if (n_elements(inda(l)) ge 15 ) then begin
dcount=0
diffa = fltarr(50)
for k=0,count-2 do begin ; Calculate difference between altitude levels
diffa(k) = aalta(col,row,inda(k+1))-aalta(col,row,inda(k))
dcount = dcount+1
endfor
diffa = diffa(0:dcount-1)
; Only interested in profiles with altitude differences less than 500 m
if (max(abs(diffa)) lt 500.0) then begin
; Calculate potential temperature for ACARS
for m=0,49 do begin
aThetaa(icount,jcount,m) = PotentialTemperature(apresa(col,row,m), atempa(col,row,m))
endfor
icount = icount + 1
endif
endif
endif
endfor
|
|
|
Re: Simplify if statements and for loops inside for loops [message #83811 is a reply to message #83677] |
Fri, 29 March 2013 15:16  |
Russell[1]
Messages: 101 Registered: August 2011
|
Senior Member |
|
|
Just to help your thread stay alive. Again, don't ask people to dissect your code, especially code that is more than 5 lines long. Instead state the data you have, making sure to say the dimensionality, datatype, and give examples (if possible). Then state what you want of your data.
R
On Friday, March 29, 2013 12:07:51 PM UTC-4, morganls...@gmail.com wrote:
> Hello,
>
>
>
> I'm going for loop crazy and can't figure out how to simplify. It just keeps getting longer and more complicated. The gist of what I'm trying to do is match each element of mdate = string[14] to array adatea=string[74] (find matching dates). Once I know where if any place it matches I want to see if criteria are met to actually use that profile. This is where I go crazy. In some cases because there could be more than one profile on a single day, index might equal more than one location. For each of those locations I need to make sure there are at least 15 non-NaN values in the array and if so, calculate the difference between each altitude level and then if those differences do not exceed 500m I can use the profile do more calculations but if not then I just want to move onto the next matching date. Is there anyway to simplify this? Thank you.
>
>
>
> Sincerely,
>
> Morgan
>
>
>
> for k=2,2 do begin
>
> indexa = where(strmatch(strmid(adatea,0,8),strmid(mdate(k),0,8)) eq 1,num)
>
> s = size(adatea)
>
> ncol = s(1)
>
> col = indexa mod ncol
>
> row = indexa/ncol
>
> if num eq 1 then begin ; If matches then go through profile critera
>
> inda = where(finite(aalta(col,row,*)), count) ; Count number of non-NaN altitudes
>
> l = 0
>
> endif else begin
>
> if num gt 1 then begin
>
> for l=0,n_elements(num)-1 do begin
>
> inda(l) = where(finite(aalta(col(l),row(l),*)), count)
>
> endfor
>
> if (n_elements(inda(l)) ge 15 ) then begin
>
> dcount=0
>
> diffa = fltarr(50)
>
> for k=0,count-2 do begin ; Calculate difference between altitude levels
>
> diffa(k) = aalta(col,row,inda(k+1))-aalta(col,row,inda(k))
>
> dcount = dcount+1
>
> endfor
>
> diffa = diffa(0:dcount-1)
>
> ; Only interested in profiles with altitude differences less than 500 m
>
> if (max(abs(diffa)) lt 500.0) then begin
>
> ; Calculate potential temperature for ACARS
>
> for m=0,49 do begin
>
> aThetaa(icount,jcount,m) = PotentialTemperature(apresa(col,row,m), atempa(col,row,m))
>
> endfor
>
> icount = icount + 1
>
> endif
>
> endif
>
> endif
>
> endfor
|
|
|