我如何将Matlab代码转换成python:UT(通用时间)到LST(本地恒星实时)的时间转换,日期tim递增

2024-09-27 00:14:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在matlab中有一个用户定义的函数UT2LSTmine。我想将相同的函数转换成Python,以与在matlab中完全相同的方式工作。我是python编程新手。我试了很多。我还使用了Matlab到Python(OMPC)的在线转换应用程序。它不符合我的要求。你知道吗

该函数应考虑开始世界时(UT)和转换为本地恒星时(LST),并考虑递增日期和时间。你知道吗

函数用于将GBD的UT小时、分钟、秒转换为LST小时、分钟、秒 此函数从对象获取输入,例如yr、mn、dd、uth、htm、uts。返回转换后的lsth、lstm、lsts。此功能还检查24小时输入并自动增加一天。你知道吗

function [t] = UT2LSTmine(varargin)

yr = varargin{1};
mn = varargin{2};
dd = varargin{3};
uth = varargin{4};
utm = varargin{5};
uts = varargin{6};

t=[yr,mn,dd,uth,utm,uts];

format long g ;

gbdlong = 5.06138;

%%% Compute Julian Date for the given date

x=[t(1,1), t(1,2), t(1,3), 0,0,0.5];
[~,indx]=ismember(x,t,'rows');%for 24 hours check
if(indx > 0)
t(indx:end,3)=t(indx:end,3)+1;%Incrementing the day manually commented                       for RTLSDR on 30th MA
x=[t(indx,1), t(indx,2), t(indx,3), 23,59,59];
end

%%% Compute Local time & UT for the given IST
utd = t(:,4) + (t(:,5)./60) + (t(:,6)/3600);

for i=1:1:length(t)

julday(i) = juliandate(t(i,1),t(i,2),t(i,3)) + 0.5000000;
jd(i) = julday(i) - 0.5; %%% JD @ 0h UT
T(i) = (jd(i) - 2451545.0)/36525;  %%% Time interval since 2000 jan 1    
12h UT


%%% Compute Gmst @ 0h UT
gmst0(i) = 24110.54841 + (8640184.812866 * T(i)) + (0.093104 * T(i) *  
T(i)) - (0.000006200 * T(i) * T(i) * T(i)); 

%%% Convert Gmst to Hours

gmst0(i) = gmst0(i) / (86400.0); %%% gmst0 in days
gmst0(i) = gmst0(i) - floor(gmst0(i)); %%% Get the fraction of the day
gmst0(i) =  gmst0(i) * 24.0;  %%% Convert into hours
if (gmst0(i) < -0.0000001);
gmst0(i) = gmst0(i) + 24;
end

%%% Compute equivalent of mean sidreal time interval from oh to UT of
%%% intrest


mst(i) = utd(i) * 1.0027379094;
if ( mst(i) - 24.0 > 0.000001);
mst(i) = mst(i) - 24.0;
end

%%% Greenwitch mean sidreal time @ required UT

gmst(i) = gmst0(i) + mst(i);
if ( gmst(i) - 24.0 > 0.000001);
gmst(i) =gmst(i) - 24.0;
end

%%% Local mean sidreal time

lst(i) = gmst(i) + gbdlong;
if ( lst(i) > 24.0);    
lst(i) = lst(i) - 24.0;    
else if ( lst(i) < 0);    
lst(i) = lst(i) + 24.0;
else    
lst(i) = lst(i);
end

lsth(i) = floor(lst(i));
lstm1(i) = (lst(i) - floor(lsth(i))) * 60.0;
lstm(i) = floor(lstm1(i));
lsts(i) = (lstm1(i) - floor(lstm(i))) * 60.0;


% IST corresponding to the given UT

istd(i) = utd(i) + 5.5; 
if ( istd(i) > 24.0);    
ist(i) = istd(i) - 24.0;    
elseif ( istd(i) < 0);    
ist(i) = istd(i) + 24.0;
else    
ist(i) = istd(i);
end
isth(i) = floor(ist(i));
istm(i) = (ist(i) - floor(ist(i))) * 60;
ists(i) = (istm(i) - floor(istm(i))) * 60;
t(i,4)=lsth(i);t(i,5)=lstm(i);t(i,6)=lsts(i);

end

%%%%%%%%%%%%%%%%%%For checking 24hours condition for LST%%%%%

 [~,index]=ismember(x,t,'rows');%for 24 hours check

 if(indx > 0 & index == 0)

 t(indx:end,3)=t(indx:end,3)-1;%If LST not completed 24 hours then do 1     
 day substraction do to the increment of UT 

 end

我试过这个-

 import numpy as np
 def UT2LSTmine(*args):
#Function to convert the UT hours, min, sec to LST hours, min, sec at GBD
#  This fuction takes the input from the obsever such as
#  yr,mn,dd,uth,htm,uts. Returs the converted lsth, lstm, lsts
#This fuction also checks for 24 hours input and automatically increments
#the day
 yr = args[:,0]
 mn = args[:,1]
 dd = args[:,2]
 uth = args[:,3]
 utm = args[:,4]
 uts = args[:,5]

 t = [yr, mn, dd, uth, utm, uts]

 format('long', 'g')
 #gbdlong = 5.162351852;%for GBD

 gbdlong = 5.06138#for IIT Indore

 #########Compute Julian Date for the given date

 x = [t(1, 1), t(1, 2), t(1, 3), 0, 0, 0.5]
 ismember(x, t, 'rows')  #for 24 hours check
 for i in x:
    index = np.where(t==i)[0]
    if index.size == 0:
        yield 0
    else:
        yield index

    if (index > 0): 
        t[slice[index:stop], 3]=  t(slice[index:end], 3) + 1           #Incrementing the day manually commented for
    #RTLSDR on 30th MA
     x = [t(index, 1), t(index, 2), t(index, 3), 23, 59, 59]


     ##### Compute Local time & UT for the given IST
     utd = t(slice[:], 4) + (t(slice[:], 5) / 60) + (t(slice[:], 6) / 3600)

     for i in slice[1:1:length(t)]:

        julday[i] = juliandate(t(i, 1), t(i, 2), t(i, 3)) + 0.5000000
        jd[i]= julday(i) - 0.5        #%% JD @ 0h UT
        T[i] = (jd(i) - 2451545.0) / 36525        #%% Time interval      since 2000 jan 1 12h UT

         ########Compute Gmst @ 0h UT
         gmst0[i] = 24110.54841 + (8640184.812866 * T(i)) + (0.093104 *     T(i) * T(i)) - (0.000006200 * T(i) * T(i) * T(i))

          ####Convert Gmst to Hours
         gmst0[i] = gmst0(i) / (86400.0)        #%% gmst0 in days
         gmst0[i]= gmst0(i) - floor(gmst0(i))        #%% Get the  fraction of the day
          gmst0[i]= gmst0(i) * 24.0        #%% Convert into hours
          if (gmst0(i) < -0.0000001):
            gmst0[i]= gmst0(i) + 24 
         ###Compute equivalent of mean sidreal time interval from oh to  UT of intrest
            mst[i] = utd(i) * 1.0027379094
            if (mst(i) - 24.0 > 0.000001):
                mst[i] = mst(i) - 24.0
                end

        ###Greenwitch mean sidreal time @ required UT

                gmst[i] = gmst0(i) + mst(i)
                if (gmst(i) - 24.0 > 0.000001):
                    gmst[i] = gmst(i) - 24.0
           # ####Local mean sidreal time

                    lst[i] = gmst(i) + gbdlong
                    if (lst(i) > 24.0):
                        lst[i]= lst(i) - 24.0
                    elif (lst(i) < 0):
                        lst[i]= lst(i) + 24.0
                    else:
                        lst[i]= lst(i)

                        lsth[i]= floor(lst(i))
                        lstm1[i]= (lst(i) - floor(lsth(i))) * 60.0
                        lstm[i]= floor(lstm1(i))
                        lsts[i]= (lstm1(i) - floor(lstm(i))) * 60.0


                        # IST corresponding to the given UT

                        istd[i]= utd(i) + 5.5
                        if (istd(i) > 24.0):
                            ist[i]= istd(i) - 24.0
                        elif (istd(i) < 0):
                            ist[i] = istd(i) + 24.0
                        else:
                            ist[i]= istd(i)

                            isth[i] = floor(ist(i))
                            istm[i]= (ist(i) - floor(ist(i))) * 60
                            ists[i]= (istm(i) - floor(istm(i))) * 60
                            t[i, 4]= lsth(i)
                            t[i, 5] = lstm(i)
                            t[i, 6] = lsts(i)


          ############For checking 24hours condition for LST ###################

                            ismember(x, t,'rows')                                         #for 24 hours check

                            if (logical_and(indx > 0, index == 0)):

                                t[slice[indx:end], 3]=                                               t(slice[indx:end], 3) - 1                                #If LST not completed     24 hours then do 1 day substraction do to the increment of UT

Tags: thetoforindexifendutmst
1条回答
网友
1楼 · 发布于 2024-09-27 00:14:43

在没有彻底检查的情况下,我已经发现了多个问题。 从编程风格来看,Matlab代码已经过时了;if语句后面是半列;函数语句没有end语句。你知道吗

此外,该函数还实现了一个juliandate()带double的函数,这个函数不再受支持了。因此,我的matlab版本(R2018b)抛出了一个错误。 我看到juliandate()-函数实际上是复制到python脚本中的,据我所知,它不是python中的函数。你知道吗

我没有太多的Python知识。但我知道缩进很重要。下面的python代码是:

if (true):
  doSomething()

  if (true):
    doSomethingElse()

等效于以下matlab代码:

if true
  doSomething()
    if true
      doSomethingElse()
    end
end

这显然不是我们想要的行为。你知道吗

听起来像是你找到了一个matlab函数,你需要用python。 但是用在线工具转换脚本对我来说是一个很大的危险信号。你知道吗

我在谷歌上搜索了一下发现: Github: Sidereal-Time-Calculator。 听起来像是你需要的。这不是一个函数,但这是可能的,没有太多的工作。你知道吗

相关问题 更多 >

    热门问题