def standalone_main(): # Load command line options parser = OptionParser(usage="usage: %prog [options] [summary_files]...") parser.add_option( "-r", "--run_dir_file", dest="run_dir_file", metavar="FILE", help="file to read list of run directories from") parser.add_option( "-o", "--output_dir", dest="output_dir", metavar="DIR", help="directory where to write plot files") parser.add_option( "-f", "--filter", dest="filter_names", metavar="REGEXP", type="string", action="append", help="filter matching filenames according to the supplied regular expression", ) parser.add_option( "-i", "--indexes", dest="indexes", metavar="NUM", type="string", action="append", help="which indexes that would generate multiple pages of plots to use", ) parser.add_option( "--log", dest="make_logs", default=False, action="store_true", help="make debugging log files per plot file") # Parse command line arguments (options, args) = parser.parse_args() # Initialize logging L2_Log_Util.init_logging() # Gather list of run directories to gather information from run_dirs = [] for arg_dir in args: run_dirs.append(arg_dir) if options.indexes != None: indexes = [] for idx_arg in options.indexes: indexes += OCO_TextUtils.index_range_list(idx_arg) else: indexes = None if options.run_dir_file != None: if not os.path.exists(options.run_dir_file): parser.error("Run directory file '%s' does not exist" % options.run_dir_file) with open(options.run_dir_file, 'r') as run_dir_fh: run_dirs += [ file_dir.strip() for file_dir in run_dir_fh.readlines() ] plot_run_comparisons(run_dirs, output_directory=options.output_dir, filename_filters=options.filter_names, page_indexes=indexes, debugging_logs=options.make_logs)
def create_dispersion_from_ascii(l1b_file, out_disp_file, disp_in_file, sounding_id=None, index_scheme=None): asc_l1b_obj = OCO_Matrix(l1b_file) if sounding_id == None: sounding_id = asc_l1b_obj.header['sounding_id'] if disp_in_file == None: raise IOError('No dispersion file specified') latitude = float(asc_l1b_obj.header['sounding_latitude'].split()[0]) sza_r = math.radians(float(asc_l1b_obj.header['sounding_solar_zenith'].split()[0])) saz_r = math.radians(float(asc_l1b_obj.header['sounding_solar_azimuth'].split()[0])) time_stamp = asc_l1b_obj.header['frame_time_stamp'] time_struct = OCO_TextUtils.convert_timestamp_to_struct(time_stamp) pixel_ranges = asc_l1b_obj.pixel_ranges() aband_data = asc_l1b_obj['Radiance'][slice(*pixel_ranges[0]), 0] disp_in_obj = OCO_Matrix(disp_in_file) dispersion_coefs = disp_in_obj[DISPERSION_ASCII_COLUMN_IDENT].transpose() create_scene_dispersion_file(sounding_id, latitude, sza_r, saz_r, time_struct, aband_data, dispersion_coefs, out_disp_file, index_scheme=index_scheme)
def plot_gosat_summ(summary_files): if len(summary_files) > 1: summary_names, common_part = OCO_TextUtils.extract_run_names( summary_files, return_common=True) else: common_part = summary_files[0] plot_data = {} for curr_summ_file in summary_files: file_obj = OCO_Matrix(curr_summ_file, as_strings=True) plot_data = file_obj.as_dict(DICT_KEY_COLUMN, existing_dict=plot_data) figure_index = 0 x_axis_index = [] for curr_yaxis in PLOT_TYPES: for curr_filter in PLOT_FILTERS: legend_names = [] plot_objs = [] fig = None curr_axis = None for leg_idx, curr_legend in enumerate(LEGEND_VALUES): # Extract plot data x_data = [] y_data = [] for scene_name, scene_data in plot_data.items(): if not re.search(curr_filter[0], scene_name) or not re.search( curr_legend[0], scene_name): continue if not scene_data.has_key( X_AXIS[0]) or not scene_data.has_key( curr_yaxis[0]): continue print 'Using scene: ', scene_name, 'for', curr_filter[ 1], curr_legend[1] x_value = scene_data[X_AXIS[0]] y_value = scene_data[curr_yaxis[0]] if len(X_AXIS) >= 3: axis_match = re.search(X_AXIS[2], x_value) x_value = axis_match.group() if len(X_AXIS) >= 4: if not x_value in x_axis_index: x_axis_index.append(x_value) x_value = x_axis_index.index(x_value) try: x_value = float(x_value) except: pass y_value = float(y_value) if abs(y_value - FILL_VALUE) > 1e-2: y_value *= curr_yaxis[2] insert_point = bisect.bisect(x_data, x_value) x_data.insert(insert_point, x_value) y_data.insert(insert_point, y_value) if len(x_data) > 0: if fig == None: fig = pyplot.figure(figure_index) fig.clf() curr_axis = pyplot.subplot(111) curr_plot_obj = pyplot.plot(x_data, y_data, 'x') plot_objs.append(curr_plot_obj) legend_names.append(curr_legend[1]) if len(plot_objs) > 0: figure_index += 1 curr_axis.set_xlabel(X_AXIS[1]) curr_axis.set_ylabel(curr_yaxis[1]) curr_axis.set_title('%s -- %s' % (curr_filter[1], common_part)) leg = curr_axis.legend( plot_objs, legend_names, loc='lower right', borderpad=0.1, labelspacing=0.05, borderaxespad=0.2, ) pyplot.show()
def plot(matrix, abscissa, abscissa_string, out_filename=None, det_windows=True): if (matrix.dims[0] == 0 or matrix.dims[1] == 0): return letter = pyx.document.paperformat.Letter # Number of spectral points size = matrix.dims[0] # All pages will be stored in this object d = pyx.document.document(pages=[]) # starting pixel indices for each window. The last entry is # the total number of pixels. windows = matrix.pixels[:] # Make a copy instead of using a reference if (len(windows) == 0): if det_windows and matrix.filename.find("high_res") == -1: windows = [ wini * (matrix.dims[0] / 3) for wini in range(3) ] else: windows = [0] windows.append(matrix.dims[0]) # Run through the columns for index in range(0, matrix.dims[1]): # Don't bother plotting wavelength try: if (matrix.labels_lower.index("wavelength") == index): continue except ValueError: pass try: xindex = matrix.labels_lower.index(abscissa) if (xindex == index): continue except ValueError: xindex = -1 pass # Plot each window separately for w in range(0, len(windows) - 1): # Look for the abscissa_string. If present, use that as # the x axis. if (xindex != -1): xaxis = matrix.data[windows[w]:windows[w+1], xindex] xtitle = abscissa_string else: xaxis = range(0, windows[w+1] - windows[w]) xtitle = "Index" # Build the list object holding the data for this window. list = [(xaxis[0], matrix.data[windows[w], index])] for i in range(0, windows[w+1] - windows[w]): list.append((xaxis[i], matrix.data[i + windows[w], index])) # use the list to create the data object to plot title=OCO_TextUtils.escape_underscore(os.path.dirname(matrix.filename)) data = pyx.graph.data.points(list, title=title, x=1, y=2) # Assign the axis ranges. xmin = xaxis[0] xmax = xaxis[windows[w+1] - windows[w] - 1] ymax = None ymin = None if (min(matrix.data[windows[w]:windows[w+1], index]) == max(matrix.data[windows[w]:windows[w+1], index])): ymax = 1 ymin = -1 # create the graph object g = pyx.graph.graphxy(width=100, key=pyx.graph.key.key(pos="br", symbolwidth=5*pyx.unit.t_cm), x=pyx.graph.axis.linear(min=xmin, max=xmax, title=xtitle), y=pyx.graph.axis.linear(min=ymin, max=ymax, title=matrix.ytitle[index])) # plot the data g.plot(data, [pyx.graph.style.line([pyx.color.rgb.red, pyx.style.linewidth.THICK])]) # Add the title filename = OCO_TextUtils.escape_underscore(matrix.filename) title = "%s: Index %d, window %d" % (filename, index, w) g.text(g.width/2, g.height + 0.4, title, [pyx.text.halign.center, pyx.text.valign.bottom, pyx.text.size.Large]) # Add this plot to the document d.append(pyx.document.page(g, paperformat=letter, margin=3*pyx.unit.t_cm, fittosize=1, rotated=1, centered=1)) # Write the postscript file if out_filename == None: out_filename = "%s.pdf" % matrix.filename print 'Writing: %s' % out_filename d.writetofile(out_filename)
def plotRMS(matrix, ref, abscissa, abscissa_string, out_filename=None, pressure_matrix=None, det_windows=True, ratio=False): if (matrix.dims[0] == 0 or matrix.dims[1] == 0): return if (ref.dims[0] == 0 or ref.dims[1] == 0): return # Number of spectral points size = matrix.dims[0] # All pages will be stored in this object d = pyx.document.document(pages=[]) # starting pixel indices for each window. The last entry is # the total number of pixels. windows = matrix.pixels[:] # Make a copy instead of using a reference if (len(windows) == 0): if det_windows and matrix.filename.find("high_res") == -1: windows = [ wini * (matrix.dims[0] / 3) for wini in range(3) ] else: windows = [0] windows.append(matrix.dims[0]) # Get pressure levels to be used for partial derivate files to name the indexes if pressure_matrix != None: pressure_list = pressure_matrix.data[:, 0] else: pressure_list = [] # Run through the columns for index in range(0, min(ref.dims[1], matrix.dims[1])): # Don't bother plotting wavelength or wavenumber if len(matrix.labels) > 0: if (matrix.labels[index].lower() == "wavelength"): continue if (matrix.labels[index].lower() == "wavenumber"): continue try: xindex = matrix.labels_lower.index(abscissa) if (xindex == index): pass # continue except ValueError: xindex = -1 pass # This is the canvas that holds both graphs c = pyx.canvas.canvas() curr_ypos = 0 # Plot each window separately num_windows = len(windows) - 1; for winEndIdx in range(num_windows, 0, -1): winBegIdx = winEndIdx-1 # Look for the abscissa_string. If present, use that as # the x axis. if (xindex != -1): xaxis = matrix.data[windows[winBegIdx]:windows[winEndIdx], xindex] xtitle = abscissa_string else: xaxis = range(0, windows[winEndIdx] - windows[winBegIdx]) xtitle = "Index" selfdata = matrix.data[windows[winBegIdx]:windows[winEndIdx], index] refdata = ref.data[windows[winBegIdx]:windows[winEndIdx], index] if ratio: residual, rms = OCO_Statistics.Ratio_RMS(selfdata, refdata) else: residual, rms = OCO_Statistics.Residual_RMS(selfdata, refdata) if (rms < 0): print "residual has NaN's" return # Set up strings used to plot and for verbose output on screen if len(matrix.labels) > index: index_name = matrix.labels[index] elif len(pressure_list) > 0 and matrix.filename.find('_pd') > 0: #print 'pll:', len(pressure_list), 'index:', index # Use pressure level for label index_name = "Pressure %.2f" % pressure_list[index] else: index_name = "Index %d" % index if num_windows > 1: plot_description = "%s: %s, window %d" % (matrix.filename, index_name, winBegIdx) else: plot_description = "%s: %s" % (matrix.filename, index_name) if ratio: rms_description = "Ratio RMS = %f" % (rms) else: rms_description = "RMS = %f%%" % (rms * 100) print "%s, %s" % (plot_description, rms_description) # build the lists to plot list0 = [(xaxis[0], selfdata[0])] list1 = [(xaxis[0], refdata[0])] if ratio: diff = [(xaxis[0], residual[0])] else: diff = [(xaxis[0], residual[0]*100.)] for i in range(0, windows[winEndIdx] - windows[winBegIdx]): list0.append((xaxis[i], selfdata[i])) list1.append((xaxis[i], refdata[i])) if ratio: diff.append((xaxis[i], residual[i])) else: diff.append((xaxis[i], residual[i]*100.)) # Create the PyX data structures title = OCO_TextUtils.escape_underscore(os.path.dirname(matrix.filename)) data0 = pyx.graph.data.points(list0, title=title, x=1, y=2) title = OCO_TextUtils.escape_underscore(os.path.dirname(ref.filename)) if (ref.filename != matrix.filename): title = "%s%s" % (title, OCO_TextUtils.escape_underscore(ref.filename)) data1 = pyx.graph.data.points(list1, title=title, x=1, y=2) data2 = pyx.graph.data.points(diff, x=1, y=2) # Assign the axis ranges. xmin = xaxis[0] xmax = xaxis[windows[winEndIdx] - windows[winBegIdx] - 1] ymax = None ymin = None if (min(residual) == max(residual)): ymax = 1 ymin = -1 # This is the lower graph if ratio: y_title_txt = "ratio" else: y_title_txt = "difference" g1 = c.insert(pyx.graph.graphxy(width=100, ratio=1.5*num_windows, ypos=curr_ypos, x=pyx.graph.axis.linear(min=xmin, max=xmax, title=xtitle), y=pyx.graph.axis.linear(min=ymin, max=ymax, title=y_title_txt))) # Plot the differences g1.plot(data2, [pyx.graph.style.line([pyx.color.rgb.red, pyx.style.linewidth.THICK])]) # Add the title if ratio: title="Ratio RMS = %f" % (rms) else: title="Residual RMS = %f\%%" % (rms * 100) g1.text(g1.width/2, curr_ypos + g1.height + 0.4, title, [pyx.text.halign.center, pyx.text.valign.bottom]) curr_ypos += g1.height+6.0 # Find the y axis range ymax = None ymin = None if (min(selfdata) == max(selfdata) and min(refdata) == max(refdata) and min(refdata) == min(selfdata)): ymax = 1 ymin = -1 # This is the upper graph g0 = c.insert(pyx.graph.graphxy(width=100, ratio=1.5*num_windows, ypos=curr_ypos, key=pyx.graph.key.key(pos="br", symbolwidth=5*pyx.unit.t_cm), x=pyx.graph.axis.linkedaxis(g1.axes["x"]), y=pyx.graph.axis.linear(min=ymin, max=ymax, title=matrix.ytitle[index]))) # Now plot the values g0.plot([data0, data1], [pyx.graph.style.line([pyx.color.gradient.Rainbow, pyx.style.linewidth.THICK])]) # Add the title title = OCO_TextUtils.escape_underscore(plot_description) g0.text(g0.width/2, curr_ypos + g0.height + 0.8, title, [pyx.text.halign.center, pyx.text.valign.bottom]) curr_ypos += g0.height+15.0 # Add this plot to the document d.append(pyx.document.page(c, paperformat=pyx.document.paperformat.Letter, margin=pyx.unit.t_cm, fittosize=1, rotated=0, centered=1)) # Write the postscript file if out_filename == None: out_filename = "%s_RMS.pdf" % matrix.filename print 'Writing: %s' % out_filename d.writetofile(out_filename)
def plot_gosat_summ(summary_files): if len(summary_files) > 1: summary_names, common_part = OCO_TextUtils.extract_run_names(summary_files, return_common=True) else: common_part = summary_files[0] plot_data = {} for curr_summ_file in summary_files: file_obj = OCO_Matrix(curr_summ_file, as_strings=True) plot_data = file_obj.as_dict(DICT_KEY_COLUMN, existing_dict=plot_data) figure_index = 0 x_axis_index = [] for curr_yaxis in PLOT_TYPES: for curr_filter in PLOT_FILTERS: legend_names = [] plot_objs = [] fig = None curr_axis = None for leg_idx, curr_legend in enumerate(LEGEND_VALUES): # Extract plot data x_data = [] y_data = [] for scene_name, scene_data in plot_data.items(): if not re.search(curr_filter[0], scene_name) or not re.search(curr_legend[0], scene_name): continue if not scene_data.has_key(X_AXIS[0]) or not scene_data.has_key(curr_yaxis[0]): continue print 'Using scene: ', scene_name, 'for', curr_filter[1], curr_legend[1] x_value = scene_data[X_AXIS[0]] y_value = scene_data[curr_yaxis[0]] if len(X_AXIS) >= 3: axis_match = re.search(X_AXIS[2], x_value) x_value = axis_match.group() if len(X_AXIS) >= 4: if not x_value in x_axis_index: x_axis_index.append(x_value) x_value = x_axis_index.index(x_value) try: x_value = float(x_value) except: pass y_value = float(y_value) if abs(y_value - FILL_VALUE) > 1e-2: y_value *= curr_yaxis[2] insert_point = bisect.bisect(x_data, x_value) x_data.insert(insert_point, x_value) y_data.insert(insert_point, y_value) if len(x_data) > 0: if fig == None: fig = pyplot.figure(figure_index) fig.clf() curr_axis = pyplot.subplot(111) curr_plot_obj = pyplot.plot(x_data, y_data, 'x') plot_objs.append( curr_plot_obj ) legend_names.append( curr_legend[1] ) if len(plot_objs) > 0: figure_index += 1 curr_axis.set_xlabel(X_AXIS[1]) curr_axis.set_ylabel(curr_yaxis[1]) curr_axis.set_title('%s -- %s' % (curr_filter[1], common_part)) leg = curr_axis.legend( plot_objs, legend_names, loc='lower right', borderpad = 0.1, labelspacing = 0.05, borderaxespad = 0.2, ) pyplot.show()