Пример #1
0
    def create_psurf_apriori_file(self, spectrum_filename,
                                  psurf_output_filename):
        base_spec_name = os.path.basename(spectrum_filename)

        # Use grep because its faster than doing it outself
        grep_cmd = "grep -E " + base_spec_name + " " + self.runlog_filename
        matched_line = os.popen(grep_cmd).readline()

        if matched_line == None or len(matched_line) == 0:
            raise IOError('Could not find spectrum name: %s in run log file: %s' % (base_spec_name, self.runlog_filename))

        try:
            matched_columns = matched_line.split()
            psurf_val = float(matched_columns[self.pout_col_idx]) * self.convert_factor
        except:
            raise ValueError('Failed to parse psurf value from: "%s" from runlog line: %s' % (matched_columns[self.pout_col_idx], matched_line))
   
        out_obj = OcoMatrix()
    
        out_obj.data = numpy.zeros((1,1), dtype=float)
        out_obj.data[0,0] = psurf_val

        out_obj.file_id = 'psurf value extracted for spectrum named: %s from runlog file: %s' % (base_spec_name, self.runlog_filename)
        out_obj.labels = ['PSURF']
        
        out_obj.write(psurf_output_filename)
Пример #2
0
    def write_oco_ascii(self, filename=None):
        if not self.model:
            self.create_model()

        fn_date_str = self.model_dt.strftime("%Y%m%d%H%M")
        id_date_str = self.model_dt.strftime("%Y-%m-%dT%H:%M:%S")
        if not filename:
            filename = "model_%s.dat" % (fn_date_str)

        nlev = self.model.pressure.data.shape[0]

        # Convert pressure from mbar to Pascals
        press_mbar = self.model.pressure
        press_pa = press_mbar._replace(data=press_mbar.data * 100.0)
        data_in_order = (press_pa, self.model.height, self.model.temperature,
                         self.model.h2o_vmr)
        data_arr = zeros((nlev, len(data_in_order)), dtype=float)

        for idx in range(len(data_in_order)):
            # Reverse data to be increasing pressure order
            data_arr[:, idx] = data_in_order[idx].data[::-1]

        # Put import here to not make this code rely on this module
        from full_physics.oco_matrix import OcoMatrix

        out_mat = OcoMatrix()

        out_mat.file_id = "Atmosphere Model Created from NCEP data interpolated to latitude: %f, longitude: %f on %s" % (
            self.site_lat, self.site_lon, id_date_str)

        out_mat.units = []
        out_mat.labels = []
        for val in data_in_order:
            out_mat.units.append(val.units)
            if val.name == "Temperature":
                out_mat.labels.append("T")
            else:
                out_mat.labels.append(val.name)

        out_mat.header["Surface_Pressure"] = self.model.surface_pressure.data
        out_mat.header[
            "Surface_Pressure_Units"] = self.model.surface_pressure.units

        out_mat.data = data_arr

        logger.debug("Writing to ASCII file: %s" % filename)
        out_mat.write(filename)
    def write_oco_ascii(self, filename=None):
        if not self.model:
            self.create_model()

        fn_date_str = self.model_dt.strftime("%Y%m%d%H%M")
        id_date_str = self.model_dt.strftime("%Y-%m-%dT%H:%M:%S")
        if not filename:
            filename = "model_%s.dat" % (fn_date_str)

        nlev = self.model.pressure.data.shape[0]

        # Convert pressure from mbar to Pascals
        press_mbar = self.model.pressure 
        press_pa = press_mbar._replace(data=press_mbar.data * 100.0)
        data_in_order = (press_pa, self.model.height, self.model.temperature, self.model.h2o_vmr)
        data_arr = zeros((nlev, len(data_in_order)), dtype=float)

        for idx in range(len(data_in_order)):
            # Reverse data to be increasing pressure order
            data_arr[:, idx] = data_in_order[idx].data[::-1]

        # Put import here to not make this code rely on this module
        from full_physics.oco_matrix import OcoMatrix

        out_mat = OcoMatrix()

        out_mat.file_id = "Atmosphere Model Created from NCEP data interpolated to latitude: %f, longitude: %f on %s" % (self.site_lat, self.site_lon, id_date_str)

        out_mat.units = [ v.units for v in data_in_order ]
        out_mat.labels = [ v.name for v in data_in_order ]

        out_mat.header["Surface_Pressure"] = self.model.surface_pressure.data
        out_mat.header["Surface_Pressure_Units"] = self.model.surface_pressure.units

        out_mat.data = data_arr

        logger.debug("Writing to ASCII file: %s" % filename)
        out_mat.write(filename)
Пример #4
0
def reformat_gfit_atmosphere(mav_file, out_file, next_spec_srch=None):

    print('Reading mav data from %s' % mav_file)

    spec_line_start = 1

    mav_data = []
    mav_fobj = open(mav_file, "r")
    file_line_idx = 0

    if next_spec_srch == None:
        found_spec = True
    else:
        found_spec = False
    for mav_line in mav_fobj.readlines():
        line_parts = mav_line.split()
        mav_data.append(line_parts)

        if mav_line.find('Next Spectrum:') >= 0 and next_spec_srch != None:
            if re.search(next_spec_srch, mav_line):
                spec_line_start = file_line_idx
                found_spec = True

        file_line_idx += 1

    if not found_spec:
        raise ValueError(
            'Could not find next spectrum search string: %s in mav file: %s' %
            (next_spec_srch, mav_file))

    print('Processing for', ' '.join(mav_data[spec_line_start]))

    mav_size_row = spec_line_start + 1
    mav_header_row = mav_size_row + 2

    try:
        (num_skip, num_cols,
         num_rows) = [int(val) for val in mav_data[mav_size_row]]
    except:
        mav_header_row = 0
        num_skip = -2
        num_cols = len(mav_data[0])
        num_rows = len(mav_data)

    print()

    print("Skip: %d, Cols %d, Rows: %d" % (num_skip, num_cols, num_rows))

    mav_beg_row = mav_size_row + num_skip + 2
    mav_end_row = mav_beg_row + num_rows - 3

    mav_all_cols = mav_data[mav_header_row]

    print("Column names:", mav_all_cols)

    out_col_idx = 0
    output_data_matrix = numpy.zeros(
        (mav_end_row - mav_beg_row + 1, len(all_col_names)), dtype=float)

    for (curr_mav_col, scale) in mav_col_extract:
        print('Processing column:', curr_mav_col)
        mav_col_idx = mav_all_cols.index(curr_mav_col)
        row_idx = mav_end_row - mav_beg_row

        for mav_row_data in mav_data[mav_beg_row:mav_end_row + 1]:
            new_col_data = float(mav_row_data[mav_col_idx]) * float(scale)
            output_data_matrix[row_idx, out_col_idx] = output_data_matrix[
                row_idx, out_col_idx] + new_col_data
            row_idx -= 1

        out_col_idx += 1

    print('Writing output file %s' % out_file)
    out_mat_obj = OcoMatrix()
    out_mat_obj.file_id = 'GFIT Atmospheric State modified from: %s' % (
        mav_file)
    out_mat_obj.dims = [len(output_data_matrix), len(all_col_names)]
    out_mat_obj.labels = all_col_names
    out_mat_obj.units = all_unit_names
    out_mat_obj.data = output_data_matrix
    out_mat_obj.write(out_file)
def reformat_gfit_atmosphere(mav_file, out_file, next_spec_srch=None):

    print 'Reading mav data from %s' % mav_file

    spec_line_start = 1

    mav_data = []
    mav_fobj = open(mav_file, "r")
    file_line_idx = 0

    if next_spec_srch == None:
        found_spec = True
    else:
        found_spec = False
    for mav_line in mav_fobj.readlines():
        line_parts = mav_line.split()
        mav_data.append( line_parts )

        if mav_line.find('Next Spectrum:') >= 0 and next_spec_srch != None:
            if re.search(next_spec_srch, mav_line):
                spec_line_start = file_line_idx
                found_spec = True
                
        file_line_idx += 1

    if not found_spec:
        raise ValueError('Could not find next spectrum search string: %s in mav file: %s' % (next_spec_srch, mav_file))

    print 'Processing for', ' '.join(mav_data[spec_line_start])

    mav_size_row   = spec_line_start + 1
    mav_header_row = mav_size_row + 2

    try:
        (num_skip, num_cols, num_rows) = [int(val) for val in mav_data[mav_size_row]]
    except:
        mav_header_row = 0
        num_skip = -2
        num_cols = len(mav_data[0])
        num_rows = len(mav_data)

    print 

    print "Skip: %d, Cols %d, Rows: %d" % (num_skip, num_cols, num_rows)

    mav_beg_row = mav_size_row + num_skip + 2
    mav_end_row = mav_beg_row + num_rows - 3

    mav_all_cols = mav_data[mav_header_row]

    print "Column names:", mav_all_cols

    out_col_idx = 0
    output_data_matrix = numpy.zeros((mav_end_row-mav_beg_row+1, len(all_col_names)), dtype=float)

    for (curr_mav_col, scale) in mav_col_extract:
        print 'Processing column:', curr_mav_col
        mav_col_idx = mav_all_cols.index(curr_mav_col)
        row_idx = mav_end_row-mav_beg_row

        for mav_row_data in mav_data[mav_beg_row:mav_end_row+1]:
            new_col_data = float(mav_row_data[mav_col_idx]) * float(scale)
            output_data_matrix[row_idx, out_col_idx] = output_data_matrix[row_idx, out_col_idx] + new_col_data
            row_idx -= 1

        out_col_idx += 1

    print 'Writing output file %s' % out_file
    out_mat_obj = OcoMatrix()
    out_mat_obj.file_id = 'GFIT Atmospheric State modified from: %s' % (mav_file)
    out_mat_obj.dims = [len(output_data_matrix), len(all_col_names)]
    out_mat_obj.labels = all_col_names
    out_mat_obj.units = all_unit_names
    out_mat_obj.data = output_data_matrix
    out_mat_obj.write(out_file)