def Load_PKL_or_Compute_Kernels_With_GC_From_Other_Map(HDFfilename, Context, Band, MapPath): Band = Band.upper() path = Context['wdir'] pklz_fn = path + 'GC/' + HDFfilename + '_' + Band + 'Ker' + '.pklz' pklz2_fn = MapPath + 'GC/' + HDFfilename + '_' + Band + 'Ker' + '.pklz' if L_Files.exists(pklz_fn): print('## Using precomputed kernels for band', Band) Observation, tita = Load_PKL_Obs(HDFfilename, Context, Band) else: #read geocorrection from other map print('## Reading GeoCorr from %s for Band %s' % (MapPath, Band)) Context2 = L_Context.Load_Context(MapPath, L_Context.NO_NEIGHBORS) Dict_Bands2 = Load_PKL_or_HDF_Ells(HDFfilename, Context2) #read data and copy geocorrection print('## Reading Ells') Dict_Bands = Load_PKL_or_HDF_Ells(HDFfilename, Context, GC=False) Dict_Bands[Band]['GeoCorrection'] = Dict_Bands2[Band]['GeoCorrection'] #compute and save kernels Observation, tita = L_Kernels.Compute_Kernels(Dict_Bands, Band, Context, Mode='Cell') if SAVE_OBS: Save_PKL_Obs([Observation, tita], HDFfilename, Context, Band) return Observation, tita
def Load_Shifted_Band_Kernels(HDFfilename, Context, Band, Shift_X, Shift_Y): path = Context['wdir'] Band = Band.upper() pklz_fn = path + 'GC/' + HDFfilename + '_' + Band + 'Ker' + '.pklz' MSG = "*******************************************************************************\n" Dict_Bands = Load_SingleBand_Ells(HDFfilename, Context, Band) Dict_Bands[Band]['GeoCorrection']['X'] += Shift_X Dict_Bands[Band]['GeoCorrection']['Y'] += Shift_Y print('######################################') print('## PRECOMPUTING K for BAND', Band, '#####') print('######################################') MSG += 'Computing Kernels for Band' + Band Observation, tita = L_Kernels.Compute_Kernels(Dict_Bands, Band, Context) #LOG localtime = time.asctime(time.localtime(time.time())) + '\n' MSG += '\n' if Observation == {}: MSG += "Error loading " + HDFfilename + '\n' else: MSG += "Loaded " + HDFfilename + '\n' MSG += (not Observation['GeoCorrection']['OK'] ) * 'FAILED GeoCorrecting: ' + Observation['GeoCorrection'][ 'OK'] * 'GeoCorrection: ' MSG += '[' + str(Observation['GeoCorrection']['X']) + ', ' + str( Observation['GeoCorrection']['Y']) + ']\n' MSG += 'LSQRSols: H ' + str( Observation['LSQRSols']['H']['Sol']) + ' Error:' + str( Observation['LSQRSols']['H']['norm']) + '\n' MSG += ' V ' + str( Observation['LSQRSols']['V']['Sol']) + ' Error:' + str( Observation['LSQRSols']['V']['norm']) + '\n' logging.info(localtime + MSG) return Observation, tita
def Load_PKL_or_Compute_Kernels(HDFfilename, Context, Band, GC=True): path = Context['wdir'] Band = Band.upper() pklz_fn = path + 'GC/' + HDFfilename + '_' + Band + 'Ker' + '.pklz' MSG = "*******************************************************************************\n" print("Presto a cargar para banda", Band) if L_Files.exists(pklz_fn): print('## Using precomputed kernels for band', Band) Observation, tita = Load_PKL_Obs(HDFfilename, Context, Band) MSG += 'From PKL' else: print("Preparando a cargar las cosas....") Dict_Bands = Load_PKL_or_HDF_Ells(HDFfilename, Context, GC=GC) print('######################################') print('## PRECOMPUTING K for BAND', Band, '#####') print('######################################') MSG += 'Computing Kernels for Band' + Band Observation, tita = L_Kernels.Compute_Kernels(Dict_Bands, Band, Context) if SAVE_OBS: MSG += '\nSaving PKL' Save_PKL_Obs([Observation, tita], HDFfilename, Context, Band) localtime = time.asctime(time.localtime(time.time())) + '\n' MSG += '\n' if Observation == {}: MSG += "Error loading " + HDFfilename + '\n' else: MSG += "Loaded " + HDFfilename + '\n' + "Band: " + Band + '\n' MSG += (not Observation['GeoCorrection']['OK'] ) * 'FAILED GeoCorrecting: ' + Observation['GeoCorrection'][ 'OK'] * 'GeoCorrection: ' MSG += '[' + str(Observation['GeoCorrection']['X']) + ', ' + str( Observation['GeoCorrection']['Y']) + ']\n' MSG += 'LSQRSols: H ' + str( Observation['LSQRSols']['H']['Sol']) + ' Error:' + str( Observation['LSQRSols']['H']['norm']) + '\n' MSG += ' V ' + str( Observation['LSQRSols']['V']['Sol']) + ' Error:' + str( Observation['LSQRSols']['V']['norm']) + '\n' logging.info(localtime + MSG) return Observation, tita
def GeoCorrect_Band(Dict_Band, WA_Cords, Dict_Grids): print(" Correcting HDF georreference:") LandPropMargin_Grid = Dict_Grids['LandTypeMargin_Grid'] GeoCorr_margin = Dict_Grids['GeoCorr_margin'] dx = WA_Cords['dx'] dy = WA_Cords['dy'] cols = WA_Cords['cols'] rows = WA_Cords['rows'] nlt = Dict_Grids['nlt'] #Select ellipses that intersect at least 90% the ROI Orig_Ells = Dict_Band['Ell'].copy() Orig_n_ell = Dict_Band['n_ell'] Grid = L_Kernels.Compute_Elliptic_Kernels(Dict_Band, WA_Cords, MinimalProp=0.9) n_ell = Dict_Band['n_ell'] Band = Dict_Band['Band'] max_error = GeoCorr_margin * dx mdsp_x = 0 ctr_x = int(max_error / dx) dsp_x = ctr_x Mdsp_x = 2 * dsp_x ndsp_x = Mdsp_x - mdsp_x mdsp_y = 0 ctr_y = int(max_error / dy) dsp_y = ctr_y Mdsp_y = 2 * dsp_y ndsp_y = Mdsp_y - mdsp_y Score = np.zeros([ndsp_x, ndsp_y, 2]) #2 because of the H&V polarizations M = np.zeros([n_ell, nlt]) #one equation for ellipse, one varible for land_type bh = np.zeros(n_ell) bv = np.zeros(n_ell) found = False ok_corr = True min_score = 65500 #%% FIND GEO CORRECTION , step 1 while (not found): #evaluate arround dsp_x, dsp_y print(" Evaluating displacement: %dmH, %dmV" % ((dsp_x - ctr_x) * dx, (dsp_y - ctr_y) * dy)) for evx, evy in [[dsp_x - 1, dsp_y], [dsp_x + 1, dsp_y], [dsp_x, dsp_y - 1], [dsp_x, dsp_y + 1], [dsp_x, dsp_y]]: if (Score[evx, evy, 0] == 0): # not yet evaluated U = LandPropMargin_Grid[evx:evx + cols, evy:evy + rows] for i in range(n_ell): for lt in range(nlt): M[i, lt] = (Grid[:, :, i] * (U[:, :] == lt)).sum() bh[i] = Dict_Band['Ell'][i]['Tbh'] S = lsqr(M, bh) s = S[3] Score[evx, evy, 0] = s if (s < min_score): min_score = s best_x = evx best_y = evy else: # already evaluated s = Score[evx, evy, 0] if (s < min_score): min_score = s best_x = evx best_y = evy if ((dsp_x != best_x) or (dsp_y != best_y)): if ((best_x < mdsp_x + 2) or (best_x > Mdsp_x - 3) or (best_y < mdsp_y + 2) or (best_y > Mdsp_y - 3)): print("Reached the border when correcting georreference!!") print("Please, manually look at the image") print("Band", Band) print("**************************************************") found = True ok_corr = False #geocorrection is not good else: dsp_x = best_x dsp_y = best_y else: found = True print( " Found best geocorrection with %dmts error margin, improving correction..." % (dx / 2)) #%% FIND GEO CORRECTION , step 2 #found dsp_x, dsp_y # EVALUATE AT: + # + # + + o + + # + # + for evx, evy in [[dsp_x - 2, dsp_y], [dsp_x - 1, dsp_y], [dsp_x, dsp_y], [dsp_x + 1, dsp_y], [dsp_x + 2, dsp_y]]: if (Score[evx, evy, 1] == 0): # not yet evaluated U = LandPropMargin_Grid[evx:evx + cols, evy:evy + rows] for i in range(n_ell): for lt in range(nlt): M[i, lt] = (Grid[:, :, i] * (U[:, :] == lt)).sum() bh[i] = Dict_Band['Ell'][i]['Tbh'] bv[i] = Dict_Band['Ell'][i]['Tbv'] S = lsqr(M, bh) s = S[3] Score[evx, evy, 0] = s S = lsqr(M, bv) s = S[3] Score[evx, evy, 1] = s for evx, evy in [[dsp_x, dsp_y - 2], [dsp_x, dsp_y - 1], [dsp_x, dsp_y], [dsp_x, dsp_y + 1], [dsp_x, dsp_y + 2]]: if (Score[evx, evy, 1] == 0): # not yet evaluated U = LandPropMargin_Grid[evx:evx + cols, evy:evy + rows] for i in range(n_ell): for lt in range(nlt): M[i, lt] = (Grid[:, :, i] * (U[:, :] == lt)).sum() bh[i] = Dict_Band['Ell'][i]['Tbh'] bv[i] = Dict_Band['Ell'][i]['Tbv'] S = lsqr(M, bh) s = S[3] Score[evx, evy, 0] = s S = lsqr(M, bv) s = S[3] Score[evx, evy, 1] = s #%% adjust quadratic and find optimum x = np.array([dsp_x - 2, dsp_x - 1, dsp_x, dsp_x + 1, dsp_x + 2]) sx = Score[dsp_x - 2:dsp_x + 3, dsp_y, 0] cpx = np.polyfit(x, sx, 2) best_x = -cpx[1] / (2 * cpx[0]) y = np.array([dsp_y - 2, dsp_y - 1, dsp_y, dsp_y + 1, dsp_y + 2]) sy = Score[dsp_x, dsp_y - 2:dsp_y + 3, 0] cpy = np.polyfit(y, sy, 2) best_y = -cpy[1] / (2 * cpy[0]) corr_xh = best_x * dx - max_error corr_yh = best_y * dy - max_error #Found best correction for H POL #comparing with V POL sx = Score[dsp_x - 2:dsp_x + 3, dsp_y, 1] cpx = np.polyfit(x, sx, 2) best_x = -cpx[1] / (2 * cpx[0]) sy = Score[dsp_x, dsp_y - 2:dsp_y + 3, 1] cpy = np.polyfit(y, sy, 2) best_y = -cpy[1] / (2 * cpy[0]) corr_xv = best_x * dx - max_error corr_yv = best_y * dy - max_error corr_x = (corr_xh + corr_xv) / 2 corr_y = (corr_yh + corr_yv) / 2 corr_diff = np.sqrt((corr_xh - corr_xv)**2 + (corr_yh - corr_yv)**2) print( " Improved geocorrection: %.0fm, %.0fm (%.0fm from best corr for each pol)" % (corr_x, corr_y, corr_diff / 2)) if (corr_diff > 3 * dx): print( "WARNING: geocorrection for different polarizations do not agree,", corr_diff) print("Please, manually look at the image") print( "****************************************************************") ok_corr = False #geocorrection is not good sys.stdout.flush() Dict_Band['GeoCorrection'] = {'X': corr_x, 'Y': corr_y, 'OK': ok_corr} Dict_Band.pop('Ell') Dict_Band['Ell'] = Orig_Ells Dict_Band['n_ell'] = Orig_n_ell
def Read_HDF_AMSR_E_L2A(path, filename, Bands, WA_Cords): Bands = [x.upper() for x in Bands] mLat = WA_Cords['mLat'] mLon = WA_Cords['mLon'] MLat = WA_Cords['MLat'] MLon = WA_Cords['MLon'] if ((filename.find('_D') * filename.find('_A')) > 0): exit() elif filename.find('_D') >= 0: PASS = '******' else: PASS = '******' inf = path + filename + '.hdf' print(inf) #uncomment to read AMSRE files #hdf = SD(inf, SDC.READ) #print (hdf.datasets()) # Get the data Lat89A = hdf.select("Latitude")[:][:] Lon89A = hdf.select("Longitude")[:][:] print("-----------------------------------------------------") print("Reading HDF:" + filename) print("Pass:"******"36.5H_Res.4_TB_(not-resampled)")[:, :][(ymin):( ymax), int(xmin):int(xmax)] Tbv['KA'] = hdf.select("36.5V_Res.4_TB_(not-resampled)")[:, :][(ymin):( ymax), int(xmin):int(xmax)] Tbh['K'] = hdf.select("23.8H_Approx._Res.3_TB_(not-resampled)")[:, :][( ymin):(ymax), int(xmin):int(xmax)] Tbv['K'] = hdf.select("23.8V_Approx._Res.3_TB_(not-resampled)")[:, :][( ymin):(ymax), int(xmin):int(xmax)] Tbh['KU'] = hdf.select("18.7H_Res.3_TB_(not-resampled)")[:, :][(ymin):( ymax), int(xmin):int(xmax)] Tbv['KU'] = hdf.select("18.7V_Res.3_TB_(not-resampled)")[:, :][(ymin):( ymax), int(xmin):int(xmax)] Tbh['X'] = hdf.select("10.7H_Res.2_TB_(not-resampled)")[:, :][(ymin):( ymax), int(xmin):int(xmax)] Tbv['X'] = hdf.select("10.7V_Res.2_TB_(not-resampled)")[:, :][(ymin):( ymax), int(xmin):int(xmax)] Tbh['C'] = hdf.select("6.9H_Res.1_TB_(not-resampled)")[:, :][(ymin):( ymax), int(xmin):int(xmax)] Tbv['C'] = hdf.select("6.9V_Res.1_TB_(not-resampled)")[:, :][(ymin):( ymax), int(xmin):int(xmax)] Nang = hdf.select("Earth_Azimuth")[:, :][(ymin):(ymax), int(xmin):int(xmax)] Dict_Bands = {} Dict_Bands['pass'] = PASS for i in range(len(Bands)): #outWM=path+'Dict/'+infilenm+'_'+Band #working area print(" Reading band:" + Band + ',', end=" ") sys.stdout.flush() n_ell = 0 Dict_Band = {} Dict_Ells = {} Dict_Band['Band'] = Band Dict_Band['HPDm'] = HPDms[Band] Dict_Band['HPDM'] = HPDMs[Band] #E=np.zeros([0,5]) for y in range(Lat89A.shape[0]): for x in range(int(Lat89A.shape[1])): #Write ellipses info to DICT #m1 = np.array([ Lat89A[y,x],Lon89A[y,x]]) nlat = Lat89A[y, x] nlon = Lon89A[y, x] #if (nlat<MLat)&(nlat>mLat)&(nlon<MLon)&(nlon>mLon): if (nlat < MLat) & (nlat > mLat) & (nlon < MLon) & (nlon > mLon): #D=np.append(D,[[nlat, nlon,nang,'Tbh':Tbh[Band][y,x]/100,'Tbv[Band][y,x],x,y]],axis=0) X, Y = utm.from_latlon(nlat, nlon, force_zone_number=21)[0:2] ###Scale factor 0.1, el offset value de +327.68 Dict_Ell = { 'lat': nlat, 'lon': nlon, 'tilt': Nang[y, x] / 100, 'Tbh': Tbh[Band][y, x] / 100 + 327.68, 'Tbv': Tbv[Band][y, x] / 100 + 327.68, 'AlongScan_Id': x + xmin, 'AlongTrack_Id': y + ymin, 'X': X, 'Y': Y } Dict_Ells[n_ell] = Dict_Ell n_ell = n_ell + 1 Dict_Band['n_ell'] = n_ell Dict_Band['filename'] = filename Dict_Band['pass'] = PASS Dict_Band['Ell'] = Dict_Ells L_Kernels.Compute_Elliptic_Kernel(Dict_Band, WA_Cords) ### NEW Dict_Bands[Band] = Dict_Band print('100%', end=" ") sys.stdout.flush() Dict_Bands['Bands'] = Bands Dict_Bands['WA_Cords'] = WA_Cords Dict_Bands['FileName'] = filename return Dict_Bands
def Read_HDF_AMSR2_L1R(path, filename, Bands, WA_Cords): #OJO L1R!!! Bands = [x.upper() for x in Bands] mLat = WA_Cords['mLat'] mLon = WA_Cords['mLon'] MLat = WA_Cords['MLat'] MLon = WA_Cords['MLon'] if ((filename.find('D_') * filename.find('A_')) >= 0): exit() elif filename.find('D_') > 0: PASS = '******' else: PASS = '******' inf = path + filename + '.h5' print(inf) infile = hdf.File(inf, "r") # Get the data Lat89A = infile["/Latitude of Observation Point for 89A"][:][:] Lon89A = infile["/Longitude of Observation Point for 89A"][:][:] print("-----------------------------------------------------") print("Reading HDF:" + filename) print("Pass:"******"/Brightness Temperature (res36,36.5GHz,H)"][(ymin):( ymax), int(xmin / 2):int(xmax / 2)] Tbv['KA'] = infile["/Brightness Temperature (res36,36.5GHz,V)"][(ymin):( ymax), int(xmin / 2):int(xmax / 2)] #Tbh['K'] = infile["/Brightness Temperature (res23,23.8GHz,H)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbv['K'] = infile["/Brightness Temperature (res23,23.8GHz,V)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbh['ku'] = infile["/Brightness Temperature (res23,18.7GHz,H)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbv['ku'] = infile["/Brightness Temperature (res23,18.7GHz,V)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbh['x'] = infile["/Brightness Temperature (res10,10.7GHz,H)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbv['x'] = infile["/Brightness Temperature (res10,10.7GHz,V)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbh['xka']= infile["/Brightness Temperature (res10,36.5GHz,H)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbv['x'] = infile["/Brightness Temperature (res10,36.5GHz,V)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbh['c'] = infile["/Brightness Temperature (res06,6.9GHz,H)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbv['c'] = infile["/Brightness Temperature (res06,6.9GHz,V)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbh['cx'] = infile["/Brightness Temperature (res06,10.7GHz,H)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] #Tbv['cx'] = infile["/Brightness Temperature (res06,10.7GHz,V)"][(ymin):(ymax),int(xmin/2):int(xmax/2)] Tbh['C'] = infile["/Brightness Temperature (res06,36.5GHz,H)"][(ymin):( ymax), int(xmin / 2):int(xmax / 2)] Tbv['C'] = infile["/Brightness Temperature (res06,36.5GHz,V)"][(ymin):( ymax), int(xmin / 2):int(xmax / 2)] Nang = infile["/Earth Azimuth"][(ymin):(ymax), int(xmin / 2):int(xmax / 2)] Dict_Bands = {} Dict_Bands['pass'] = PASS for i in range(len(Bands)): #outWM=path+'Dict/'+infilenm+'_'+Band #working area print(" Reading band:" + Band + ',', end=" ") sys.stdout.flush() n_ell = 0 Dict_Band = {} Dict_Ells = {} Dict_Band['Band'] = Band Dict_Band['HPDm'] = HPDms[Band] Dict_Band['HPDM'] = HPDMs[Band] #E=np.zeros([0,5]) for y in range(Lat89A.shape[0]): for x in range(int(Lat89A.shape[1] / 2)): #Write ellipses info to DICT m1 = np.array([Lat89A[y, 2 * x], Lon89A[y, 2 * x]]) m2 = np.array([Lat89A[y, 2 * x + 1], Lon89A[y, 2 * x + 1]]) #print m1,m2, P1 = (np.cos(m1[1] * np.pi / 180) * np.cos(m1[0] * np.pi / 180), np.sin(m1[1] * np.pi / 180) * np.cos(m1[0] * np.pi / 180), np.sin(m1[0] * np.pi / 180)) P2 = (np.cos(m2[1] * np.pi / 180) * np.cos(m2[0] * np.pi / 180), np.sin(m2[1] * np.pi / 180) * np.cos(m2[0] * np.pi / 180), np.sin(m2[0] * np.pi / 180)) ex = np.array(P1) C = np.cross(ex, np.array(P2)) ez = C / la.norm(C) ey = np.cross(ez, ex) th = np.arccos(np.dot(P1, P2)) A1 = A1s[Band] A2 = A2s[Band] nP = np.cos(A2 * th) * np.cos(A1 * th) * ex + np.cos( A2 * th) * np.sin(A1 * th) * ey + np.sin(A2 * th) * ez nlat = np.arcsin(nP[2]) nlon = 180 / np.pi * np.arcsin(nP[1] / np.cos(nlat)) nlat = 180 / np.pi * nlat LocalNorth = np.dot(np.array([0, 0, 1]), ey) * ey + np.dot( np.array([0, 0, 1]), ez) * ez LocalNorth /= la.norm(LocalNorth) LocalWest = np.cross(ex, LocalNorth) #####RAFA!!para el tilt usamos el earth azimut o dejamos el cálculo? ver en lector de AMSR-E nang = 180 / np.pi * np.arccos(np.dot( LocalNorth, ez)) * np.sign(np.dot(LocalWest, ez)) #if (nlat<MLat)&(nlat>mLat)&(nlon<MLon)&(nlon>mLon): if (m1[0] < MLat) & (m1[0] > mLat) & (m1[1] < MLon) & (m1[1] > mLon): #D=np.append(D,[[nlat, nlon,nang,'Tbh':Tbh[Band][y,x]/100,'Tbv[Band][y,x],x,y]],axis=0) X, Y = utm.from_latlon(nlat, nlon, force_zone_number=21)[0:2] Dict_Ell = { 'lat': nlat, 'lon': nlon, 'tilt': Nang[y, x] / 100, 'Tbh': Tbh[Band][y, x] / 100, 'Tbv': Tbv[Band][y, x] / 100, 'AlongScan_Id': x + xmin, 'AlongTrack_Id': y + ymin, 'X': X, 'Y': Y } Dict_Ells[n_ell] = Dict_Ell n_ell = n_ell + 1 Dict_Band['n_ell'] = n_ell Dict_Band['filename'] = filename Dict_Band['pass'] = PASS Dict_Band['Ell'] = Dict_Ells L_Kernels.Compute_Elliptic_Kernel(Dict_Band, WA_Cords) ### NEW Dict_Bands[Band] = Dict_Band print('100%', end=" ") sys.stdout.flush() Dict_Bands['Bands'] = Bands Dict_Bands['WA_Cords'] = WA_Cords Dict_Bands['FileName'] = filename return Dict_Bands