def main(coefficientPath, sensor_id): thisDir = os.path.dirname(os.path.abspath(__file__)) cases = os.listdir(os.path.join(thisDir, 'data')) cases.sort() # create 4 profiles for each of the 4 cases profiles = profilesCreate(4, 92, nClouds=0, nAerosols=0) storedTb = [] storedEmis = [] # populate the cases, and previously calculated Tb from crtm test program. for i, c in enumerate(cases): h5 = h5py.File(os.path.join(thisDir, 'data', c), 'r') profiles.Angles[i, 0] = h5['zenithAngle'][()] profiles.Angles[i, 1] = 999.9 profiles.Angles[i, 2] = 100.0 # 100 degrees zenith below horizon. profiles.Angles[i, 3] = 0.0 # zero solar azimuth profiles.Angles[i, 4] = h5['scanAngle'][()] profiles.DateTimes[i, 0] = 2001 profiles.DateTimes[i, 1] = 1 profiles.DateTimes[i, 2] = 1 profiles.Pi[i, :] = np.asarray(h5['pressureLevels']) profiles.P[i, :] = np.asarray(h5['pressureLayers'][()]) profiles.T[i, :] = np.asarray(h5['temperatureLayers']) profiles.Q[i, :] = np.asarray(h5['humidityLayers']) profiles.O3[i, :] = np.asarray(h5['ozoneConcLayers']) profiles.climatology[i] = h5['climatology'][()] profiles.surfaceFractions[i, :] = h5['surfaceFractions'] profiles.surfaceTemperatures[i, :] = h5['surfaceTemperatures'] profiles.S2m[i, 1] = 33.0 # just use salinity out of S2m for the moment. profiles.windSpeed10m[i] = 5.0 profiles.LAI[i] = h5['LAI'][()] profiles.windDirection10m[i] = h5['windDirection10m'][()] # land, soil, veg, water, snow, ice profiles.surfaceTypes[i, 0] = h5['landType'][()] profiles.surfaceTypes[i, 1] = h5['soilType'][()] profiles.surfaceTypes[i, 2] = h5['vegType'][()] profiles.surfaceTypes[i, 3] = h5['waterType'][()] profiles.surfaceTypes[i, 4] = h5['snowType'][()] profiles.surfaceTypes[i, 5] = h5['iceType'][()] storedTb.append(np.asarray(h5['Tb_cris_no_clouds'])) storedEmis.append(np.asarray(h5['emissivity_cris_no_clouds'])) h5.close() crtmOb = pyCRTM() crtmOb.profiles = profiles crtmOb.coefficientPath = pathInfo['CRTM']['coeffs_dir'] crtmOb.sensor_id = sensor_id crtmOb.nThreads = 4 crtmOb.loadInst() crtmOb.runDirect() forwardTb = crtmOb.Bt forwardEmissivity = crtmOb.surfEmisRefl[0, :] crtmOb.surfEmisRefl = [] crtmOb.runK() kTb = crtmOb.Bt kEmissivity = crtmOb.surfEmisRefl[0, :] if (all( np.abs(forwardTb.flatten() - np.asarray(storedTb).flatten()) <= 1e-5) and all( np.abs(kTb.flatten() - np.asarray(storedTb).flatten()) <= 1e-5)): print( "Yay! all values are close enough to what CRTM test program produced!" ) else: print("Boo! something failed. Look at cris plots") wavenumbers = np.zeros([4, 1305]) wavenumbers[0:4, :] = np.linspace(1, 1306, 1305) plt.figure() plt.plot(wavenumbers.T, forwardTb.T - np.asarray(storedTb).T) plt.legend(['1', '2', '3', '4']) plt.savefig(os.path.join(thisDir, 'cris' + '_spectrum_forward.png')) plt.figure() plt.plot(wavenumbers.T, forwardEmissivity.T - np.asarray(storedEmis).T) plt.savefig(os.path.join(thisDir, 'cris' + '_emissivity_forward.png')) plt.figure() plt.plot(wavenumbers.T, kTb.T - np.asarray(storedTb).T) plt.savefig(os.path.join(thisDir, 'cris' + '_spectrum_k.png')) plt.figure() plt.plot(wavenumbers.T, kEmissivity.T - np.asarray(storedEmis).T) plt.savefig(os.path.join(thisDir, 'cris' + '_emissivity_k.png')) #for i,c in enumerate(cases): # h5 = h5py.File(os.path.join(thisDir,'data',c) , 'r+') # h5.create_dataset('Tb_cris_no_clouds', data = forwardTb[i,:]) # h5.create_dataset('emissivity_cris_no_clouds', data = forwardEmissivity[i,:]) sys.exit("Boo! didn't pass tolerance with CRTM test program.")
def main(coefficientPath, sensor_id, fin, experiment): # the profile from grb2 has 45 levels gfs = xr.open_dataset(fin) lat = gfs.lat_0 lon = gfs.lon_0 profiles = profilesCreate(len(lat) * len(lon), 45, nAerosols=0, nClouds=4) angles = gfs.xangles.stack(z=("lat_0", "lon_0")).transpose() #angles[:,:]=30.0 # np.where(angles>60, 60, angles) #angles=np.where(angles<-60, -60, angles) # profiles.Angles[:,:] = angles[:,:] profiles.Angles[:, 0] = angles[:, 0] #h5['zenithAngle'][()] profiles.Angles[:, 1] = angles[:, 1] profiles.Angles[:, 2] = angles[:, 3] # 100 degrees zenith below horizon. profiles.Angles[:, 3] = angles[:, 4] # zero solar azimuth profiles.Angles[:, 4] = angles[:, 2] # h5['scanAngle'][()] # date time not used. datetimes = gfs.valid_time.stack(z=("lat_0", "lon_0")).transpose() profiles.DateTimes[:, :] = datetimes[:, :] profiles.Pi[:, :] = gfs.plevel.broadcast_like( gfs.player).stack(z=("lat_0", "lon_0")).transpose() / 100.0 profiles.P[:, :] = gfs.player[1:, :, :].stack( z=("lat_0", "lon_0")).transpose() / 100.0 profiles.T[:, :] = gfs.temp[1:, :, :].stack(z=("lat_0", "lon_0")).transpose() Q = gfs.moisture[1:, :, :].stack(z=("lat_0", "lon_0")).transpose() profiles.Q[:, :] = xr.where(Q > 0, Q, 0) #profiles.O3[:,:]=gfs.o3[1:,:,:].stack(z=("lat_0","lon_0")).transpose() cld = gfs.water_cloud[1:, :, :].stack(z=("lat_0", "lon_0")).transpose() cld = np.where(cld < 0, 0, cld) profiles.clouds[:, :, 0, 0] = cld profiles.clouds[:, :, 0, 1] = 10.0 ice = gfs.ice_cloud[1:, :, :].stack(z=("lat_0", "lon_0")).transpose() ice = np.where(ice < 0, 0, ice) profiles.clouds[:, :, 1, 0] = ice profiles.clouds[:, :, 1, 1] = 75 rain = gfs.rain_cloud[1:, :, :].stack(z=("lat_0", "lon_0")).transpose() rain = np.where(rain < 0, 0, rain) profiles.clouds[:, :, 2, 0] = rain profiles.clouds[:, :, 2, 1] = 50 snow = gfs.snow_cloud[1:, :, :].stack(z=("lat_0", "lon_0")).transpose() snow = np.where(snow < 0, 0, snow) profiles.clouds[:, :, 3, 0] = snow profiles.clouds[:, :, 3, 1] = 50 #graupel=gfs.graupel_cloud[1:,:,:].stack(z=("lat_0","lon_0")).transpose() #graupel=np.where(graupel>0,0,-graupel) #profiles.clouds[:,:,4,0]=graupel #profiles.clouds[:,:,4,1]=50 profiles.cloudType[:, 0] = 1 profiles.cloudType[:, 1] = 2 profiles.cloudType[:, 2] = 3 profiles.cloudType[:, 3] = 4 #profiles.cloudType[:,4]=5 profiles.climatology[:] = 6 #US_STANDARD_ATMOSPHERE #6 # place holder don't know lands = gfs.landmask.stack(z=("lat_0", "lon_0")).transpose() snow = gfs.snow_cover.stack(z=("lat_0", "lon_0")).transpose() #ice=gfs.ice_cover.stack(z=("lat_0","lon_0")).transpose() profiles.surfaceFractions[:, 0] = np.where(lands == 1, int(1), int(0)) profiles.surfaceFractions[:, 1] = np.where( (lands == 0) | (lands == np.nan), int(1), int(0)) profiles.surfaceFractions[:, 2] = 0 profiles.surfaceFractions[:, 3] = 0 profiles.surfaceTemperatures[:, 0] = gfs.sfctemp.stack(z=("lat_0", "lon_0")) profiles.surfaceTemperatures[:, 1] = gfs.sfctemp.stack(z=("lat_0", "lon_0")) profiles.surfaceTemperatures[:, 2] = gfs.sfctemp.stack(z=("lat_0", "lon_0")) profiles.surfaceTemperatures[:, 3] = gfs.sfctemp.stack(z=("lat_0", "lon_0")) profiles.S2m[:, 1] = 33.0 # just use salinity out of S2m for the moment. wind_speed = gfs.wind_speed.stack(z=("lat_0", "lon_0")).transpose() profiles.windSpeed10m[:] = wind_speed[:] # wind_speed # lai not affecting profiles.LAI[:] = 0.57 # h5['LAI'][()] wind_dir = gfs.wind_dir.stack(z=("lat_0", "lon_0")).transpose() wind_dir = np.where(wind_dir < 0, wind_dir + 360, wind_dir) profiles.windDirection10m[:] = wind_dir[:] # h5['windDirection10m'][()] landtype = gfs.land_type[:, :, 0].stack(z=("lat_0", "lon_0")).transpose() landtype = np.where(landtype > 14, 16.0, landtype) landtype = np.where(landtype < 1.0, 1.0, landtype) profiles.surfaceTypes[:, 0] = landtype profiles.surfaceTypes[:, 1] = 6 profiles.surfaceTypes[:, 2] = 2 profiles.surfaceTypes[:, 3] = 1 profiles.surfaceTypes[:, 4] = 3 profiles.surfaceTypes[:, 5] = 1 profiles = experiment(profiles) crtmOb = pyCRTM() crtmOb.profiles = profiles crtmOb.coefficientPath = pathInfo['CRTM']['coeffs_dir'] crtmOb.sensor_id = sensor_id crtmOb.nThreads = 1 print("hello 002") crtmOb.loadInst() crtmOb.runDirect() forwardTb = crtmOb.Bt # forwardEmissivity = crtmOb.surfEmisRefl[0,:] # #make K matrix run surfEmisRefl again. # crtmOb.surfEmisRefl = [] # crtmOb.runK() # kTb = crtmOb.Bt # kEmissivity = crtmOb.surfEmisRefl[0,:] print("hello 003") result = xr.DataArray(np.reshape(forwardTb.T, (10, len(lat), len(lon))), dims=("channel", "lat_0", "lon_0"), coords=(np.arange(0, 10), gfs.lat_0, gfs.lon_0)) #fig,ax=plt.subplots(5,2) # result[:,30,30].plot() # for i in range(5): # for j in range(2): # ax[i,j].pcolor(result[i*2+j,:,:].where(result[i*2+j,:,:]>-100), vmin=201, vmax=300) # # prepare for interpolation ds_in = result.rename({"lat_0": 'lat', "lon_0": 'lon'}) fo = xr.open_dataset(fobs) lato = fo.lat.data lono = fo.lon.data ds_out = xr.Dataset({ 'lat': (['lat'], lato), 'lon': (['lon'], lono), }) regridder = xe.Regridder(ds_in, ds_out, 'bilinear', reuse_weights=True) dr_out = regridder(result) out = dr_out.where(fo.obs.data > 0) fout = fin[0:len(fin) - 3] + "_output_" + experiment.__name__ + ".nc" if os.path.exists(fout): os.remove(fout) result.to_netcdf(fout, "w") # plt.pcolor(0.5*(out[7]+out[8]),vmin=230,vmax=290) # # plt.show() print(result.max()) print(result.min())