IDL三个作业程序

原创3个编程题:包括题意、程序
将普通时间转换成GPS时间(getgpstime.pro)

;将普通时间转换为GPS系统时间
function GetGPSTime,year,month,day,hour,minute,second
  weekno=0
  dayofy=day
  dinmth=[0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  ;检查时间是否超限
  if (year < 1981 || month < 1 || month > 12 || day < 1 || day > 31) then weekno=0
  ;计算为今年中的哪一天
  if (month eq 1) then begin dayofy=day
  endif else begin
    dayofy = 0
    for m=1,month-1 do begin
      dayofy += dinmth(m)
      if(m eq 2) then begin
        if(year mod 4 eq 0 && year mod 100 ne 0 || year mod 400 eq 0) then  dayofy += 1
      endif     
    endfor
    dayofy += day
  endelse
  ;计算GPS周与GPS秒
  ttday=360
  for yr=1981,year-1 do begin
    ttday += 365
    if (yr mod 4 eq 0 && yr mod 100 ne 0 || yr mod 400 eq 0) then  ttday += 1
  endfor
  ttday += dayofy
  weekno = ttday / 7
  dayofw = ttday - 7*weekno
  gpstime = hour * 3600 + minute * 60 + second + dayofw * 86400
  if(weekno gt 1024) then weekno -= 1024
  gps = [weekno,gpstime]
  return,gps
end

;测试一下
year=0 & month=0 & day=0 & hour=0 & minute=0 & second=0
read,year,month,day,hour,minute,second,prompt="请输入时间(年,月,日,时,分,秒):",Format='(4I,2I,2I,2I,2I,2I)'
gpstime = GetGPSTime(year,month,day,hour,minute,second)
print,gpstime
end

读取YUMA星历数据(yuma.pro)
YUMA星历地址:http://www.navcen.uscg.gov/gps/current/current.alm

;读取YUMA星历数据
YUMAData = {ID:0, Health:0, Eccentricity:0.0, TimeOfApplicability:0, OrbitalInclination:0.0, RateOfRightAscen:0.0, SQRTA:0.0, RightAscenAtWeek:0.0, ArgumentOfPerigee:0.0, MeanAnom:0.0, Af0:0.0, Af1:0.0, week:0 }
yumas = make_array(32,value=YUMAData)
filters = ['*.txt;*.txt']
File = Dialog_PickFile(/Read,filter=filters)
OpenR, YumaFile, File,/Get_Lun
id='' & hea='' & ecc='' & tim='' & orb='' & rat='' & sqr='' & rig='' & arg='' & mea='' & af0='' & af1='' & wee=''
for i=1,31 do begin
ReadF,YumaFile,id,id,hea,ecc,tim,orb,rat,sqr,rig,arg,mea,af0,af1,wee
s = strtrim(id,2)
s_array = byte(s)
yumas[i].ID = string(s_array[28:29])

s = strtrim(hea,2)
s_array = byte(s)
yumas[i].Health = string(s_array[28:30])

s = strtrim(ecc,2)
s_array = byte(s)
yumas[i].Eccentricity = string(s_array[28:44])

s = strtrim(tim,2)
s_array = byte(s)
yumas[i].TimeOfApplicability = string(s_array[27:37])

s = strtrim(orb,2)
s_array = byte(s)
yumas[i].OrbitalInclination = string(s_array[28:39])

s = strtrim(rat,2)
s_array = byte(s)
yumas[i].RateOfRightAscen = string(s_array[27:44])

s = strtrim(sqr,2)
s_array = byte(s)
yumas[i].SQRTA = string(s_array[28:38])

s = strtrim(rig,2)
s_array = byte(s)
yumas[i].RightAscenAtWeek = string(s_array[27:44])

s = strtrim(arg,2)
s_array = byte(s)
yumas[i].ArgumentOfPerigee = string(s_array[28:38])

s = strtrim(mea,2)
s_array = byte(s)
yumas[i].MeanAnom = string(s_array[27:44])

s = strtrim(af0,2)
s_array = byte(s)
yumas[i].Af0 = string(s_array[27:44])

s = strtrim(af1,2)
s_array = byte(s)
yumas[i].Af1 = string(s_array[27:44])

s = strtrim(wee,2)
s_array = byte(s)
yumas[i].Week = string(s_array[29:31])

if(i ne 31) then ReadF,YumaFile,id

print,yumas[i]
endfor

Free_Lun,YumaFile
end

解算九宫格(shudu.pro)
这个程序我写过非常多个版本了,用C、.NET、Delphi、WEB均实现过,现在正学习IDL,于是又写了一个IDL版本。
首先说明一下九宫格数独,就是在9*9个宫格中填上1-9这九个数,使1-9每个数字在每一行、每一列和每一宫中都只出现一次。

;解算九宫格数独
;判断九宫格是否合格
function isvalid,i,j,pu,query
  n = pu[i,j]
  for t=0,8 do begin
    if((t ne i && pu[t,j] eq n) || (t ne j && pu[i,t] eq n)) then return,0 ;0-9的数字,每行每列都不能重复
  endfor
  for t=query[i],query[i]+2 do begin  ;9个宫的3¡Á3里也不能重复
    for u=query[j],query[j]+2 do begin
      if ((t ne i || u ne j) && (pu[t,u] eq n)) then return,0
    endfor
  endfor
  return,1
end
;解算九宫格
pro try,n,pu,query
  if(n eq 81) then begin 
    out,pu
    return
  endif
  i = n / 9 
  j = n mod 9
  if(pu[i,j] ne 0) then begin
    try,n+1,pu,query
    return
  endif
  for k=0,8 do begin
    pu[i,j]++
    if(isvalid(i,j,pu,query)) then try,n+1,pu,query
  endfor
  pu[i,j] = 0
end

pro out,pu
  print,pu
end
;要解的九宫格数据,0表示待求的数
pu0 = [[0,8,0,0,0,3,0,9,0],[0,0,7,6,4,0,0,5,0],[3,2,0,0,0,7,4,6,0],[0,4,0,0,0,1,0,3,0],[2,0,3,0,0,6,8,0,1],[0,5,0,7,0,9,0,4,0],[0,7,1,3,0,0,0,8,9],[0,3,0,0,1,4,7,0,0],[0,6,0,9,0,0,0,1,0]]
query0 = [0, 0, 0, 3, 3, 3, 6, 6, 6]
try,0,pu0,query0
end