def calc_rad( type, list_l0, z_layers, t_layers, p_layers, q_layers, mu_atm, mu_H2O, tsurf, pool, factor, albedo ): if ( pool == 0 ): atmfile = ATMFILE_W else: atmfile = ATMFILE_C #------------------------------------------------ # set up atmospheric data profile #------------------------------------------------ zkm_layers = z_layers * 1e-3 #------------------------------------------------ # set up atmospheric data profile #------------------------------------------------ atmprof0, dict_n_layers = make_dic_n_layers( "/Users/yuka/libRadtran-1.7/data/atmmod/afglt.dat", zkm_layers ) write_atmfile( zkm_layers, t_layers, p_layers, q_layers, mu_atm, mu_H2O, dict_n_layers, atmprof0, tsurf, atmfile ) #------------------------------------------------ # set input file to be passed to libRadtran #------------------------------------------------ list_z = [] for ii in xrange( len(list_l0) ): list_z.append( zkm_layers[list_l0[ii]] ) # km if ( type == "mir" ): write_inputfile_mir( list_z, tsurf, atmfile ) input = INPUTFILE_MIR output = OUTPUTFILE_MIR elif ( type == "vis" ): write_inputfile_vis( list_z, atmfile, albedo ) input = INPUTFILE_VIS output = OUTPUTFILE_VIS else : errors.exit_msg("Invalid radiation type.") check = os.system( 'uvspec < ' + input + " > " + output ) if check != 0 : errors.exit_msg("Something is wrong with uvspec.") #------------------------------------------------ # read the output #------------------------------------------------ array_zout, array_rad = read_outputfile( output ) print "array_zout", array_zout print "factor", factor print "array_rad", array_rad return array_zout, factor*array_rad
def read_cldfile(infile, tuple_func_atmprof): names = ['z', 'LWC', 'LWC2', 'SWC', 'SWC2'] formats = ['f8', 'f8', 'f8', 'f8', 'f8'] cld_profile = np.loadtxt(infile, dtype={'names':tuple(names), 'formats':tuple(formats)}, comments='#') #------------------------------------------------ # km => cm #------------------------------------------------ cld_profile['z'] = cld_profile['z']*1e5 #------------------------------------------------ #------------------------------------------------ # combine lwc and lwc2 #------------------------------------------------ cld_profile['LWC'] = ( cld_profile['LWC'] + cld_profile['LWC2'] ) * 0.5 cld_profile['SWC'] = ( cld_profile['SWC'] + cld_profile['SWC2'] ) * 0.5 #------------------------------------------------ #------------------------------------------------ # check the order of altitude #------------------------------------------------ z_old = cld_profile['z'][0] reverse = 0 for zi in xrange(1,len(cld_profile['z'])): if (cld_profile['z'][zi] < z_old): reverse = reverse + 1 if (reverse == len(cld_profile['z'])-1): cld_profile['z'] = cld_profile['z'][::-1] cld_profile['LWC'] = cld_profile['LWC'][::-1] cld_profile['SWC'] = cld_profile['SWC'][::-1] elif (reverse != 0): errors.exit_msg("Check the order of FILE_CLD.") #------------------------------------------------ # actually, 'P', 'cldh2o', 'R_eff' func_TofZ, func_PofZ, func_MUofZ, dict_func_NofZ = tuple_func_atmprof cldlayer_P = func_PofZ(cld_profile['z']) cldlayer_T = func_TofZ(cld_profile['z']) cldlayer_MU = func_MUofZ(cld_profile['z']) cldlayer_rho = cldlayer_MU*cldlayer_P/(cgs.RR*cldlayer_T) cld_profile['LWC'] = cld_profile['LWC']*cldlayer_rho cld_profile['SWC'] = cld_profile['SWC']*cldlayer_rho # print "cld_profile['LWC']", cld_profile['LWC'] # print "cld_profile['SWC']", cld_profile['SWC'] # func_RHOofZ = util_interp.interp_1d_boundary(cld_profile['z'], cld_profile['LWC'], array_rho, logx=False, logy=False, order=1) return cld_profile
def read_cldfile(infile, tuple_func_atmprof): names = ['z', 'LWC', 'LWC2', 'SWC', 'SWC2'] formats = ['f8', 'f8', 'f8', 'f8', 'f8'] cld_profile = np.loadtxt(infile, dtype={ 'names': tuple(names), 'formats': tuple(formats) }, comments='#') #------------------------------------------------ # km => cm #------------------------------------------------ cld_profile['z'] = cld_profile['z'] * 1e5 #------------------------------------------------ #------------------------------------------------ # combine lwc and lwc2 #------------------------------------------------ cld_profile['LWC'] = (cld_profile['LWC'] + cld_profile['LWC2']) * 0.5 cld_profile['SWC'] = (cld_profile['SWC'] + cld_profile['SWC2']) * 0.5 #------------------------------------------------ #------------------------------------------------ # check the order of altitude #------------------------------------------------ z_old = cld_profile['z'][0] reverse = 0 for zi in xrange(1, len(cld_profile['z'])): if (cld_profile['z'][zi] < z_old): reverse = reverse + 1 if (reverse == len(cld_profile['z']) - 1): cld_profile['z'] = cld_profile['z'][::-1] cld_profile['LWC'] = cld_profile['LWC'][::-1] cld_profile['SWC'] = cld_profile['SWC'][::-1] elif (reverse != 0): errors.exit_msg("Check the order of FILE_CLD.") #------------------------------------------------ # actually, 'P', 'cldh2o', 'R_eff' func_TofZ, func_PofZ, func_MUofZ, dict_func_NofZ = tuple_func_atmprof cldlayer_P = func_PofZ(cld_profile['z']) cldlayer_T = func_TofZ(cld_profile['z']) cldlayer_MU = func_MUofZ(cld_profile['z']) cldlayer_rho = cldlayer_MU * cldlayer_P / (cgs.RR * cldlayer_T) cld_profile['LWC'] = cld_profile['LWC'] * cldlayer_rho cld_profile['SWC'] = cld_profile['SWC'] * cldlayer_rho # print "cld_profile['LWC']", cld_profile['LWC'] # print "cld_profile['SWC']", cld_profile['SWC'] # func_RHOofZ = util_interp.interp_1d_boundary(cld_profile['z'], cld_profile['LWC'], array_rho, logx=False, logy=False, order=1) return cld_profile
def calc_rad_sol(pool, list_l0): print 'Cl_run_cdf -s $RAD_DATA/spectra/dev/sp_sw_6_jm2 -R 1 6 -S -g 2 -c -t 16 -v 13 -C 5 -B ' + BASE_NAME[ pool] check = os.system( 'Cl_run_cdf -s $RAD_DATA/spectra/dev/sp_sw_6_jm2 -R 1 6 -S -g 2 -c -t 16 -v 13 -C 5 -B ' + BASE_NAME[pool]) if check != 0: errors.exit_msg("Something is wrong with Cl_run_cdf.") array_rad = read_outputfile(BASE_NAME[pool], list_l0) return array_rad
def read_lookuptable(xsfile, wn_limit): """ Extract Look-up Table """ if '.nc' in xsfile: WN_grid_org, PP_grid_org, TT_grid, XS_grid_org = io_nc.read_xstbl( xsfile) elif '.npz' in xsfile: data = np.load(xsfile) WN_grid_org = data['WN'] PP_grid_org = data['P'] TT_grid = data['T'] XS_grid_org = data['XS'] else: # error! errors.exit_msg("Unknown file type of cross section tables.") # if PP_grid_org.units=='mbar' PP_grid = PP_grid_org * 1.e3 # mbar => barye (x 1000) #------------------------------------------------ # check wavenumber range #------------------------------------------------ if ((wn_limit[0] < min(WN_grid_org)) or (wn_limit[1] > max(WN_grid_org))): errors.exit_msg("Wavenumbers set are out of range of look-up tables.") #------------------------------------------------ indx_min = nearestindex_WN(WN_grid_org, wn_limit[0]) indx_max = nearestindex_WN(WN_grid_org, wn_limit[1]) WN_grid = WN_grid_org[indx_min:indx_max + 1] del WN_grid_org XS_grid = XS_grid_org[indx_min:indx_max + 1] del XS_grid_org #### TEST (to be eventually removed) id1, id2, id3 = np.where(XS_grid <= 0) for i1, i2, i3 in zip(id1, id2, id3): XS_grid[i1][i2][i3] = 1.e-100 #### TEST return WN_grid, TT_grid, PP_grid, XS_grid
def read_nc( infile, mode ): """ To read netCDF file and extract albedo / outgoing flux """ if ( mode == "albedo" ): param = 'plan_alb' elif ( mode == "thermal" ): param = 'trnf_toa' else: errors.exit_msg( "Invalid mode ( albedo or thermal )" ) ncfile_r = netCDF4.Dataset( infile, 'r', format='NETCDF3_64BIT') lat = ncfile_r.variables['lat'][:] lon = ncfile_r.variables['lon'][:] nlat = len(lat) nlon = len(lon) #-------------------------------- lat2 = np.zeros_like(lat) for ilat in xrange( nlat ): lat2[ilat] = 90.0 - ( 180.0 / nlat ) * ( ilat + 0.5 ) lat = lat2 #-------------------------------- lon_mesh, lat_mesh = np.meshgrid( lon, lat ) lat_flatten = lat_mesh.flatten() lon_flatten = lon_mesh.flatten() data = ncfile_r.variables[param][:] if ( mode == "albedo" ): data = data * 0.01 elif ( mode == "thermal" ): data = data * -1. data_flatten = data.flatten() return nlat, nlon, lat_flatten, lon_flatten, data_flatten
if __name__ == "__main__": filename = sys.argv[1] if '.nc' in filename: import io_nc WN_grid, P_grid, T_grid, XS_grid = io_nc.read_xstbl(filename) elif '.npz' in filename: data = np.load(filename) WN_grid = data['WN'] P_grid = data['P'] T_grid = data['T'] XS_grid = data['XS'] else: # error! errors.exit_msg("Unknown file type of cross section tables.") print '--------------------------------------------------' print ' select pressure [mbar] : ' for ii in xrange(len(P_grid)): print '({0:d}) {1:e}'.format(ii, P_grid[ii]), print '' s_pres = raw_input() i_pres = int(s_pres) print '--------------------------------------------------' print ' select temperature [K] : ' for ii in xrange(len(T_grid)): print '({0:d}) {1:3f}'.format(ii, T_grid[ii]),
def calc_nXSofZ_molabs(layer_z, grid_wn, tuple_func_atmprof, molname, xsfile_tag, xsfile_suffix, cntnm_on=True, xsfile_tag_cntnm=None): func_TofZ, func_PofZ, func_MUofZ, dict_func_NofZ = tuple_func_atmprof layer_P = func_PofZ(layer_z) layer_T = func_TofZ(layer_z) layer_n0 = layer_P / (cgs.RR * layer_T) * cgs.NA layer_n = dict_func_NofZ[molname](layer_z) * 1e-6 * layer_n0 #------------------------------------------------ # set up lookup tables #------------------------------------------------ xsfile = xsfile_tag + molname + xsfile_suffix WN_lookuptable, TT_lookuptable, PP_lookuptable, XS_lookuptable = read_lookuptable( xsfile, (grid_wn[0], grid_wn[-1])) #------------------------------------------------ # check range of lookup table #------------------------------------------------ if (layer_P[-1] < PP_lookuptable[0]): errors.exit_msg( "Pressure grids in FILE_ATM smaller than the range of lookuptable." ) if (layer_P[0] > PP_lookuptable[-1]): errors.exit_msg( "Pressure grids in FILE_ATM is larger than the range of lookuptable." ) #------------------------------------------------ # CONTINUUM? (currently available only for CH4) #------------------------------------------------ if (cntnm_on and (molname == "H2O")): print " including continuum" xsfile = xsfile_tag_cntnm + molname + xsfile_suffix WN_lookuptable, TT_lookuptable, PP_lookuptable, XS_cntnm = read_lookuptable( xsfile, (grid_wn[0], grid_wn[-1])) XS_lookuptable = XS_lookuptable + XS_cntnm #------------------------------------------------ # read lookup table #------------------------------------------------ dict_griddata_logXSofWNTP = {} m_Tgrid, m_WNgrid, m_logPgrid = np.meshgrid(TT_lookuptable, WN_lookuptable, np.log(PP_lookuptable)) dict_griddata_logXSofWNTP['coords'] = np.dstack( [m_WNgrid.flatten(), m_Tgrid.flatten(), m_logPgrid.flatten()])[0] dict_griddata_logXSofWNTP[molname] = np.log(XS_lookuptable.flatten()) mesh_n = np.tile(layer_n, (len(grid_wn), 1)) mesh_logP, mesh_wn = np.meshgrid(np.log(layer_P), grid_wn) mesh_T, mesh_wn = np.meshgrid(layer_T, grid_wn) flat_points = np.dstack( [mesh_wn.flatten(), mesh_T.flatten(), mesh_logP.flatten()]) flat_logXS = interpolate.griddata(dict_griddata_logXSofWNTP['coords'], dict_griddata_logXSofWNTP[molname], flat_points, method='nearest')[0] mesh_logXS = flat_logXS.reshape(len(grid_wn), len(layer_z)) mesh_nXS = mesh_n * np.exp(mesh_logXS) # returned numpy.array # WN_NUM x Z_NUM # [[ wn1 x z1, wn1 x z2, .., wn1 x zN ], # [ wn2 x z1, wn2 x z2, .., wn2 x zN ],... return mesh_nXS
def read_atmprofile(infile, key=0): """ Read profile of amtmosphere """ with open(infile, 'r') as f_linefile: for line in f_linefile: elements = line.rsplit() if elements[1] == "molecules:": list_mol = elements[2:len(elements)] if elements[0] != '#': break names = ['z', 'P', 'T', 'MU'] formats = ['f8', 'f8', 'f8', 'f8'] for ii in xrange(len(list_mol)): names.append(list_mol[ii]) formats.append('f8') dict_atmprof = np.loadtxt(infile, dtype={ 'names': tuple(names), 'formats': tuple(formats) }, comments='#') #------------------------------------------------ # mbar => barye (x 1000) #------------------------------------------------ dict_atmprof['P'] = dict_atmprof['P'] * 1e3 #------------------------------------------------ # km => cm #------------------------------------------------ dict_atmprof['z'] = dict_atmprof['z'] * 1e5 #------------------------------------------------ #------------------------------------------------ # N => N with other order of magnitude #------------------------------------------------ dict_atmprof[list_mol[2]] = dict_atmprof[list_mol[2]] * 1e12 #------------------------------------------------ #------------------------------------------------ # check the order of altitude in FILE_ATM #------------------------------------------------ reverse = 0 z_old = dict_atmprof['z'][0] for zi in xrange(1, len(dict_atmprof['z'])): if (dict_atmprof['z'][zi] < z_old): reverse = reverse + 1 if (reverse == len(dict_atmprof['z']) - 1): dict_atmprof['z'] = dict_atmprof['z'][::-1] dict_atmprof['P'] = dict_atmprof['P'][::-1] dict_atmprof['T'] = dict_atmprof['T'][::-1] dict_atmprof['MU'] = dict_atmprof['MU'][::-1] for ii in xrange(len(list_mol)): dict_atmprof[list_mol[ii]] = dict_atmprof[list_mol[ii]][::-1] elif (reverse != 0): errors.exit_msg("Check the order of FILE_ATM.") #------------------------------------------------ if key: return list_mol, dict_atmprof[key] else: return list_mol, dict_atmprof
#------------------------------------------------ if (OBSMODE == "trans"): outdir = "out/transmission/" import obsmode_trans #------------------------------------------------ elif (OBSMODE == "disk"): outdir = "out/emission/" import obsmode_disk #------------------------------------------------ elif (OBSMODE == "nadir"): outdir = "out/nadir/" import obsmode_nadir #------------------------------------------------ else: errors.exit_msg("INVALID OBSMODE") #------------------------------------------------ #============================================================================= # functions #============================================================================= def set_atmprof(infile): """ Set up profile of amtmosphere """ list_mol, dict_atmprof = read_atmprofile(infile) func_TofZ = util_interp.interp_1d_boundary(dict_atmprof['z'], dict_atmprof['T'], order=1,
def read_atmprofile(infile, key=0) : """ Read profile of amtmosphere """ with open(infile,'r') as f_linefile : for line in f_linefile : elements = line.rsplit() if elements[1] == "molecules:" : list_mol = elements[2:len(elements)] if elements[0] != '#' : break names = ['z', 'P', 'T', 'MU'] formats = ['f8', 'f8', 'f8', 'f8'] for ii in xrange(len(list_mol)) : names.append(list_mol[ii]) formats.append('f8') dict_atmprof = np.loadtxt(infile, dtype={'names':tuple(names), 'formats':tuple(formats)}, comments='#') #------------------------------------------------ # mbar => barye (x 1000) #------------------------------------------------ dict_atmprof['P'] = dict_atmprof['P']*1e3 #------------------------------------------------ # km => cm #------------------------------------------------ dict_atmprof['z'] = dict_atmprof['z']*1e5 #------------------------------------------------ #------------------------------------------------ # N => N with other order of magnitude #------------------------------------------------ dict_atmprof[list_mol[2]] = dict_atmprof[list_mol[2]]*1e12 #------------------------------------------------ #------------------------------------------------ # check the order of altitude in FILE_ATM #------------------------------------------------ reverse = 0 z_old = dict_atmprof['z'][0] for zi in xrange(1,len(dict_atmprof['z'])): if (dict_atmprof['z'][zi] < z_old): reverse = reverse + 1 if (reverse == len(dict_atmprof['z'])-1): dict_atmprof['z'] = dict_atmprof['z'][::-1] dict_atmprof['P'] = dict_atmprof['P'][::-1] dict_atmprof['T'] = dict_atmprof['T'][::-1] dict_atmprof['MU'] = dict_atmprof['MU'][::-1] for ii in xrange(len(list_mol)) : dict_atmprof[list_mol[ii]] = dict_atmprof[list_mol[ii]][::-1] elif (reverse != 0): errors.exit_msg("Check the order of FILE_ATM.") #------------------------------------------------ if key : return list_mol, dict_atmprof[key] else : return list_mol, dict_atmprof
#------------------------------------------------ if (OBSMODE=="trans"): outdir = "out/transmission/" import obsmode_trans #------------------------------------------------ elif (OBSMODE=="disk"): outdir = "out/emission/" import obsmode_disk #------------------------------------------------ elif (OBSMODE=="nadir"): outdir = "out/nadir/" import obsmode_nadir #------------------------------------------------ else: errors.exit_msg("INVALID OBSMODE") #------------------------------------------------ #============================================================================= # functions #============================================================================= def set_atmprof(infile): """ Set up profile of amtmosphere """ list_mol, dict_atmprof = read_atmprofile(infile) func_TofZ = util_interp.interp_1d_boundary(dict_atmprof['z'], dict_atmprof['T'], order=1, logx=False, logy=False) func_PofZ = util_interp.interp_1d_boundary(dict_atmprof['z'], dict_atmprof['P'], order=1, logx=False) func_MUofZ = util_interp.interp_1d_boundary(dict_atmprof['z'], dict_atmprof['MU'], order=1, logx=False, logy=False)