def undistort(self, rgb=True, day_only=True):    
        """
        Undistort the raw image, set rgb, red, rbr, cos_g
        Input: rgb and day_only flags
        Output: rgb, red, rbr, cos_g will be specified.
        """           
        #####get the image acquisition time, this need to be adjusted whenever the naming convention changes 
        t_cur=datetime.strptime(self.fn[-18:-4],'%Y%m%d%H%M%S');     
        t_std = t_cur-timedelta(hours=5)     #####adjust UTC time into local standard time            
        sz = 90-ps.get_altitude(self.cam.lat,self.cam.lon,t_std); sz*=deg2rad;
        self.sz = sz
        if day_only and sz>85*deg2rad:
            return
             
        saz = 360-ps.get_azimuth(self.cam.lat,self.cam.lon,t_std); saz=(saz%360)*deg2rad;
        self.saz = saz

        try:
            im0=plt.imread(self.fn);
        except:
            print('Cannot read file:', self.fn)
            return None
        im0=im0[self.cam.roi]
        im0[~self.cam.valid0,:]=0

        cos_sz=np.cos(sz)        
        cos_g=cos_sz*np.cos(self.cam.theta0)+np.sin(sz)*np.sin(self.cam.theta0)*np.cos(self.cam.phi0-saz);   
        
        red0=im0[:,:,0].astype(np.float32); red0[red0<=0]=np.nan
        rbr0=(red0-im0[:,:,2])/(im0[:,:,2]+red0)        

        if np.nanmean(red0[(cos_g>0.995) & (red0>=1)])>230: 
            mk=cos_g>0.98
            red0[mk]=np.nan 
            rbr0[mk]=np.nan 
        
        rbr=st.fast_bin_average2(rbr0,self.cam.weights); 
        rbr=st.fill_by_mean2(rbr,7, mask=(np.isnan(rbr)) & self.cam.valid) 
        self.rbr=rbr
        
        red0-=st.rolling_mean2(red0,300,ignore=np.nan)
        red=st.fast_bin_average2(red0,self.cam.weights); 
        red=st.fill_by_mean2(red,7, mask=(np.isnan(red)) & self.cam.valid)
        red[red>50]=50; red[red<-50]=-50
        red=(red+51)*2.5+0.5;       
        self.red=red.astype(np.uint8)
                
        if rgb:             
            im=np.zeros((self.cam.ny,self.cam.nx,3),dtype=im0.dtype)   
            for i in range(3):
                im[:,:,i]=st.fast_bin_average2(im0[:,:,i],self.cam.weights); 
                im[:,:,i]=st.fill_by_mean2(im[:,:,i],7, ignore=0, mask=(im[:,:,i]==0) & (self.cam.valid))
#                 im[:,:,i]=st.fill_by_mean2(im[:,:,i],7, ignore=0, mask=np.isnan(red))   
            im[red<=0]=0
            self.rgb=im   
Example #2
0
    def undistort(self, rgb=True, day_only=True):
        """
        Undistort the raw image, set rgb, red, rbr, cos_g
        Input: rgb and day_only flags
        Output: rgb, red, rbr, cos_g will be specified.
        """
        #####get the image acquisition time, this need to be adjusted whenever the naming convention changes
        self.time = datetime.strptime(self.fn[-18:-4], '%Y%m%d%H%M%S')
        t_std = self.time - timedelta(
            hours=5)  #####adjust UTC time into local standard time
        sz = 90 - ps.get_altitude(self.cam.lat, self.cam.lon, t_std)
        sz *= deg2rad
        self.sz = sz
        if day_only and sz > 75 * deg2rad:
            return

        saz = 360 - ps.get_azimuth(self.cam.lat, self.cam.lon, t_std)
        saz = (saz % 360) * deg2rad
        self.saz = saz

        try:
            im0 = plt.imread(self.fn)
        except:
            print('Cannot read file:', self.fn)
            return None
        im0 = im0[self.cam.roi]
        im0[~self.cam.valid0, :] = 0

        cos_sz = np.cos(sz)
        cos_g = cos_sz * np.cos(self.cam.theta0) + np.sin(sz) * np.sin(
            self.cam.theta0) * np.cos(self.cam.phi0 - saz)

        red0 = im0[:, :, 0].astype(np.float32)
        red0[red0 <= 0] = np.nan
        rbr0 = (red0 - im0[:, :, 2]) / (im0[:, :, 2] + red0)

        if np.nanmean(red0[(cos_g > 0.995) & (red0 >= 1)]) > 230:
            mk = cos_g > 0.98
            red0[mk] = np.nan
            rbr0[mk] = np.nan

        rbr = st.fast_bin_average2(rbr0, self.cam.weights)
        rbr = st.fill_by_mean2(rbr, 7, mask=(np.isnan(rbr)) & self.cam.valid)
        rbr[self.cam.invalid] = np.nan
        #         if np.nanmean(rbr[rbr>-0.7])<0:
        #             hist=np.histogram(rbr,bins=100,range=[-0.6,0.15]); bins=(hist[1][:-1]+hist[1][1:])/2
        #             cum=np.cumsum(hist[0]); cumi=np.cumsum(hist[0][::-1])
        #             x1=bins[np.argmax(cum>0.02*cum[-1])]; x2=bins[99-np.argmax(cumi>0.02*cum[-1])]
        #             print(x1,x2,np.nanmean(rbr[rbr>-0.7]))
        #             if (x2-x1)>=0.15 or ((x2-x1)>0.1 and x1<-0.2):
        #                 rbr=-0.6+0.75/(x2-x1)*(rbr-x1);
        self.rbr = rbr

        red0 -= st.rolling_mean2(red0, 300, ignore=np.nan)
        red = st.fast_bin_average2(red0, self.cam.weights)
        red = st.fill_by_mean2(red, 7, mask=(np.isnan(red)) & self.cam.valid)
        red[red > 50] = 50
        red[red < -50] = -50
        red = (red + 51) * 2.5 + 0.5
        red[self.cam.invalid] = 0
        self.red = red.astype(np.uint8)

        if rgb:
            im = np.zeros((self.cam.ny, self.cam.nx, 3), dtype=im0.dtype)
            for i in range(3):
                im[:, :, i] = st.fast_bin_average2(im0[:, :, i],
                                                   self.cam.weights)
                im[:, :, i] = st.fill_by_mean2(im[:, :, i],
                                               7,
                                               ignore=0,
                                               mask=(im[:, :, i] == 0) &
                                               (self.cam.valid))
#                 im[:,:,i]=st.fill_by_mean2(im[:,:,i],7, ignore=0, mask=np.isnan(red))
            im[(red <= 0) | (self.cam.invalid)] = 0
            self.rgb = im
Example #3
0
    def undistort(self, cam, rgb=True, day_only=True):
        """
        Undistort the raw image, set rgb, red, rbr, cos_g
        Input: rgb and day_only flags
        Output: rgb, red, rbr, cos_g will be specified.
        """
        #####get the image acquisition time, this need to be adjusted whenever the naming convention changes
        #ts=localToUTCtimestamp(datetime.strptime(self.fn[-18:-4],'%Y%m%d%H%M%S'),cam.cam_tz)    #get UTC timestamp
        #t_std=UTCtimestampTolocal(ts, pytz.timezone("UTC"))                                      #create datetime in UTC
        t_std = localToUTC(datetime.strptime(self.fn[-18:-4], '%Y%m%d%H%M%S'),
                           self.cam_tz)
        #t_std=datetime.strptime(self.fn[-18:-4],'%Y%m%d%H%M%S');
        #t_std = t_local + timedelta(hours=5) #replace(tzinfo=timezone(-timedelta(hours=5)))
        #print("\tUndistortion->t_local=%s\t\tt_std=%s\n" % (str(t_local),str(t_std)))
        print("\tUndistortion->t_std=%s\n" % (str(t_std)))
        gatech = ephem.Observer()
        gatech.date = t_std.strftime('%Y/%m/%d %H:%M:%S')
        gatech.lat, gatech.lon = str(self.lat), str(self.lon)
        sun = ephem.Sun()
        sun.compute(gatech)
        sz = np.pi / 2 - sun.alt
        self.sz = sz
        if day_only and sz > 75 * deg2rad:
            print("Night time (sun angle = %f), skipping\n" % sz)
            return

        saz = 180 + sun.az / deg2rad
        saz = (saz % 360) * deg2rad
        self.saz = saz

        try:
            im0 = plt.imread(self.fn)
        except:
            print('Cannot read file:', self.fn)
            return None
        im0 = im0[cam.roi]

        cos_sz = np.cos(sz)
        cos_g = cos_sz * np.cos(cam.theta0) + np.sin(sz) * np.sin(
            cam.theta0) * np.cos(cam.phi0 - saz)

        red0 = im0[:, :, 0].astype(np.float32)
        red0[red0 <= 0] = np.nan
        #         rbr0=(red0-im0[:,:,2])/(im0[:,:,2]+red0)
        if np.nanmean(red0[(cos_g > 0.995) & (red0 >= 1)]) > 230:
            mk = cos_g > 0.98
            red0[mk] = np.nan

        xsun, ysun = np.tan(sz) * np.sin(saz), np.tan(sz) * np.cos(saz)
        self.sun_x, self.sun_y = int(
            0.5 * self.nx * (1 + xsun / cam.max_tan)), int(
                0.5 * self.ny * (1 + ysun / cam.max_tan))

        invalid = ~cam.valid

        red = st.fast_bin_average2(red0, cam.weights)
        red = st.fill_by_mean2(red, 7, mask=(np.isnan(red)) & cam.valid)
        red[invalid] = np.nan
        #         plt.figure(); plt.imshow(red); plt.show();
        red -= st.rolling_mean2(red, int(self.nx // 6.666))
        red[red > 50] = 50
        red[red < -50] = -50
        red = (red + 50) * 2.54 + 1
        red[invalid] = 0
        self.red = red.astype(np.uint8)
        #         plt.figure(); plt.imshow(self.red); plt.show();

        if rgb:
            im = np.zeros((self.ny, self.nx, 3), dtype=im0.dtype)
            for i in range(3):
                im[:, :, i] = st.fast_bin_average2(im0[:, :, i], cam.weights)
                im[:, :,
                   i] = st.fill_by_mean2(im[:, :, i],
                                         7,
                                         ignore=0,
                                         mask=(im[:, :, i] == 0) & (cam.valid))


#                 im[:,:,i]=st.fill_by_mean2(im[:,:,i],7, ignore=0, mask=np.isnan(red))
            im[self.red <= 0] = 0

            #             plt.figure(); plt.imshow(im); plt.show()
            self.rgb = im
Example #4
0
    def undistort(self, cam, rgb=True, day_only=True):    
        """
        Undistort the raw image, set rgb, red, rbr, cos_g
        Input: rgb and day_only flags
        Output: rgb, red, rbr, cos_g will be specified.
        """           
        #####get the image acquisition time, this need to be adjusted whenever the naming convention changes 
        self.time=datetime.strptime(self.fn[-18:-4],'%Y%m%d%H%M%S');     
        gatech = ephem.Observer(); 
        gatech.date = self.time.strftime('%Y/%m/%d %H:%M:%S')
        gatech.lat, gatech.lon = str(self.lat),str(self.lon)
        sun=ephem.Sun()  ; sun.compute(gatech);        
        sz = np.pi/2-sun.alt; 
        self.sz = sz
        if day_only and sz>75*deg2rad:
            return
             
        saz = 180+sun.az/deg2rad; saz=(saz%360)*deg2rad;
        self.saz = saz

        try:
            im0=plt.imread(self.fn);
        except:
            print('Cannot read file:', self.fn)
            return None     
        im0=im0[cam.roi]

        cos_sz=np.cos(sz)        
        cos_g=cos_sz*np.cos(cam.theta0)+np.sin(sz)*np.sin(cam.theta0)*np.cos(cam.phi0-saz);   
        
        red0=im0[:,:,0].astype(np.float32); red0[red0<=0]=np.nan
        rbr0=(red0-im0[:,:,2])/(im0[:,:,2]+red0)        
        if np.nanmean(red0[(cos_g>0.995) & (red0>=1)])>30: 
            mk=cos_g>0.98
            red0[mk]=np.nan 
            rbr0[mk]=np.nan 
        
        xsun, ysun = np.tan(sz)*np.sin(saz), np.tan(sz)*np.cos(saz)
        self.sun_x,self.sun_y = int(0.5*self.nx*(1+xsun/cam.max_tan)), int(0.5*self.ny*(1+ysun/cam.max_tan))

        invalid=~cam.valid
        rbr=st.fast_bin_average2(rbr0,cam.weights); 
        rbr=st.fill_by_mean2(rbr,7, mask=(np.isnan(rbr)) & cam.valid) 
        rbr[invalid]=np.nan              
        rbr -= st.rolling_mean2(rbr,int(self.nx//6.666),ignore=np.nan)
        rbr[rbr>0.08]=0.08; rbr[rbr<-0.08]=-0.08;
        rbr=(rbr+0.08)*1587.5+1;
        rbr[invalid]=0              
        self.rbr=rbr.astype(np.uint8)
        
        red=st.fast_bin_average2(red0,cam.weights); 
        red=st.fill_by_mean2(red,7, mask=(np.isnan(red)) & cam.valid)
        red[invalid]=np.nan;
        red -= st.rolling_mean2(red,int(self.nx//6.666))
        red[red>50]=50; red[red<-50]=-50
        red=(red+50)*2.54+1; 
        red[invalid]=0;
        self.red=red.astype(np.uint8)

        if rgb:             
            im=np.zeros((self.ny,self.nx,3),dtype=im0.dtype)   
            for i in range(3):
                im[:,:,i]=st.fast_bin_average2(im0[:,:,i],cam.weights); 
                im[:,:,i]=st.fill_by_mean2(im[:,:,i],7, ignore=0, mask=(im[:,:,i]==0) & (cam.valid))
#                 im[:,:,i]=st.fill_by_mean2(im[:,:,i],7, ignore=0, mask=np.isnan(red))   
            im[self.red<=0]=0
            self.rgb=im