def handle(self, path, *args, **options):
     """Assumes a csv file with headers, comma delimiters, and quoted 
     values. Values accessed by keys SiteCode, Date, and AvgOfTemperatureC.
     
     Will remove any existing records for the project.
             
     """
     kfm = Project.objects.get(name="NPS Kelp Forest Monitoring")
     WaterTemperature.objects.filter(site__project=kfm).delete()
     f = open(path)
     reader = csv.DictReader(f)
     count = 0
     progress = ProgressBar(maxval=file_len(path) - 1).start()
     for row in reader:
         try:
             progress.update(reader.line_num - 1)
             count += 1
             sitecode = str(row['SiteCode'])
             datet = datetime.strptime(row['Date'], "%m/%d/%Y %H:%M:%S")
             celsius = Decimal(row['AvgOfTemperatureC'])
             site = getSite(sitecode, kfm)
             if site:
                 temp = WaterTemperature(
                     site=site, 
                     date=datet,
                     celsius=celsius)
                 temp.save()
         except:
             highlightError(reader, f, row)
             raise
     
     progress.finish()    
     print "done. added %s temperature records" % (count, )
    def handle(self, path, *args, **options):
        """Assumes a csv file with headers, comma delimiters, and quoted 
        values. Values accessed by keys Species, Species Name, CommonName.
        
        SpeciesName will be split for genus and species
        
        This command will not overwrite taxa with the same "Species" and 
        Project. It will update that taxon with new names only if those 
        existing fields are blank.
        """
        kfm = Project.objects.get(name="NPS Kelp Forest Monitoring")
        f = open(path)
        reader = csv.DictReader(open(path))
        count = 0
        for row in reader:
            count += 1
            code = None
            species = ""
            genus = ""
            common_name = ""
            scientific_name = ""
            code = str(row["Species"])
            common_name = row["CommonName"]
            sp = row["Species Name"].split(" ")
            if len(sp) >= 2:
                species = sp[1]
                if species in ("Spp.", "Spp", "SPP", "SPP.", "spp", "spp."):
                    genus = sp[0]
            scientific_name = row["Species Name"]
            if code == None or len(code) < 1:
                highlightError(reader, f, row, "Species")
                raise ValueError("Species code not defined")
            taxa = Taxon.objects.filter(project=kfm, code=code)
            try:
                if len(taxa):
                    taxon = taxa[0]
                    if taxon.common_name is "":
                        taxon.common_name = common_name
                    if taxon.scientific_name is "":
                        taxon.scientific_name = scientific_name
                    if taxon.genus is "":
                        taxon.genus = genus
                    taxon.full_clean()
                    taxon.save()
                else:
                    taxon = Taxon(
                        code=code, project=kfm, common_name=common_name, scientific_name=scientific_name, genus=genus
                    )
                    taxon.save()
            except:
                highlightError(reader, f, row)
                raise

        print "done. added or modified %s taxa" % (count,)
 def handle(self, path, *args, **options):
     """Assumes a csv file with headers, comma delimiters, and quoted 
     values. Values accessed by keys SiteName, SiteCode, Latitude, 
     Longitude. Coordinates in degress decimal minutes.
     
     This command will not overwrite sites with the same name and project.
     """
     f = open(path)
     reader = csv.DictReader(f)
     count = 0
     for row in reader:
         count += 1
         kfm = Project.objects.get(name="NPS Kelp Forest Monitoring")
         try:
             lng = ddm2dd("-" + row["Longitude"])
         except ValueError:
             highlightError(reader, f, row, "Longitude")
             raise
         except:
             highlightError(reader, f, row)
             raise
         try:
             lat = ddm2dd(row["Latitude"])
         except ValueError:
             highlightError(reader, f, row, "Latitude")
             raise
         except:
             highlightError(reader, f, row)
             raise
         try:
             point = Point(lng, lat)
             site = SamplingSite(name=row["SiteName"], code=row["SiteCode"], point=point, project=kfm)
             site.save()
         except:
             highlightError(reader, f, row)
             raise
     print "done. added %s sites" % (count,)