def addmon(request, f): user = request.user generator = Generator.objects.get(name='Schlumberger') contents = f.read() io = StringIO.StringIO(contents) mon, channels = monfile.create(io) mon.user = user serial = mon.serial_number logger, new_logger = Datalogger.objects.get_or_create( serial=serial, defaults={'model': mon.instrument_type}) pos, new_pos = logger.loggerpos_set.get_or_create( start_date=mon.start_date, end_date=mon.end_date) if new_pos: # let user fill in screen, refpnt and baro pass loc = MeetLocatie.objects.get(name=unicode(pos.screen)) # get/create datasource for logger ds, new_ds = LoggerDatasource.objects.get_or_create(name=serial, meetlocatie=loc, defaults={ 'logger': logger, 'generator': generator, 'user': user, 'timezone': 'CET' }) mon.crc = abs(binascii.crc32(contents)) try: ds.sourcefiles.get(crc=mon.crc) except SourceFile.DoesNotExist: # add source file mon.name = mon.filename = f.name mon.datasource = ds mon.user = ds.user contentfile = ContentFile(contents) mon.file.save(name=mon.name, content=contentfile) mon.get_dimensions() mon.save() mon.channel_set.add(*channels) pos.monfile_set.add(mon) print pos.screen, serial, mon.num_points, mon.start_date, mon.end_date
def process_zip(self,zip): print 'Processing', zip user = User.objects.get(username='******') generator = Generator.objects.get(name='Schlumberger') with zipfile.ZipFile(zip,'r') as z: count = 0 for info in z.infolist(): if info.filename.endswith('.MON'): name = re.split(r'[_\.\/]',info.filename) if name[-1].startswith('MON'): print 'File', info.filename try: put, filter = name[0].split('-') except: continue try: try: well = Well.objects.get(nitg=put) except Well.DoesNotExist: well = Well.objects.get(name=put) screen = well.screen_set.get(nr=int(filter)) try: loc = MeetLocatie.objects.get(name=unicode(screen)) except: loc = MeetLocatie.objects.get(name='%s/%s' % (put,filter)) with z.open(info.filename, 'r') as f: contents = f.read() io = StringIO.StringIO(contents) mon, channels = monfile.create(io) serial = mon.serial_number logger, created = Datalogger.objects.get_or_create(serial=serial,defaults={'model': mon.instrument_type}) result = logger.loggerpos_set.filter(screen=screen,start_date=mon.start_date,end_date=mon.end_date,refpnt=screen.refpnt) if result: pos = result[0] else: pos, created = logger.loggerpos_set.get_or_create(screen=screen,start_date=mon.start_date,end_date=mon.end_date,refpnt=screen.refpnt) # get/create datasource for logger ds, created = LoggerDatasource.objects.get_or_create(name=logger.serial,meetlocatie=loc, defaults = {'logger': logger, 'generator': generator, 'user': user, 'timezone': 'CET'}) mon.crc = abs(binascii.crc32(contents)) try: sf = ds.sourcefiles.get(crc=mon.crc) print 'Mon file already exist: ', sf except SourceFile.DoesNotExist: # add source file filename = os.path.basename(info.filename) mon.name = mon.filename = filename mon.datasource = ds mon.user = ds.user contentfile = ContentFile(contents) mon.file.save(name=filename, content=contentfile) mon.get_dimensions() mon.save() mon.channel_set.add(*channels) pos.monfile_set.add(mon) print screen, serial, mon.num_points, mon.start_date, mon.end_date count += 1 except Well.DoesNotExist: print 'Well not found:', put continue except Screen.DoesNotExist: print 'Screen not found: %s/%s' % (put, filter) continue except Exception as e: print e, put, filter return count
def handle(self, *args, **options): user = User.objects.get(username='******') generator = Generator.objects.get(name='Schlumberger') zip = options.get('zip') if zip: with zipfile.ZipFile(zip,'r') as z: count = 0 for info in z.infolist(): if info.filename.endswith('.MON'): name = re.split(r'[_\.\/]',info.filename) if name[0].startswith('MON'): try: put, filter = name[1].split('-') except: continue try: try: well = Well.objects.get(nitg=put) except Well.DoesNotExist: well = Well.objects.get(name=put) screen = well.screen_set.get(nr=int(filter)) with z.open(info.filename, 'r') as f: contents = f.read() io = StringIO.StringIO(contents) mon, channels = monfile.create(io) serial = mon.serial_number logger, created = Datalogger.objects.get_or_create(serial=serial,defaults={'model': mon.instrument_type}) pos, created = logger.loggerpos_set.get_or_create(screen=screen,start_date=mon.start_date,end_date=mon.end_date,refpnt=screen.refpnt) # get meetlocatie loc = MeetLocatie.objects.get(name=unicode(screen)) # get/create datasource for logger ds, created = LoggerDatasource.objects.get_or_create(name=logger.serial,meetlocatie=loc, defaults = {'logger': logger, 'generator': generator, 'user': user, 'timezone': 'CET'}) mon.crc = abs(binascii.crc32(contents)) try: ds.sourcefiles.get(crc=mon.crc) except SourceFile.DoesNotExist: # add source file filename = os.path.basename(info.filename) mon.name = mon.filename = filename mon.datasource = ds mon.user = ds.user contentfile = ContentFile(contents) mon.file.save(name=filename, content=contentfile) mon.get_dimensions() mon.save() mon.channel_set.add(*channels) pos.monfile_set.add(mon) print screen, serial, mon.num_points, mon.start_date, mon.end_date count += 1 except Well.DoesNotExist: continue except Screen.DoesNotExist: #print 'screen not found:', well.name, filter continue except Exception as e: print e print count, '.MON bestanden toegevoegd'