Пример #1
0
 def close_bpch2_file(self):
     if (self.funit <> None):
         stat = bpch2_rw_py.close_bpch2_file(self.funit)
         self.stat = stat
         self.funit = None
Пример #2
0
    def __init__(self, flnm, mode, maxtracer=300, \
      tmpflnm=None,\
      do_read=0, \
      ftracerinfo=gcdf.def_tracerinfo_file, \
      fdiaginfo=gcdf.def_diaginfo_file,\
      sel_categorys=None, sel_tracers=None, sel_taus=None ):
        """	initialize
		augments:
		flnm, string, the bpch file name
		mode, string,  w/r
		maxtracer, integer,  the max number of tracers
		tmpflnm,   string,   file name for  temp information 
        	"""

        self.flnm = flnm
        self.cur_tracer = -1
        self.maxtracer = maxtracer
        self.title = None
        self.modelname = None

        self.data = list()
        self.stat = -1
        self.ntracers = 0
        self.read_mode = -1

        # grid

        self.tracerinfo = tracer_info(ftracerinfo)
        self.diaginfo = diag_info(fdiaginfo)

        if (tmpflnm == None):
            flnm_tmp = flnm.strip() + '_info_py'
        else:
            flnm_tmp = tmpflnm.strip()

        if (mode == 'r'):
            if (do_read == 0):  # only the the heads will be first
                ios,title,tracer_id, lonres, latres,\
                       ix,jx,lx,\
                       ifirst, jfirst, lfirst,\
                       halfpolar, centre180, \
                       tau0,tau1,ntracers= \
                       bpch2_rw_py.read_bpch2_head(flnm,flnm_tmp, maxtracer)

                # print bpch2_rw_py.__doc_

                if (ios > 0):
                    print 'error in read data', ios
                    self.read_mode = -1
                    self.stat = ios
                else:
                    self.title = title.strip()

                    tf = open(flnm_tmp, 'r')
                    lines = tf.readlines()
                    tf.close()

                    ic = 0

                    category = list()
                    modelname = list()
                    unit = list()
                    reserved = list()
                    if (ntracers == 1):

                        line = lines[0]
                        print type(line)
                        print 'line', line
                        terms = line.split(',')
                        category.append(terms[0])
                        modelname.append(terms[1])
                        unit.append(terms[2])
                        reserved.append(terms[3].strip())
                    else:
                        for line in lines:
                            line = line.replace('\n', '')
                            terms = line.split(',')
                            category.append(terms[0])
                            modelname.append(terms[1])
                            unit.append(terms[2])
                            reserved.append(terms[3])

                    self.title = title
                    self.stat = 0
                    self.data = list()
                    self.ntracers = ntracers
                    """ store the data into the bpch2_data class """
                    for isp in range(ntracers):
                        data_grid=bpgrid(ix[isp], jx[isp], lx[isp], \
                           ifirst[isp], jfirst[isp], lfirst[isp],\
                           halfpolar[isp], centre180[isp], \
                           lonres[isp], latres[isp])

                        offset = self.diaginfo.get_offset(category[isp])
                        if (offset < 0):
                            offset = 0

                        real_id = tracer_id[isp] + offset

                        traname, trafullname, tramolew, trascale, traunit = self.tracerinfo.get_tracer_info(
                            real_id)
                        pbdata=bpch2_data(tracer_id[isp],category[isp], unit[isp],
                            data_grid, None, \
                            tau0=tau0[isp],tau1=tau1[isp], modelname=modelname[isp],\
                            reserved=reserved[isp], offset=offset, name=traname, \
                            fullname=trafullname, molew=tramolew, scale=trascale, traunit=traunit, id=real_id)

                        self.data.append(pbdata)
                    self.read_mode = do_read
            if (do_read == 1):
                print self.flnm

                funit = 199
                fti, title, stat = bpch2_rw_py.open_bpch2_for_read(
                    funit, self.flnm)
                self.title = title.strip()

                if (stat <> 0):
                    print 'error in read :', stat
                    self.stat = self.stat
                else:
                    vtracer_id,vhalfpolar,vcentre180,vni,vnj,vnl,vifirst,vjfirst,vlfirst,vlonres,vlatres,\
                       vtau0,vtau1,vmodelname,vcategory,vunit,vreserved,vdata_array,stat = bpch2_rw_py.read_bpch2_record(funit)
                    while (stat == 0):

                        data_grid=bpgrid(vni, vnj, vnl,
                           vifirst, vjfirst, vlfirst,\
                           vhalfpolar, vcentre180, \
                           vlonres, vlatres)
                        offset = self.diaginfo.get_offset(vcategory)
                        if (offset < 0):
                            offset = 0

                        real_id = vtracer_id + offset
                        traname, trafullname, tramolew, trascale, traunit = self.tracerinfo.get_tracer_info(
                            real_id)
                        vdata = zeros([vni, vnj, vnl], float)
                        vdata[0:vni, 0:vnj,
                              0:vnl] = vdata_array[(vifirst - 1):(vifirst +
                                                                  vni - 1),
                                                   (vjfirst - 1):(vjfirst +
                                                                  vnj - 1),
                                                   (vlfirst - 1):(vlfirst +
                                                                  vnl - 1)]
                        pbdata=bpch2_data(vtracer_id,vcategory.strip(), vunit.strip(),
                            data_grid,vdata, \
                            tau0=vtau0,tau1=vtau1, modelname=vmodelname.strip(),\
                             reserved=vreserved, offset=offset, name=traname.strip(), \
                            fullname=trafullname.strip(), molew=tramolew, scale=trascale, traunit=traunit.strip(), id=real_id)

                        self.data.append(pbdata)
                        self.ntracers = self.ntracers + 1
                        # print traname, trafullname, self.ntracers

                        vtracer_id,vhalfpolar,vcentre180,vni,vnj,vnl,vifirst,vjfirst,vlfirst,vlonres,vlatres,\
                                         vtau0,vtau1,vmodelname,vcategory,vunit,vreserved,vdata_array,stat = bpch2_rw_py.read_bpch2_record(funit)
                        #		print 'vtracer_id', vtracer_id, traname, trafullname, tramolew, trascale, traunit

                    if (stat == -1 or stat == 29):
                        self.stat = 0
                        self.read_mode = do_read
                    else:
                        self.stat = stat
                        self.read_mode = -1

                    stat = bpch2_rw_py.close_bpch2_file(funit)
            if (do_read == 2):
                funit = 199
                fti, title, stat = bpch2_rw_py.open_bpch2_for_read(
                    funit, self.flnm)
                self.title = title.strip()

                if (stat <> 0):
                    print 'error in read :', stat
                    self.stat = self.stat
                else:
                    for iname in range(len(sel_categorys)):
                        cname = sel_categorys[iname]
                        tid = sel_tracers[iname]
                        tau0 = sel_taus[iname]
                        vtracer_id,vhalfpolar,vcentre180,vni,vnj,vnl,vifirst,vjfirst,vlfirst,vlonres,vlatres,\
                               vtau0,vtau1,vmodelname,vcategory,vunit,vreserved,vdata_array,stat = \
                               bpch2_rw_py.sel_bpch2_record(funit, cname, tid, tau0)
                        print 'stat in read', stat

                        if (stat == 0):
                            data_grid=bpgrid(vni, vnj, vnl,
                               vifirst, vjfirst, vlfirst,\
                               vhalfpolar, vcentre180, \
                               vlonres, vlatres)
                            offset = self.diaginfo.get_offset(vcategory)
                            if (offset < 0):
                                offset = 0

                            real_id = vtracer_id + offset
                            traname, trafullname, tramolew, trascale, traunit = self.tracerinfo.get_tracer_info(
                                real_id)
                            vdata = zeros([vni, vnj, vnl], float)
                            vdata[0:vni, 0:vnj,
                                  0:vnl] = vdata_array[(vifirst - 1):(vifirst +
                                                                      vni - 1),
                                                       (vjfirst - 1):(vjfirst +
                                                                      vnj - 1),
                                                       (vlfirst - 1):(vlfirst +
                                                                      vnl - 1)]
                            pbdata=bpch2_data(vtracer_id,vcategory.strip(), vunit.strip(),
                                data_grid,vdata, \
                                tau0=vtau0,tau1=vtau1, modelname=vmodelname.strip(),\
                                reserved=vreserved, offset=offset, name=traname.strip(), \
                                fullname=trafullname.strip(), molew=tramolew, scale=trascale, traunit=traunit.strip(), id=real_id)

                            self.data.append(pbdata)
                            self.ntracers = self.ntracers + 1

                        else:
                            break

                    # print 'tracers ', self.ntracers, trafullname, vcategory.strip(), vtracer_id

                    stat = bpch2_rw_py.close_bpch2_file(funit)